~ubuntu-branches/ubuntu/raring/clucene-core/raring-proposed

« back to all changes in this revision

Viewing changes to src/core/CLucene/index/_SegmentHeader.h

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2012-08-11 09:33:38 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120811093338-fgrx41ftqew3qt6a
Tags: 2.3.3.4-1
* New upstream release (Closes: #661703).
* Convert package to multiarch.
* Drop obsolete patches:
  - 01_add_missing_include_bug505667.diff
  - 02_posixness_fix_bug530308.diff
* Add patches:
  - Fixing_ZLIB_configuration_in_shared_CMakeLists.patch
  - Fix-pkgconfig-file-by-adding-clucene-shared-library.patch
  - Install-contribs-lib.patch
  - multiarch.patch
* Update debian/compat: bump to 8.
* Update debian/control:
  - update build dependencies (add cmake, libboost-dev and libz-dev).
  - bump Standards-Version to 3.9.3.
  - rename packages due to ABI bump: libclucene0ldbl -> libclucene-core1.
  - add libclucene-contribs1 package.
* Update debian/rules:
  - rewrite to use CMake.
  - add multiarch support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------------
 
2
 * Copyright (C) 2003-2006 Ben van Klinken and the CLucene Team
 
3
 *
 
4
 * Distributable under the terms of either the Apache License (Version 2.0) or
 
5
 * the GNU Lesser General Public License, as specified in the COPYING file.
 
6
------------------------------------------------------------------------------*/
 
7
#ifndef _lucene_index_SegmentHeader_
 
8
#define _lucene_index_SegmentHeader_
 
9
 
 
10
#include "_SegmentInfos.h"
 
11
#include "CLucene/util/BitSet.h"
 
12
//#include "CLucene/util/VoidMap.h"
 
13
#include "CLucene/store/IndexInput.h"
 
14
#include "CLucene/store/IndexOutput.h"
 
15
#include "CLucene/index/IndexReader.h"
 
16
#include "Term.h"
 
17
#include "Terms.h"
 
18
#include "_TermInfo.h"
 
19
//#include "FieldInfos.h"
 
20
#include "_FieldsReader.h"
 
21
#include "_TermVector.h"
 
22
//#include "IndexReader.h"
 
23
#include "_TermInfosReader.h"
 
24
#include "_CompoundFile.h"
 
25
#include "DirectoryIndexReader.h"
 
26
#include "_SkipListReader.h"
 
27
#include "CLucene/util/_ThreadLocal.h"
 
28
 
 
29
CL_NS_DEF(index)
 
30
class SegmentReader;
 
31
 
 
32
class SegmentTermDocs:public virtual TermDocs {
 
33
protected:
 
34
  const SegmentReader* parent;
 
35
  CL_NS(store)::IndexInput* freqStream;
 
36
  int32_t count;
 
37
  int32_t df;
 
38
  CL_NS(util)::BitSet* deletedDocs;
 
39
  int32_t _doc;
 
40
  int32_t _freq;
 
41
 
 
42
private:
 
43
  int32_t skipInterval;
 
44
  int32_t maxSkipLevels;
 
45
  DefaultSkipListReader* skipListReader;
 
46
 
 
47
  int64_t freqBasePointer;
 
48
  int64_t proxBasePointer;
 
49
 
 
50
  int64_t skipPointer;
 
51
  bool haveSkipped;
 
52
 
 
53
protected:
 
54
  bool currentFieldStoresPayloads;
 
55
 
 
56
public:
 
57
  ///\param Parent must be a segment reader
 
58
  SegmentTermDocs( const SegmentReader* Parent);
 
59
  virtual ~SegmentTermDocs();
 
60
 
 
61
  virtual void seek(Term* term);
 
62
  virtual void seek(TermEnum* termEnum);
 
63
  virtual void seek(const TermInfo* ti,Term* term);
 
64
 
 
65
  virtual void close();
 
66
  virtual int32_t doc()const;
 
67
  virtual int32_t freq()const;
 
68
 
 
69
  virtual bool next();
 
70
 
 
71
  /** Optimized implementation. */
 
72
  virtual int32_t read(int32_t* docs, int32_t* freqs, int32_t length);
 
73
 
 
74
  /** Optimized implementation. */
 
75
  virtual bool skipTo(const int32_t target);
 
76
 
 
77
  virtual TermPositions* __asTermPositions();
 
78
 
 
79
protected:
 
80
  virtual void skippingDoc(){}
 
81
  virtual void skipProx(const int64_t /*proxPointer*/, const int32_t /*payloadLength*/){}
 
82
};
 
83
 
 
84
 
 
85
class SegmentTermPositions: public SegmentTermDocs, public TermPositions {
 
86
private:
 
87
  CL_NS(store)::IndexInput* proxStream;
 
88
  int32_t proxCount;
 
89
  int32_t position;
 
90
 
 
91
  // the current payload length
 
92
  int32_t payloadLength;
 
93
  // indicates whether the payload of the currend position has
 
94
  // been read from the proxStream yet
 
95
  bool needToLoadPayload;
 
96
 
 
97
  // these variables are being used to remember information
 
98
  // for a lazy skip
 
99
  int64_t lazySkipPointer;
 
100
  int32_t lazySkipProxCount;
 
101
 
 
102
public:
 
103
  ///\param Parent must be a segment reader
 
104
  SegmentTermPositions(const SegmentReader* Parent);
 
105
  virtual ~SegmentTermPositions();
 
106
 
 
107
private:
 
108
  void seek(const TermInfo* ti, Term* term);
 
109
 
 
110
public:
 
111
  void close();
 
112
 
 
113
  int32_t nextPosition();
 
114
private:
 
115
  int32_t readDeltaPosition();
 
116
 
 
117
protected:
 
118
  void skippingDoc();
 
119
 
 
120
public:
 
121
  bool next();
 
122
  int32_t read(int32_t* docs, int32_t* freqs, int32_t length);
 
123
 
 
124
protected:
 
125
  /** Called by super.skipTo(). */
 
126
  void skipProx(const int64_t proxPointer, const int32_t _payloadLength);
 
127
 
 
128
private:
 
129
  void skipPositions( int32_t n );
 
130
  void skipPayload();
 
131
 
 
132
  // It is not always neccessary to move the prox pointer
 
133
  // to a new document after the freq pointer has been moved.
 
134
  // Consider for example a phrase query with two terms:
 
135
  // the freq pointer for term 1 has to move to document x
 
136
  // to answer the question if the term occurs in that document. But
 
137
  // only if term 2 also matches document x, the positions have to be
 
138
  // read to figure out if term 1 and term 2 appear next
 
139
  // to each other in document x and thus satisfy the query.
 
140
  // So we move the prox pointer lazily to the document
 
141
  // as soon as positions are requested.
 
142
  void lazySkip();
 
143
 
 
144
public:
 
145
  int32_t getPayloadLength() const;
 
146
 
 
147
  uint8_t* getPayload(uint8_t* data);
 
148
 
 
149
  bool isPayloadAvailable() const;
 
150
 
 
151
private:
 
152
  virtual TermDocs* __asTermDocs();
 
153
  virtual TermPositions* __asTermPositions();
 
154
 
 
155
  //resolve SegmentTermDocs/TermPositions ambiguity
 
156
  void seek(Term* term){ SegmentTermDocs::seek(term); }
 
157
  void seek(TermEnum* termEnum){ SegmentTermDocs::seek(termEnum); }
 
158
  int32_t doc() const{ return SegmentTermDocs::doc(); }
 
159
  int32_t freq() const{ return SegmentTermDocs::freq(); }
 
160
  bool skipTo(const int32_t target){ return SegmentTermDocs::skipTo(target); }
 
161
};
 
162
 
 
163
 
 
164
 
 
165
 
 
166
/**
 
167
 * An IndexReader responsible for reading 1 segment of an index
 
168
 */
 
169
class SegmentReader: public DirectoryIndexReader {
 
170
  /**
 
171
   * The class Norm represents the normalizations for a field.
 
172
   * These normalizations are read from an IndexInput in into an array of bytes called bytes
 
173
   */
 
174
  class Norm :LUCENE_BASE{
 
175
    int32_t number;
 
176
    int64_t normSeek;
 
177
    SegmentReader* _this;
 
178
    const char* segment; ///< pointer to segment name
 
179
    volatile int32_t refCount;
 
180
    bool useSingleNormStream;
 
181
    bool rollbackDirty;
 
182
 
 
183
 
 
184
    /** Closes the underlying IndexInput for this norm.
 
185
     * It is still valid to access all other norm properties after close is called.
 
186
     * @throws IOException
 
187
     */
 
188
    void close();
 
189
  public:
 
190
    DEFINE_MUTEX(THIS_LOCK)
 
191
 
 
192
    CL_NS(store)::IndexInput* in;
 
193
    uint8_t* bytes;
 
194
    bool dirty;
 
195
    //Constructor
 
196
    Norm(CL_NS(store)::IndexInput* instrm, bool useSingleNormStream, int32_t number, int64_t normSeek, SegmentReader* reader, const char* segment);
 
197
    //Destructor
 
198
    ~Norm();
 
199
 
 
200
    void reWrite(SegmentInfo* si);
 
201
 
 
202
    void incRef();
 
203
    void decRef();
 
204
    friend class SegmentReader;
 
205
 
 
206
    static void doDelete(Norm* norm);
 
207
  };
 
208
  friend class SegmentReader::Norm;
 
209
 
 
210
  //Holds the name of the segment that is being read
 
211
  std::string segment;
 
212
  SegmentInfo* si;
 
213
  int32_t readBufferSize;
 
214
 
 
215
  //Indicates if there are documents marked as deleted
 
216
  bool deletedDocsDirty;
 
217
  bool normsDirty;
 
218
  bool undeleteAll;
 
219
 
 
220
  bool rollbackDeletedDocsDirty;
 
221
  bool rollbackNormsDirty;
 
222
  bool rollbackUndeleteAll;
 
223
 
 
224
 
 
225
  //Holds all norms for all fields in the segment
 
226
  typedef CL_NS(util)::CLHashtable<const TCHAR*,Norm*,
 
227
    CL_NS(util)::Compare::TChar, CL_NS(util)::Equals::TChar,
 
228
    CL_NS(util)::Deletor::Dummy,
 
229
    Norm > NormsType;
 
230
  NormsType _norms;
 
231
 
 
232
  uint8_t* ones;
 
233
  uint8_t* fakeNorms();
 
234
 
 
235
  // optionally used for the .nrm file shared by multiple norms
 
236
  CL_NS(store)::IndexInput* singleNormStream;
 
237
 
 
238
  // Compound File Reader when based on a compound file segment
 
239
  CompoundFileReader* cfsReader;
 
240
  CompoundFileReader* storeCFSReader;
 
241
 
 
242
  ///Reads the Field Info file
 
243
  FieldsReader* fieldsReader;
 
244
  TermVectorsReader* termVectorsReaderOrig;
 
245
  CL_NS(util)::ThreadLocal<TermVectorsReader*,
 
246
  CL_NS(util)::Deletor::Object<TermVectorsReader> >termVectorsLocal;
 
247
 
 
248
  void initialize(SegmentInfo* si, int32_t readBufferSize, bool doOpenStores, bool doingReopen);
 
249
 
 
250
  /**
 
251
   * Create a clone from the initial TermVectorsReader and store it in the ThreadLocal.
 
252
   * @return TermVectorsReader
 
253
   */
 
254
  TermVectorsReader* getTermVectorsReader();
 
255
 
 
256
  FieldsReader* getFieldsReader();
 
257
  FieldInfos* getFieldInfos();
 
258
 
 
259
protected:
 
260
  ///Marks document docNum as deleted
 
261
  void doDelete(const int32_t docNum);
 
262
  void doUndeleteAll();
 
263
  void commitChanges();
 
264
  void doSetNorm(int32_t doc, const TCHAR* field, uint8_t value);
 
265
 
 
266
  // can return null if norms aren't stored
 
267
  uint8_t* getNorms(const TCHAR* field);
 
268
 
 
269
  /**
 
270
   * Decrements the RC of the norms this reader is using
 
271
   */
 
272
  void decRefNorms();
 
273
 
 
274
 
 
275
  DirectoryIndexReader* doReopen(SegmentInfos* infos);
 
276
 
 
277
public:
 
278
  /**
 
279
   * @throws CorruptIndexException if the index is corrupt
 
280
   * @throws IOException if there is a low-level IO error
 
281
   */
 
282
  static SegmentReader* get(SegmentInfo* si, bool doOpenStores=true);
 
283
 
 
284
  /**
 
285
   * @throws CorruptIndexException if the index is corrupt
 
286
   * @throws IOException if there is a low-level IO error
 
287
   */
 
288
  static SegmentReader* get(SegmentInfo* si, int32_t readBufferSize, bool doOpenStores=true);
 
289
 
 
290
  /**
 
291
   * @throws CorruptIndexException if the index is corrupt
 
292
   * @throws IOException if there is a low-level IO error
 
293
   */
 
294
  static SegmentReader* get(SegmentInfos* sis, SegmentInfo* si, bool closeDir);
 
295
 
 
296
  /**
 
297
   * @throws CorruptIndexException if the index is corrupt
 
298
   * @throws IOException if there is a low-level IO error
 
299
   * @param readBufferSize defaults to BufferedIndexInput::BUFFER_SIZE
 
300
   */
 
301
  static SegmentReader* get(CL_NS(store)::Directory* dir, SegmentInfo* si,
 
302
      SegmentInfos* sis,
 
303
      bool closeDir, bool ownDir,
 
304
      int32_t readBufferSize=-1,
 
305
      bool doOpenStores=true);
 
306
 
 
307
 
 
308
 
 
309
  SegmentReader();
 
310
  ///Destructor.
 
311
  virtual ~SegmentReader();
 
312
 
 
313
  ///Closes all streams to the files of a single segment
 
314
  void doClose();
 
315
 
 
316
  ///Checks if a segment managed by SegmentInfo si has deletions
 
317
  static bool hasDeletions(const SegmentInfo* si);
 
318
  bool hasDeletions() const;
 
319
  bool hasNorms(const TCHAR* field);
 
320
 
 
321
  ///Returns all file names managed by this SegmentReader
 
322
  void files(std::vector<std::string>& retarray);
 
323
  ///Returns an enumeration of all the Terms and TermInfos in the set.
 
324
  TermEnum* terms();
 
325
  ///Returns an enumeration of terms starting at or after the named term t
 
326
  TermEnum* terms(const Term* t);
 
327
 
 
328
  ///Gets the document identified by n
 
329
  bool document(int32_t n, CL_NS(document)::Document& doc, const CL_NS(document)::FieldSelector* fieldSelector);
 
330
 
 
331
  ///Checks if the n-th document has been marked deleted
 
332
  bool isDeleted(const int32_t n);
 
333
 
 
334
  ///Returns an unpositioned TermDocs enumerator.
 
335
  TermDocs* termDocs();
 
336
  ///Returns an unpositioned TermPositions enumerator.
 
337
  TermPositions* termPositions();
 
338
 
 
339
  ///Returns the number of documents which contain the term t
 
340
  int32_t docFreq(const Term* t);
 
341
 
 
342
  ///Returns the actual number of documents in the segment
 
343
  int32_t numDocs();
 
344
  ///Returns the number of  all the documents in the segment including the ones that have
 
345
  ///been marked deleted
 
346
  int32_t maxDoc() const;
 
347
 
 
348
 
 
349
  void setTermInfosIndexDivisor(int32_t indexDivisor);
 
350
 
 
351
  int32_t getTermInfosIndexDivisor();
 
352
 
 
353
  ///Returns the bytes array that holds the norms of a named field.
 
354
  ///Returns fake norms if norms aren't available
 
355
  uint8_t* norms(const TCHAR* field);
 
356
 
 
357
  ///Reads the Norms for field from disk
 
358
  void norms(const TCHAR* field, uint8_t* bytes);
 
359
 
 
360
  ///concatenating segment with ext and x
 
361
  std::string SegmentName(const char* ext, const int32_t x=-1);
 
362
  ///Creates a filename in buffer by concatenating segment with ext and x
 
363
  void SegmentName(char* buffer,int32_t bufferLen,const char* ext, const int32_t x=-1 );
 
364
 
 
365
  /**
 
366
   * @see IndexReader#getFieldNames(IndexReader.FieldOption fldOption)
 
367
   */
 
368
  void getFieldNames(FieldOption fldOption, StringArrayWithDeletor& retarray);
 
369
 
 
370
  static bool usesCompoundFile(SegmentInfo* si);
 
371
 
 
372
  /** Return a term frequency vector for the specified document and field. The
 
373
   *  vector returned contains term numbers and frequencies for all terms in
 
374
   *  the specified field of this document, if the field had storeTermVector
 
375
   *  flag set.  If the flag was not set, the method returns null.
 
376
   * @throws IOException
 
377
   */
 
378
  TermFreqVector* getTermFreqVector(int32_t docNumber, const TCHAR* field=NULL);
 
379
 
 
380
  void getTermFreqVector(int32_t docNumber, const TCHAR* field, TermVectorMapper* mapper);
 
381
  void getTermFreqVector(int32_t docNumber, TermVectorMapper* mapper);
 
382
 
 
383
  /** Return an array of term frequency vectors for the specified document.
 
384
   *  The array contains a vector for each vectorized field in the document.
 
385
   *  Each vector vector contains term numbers and frequencies for all terms
 
386
   *  in a given vectorized field.
 
387
   *  If no such fields existed, the method returns null.
 
388
   * @throws IOException
 
389
   */
 
390
  CL_NS(util)::ArrayBase<TermFreqVector*>* getTermFreqVectors(int32_t docNumber);
 
391
 
 
392
  static const char* getClassName();
 
393
  const char* getObjectName() const;
 
394
 
 
395
  // for testing only
 
396
  bool normsClosed();
 
397
 
 
398
private:
 
399
  //Open all norms files for all fields
 
400
  void openNorms(CL_NS(store)::Directory* cfsDir, int32_t readBufferSize);
 
401
 
 
402
  ///a bitVector that manages which documents have been deleted
 
403
  CL_NS(util)::BitSet* deletedDocs;
 
404
  ///an IndexInput to the frequency file
 
405
  CL_NS(store)::IndexInput* freqStream;
 
406
  ///For reading the fieldInfos file
 
407
  FieldInfos* _fieldInfos;
 
408
  ///For reading the Term Dictionary .tis file
 
409
  TermInfosReader* tis;
 
410
  ///an IndexInput to the prox file
 
411
  CL_NS(store)::IndexInput* proxStream;
 
412
 
 
413
  static bool hasSeparateNorms(SegmentInfo* si);
 
414
  static uint8_t* createFakeNorms(int32_t size);
 
415
 
 
416
  void loadDeletedDocs();
 
417
  SegmentReader* reopenSegment(SegmentInfo* si);
 
418
 
 
419
  /** Returns the field infos of this segment */
 
420
  FieldInfos* fieldInfos();
 
421
 
 
422
  /**
 
423
   * Return the name of the segment this reader is reading.
 
424
   */
 
425
  const char* getSegmentName();
 
426
 
 
427
  /**
 
428
   * Return the SegmentInfo of the segment this reader is reading.
 
429
   */
 
430
  SegmentInfo* getSegmentInfo();
 
431
  void setSegmentInfo(SegmentInfo* info);
 
432
  void startCommit();
 
433
  void rollbackCommit();
 
434
 
 
435
  //allow various classes to access the internals of this. this allows us to have
 
436
  //a more tight idea of the package
 
437
  friend class IndexReader;
 
438
  friend class IndexWriter;
 
439
  friend class SegmentTermDocs;
 
440
  friend class SegmentTermPositions;
 
441
  friend class MultiReader;
 
442
  friend class MultiSegmentReader;
 
443
  friend class SegmentMerger;
 
444
};
 
445
 
 
446
CL_NS_END
 
447
#endif