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_IndexWriter_ 00008 #define _lucene_index_IndexWriter_ 00009 00010 #if defined(_LUCENE_PRAGMA_ONCE) 00011 # pragma once 00012 #endif 00013 00014 #include "CLucene/analysis/AnalysisHeader.h" 00015 #include "CLucene/util/VoidList.h" 00016 #include "CLucene/search/Similarity.h" 00017 #include "CLucene/store/Lock.h" 00018 #include "CLucene/store/TransactionalRAMDirectory.h" 00019 00020 #include "SegmentHeader.h" 00021 00022 CL_NS_DEF(index) 00023 00024 00047 class IndexWriter:LUCENE_BASE { 00048 class LockWith2:public CL_NS(store)::LuceneLockWith<void>{ 00049 public: 00050 CL_NS(util)::CLVector<SegmentReader*>* segmentsToDelete; 00051 IndexWriter* writer; 00052 bool create; 00053 void doBody(); 00054 LockWith2(CL_NS(store)::LuceneLock* lock, int64_t lockWaitTimeout, 00055 IndexWriter* wr, 00056 CL_NS(util)::CLVector<SegmentReader*>* std, 00057 bool create); 00058 ~LockWith2(){ 00059 } 00060 }; 00061 friend class LockWith2; 00062 00063 class LockWithCFS:public CL_NS(store)::LuceneLockWith<void>{ 00064 public: 00065 CL_NS(store)::Directory* directory; 00066 IndexWriter* writer; 00067 const char* segName; 00068 CL_NS(util)::AStringArrayWithDeletor* filesToDelete; 00069 void doBody(); 00070 LockWithCFS(CL_NS(store)::LuceneLock* lock, int64_t lockWaitTimeout, 00071 CL_NS(store)::Directory* dir, 00072 IndexWriter* wr, 00073 const char* segName, 00074 CL_NS(util)::AStringArrayWithDeletor* ftd); 00075 ~LockWithCFS(){ 00076 } 00077 }; 00078 friend class IndexWriter::LockWithCFS; 00079 00080 bool isOpen; //indicates if the writers is open - this way close can be called multiple times 00081 00082 // how to analyze text 00083 CL_NS(analysis)::Analyzer* analyzer; 00084 00085 CL_NS(search)::Similarity* similarity; // how to normalize 00086 00092 bool useCompoundFile; 00093 bool closeDir; 00094 00095 CL_NS(store)::TransactionalRAMDirectory* ramDirectory; // for temp segs 00096 00097 CL_NS(store)::LuceneLock* writeLock; 00098 00099 void _IndexWriter(const bool create); 00100 00101 void _finalize(); 00102 00103 // where this index resides 00104 CL_NS(store)::Directory* directory; 00105 00106 00107 int32_t getSegmentsCounter(){ return segmentInfos->counter; } 00108 int32_t maxFieldLength; 00109 int32_t mergeFactor; 00110 int32_t minMergeDocs; 00111 int32_t maxMergeDocs; 00112 int32_t termIndexInterval; 00113 00114 int64_t writeLockTimeout; 00115 int64_t commitLockTimeout; 00116 public: 00117 DEFINE_MUTEX(THIS_LOCK) 00118 00119 // Release the write lock, if needed. 00120 SegmentInfos* segmentInfos; 00121 00122 // Release the write lock, if needed. 00123 ~IndexWriter(); 00124 00138 LUCENE_STATIC_CONSTANT(int32_t, DEFAULT_MAX_FIELD_LENGTH = 10000); 00139 LUCENE_STATIC_CONSTANT(int32_t, FIELD_TRUNC_POLICY__WARN = -1); 00140 int32_t getMaxFieldLength() const{ return maxFieldLength; } 00141 void setMaxFieldLength(int32_t val){ maxFieldLength = val; } 00142 00146 LUCENE_STATIC_CONSTANT(int32_t, DEFAULT_MAX_BUFFERED_DOCS = 10); 00154 void setMaxBufferedDocs(int32_t val){ minMergeDocs = val; } 00158 int32_t getMaxBufferedDocs(){ return minMergeDocs; } 00159 00163 LUCENE_STATIC_CONSTANT(int64_t, WRITE_LOCK_TIMEOUT = 1000); 00167 void setWriteLockTimeout(int64_t writeLockTimeout) { this->writeLockTimeout = writeLockTimeout; } 00171 int64_t getWriteLockTimeout() { return writeLockTimeout; } 00172 00176 LUCENE_STATIC_CONSTANT(int64_t, COMMIT_LOCK_TIMEOUT = 10000); 00180 void setCommitLockTimeout(int64_t commitLockTimeout) { this->commitLockTimeout = commitLockTimeout; } 00184 int64_t getCommitLockTimeout() { return commitLockTimeout; } 00185 00186 static const char* WRITE_LOCK_NAME; //"write.lock"; 00187 static const char* COMMIT_LOCK_NAME; //"commit.lock"; 00188 00192 LUCENE_STATIC_CONSTANT(int32_t, DEFAULT_MERGE_FACTOR = 10); 00193 /* Determines how often segment indices are merged by addDocument(). With 00194 * smaller values, less RAM is used while indexing, and searches on 00195 * unoptimized indices are faster, but indexing speed is slower. With larger 00196 * values more RAM is used while indexing and searches on unoptimized indices 00197 * are slower, but indexing is faster. Thus larger values (> 10) are best 00198 * for batched index creation, and smaller values (< 10) for indices that are 00199 * interactively maintained. 00200 * 00201 * <p>This must never be less than 2. The default value is 10. 00202 */ 00203 int32_t getMergeFactor() const{ return mergeFactor; } 00204 void setMergeFactor(int32_t val){ mergeFactor = val; } 00205 00206 00213 LUCENE_STATIC_CONSTANT(int32_t, DEFAULT_TERM_INDEX_INTERVAL = 128); 00235 void setTermIndexInterval(int32_t interval) { termIndexInterval = interval; } 00240 int32_t getTermIndexInterval() { return termIndexInterval; } 00241 00249 int32_t getMinMergeDocs() const{ return minMergeDocs; } 00250 void setMinMergeDocs(int32_t val){ minMergeDocs = val; } 00251 00259 LUCENE_STATIC_CONSTANT(int32_t, DEFAULT_MAX_MERGE_DOCS = LUCENE_INT32_MAX_SHOULDBE); 00267 int32_t getMaxMergeDocs() const{ return maxMergeDocs; } 00268 void setMaxMergeDocs(int32_t val){ maxMergeDocs = val; } 00269 00285 IndexWriter(const char* path, CL_NS(analysis)::Analyzer* a, const bool create, const bool closeDir=true); 00286 00287 00293 IndexWriter(CL_NS(store)::Directory* d, CL_NS(analysis)::Analyzer* a, const bool create, const bool closeDir=false); 00294 00299 void close(); 00300 00304 int32_t docCount(); 00305 00306 00313 void addDocument(CL_NS(document)::Document* doc, CL_NS(analysis)::Analyzer* analyzer=NULL); 00314 00315 00320 void optimize(); 00321 00322 00334 void addIndexes(CL_NS(store)::Directory** dirs); 00335 00340 void addIndexes(IndexReader** readers); 00341 00342 00344 CL_NS(store)::Directory* getDirectory() { return directory; } 00345 00351 bool getUseCompoundFile() { return useCompoundFile; } 00352 00357 void setUseCompoundFile(bool value) { useCompoundFile = value; } 00358 00359 00364 void setSimilarity(CL_NS(search)::Similarity* similarity) { this->similarity = similarity; } 00365 00370 CL_NS(search)::Similarity* getSimilarity() { return this->similarity; } 00371 00373 CL_NS(analysis)::Analyzer* getAnalyzer() { return analyzer; } 00374 00375 private: 00377 void flushRamSegments(); 00378 00380 void maybeMergeSegments(); 00381 00385 void mergeSegments(const uint32_t minSegment); 00386 00389 void mergeSegments(const uint32_t minSegment, const uint32_t end); 00390 00391 void deleteFiles(CL_NS(util)::AStringArrayWithDeletor& files); 00392 void readDeleteableFiles(CL_NS(util)::AStringArrayWithDeletor& files); 00393 void writeDeleteableFiles(CL_NS(util)::AStringArrayWithDeletor& files); 00394 00395 /* 00396 * Some operating systems (e.g. Windows) don't permit a file to be deleted 00397 * while it is opened for read (e.g. by another process or thread). So we 00398 * assume that when a delete fails it is because the file is open in another 00399 * process, and queue the file for subsequent deletion. 00400 */ 00401 void deleteSegments(CL_NS(util)::CLVector<SegmentReader*>* segments); 00402 void deleteFiles(CL_NS(util)::AStringArrayWithDeletor& files, CL_NS(store)::Directory* directory); 00403 void deleteFiles(CL_NS(util)::AStringArrayWithDeletor& files, CL_NS(util)::AStringArrayWithDeletor& deletable); 00404 00405 00406 // synchronized 00407 char* newSegmentName(); 00408 }; 00409 00410 CL_NS_END 00411 #endif