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_FSDirectory_ 00008 #define _lucene_store_FSDirectory_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "Directory.h" 00015 #include "Lock.h" 00016 #include "CLucene/util/VoidMap.h" 00017 #include "CLucene/util/StringBuffer.h" 00018 00019 CL_NS_DEF(store) 00020 00021 00028 class FSDirectory:public Directory{ 00029 private: 00030 class FSLock: public LuceneLock{ 00031 public: 00032 // const char* fname; 00033 char lockFile[CL_MAX_PATH]; 00034 char* lockDir; 00035 FSLock ( const char* lockDir, const char* name ); 00036 ~FSLock(); 00037 bool obtain(); 00038 void release(); 00039 bool isLocked(); 00040 TCHAR* toString(); 00041 }; 00042 friend class FSDirectory::FSLock; 00043 00044 #if defined(LUCENE_FS_MMAP) 00045 class MMapIndexInput: public IndexInput{ 00046 uint8_t* data; 00047 int64_t pos; 00048 #ifdef _CLCOMPILER_MSVC 00049 HANDLE mmaphandle; 00050 HANDLE fhandle; 00051 #else 00052 int fhandle; 00053 #endif 00054 bool isClone; 00055 int64_t _length; 00056 00057 MMapIndexInput(const MMapIndexInput& clone); 00058 public: 00059 MMapIndexInput(const char* path); 00060 ~MMapIndexInput(); 00061 IndexInput* clone() const; 00062 00063 inline uint8_t readByte(); 00064 int32_t readVInt(); 00065 void readBytes(uint8_t* b, const int32_t len); 00066 void close(); 00067 int64_t getFilePointer() const; 00068 void seek(const int64_t pos); 00069 int64_t length(){ return _length; } 00070 00071 static const char* DirectoryType(){ return "MMAP"; } 00072 const char* getDirectoryType() const{ return DirectoryType(); } 00073 }; 00074 friend class FSDirectory::MMapIndexInput; 00075 #endif 00076 00077 class FSIndexInput:public BufferedIndexInput { 00084 class SharedHandle: LUCENE_REFBASE{ 00085 public: 00086 int32_t fhandle; 00087 int64_t _length; 00088 int64_t _fpos; 00089 DEFINE_MUTEX(*THIS_LOCK) 00090 char path[CL_MAX_DIR]; //todo: this is only used for cloning, better to get information from the fhandle 00091 SharedHandle(); 00092 ~SharedHandle() throw(CLuceneError&); 00093 }; 00094 SharedHandle* handle; 00095 int64_t _pos; 00096 protected: 00097 FSIndexInput(const FSIndexInput& clone); 00098 public: 00099 FSIndexInput(const char* path, int32_t bufferSize=CL_NS(store)::BufferedIndexOutput::BUFFER_SIZE); 00100 ~FSIndexInput(); 00101 00102 IndexInput* clone() const; 00103 void close(); 00104 int64_t length(){ return handle->_length; } 00105 00106 const char* getDirectoryType() const{ return FSDirectory::DirectoryType(); } 00107 protected: 00108 // Random-access methods 00109 void seekInternal(const int64_t position); 00110 // IndexInput methods 00111 void readInternal(uint8_t* b, const int32_t len); 00112 }; 00113 friend class FSDirectory::FSIndexInput; 00114 00115 class FSIndexOutput: public BufferedIndexOutput { 00116 private: 00117 int32_t fhandle; 00118 protected: 00119 // output methods: 00120 void flushBuffer(const uint8_t* b, const int32_t size); 00121 public: 00122 FSIndexOutput(const char* path); 00123 ~FSIndexOutput(); 00124 00125 // output methods: 00126 void close(); 00127 00128 // Random-access methods 00129 void seek(const int64_t pos); 00130 int64_t length(); 00131 }; 00132 friend class FSDirectory::FSIndexOutput; 00133 00134 protected: 00135 FSDirectory(const char* path, const bool createDir); 00136 private: 00137 char directory[CL_MAX_PATH]; 00138 int refCount; 00139 void create(); 00140 00141 static const char* LOCK_DIR; 00142 static const char* getLockDir(); 00143 char lockDir[CL_MAX_PATH]; 00144 char* getLockPrefix() const; 00145 static bool disableLocks; 00146 00147 void priv_getFN(char* buffer, const char* name) const; 00148 bool useMMap; 00149 00150 protected: 00152 bool doDeleteFile(const char* name); 00153 00154 public: 00158 ~FSDirectory(); 00159 00161 void list(vector<string>* names) const; 00162 00164 bool fileExists(const char* name) const; 00165 00167 const char* getDirName() const; 00168 00169 00184 static FSDirectory* getDirectory(const char* file, const bool create); 00185 00187 int64_t fileModified(const char* name) const; 00188 00189 //static 00191 static int64_t fileModified(const char* dir, const char* name); 00192 00193 //static 00195 int64_t fileLength(const char* name) const; 00196 00198 IndexInput* openInput(const char* name); 00199 IndexInput* openInput(const char* name, int32_t bufferSize); 00200 00201 #ifdef LUCENE_FS_MMAP 00202 IndexInput* openMMapFile(const char* name, int32_t bufferSize=LUCENE_STREAM_BUFFER_SIZE); 00203 #endif 00204 00206 void renameFile(const char* from, const char* to); 00207 00209 void touchFile(const char* name); 00210 00213 IndexOutput* createOutput(const char* name); 00214 00217 LuceneLock* makeLock(const char* name); 00218 00222 void close(); 00223 00228 void setUseMMap(bool value){ useMMap = value; } 00232 bool getUseMMap() const{ return useMMap; } 00233 00234 TCHAR* toString() const; 00235 00236 static const char* DirectoryType(){ return "FS"; } 00237 const char* getDirectoryType() const{ return "FS"; } 00238 00244 static void setDisableLocks(bool doDisableLocks) { disableLocks = doDisableLocks; } 00245 00250 static bool getDisableLocks() { return disableLocks; } 00251 00252 }; 00253 00254 CL_NS_END 00255 #endif