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_MultiReader 00008 #define _lucene_index_MultiReader 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "SegmentHeader.h" 00015 #include "IndexReader.h" 00016 #include "CLucene/document/Document.h" 00017 #include "Terms.h" 00018 #include "SegmentMergeQueue.h" 00019 00020 CL_NS_DEF(index) 00021 00022 00024 class MultiTermDocs:public virtual TermDocs { 00025 private: 00026 protected: 00027 TermDocs** readerTermDocs; 00028 00029 IndexReader** subReaders; 00030 int32_t subReadersLength; 00031 const int32_t* starts; 00032 Term* term; 00033 00034 int32_t base; 00035 int32_t pointer; 00036 00037 TermDocs* current; // == segTermDocs[pointer] 00038 public: 00039 MultiTermDocs(); 00040 MultiTermDocs(IndexReader** subReaders, const int32_t* s); 00041 virtual ~MultiTermDocs(); 00042 00043 int32_t doc() const; 00044 int32_t freq() const; 00045 00046 void seek(TermEnum* termEnum); 00047 void seek(Term* tterm); 00048 bool next(); 00049 00051 int32_t read(int32_t* docs, int32_t* freqs, int32_t length); 00052 00054 bool skipTo(const int32_t target); 00055 00056 void close(); 00057 00058 virtual TermPositions* __asTermPositions(); 00059 protected: 00060 virtual TermDocs* termDocs(const IndexReader* reader) const; 00061 private: 00062 TermDocs* termDocs(const int32_t i) const; 00063 00064 }; 00065 00066 00067 //MultiTermEnum represents the enumeration of all terms of all readers 00068 class MultiTermEnum:public TermEnum { 00069 private: 00070 SegmentMergeQueue* queue; 00071 00072 Term* _term; 00073 int32_t _docFreq; 00074 public: 00075 //Constructor 00076 //Opens all enumerations of all readers 00077 MultiTermEnum(IndexReader** subReaders, const int32_t* starts, const Term* t); 00078 00079 //Destructor 00080 ~MultiTermEnum(); 00081 00082 //Move the current term to the next in the set of enumerations 00083 bool next(); 00084 00085 //Returns a pointer to the current term of the set of enumerations 00086 Term* term(); 00087 Term* term(bool pointer); 00088 00089 //Returns the document frequency of the current term in the set 00090 int32_t docFreq() const; 00091 00092 //Closes the set of enumerations in the queue 00093 void close(); 00094 00095 00096 const char* getObjectName(){ return MultiTermEnum::getClassName(); } 00097 static const char* getClassName(){ return "MultiTermEnum"; } 00098 }; 00099 00100 00101 class MultiTermPositions:public MultiTermDocs,public TermPositions { 00102 public: 00103 MultiTermPositions(IndexReader** subReaders, const int32_t* s); 00104 ~MultiTermPositions() {}; 00105 int32_t nextPosition(); 00106 00107 00108 virtual TermDocs* __asTermDocs(); 00109 virtual TermPositions* __asTermPositions(); 00110 protected: 00111 TermDocs* termDocs(const IndexReader* reader) const; 00112 }; 00113 00114 00115 class MultiReader:public IndexReader{ 00116 private: 00117 bool _hasDeletions; 00118 IndexReader** subReaders; 00119 int32_t subReadersLength; 00120 int32_t* starts; // 1st docno for each segment 00121 00122 CL_NS(util)::CLHashtable<const TCHAR*,uint8_t*, 00123 CL_NS(util)::Compare::TChar, 00124 CL_NS(util)::Equals::TChar, 00125 CL_NS(util)::Deletor::tcArray, 00126 CL_NS(util)::Deletor::Array<uint8_t> > normsCache; 00127 int32_t _maxDoc; 00128 int32_t _numDocs; 00129 void initialize(IndexReader** subReaders); 00130 00131 int32_t readerIndex(const int32_t n) const; 00132 00133 bool hasNorms(const TCHAR* field); 00134 uint8_t* ones; 00135 uint8_t* fakeNorms(); 00136 protected: 00137 void doSetNorm(int32_t n, const TCHAR* field, uint8_t value); 00138 void doUndeleteAll(); 00139 void doCommit(); 00140 // synchronized 00141 void doClose(); 00142 00143 // synchronized 00144 void doDelete(const int32_t n); 00145 public: 00147 MultiReader(CL_NS(store)::Directory* directory, SegmentInfos* sis, IndexReader** subReaders); 00148 00157 MultiReader(IndexReader** subReaders); 00158 00159 ~MultiReader(); 00160 00167 bool getTermFreqVectors(int32_t n, Array<TermFreqVector*>& result); 00168 TermFreqVector* getTermFreqVector(int32_t n, const TCHAR* field); 00169 00170 00171 // synchronized 00172 int32_t numDocs(); 00173 00174 int32_t maxDoc() const; 00175 00176 bool document(int32_t n, CL_NS(document)::Document* doc); 00177 00178 bool isDeleted(const int32_t n); 00179 bool hasDeletions() const{ return _hasDeletions; } 00180 00181 // synchronized 00182 uint8_t* norms(const TCHAR* field); 00183 void norms(const TCHAR* field, uint8_t* result); 00184 00185 TermEnum* terms() const; 00186 TermEnum* terms(const Term* term) const; 00187 00188 //Returns the document frequency of the current term in the set 00189 int32_t docFreq(const Term* t=NULL) const; 00190 TermDocs* termDocs() const; 00191 TermPositions* termPositions() const; 00192 00193 00197 void getFieldNames(FieldOption fldOption, CL_NS(util)::StringArrayWithDeletor& retarray); 00198 }; 00199 00200 00201 CL_NS_END 00202 #endif