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_index_IndexReader_ 00008 #define _lucene_index_IndexReader_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "CLucene/store/Directory.h" 00015 #include "CLucene/store/FSDirectory.h" 00016 #include "CLucene/store/Lock.h" 00017 #include "CLucene/document/Document.h" 00018 #include "CLucene/index/TermVector.h" 00019 #include "SegmentInfos.h" 00020 #include "Terms.h" 00021 00022 00023 CL_NS_DEF(index) 00024 00025 00026 00042 class IndexReader :LUCENE_BASE{ 00043 public: 00044 //Callback for classes that need to know if IndexReader is closing. 00045 typedef void (*CloseCallback)(IndexReader*, void*); 00046 00047 class CloseCallbackCompare:public CL_NS(util)::Compare::_base{ 00048 public: 00049 bool operator()( CloseCallback t1, CloseCallback t2 ) const{ 00050 return t1 > t2; 00051 } 00052 static void doDelete(CloseCallback dummy){ 00053 } 00054 }; 00055 00056 00057 enum FieldOption { 00058 // all fields 00059 ALL = 1, 00060 // all indexed fields 00061 INDEXED = 2, 00062 // all fields which are not indexed 00063 UNINDEXED = 4, 00064 // all fields which are indexed with termvectors enables 00065 INDEXED_WITH_TERMVECTOR = 8, 00066 // all fields which are indexed but don't have termvectors enabled 00067 INDEXED_NO_TERMVECTOR = 16, 00068 // all fields where termvectors are enabled. Please note that only standard termvector fields are returned 00069 TERMVECTOR = 32, 00070 // all field with termvectors wiht positions enabled 00071 TERMVECTOR_WITH_POSITION = 64, 00072 // all fields where termvectors with offset position are set 00073 TERMVECTOR_WITH_OFFSET = 128, 00074 // all fields where termvectors with offset and position values set 00075 TERMVECTOR_WITH_POSITION_OFFSET = 256 00076 }; 00077 00078 00079 private: 00080 CL_NS(store)::LuceneLock* writeLock; 00081 00082 bool directoryOwner; 00083 bool stale; 00084 bool hasChanges; 00085 bool closeDirectory; 00086 00087 SegmentInfos* segmentInfos; 00088 00089 CL_NS(store)::Directory* directory; 00090 typedef CL_NS(util)::CLSet<CloseCallback, void*, 00091 CloseCallbackCompare, 00092 CloseCallbackCompare> CloseCallbackMap; 00093 CloseCallbackMap closeCallbacks; 00094 00096 virtual void doCommit() = 0; 00097 00104 void aquireWriteLock(); 00105 protected: 00112 IndexReader(CL_NS(store)::Directory* dir); 00113 00122 IndexReader(CL_NS(store)::Directory* directory, SegmentInfos* segmentInfos, bool closeDirectory); 00123 00124 00126 virtual void doClose() = 0; 00127 00129 virtual void doSetNorm(int32_t doc, const TCHAR* field, uint8_t value) = 0; 00130 00132 virtual void doUndeleteAll() = 0; 00133 00134 00138 virtual void doDelete(const int32_t docNum) = 0; 00139 00140 public: 00141 00142 DEFINE_MUTEX(THIS_LOCK) 00143 00144 00145 virtual void commit(); 00146 00147 00149 void undeleteAll(); 00150 00158 virtual void getFieldNames(FieldOption fldOption, CL_NS(util)::StringArrayWithDeletor& retarray) = 0; 00159 00160 _CL_DEPRECATED( getFieldNames(FieldOption, StringArrayWithDeletor&) ) virtual TCHAR** getFieldNames(); 00161 _CL_DEPRECATED( getFieldNames(FieldOption, StringArrayWithDeletor&) ) virtual TCHAR** getFieldNames(bool indexed); 00162 00170 virtual uint8_t* norms(const TCHAR* field) = 0; 00171 00172 00178 virtual void norms(const TCHAR* field, uint8_t* bytes) = 0; 00179 00186 void setNorm(int32_t doc, const TCHAR* field, float_t value); 00187 00197 void setNorm(int32_t doc, const TCHAR* field, uint8_t value); 00198 00200 virtual ~IndexReader(); 00201 00203 static IndexReader* open(const char* path); 00204 00206 static IndexReader* open( CL_NS(store)::Directory* directory, bool closeDirectory=false); 00207 00213 static uint64_t lastModified(const char* directory); 00214 00220 static uint64_t lastModified(const CL_NS(store)::Directory* directory); 00221 00222 00232 static int64_t getCurrentVersion(CL_NS(store)::Directory* directory); 00233 00243 static int64_t getCurrentVersion(const char* directory); 00244 00248 int64_t getVersion(); 00249 00257 bool isCurrent(); 00258 00259 00274 virtual bool getTermFreqVectors(int32_t docNumber, Array<TermFreqVector*>& result) =0; 00275 00290 virtual TermFreqVector* getTermFreqVector(int32_t docNumber, const TCHAR* field) = 0; 00291 00298 static bool indexExists(const char* directory); 00299 00307 static bool indexExists(const CL_NS(store)::Directory* directory); 00308 00310 virtual int32_t numDocs() = 0; 00311 00316 virtual int32_t maxDoc() const = 0; 00317 00323 virtual bool document(int32_t n, CL_NS(document)::Document*) =0; 00324 00325 _CL_DEPRECATED( document(i, document) ) CL_NS(document)::Document* document(const int32_t n); 00326 00328 virtual bool isDeleted(const int32_t n) = 0; 00329 00331 virtual bool hasDeletions() const = 0; 00332 00334 virtual bool hasNorms(const TCHAR* field); 00335 00341 virtual TermEnum* terms() const =0; 00342 00348 virtual TermEnum* terms(const Term* t) const = 0; 00349 00351 virtual int32_t docFreq(const Term* t) const = 0; 00352 00353 /* Returns an unpositioned TermPositions enumerator. 00354 * @memory Caller must clean up 00355 */ 00356 virtual TermPositions* termPositions() const = 0; 00357 00375 TermPositions* termPositions(Term* term) const; 00376 00380 virtual TermDocs* termDocs() const = 0; 00381 00391 TermDocs* termDocs(Term* term) const; 00392 00400 void deleteDocument(const int32_t docNum); 00401 00403 _CL_DEPRECATED( deleteDocument ) void deleteDoc(const int32_t docNum){ deleteDocument(docNum); } 00404 00414 int32_t deleteDocuments(Term* term); 00415 00417 _CL_DEPRECATED( deleteDocuments ) int32_t deleteTerm(Term* term){ return deleteDocuments(term); } 00418 00423 void close(); 00424 00426 static bool isLocked(CL_NS(store)::Directory* directory); 00427 00429 static bool isLocked(const char* directory); 00430 00431 00436 static void unlock(CL_NS(store)::Directory* directory); 00437 static void unlock(const char* path); 00438 00440 CL_NS(store)::Directory* getDirectory() { return directory; } 00441 00443 static bool isLuceneFile(const char* filename); 00444 00449 void addCloseCallback(CloseCallback callback, void* parameter); 00450 00451 protected: 00452 class LockWith:public CL_NS(store)::LuceneLockWith<IndexReader*>{ 00453 public: 00454 CL_NS(store)::Directory* directory; 00455 IndexReader* indexReader; 00456 00457 //Constructor 00458 LockWith(CL_NS(store)::LuceneLock* lock, CL_NS(store)::Directory* dir); 00459 00460 //Reads the segmentinfo file and depending on the number of segments found 00461 //it returns a MultiReader or a SegmentReader 00462 IndexReader* doBody(); 00463 00464 }; 00465 friend class IndexReader::LockWith; 00466 00467 class CommitLockWith:public CL_NS(store)::LuceneLockWith<void>{ 00468 private: 00469 IndexReader* reader; 00470 public: 00471 00472 //Constructor 00473 CommitLockWith( CL_NS(store)::LuceneLock* lock, IndexReader* r ); 00474 void doBody(); 00475 }; 00476 friend class IndexReader::CommitLockWith; 00477 }; 00478 00479 CL_NS_END 00480 #endif 00481 00482