7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_Search_Lucene
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
23
/** Zend_Search_Lucene_Exception */
24
require_once 'Zend/Search/Lucene/Exception.php';
26
/** Zend_Search_Lucene_Index_SegmentInfo */
27
require_once 'Zend/Search/Lucene/Index/SegmentInfo.php';
32
* @package Zend_Search_Lucene
34
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
35
* @license http://framework.zend.com/license/new-bsd New BSD License
37
abstract class Zend_Search_Lucene_Index_SegmentWriter
40
* Expert: The fraction of terms in the "dictionary" which should be stored
41
* in RAM. Smaller values use more memory, but make searching slightly
42
* faster, while larger values use less memory and make searching slightly
43
* slower. Searching is typically not dominated by dictionary lookup, so
44
* tweaking this is rarely useful.
48
public static $indexInterval = 128;
51
* Expert: The fraction of TermDocs entries stored in skip tables.
52
* Larger values result in smaller indexes, greater acceleration, but fewer
53
* accelerable cases, while smaller values result in bigger indexes,
54
* less acceleration and more
55
* accelerable cases. More detailed experiments would be useful here.
57
* 0x7FFFFFFF indicates that we don't use skip data
59
* Note: not used in current implementation
63
public static $skipInterval = 0x7FFFFFFF;
66
* Expert: The maximum number of skip levels. Smaller values result in
67
* slightly smaller indexes, but slower skipping in big posting lists.
69
* 0 indicates that we don't use skip data
71
* Note: not used in current implementation
75
public static $maxSkipLevels = 0;
78
* Number of docs in a segment
82
protected $_docCount = 0;
92
* File system adapter.
94
* @var Zend_Search_Lucene_Storage_Directory
96
protected $_directory;
99
* List of the index files.
100
* Used for automatic compound file generation
104
protected $_files = array();
107
* Segment fields. Array of Zend_Search_Lucene_Index_FieldInfo objects for this segment
111
protected $_fields = array();
114
* Normalization factors.
115
* An array fieldName => normVector
116
* normVector is a binary string.
117
* Each byte corresponds to an indexed document in a segment and
118
* encodes normalization factor (float value, encoded by
119
* Zend_Search_Lucene_Search_Similarity::encodeNorm())
123
protected $_norms = array();
127
* '.fdx' file - Stored Fields, the field index.
129
* @var Zend_Search_Lucene_Storage_File
131
protected $_fdxFile = null;
134
* '.fdt' file - Stored Fields, the field data.
136
* @var Zend_Search_Lucene_Storage_File
138
protected $_fdtFile = null;
142
* Object constructor.
144
* @param Zend_Search_Lucene_Storage_Directory $directory
145
* @param string $name
147
public function __construct(Zend_Search_Lucene_Storage_Directory $directory, $name)
149
$this->_directory = $directory;
150
$this->_name = $name;
155
* Add field to the segment
157
* Returns actual field number
159
* @param Zend_Search_Lucene_Field $field
162
public function addField(Zend_Search_Lucene_Field $field)
164
if (!isset($this->_fields[$field->name])) {
165
$fieldNumber = count($this->_fields);
166
$this->_fields[$field->name] =
167
new Zend_Search_Lucene_Index_FieldInfo($field->name,
170
$field->storeTermVector);
174
$this->_fields[$field->name]->isIndexed |= $field->isIndexed;
175
$this->_fields[$field->name]->storeTermVector |= $field->storeTermVector;
177
return $this->_fields[$field->name]->number;
182
* Add fieldInfo to the segment
184
* Returns actual field number
186
* @param Zend_Search_Lucene_Index_FieldInfo $fieldInfo
189
public function addFieldInfo(Zend_Search_Lucene_Index_FieldInfo $fieldInfo)
191
if (!isset($this->_fields[$fieldInfo->name])) {
192
$fieldNumber = count($this->_fields);
193
$this->_fields[$fieldInfo->name] =
194
new Zend_Search_Lucene_Index_FieldInfo($fieldInfo->name,
195
$fieldInfo->isIndexed,
197
$fieldInfo->storeTermVector);
201
$this->_fields[$fieldInfo->name]->isIndexed |= $fieldInfo->isIndexed;
202
$this->_fields[$fieldInfo->name]->storeTermVector |= $fieldInfo->storeTermVector;
204
return $this->_fields[$fieldInfo->name]->number;
209
* Returns array of FieldInfo objects.
213
public function getFieldInfos()
215
return $this->_fields;
219
* Add stored fields information
221
* @param array $storedFields array of Zend_Search_Lucene_Field objects
223
public function addStoredFields($storedFields)
225
if (!isset($this->_fdxFile)) {
226
$this->_fdxFile = $this->_directory->createFile($this->_name . '.fdx');
227
$this->_fdtFile = $this->_directory->createFile($this->_name . '.fdt');
229
$this->_files[] = $this->_name . '.fdx';
230
$this->_files[] = $this->_name . '.fdt';
233
$this->_fdxFile->writeLong($this->_fdtFile->tell());
234
$this->_fdtFile->writeVInt(count($storedFields));
235
foreach ($storedFields as $field) {
236
$this->_fdtFile->writeVInt($this->_fields[$field->name]->number);
237
$fieldBits = ($field->isTokenized ? 0x01 : 0x00) |
238
($field->isBinary ? 0x02 : 0x00) |
239
0x00; /* 0x04 - third bit, compressed (ZLIB) */
240
$this->_fdtFile->writeByte($fieldBits);
241
if ($field->isBinary) {
242
$this->_fdtFile->writeVInt(strlen($field->value));
243
$this->_fdtFile->writeBytes($field->value);
245
$this->_fdtFile->writeString($field->getUtf8Value());
253
* Returns the total number of documents in this segment.
257
public function count()
259
return $this->_docCount;
263
* Return segment name
267
public function getName()
273
* Dump Field Info (.fnm) segment file
275
protected function _dumpFNM()
277
$fnmFile = $this->_directory->createFile($this->_name . '.fnm');
278
$fnmFile->writeVInt(count($this->_fields));
280
$nrmFile = $this->_directory->createFile($this->_name . '.nrm');
282
$nrmFile->writeBytes('NRM');
283
// Write format specifier
284
$nrmFile->writeByte((int)0xFF);
286
foreach ($this->_fields as $field) {
287
$fnmFile->writeString($field->name);
288
$fnmFile->writeByte(($field->isIndexed ? 0x01 : 0x00) |
289
($field->storeTermVector ? 0x02 : 0x00)
290
// not supported yet 0x04 /* term positions are stored with the term vectors */ |
291
// not supported yet 0x08 /* term offsets are stored with the term vectors */ |
294
if ($field->isIndexed) {
295
// pre-2.1 index mode (not used now)
296
// $normFileName = $this->_name . '.f' . $field->number;
297
// $fFile = $this->_directory->createFile($normFileName);
298
// $fFile->writeBytes($this->_norms[$field->name]);
299
// $this->_files[] = $normFileName;
301
$nrmFile->writeBytes($this->_norms[$field->name]);
305
$this->_files[] = $this->_name . '.fnm';
306
$this->_files[] = $this->_name . '.nrm';
312
* Term Dictionary file
314
* @var Zend_Search_Lucene_Storage_File
316
private $_tisFile = null;
319
* Term Dictionary index file
321
* @var Zend_Search_Lucene_Storage_File
323
private $_tiiFile = null;
328
* @var Zend_Search_Lucene_Storage_File
330
private $_frqFile = null;
335
* @var Zend_Search_Lucene_Storage_File
337
private $_prxFile = null;
340
* Number of written terms
350
* @var Zend_Search_Lucene_Index_Term
355
* Last saved term info
357
* @var Zend_Search_Lucene_Index_TermInfo
359
private $_prevTermInfo;
362
* Last saved index term
364
* @var Zend_Search_Lucene_Index_Term
366
private $_prevIndexTerm;
369
* Last saved index term info
371
* @var Zend_Search_Lucene_Index_TermInfo
373
private $_prevIndexTermInfo;
376
* Last term dictionary file position
380
private $_lastIndexPosition;
383
* Create dicrionary, frequency and positions files and write necessary headers
385
public function initializeDictionaryFiles()
387
$this->_tisFile = $this->_directory->createFile($this->_name . '.tis');
388
$this->_tisFile->writeInt((int)0xFFFFFFFD);
389
$this->_tisFile->writeLong(0 /* dummy data for terms count */);
390
$this->_tisFile->writeInt(self::$indexInterval);
391
$this->_tisFile->writeInt(self::$skipInterval);
392
$this->_tisFile->writeInt(self::$maxSkipLevels);
394
$this->_tiiFile = $this->_directory->createFile($this->_name . '.tii');
395
$this->_tiiFile->writeInt((int)0xFFFFFFFD);
396
$this->_tiiFile->writeLong(0 /* dummy data for terms count */);
397
$this->_tiiFile->writeInt(self::$indexInterval);
398
$this->_tiiFile->writeInt(self::$skipInterval);
399
$this->_tiiFile->writeInt(self::$maxSkipLevels);
401
/** Dump dictionary header */
402
$this->_tiiFile->writeVInt(0); // preffix length
403
$this->_tiiFile->writeString(''); // suffix
404
$this->_tiiFile->writeInt((int)0xFFFFFFFF); // field number
405
$this->_tiiFile->writeByte((int)0x0F);
406
$this->_tiiFile->writeVInt(0); // DocFreq
407
$this->_tiiFile->writeVInt(0); // FreqDelta
408
$this->_tiiFile->writeVInt(0); // ProxDelta
409
$this->_tiiFile->writeVInt(24); // IndexDelta
411
$this->_frqFile = $this->_directory->createFile($this->_name . '.frq');
412
$this->_prxFile = $this->_directory->createFile($this->_name . '.prx');
414
$this->_files[] = $this->_name . '.tis';
415
$this->_files[] = $this->_name . '.tii';
416
$this->_files[] = $this->_name . '.frq';
417
$this->_files[] = $this->_name . '.prx';
419
$this->_prevTerm = null;
420
$this->_prevTermInfo = null;
421
$this->_prevIndexTerm = null;
422
$this->_prevIndexTermInfo = null;
423
$this->_lastIndexPosition = 24;
424
$this->_termCount = 0;
431
* Term positions is an array( docId => array(pos1, pos2, pos3, ...), ... )
433
* @param Zend_Search_Lucene_Index_Term $termEntry
434
* @param array $termDocs
436
public function addTerm($termEntry, $termDocs)
438
$freqPointer = $this->_frqFile->tell();
439
$proxPointer = $this->_prxFile->tell();
442
foreach ($termDocs as $docId => $termPositions) {
443
$docDelta = ($docId - $prevDoc)*2;
445
if (count($termPositions) > 1) {
446
$this->_frqFile->writeVInt($docDelta);
447
$this->_frqFile->writeVInt(count($termPositions));
449
$this->_frqFile->writeVInt($docDelta + 1);
453
foreach ($termPositions as $position) {
454
$this->_prxFile->writeVInt($position - $prevPosition);
455
$prevPosition = $position;
459
if (count($termDocs) >= self::$skipInterval) {
461
* @todo Write Skip Data to a freq file.
462
* It's not used now, but make index more optimal
464
$skipOffset = $this->_frqFile->tell() - $freqPointer;
469
$term = new Zend_Search_Lucene_Index_Term($termEntry->text,
470
$this->_fields[$termEntry->field]->number);
471
$termInfo = new Zend_Search_Lucene_Index_TermInfo(count($termDocs),
472
$freqPointer, $proxPointer, $skipOffset);
474
$this->_dumpTermDictEntry($this->_tisFile, $this->_prevTerm, $term, $this->_prevTermInfo, $termInfo);
476
if (($this->_termCount + 1) % self::$indexInterval == 0) {
477
$this->_dumpTermDictEntry($this->_tiiFile, $this->_prevIndexTerm, $term, $this->_prevIndexTermInfo, $termInfo);
479
$indexPosition = $this->_tisFile->tell();
480
$this->_tiiFile->writeVInt($indexPosition - $this->_lastIndexPosition);
481
$this->_lastIndexPosition = $indexPosition;
490
public function closeDictionaryFiles()
492
$this->_tisFile->seek(4);
493
$this->_tisFile->writeLong($this->_termCount);
495
$this->_tiiFile->seek(4);
496
$this->_tiiFile->writeLong(ceil(($this->_termCount + 2)/self::$indexInterval));
501
* Dump Term Dictionary segment file entry.
502
* Used to write entry to .tis or .tii files
504
* @param Zend_Search_Lucene_Storage_File $dicFile
505
* @param Zend_Search_Lucene_Index_Term $prevTerm
506
* @param Zend_Search_Lucene_Index_Term $term
507
* @param Zend_Search_Lucene_Index_TermInfo $prevTermInfo
508
* @param Zend_Search_Lucene_Index_TermInfo $termInfo
510
protected function _dumpTermDictEntry(Zend_Search_Lucene_Storage_File $dicFile,
511
&$prevTerm, Zend_Search_Lucene_Index_Term $term,
512
&$prevTermInfo, Zend_Search_Lucene_Index_TermInfo $termInfo)
514
if (isset($prevTerm) && $prevTerm->field == $term->field) {
516
$maxBytes = min(strlen($prevTerm->text), strlen($term->text));
517
while ($matchedBytes < $maxBytes &&
518
$prevTerm->text[$matchedBytes] == $term->text[$matchedBytes]) {
522
// Calculate actual matched UTF-8 pattern
525
while ($prefixBytes < $matchedBytes) {
527
if ((ord($term->text[$prefixBytes]) & 0xC0) == 0xC0) {
529
if (ord($term->text[$prefixBytes]) & 0x20 ) {
531
if (ord($term->text[$prefixBytes]) & 0x10 ) {
537
if ($prefixBytes + $charBytes > $matchedBytes) {
538
// char crosses matched bytes boundary
544
$prefixBytes += $charBytes;
547
// Write preffix length
548
$dicFile->writeVInt($prefixChars);
550
$dicFile->writeString(substr($term->text, $prefixBytes));
552
// Write preffix length
553
$dicFile->writeVInt(0);
555
$dicFile->writeString($term->text);
557
// Write field number
558
$dicFile->writeVInt($term->field);
559
// DocFreq (the count of documents which contain the term)
560
$dicFile->writeVInt($termInfo->docFreq);
564
if (!isset($prevTermInfo)) {
566
$dicFile->writeVInt($termInfo->freqPointer);
568
$dicFile->writeVInt($termInfo->proxPointer);
571
$dicFile->writeVInt($termInfo->freqPointer - $prevTermInfo->freqPointer);
573
$dicFile->writeVInt($termInfo->proxPointer - $prevTermInfo->proxPointer);
575
// Write SkipOffset - it's not 0 when $termInfo->docFreq > self::$skipInterval
576
if ($termInfo->skipOffset != 0) {
577
$dicFile->writeVInt($termInfo->skipOffset);
580
$prevTermInfo = $termInfo;
585
* Generate compound index file
587
protected function _generateCFS()
589
$cfsFile = $this->_directory->createFile($this->_name . '.cfs');
590
$cfsFile->writeVInt(count($this->_files));
592
$dataOffsetPointers = array();
593
foreach ($this->_files as $fileName) {
594
$dataOffsetPointers[$fileName] = $cfsFile->tell();
595
$cfsFile->writeLong(0); // write dummy data
596
$cfsFile->writeString($fileName);
599
foreach ($this->_files as $fileName) {
600
// Get actual data offset
601
$dataOffset = $cfsFile->tell();
602
// Seek to the data offset pointer
603
$cfsFile->seek($dataOffsetPointers[$fileName]);
604
// Write actual data offset value
605
$cfsFile->writeLong($dataOffset);
606
// Seek back to the end of file
607
$cfsFile->seek($dataOffset);
609
$dataFile = $this->_directory->getFileObject($fileName);
611
$byteCount = $this->_directory->fileLength($fileName);
612
while ($byteCount > 0) {
613
$data = $dataFile->readBytes(min($byteCount, 131072 /*128Kb*/));
614
$byteCount -= strlen($data);
615
$cfsFile->writeBytes($data);
618
$this->_directory->deleteFile($fileName);
624
* Close segment, write it to disk and return segment info
626
* @return Zend_Search_Lucene_Index_SegmentInfo
628
abstract public function close();