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_SegmentHeader_ 00008 #define _lucene_index_SegmentHeader_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "SegmentInfos.h" 00015 #include "CLucene/util/BitSet.h" 00016 #include "CLucene/util/VoidMap.h" 00017 #include "Term.h" 00018 #include "FieldInfos.h" 00019 #include "FieldsReader.h" 00020 #include "IndexReader.h" 00021 #include "TermInfosReader.h" 00022 #include "CompoundFile.h" 00023 #include "CLucene/util/ThreadLocal.h" 00024 00025 CL_NS_DEF(index) 00026 class SegmentReader; 00027 00028 class SegmentTermDocs:public virtual TermDocs { 00029 int32_t _doc; 00030 int32_t skipInterval; 00031 int32_t numSkips; 00032 int32_t skipCount; 00033 CL_NS(store)::IndexInput* skipStream; 00034 int32_t skipDoc; 00035 int64_t freqPointer; 00036 int64_t proxPointer; 00037 int64_t skipPointer; 00038 bool haveSkipped; 00039 protected: 00040 // SegmentReader parent 00041 const SegmentReader* parent; 00042 CL_NS(store)::IndexInput* freqStream; 00043 int32_t count; 00044 int32_t df; 00045 int32_t _freq; 00046 CL_NS(util)::BitSet* deletedDocs; 00047 public: 00048 virtual ~SegmentTermDocs(); 00049 00050 virtual void seek(TermEnum* termEnum); 00051 virtual void seek(Term* term); 00052 virtual void seek(const TermInfo* ti); 00053 00054 virtual void close(); 00055 virtual int32_t doc()const; 00056 virtual int32_t freq()const; 00057 00058 virtual bool next(); 00059 00061 virtual int32_t read(int32_t* docs, int32_t* freqs, int32_t length); 00062 00064 virtual bool skipTo(const int32_t target); 00065 00066 virtual TermPositions* __asTermPositions(); 00067 00069 SegmentTermDocs( const SegmentReader* Parent); 00070 protected: 00071 virtual void skippingDoc(){} 00072 virtual void skipProx(int64_t proxPointer){} 00073 }; 00074 00075 00076 class SegmentTermPositions: public SegmentTermDocs, public TermPositions { 00077 private: 00078 CL_NS(store)::IndexInput* proxStream; 00079 int32_t proxCount; 00080 int32_t position; 00081 00082 public: 00084 SegmentTermPositions(const SegmentReader* Parent); 00085 ~SegmentTermPositions(); 00086 00087 void seek(const TermInfo* ti); 00088 void close(); 00089 int32_t nextPosition(); 00090 bool next(); 00091 int32_t read(int32_t* docs, int32_t* freqs, int32_t length); 00092 virtual TermDocs* __asTermDocs(); 00093 virtual TermPositions* __asTermPositions(); 00094 00095 //resolve SegmentTermDocs/TermPositions ambiguity 00096 void seek(Term* term){ SegmentTermDocs::seek(term); } 00097 void seek(TermEnum* termEnum){ SegmentTermDocs::seek(termEnum); } 00098 int32_t doc() const{ return SegmentTermDocs::doc(); } 00099 int32_t freq() const{ return SegmentTermDocs::freq(); } 00100 bool skipTo(const int32_t target){ return SegmentTermDocs::skipTo(target); } 00101 00102 protected: 00103 void skippingDoc(); 00105 void skipProx(int64_t proxPointer); 00106 }; 00107 00108 00109 00110 00114 class SegmentReader: public IndexReader{ 00119 class Norm :LUCENE_BASE{ 00120 int32_t number; 00121 SegmentReader* reader; 00122 const char* segment; 00123 public: 00124 CL_NS(store)::IndexInput* in; 00125 uint8_t* bytes; 00126 bool dirty; 00127 //Constructor 00128 Norm(CL_NS(store)::IndexInput* instrm, int32_t number, SegmentReader* reader, const char* segment); 00129 //Destructor 00130 ~Norm(); 00131 00132 void reWrite(); 00133 }; 00134 friend class SegmentReader::Norm; 00135 00136 //Holds the name of the segment that is being read 00137 const char* segment; 00138 00139 //Indicates if there are documents marked as deleted 00140 bool deletedDocsDirty; 00141 bool normsDirty; 00142 bool undeleteAll; 00143 00144 //Holds all norms for all fields in the segment 00145 typedef CL_NS(util)::CLHashtable<const TCHAR*,Norm*,CL_NS(util)::Compare::TChar, CL_NS(util)::Equals::TChar> NormsType; 00146 NormsType _norms; 00147 00148 uint8_t* ones; 00149 uint8_t* fakeNorms(); 00150 00151 // Compound File Reader when based on a compound file segment 00152 CompoundFileReader* cfsReader; 00154 FieldsReader* fieldsReader; 00155 TermVectorsReader* termVectorsReaderOrig; 00156 CL_NS(util)::ThreadLocal<TermVectorsReader*, 00157 CL_NS(util)::Deletor::Object<TermVectorsReader> >termVectorsLocal; 00158 00159 void initialize(SegmentInfo* si); 00160 00165 TermVectorsReader* getTermVectorsReader(); 00166 00167 protected: 00169 void doDelete(const int32_t docNum); 00170 void doUndeleteAll(); 00171 void doCommit(); 00172 void doSetNorm(int32_t doc, const TCHAR* field, uint8_t value); 00173 00174 // can return null if norms aren't stored 00175 uint8_t* getNorms(const TCHAR* field); 00176 00177 public: 00198 SegmentReader(SegmentInfo* si); 00199 00200 SegmentReader(SegmentInfos* sis, SegmentInfo* si); 00202 virtual ~SegmentReader(); 00203 00205 void doClose(); 00206 00208 static bool hasDeletions(const SegmentInfo* si); 00209 bool hasDeletions() const; 00210 bool hasNorms(const TCHAR* field) const; 00211 00213 void files(CL_NS(util)::AStringArrayWithDeletor& retarray); 00215 TermEnum* terms() const; 00217 TermEnum* terms(const Term* t) const; 00218 00220 bool document(int32_t n, CL_NS(document)::Document* doc); 00221 00223 bool isDeleted(const int32_t n); 00224 00226 TermDocs* termDocs() const; 00228 TermPositions* termPositions() const; 00229 00231 int32_t docFreq(const Term* t) const; 00232 00234 int32_t numDocs(); 00237 int32_t maxDoc() const; 00238 00241 uint8_t* norms(const TCHAR* field); 00242 00244 void norms(const TCHAR* field, uint8_t* bytes); 00245 00247 char* SegmentName(const char* ext, const int32_t x=-1); 00249 void SegmentName(char* buffer,int32_t bufferLen,const char* ext, const int32_t x=-1 ); 00250 00254 void getFieldNames(FieldOption fldOption, CL_NS(util)::StringArrayWithDeletor& retarray); 00255 00256 static bool usesCompoundFile(SegmentInfo* si); 00257 00264 TermFreqVector* getTermFreqVector(int32_t docNumber, const TCHAR* field=NULL); 00265 00273 bool getTermFreqVectors(int32_t docNumber, Array<TermFreqVector*>& result); 00274 private: 00275 //Open all norms files for all fields 00276 void openNorms(CL_NS(store)::Directory* cfsDir); 00277 //Closes all norms files 00278 void closeNorms(); 00279 00281 CL_NS(util)::BitSet* deletedDocs; 00283 CL_NS(store)::IndexInput* freqStream; 00285 FieldInfos* fieldInfos; 00287 TermInfosReader* tis; 00289 CL_NS(store)::IndexInput* proxStream;\ 00290 00291 static bool hasSeparateNorms(SegmentInfo* si); 00292 static uint8_t* createFakeNorms(int32_t size); 00293 00294 //allow various classes to access the internals of this. this allows us to have 00295 //a more tight idea of the package 00296 friend class IndexReader; 00297 friend class IndexWriter; 00298 friend class SegmentTermDocs; 00299 friend class SegmentTermPositions; 00300 friend class MultiReader; 00301 }; 00302 00303 CL_NS_END 00304 #endif