CLucene - a full-featured, c++ search engine
API Documentation
00001 /*------------------------------------------------------------------------------ 00002 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team 00003 * 00004 * Distributable under the terms of either the Apache License (Version 2.0) or 00005 * the GNU Lesser General Public License, as specified in the COPYING file. 00006 ------------------------------------------------------------------------------*/ 00007 #ifndef _lucene_store_RAMDirectory_ 00008 #define _lucene_store_RAMDirectory_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "Lock.h" 00015 #include "Directory.h" 00016 #include "CLucene/util/VoidMap.h" 00017 #include "CLucene/util/Arrays.h" 00018 00019 CL_NS_DEF(store) 00020 00021 class RAMFile:LUCENE_BASE { 00022 public: 00023 CL_NS(util)::CLVector<uint8_t*,CL_NS(util)::Deletor::Array<uint8_t> > buffers; 00024 int64_t length; 00025 uint64_t lastModified; 00026 00027 #ifdef _DEBUG 00028 const char* filename; 00029 #endif 00030 00031 RAMFile(); 00032 ~RAMFile(); 00033 }; 00034 00035 class RAMIndexOutput: public BufferedIndexOutput { 00036 protected: 00037 RAMFile* file; 00038 int32_t pointer; 00039 bool deleteFile; 00040 00041 // output methods: 00042 void flushBuffer(const uint8_t* src, const int32_t len); 00043 public: 00044 RAMIndexOutput(RAMFile* f); 00045 RAMIndexOutput(); 00047 virtual ~RAMIndexOutput(); 00048 00049 virtual void close(); 00050 00051 // Random-at methods 00052 virtual void seek(const int64_t pos); 00053 int64_t length(); 00055 void reset(); 00057 void writeTo(IndexOutput* output); 00058 }; 00059 00060 class RAMIndexInput:public BufferedIndexInput { 00061 private: 00062 RAMFile* file; 00063 int32_t pointer; 00064 int64_t _length; 00065 protected: 00067 RAMIndexInput(const RAMIndexInput& clone); 00068 void readInternal(uint8_t *dest, const int32_t len); 00069 00071 void seekInternal(const int64_t pos); 00072 public: 00073 RAMIndexInput(RAMFile* f); 00074 ~RAMIndexInput(); 00075 IndexInput* clone() const; 00076 00077 void close(); 00078 int64_t length(); 00079 const char* getDirectoryType() const; 00080 }; 00081 00082 00086 class RAMDirectory:public Directory{ 00087 00088 class RAMLock: public LuceneLock{ 00089 private: 00090 RAMDirectory* directory; 00091 char* fname; 00092 public: 00093 RAMLock(const char* name, RAMDirectory* dir); 00094 virtual ~RAMLock(); 00095 bool obtain(); 00096 void release(); 00097 bool isLocked(); 00098 virtual TCHAR* toString(); 00099 }; 00100 00101 typedef CL_NS(util)::CLHashMap<const char*,RAMFile*, 00102 CL_NS(util)::Compare::Char, CL_NS(util)::Equals::Char, 00103 CL_NS(util)::Deletor::acArray , CL_NS(util)::Deletor::Object<RAMFile> > FileMap; 00104 protected: 00106 virtual bool doDeleteFile(const char* name); 00107 00118 void _copyFromDir(Directory* dir, bool closeDir); 00119 FileMap files; // unlike the java Hashtable, FileMap is not synchronized, and all access must be protected by a lock 00120 public: 00121 #ifndef _CL_DISABLE_MULTITHREADING //do this so that the mutable keyword still works without mt enabled 00122 mutable DEFINE_MUTEX(files_mutex) // mutable: const methods must also be able to synchronize properly 00123 #endif 00124 00126 void list(vector<string>* names) const; 00127 00129 RAMDirectory(); 00130 00134 virtual ~RAMDirectory(); 00135 RAMDirectory(Directory* dir); 00136 00142 RAMDirectory(const char* dir); 00143 00145 bool fileExists(const char* name) const; 00146 00148 int64_t fileModified(const char* name) const; 00149 00151 int64_t fileLength(const char* name) const; 00152 00154 virtual void renameFile(const char* from, const char* to); 00155 00157 void touchFile(const char* name); 00158 00161 virtual IndexOutput* createOutput(const char* name); 00162 00165 LuceneLock* makeLock(const char* name); 00166 00168 IndexInput* openInput(const char* name); 00169 00170 virtual void close(); 00171 00172 TCHAR* toString() const; 00173 00174 static const char* DirectoryType() { return "RAM"; } 00175 const char* getDirectoryType() const{ return DirectoryType(); } 00176 }; 00177 CL_NS_END 00178 #endif