Ejemplo n.º 1
0
 /**
  * Opens the index.
  *
  * IndexReader constructor needs Directory as a parameter. It should be
  * a string with a path to the index folder or a Directory object.
  *
  * @param mixed $directory
  * @throws Zend_Search_Lucene_Exception
  */
 public function __construct($directory = null, $create = false)
 {
     if ($directory === null) {
         throw new Zend_Search_Exception('No index directory specified');
     }
     if ($directory instanceof ZSearchFSDirectory) {
         $this->_directory = $directory;
     } else {
         $this->_directory = new ZSearchFSDirectory($directory);
     }
     if ($create) {
         $this->_writer = new Zend_Search_Lucene_Index_Writer($this->_directory, true);
     } else {
         $this->_writer = null;
     }
     $this->_segmentInfos = array();
     $segmentsFile = $this->_directory->getFileObject('segments');
     $format = $segmentsFile->readInt();
     if ($format != (int) 0xffffffff) {
         throw new Zend_Search_Lucene_Exception('Wrong segments file format');
     }
     // read version
     $segmentsFile->readLong();
     // read counter
     $segmentsFile->readInt();
     $segments = $segmentsFile->readInt();
     $this->_docCount = 0;
     // read segmentInfos
     for ($count = 0; $count < $segments; $count++) {
         $segName = $segmentsFile->readString();
         $segSize = $segmentsFile->readInt();
         $this->_docCount += $segSize;
         $this->_segmentInfos[$count] = new Zend_Search_Lucene_Index_SegmentInfo($segName, $segSize, $this->_directory);
     }
 }
Ejemplo n.º 2
0
 /**
  * Generate compound index file
  */
 private function _generateCFS()
 {
     $cfsFile = $this->_directory->createFile($this->_name . '.cfs');
     $cfsFile->writeVInt(count($this->_files));
     $dataOffsetPointers = array();
     foreach ($this->_files as $fileName) {
         $dataOffsetPointers[$fileName] = $cfsFile->tell();
         $cfsFile->writeLong(0);
         // write dummy data
         $cfsFile->writeString($fileName);
     }
     foreach ($this->_files as $fileName) {
         // Get actual data offset
         $dataOffset = $cfsFile->tell();
         // Seek to the data offset pointer
         $cfsFile->seek($dataOffsetPointers[$fileName]);
         // Write actual data offset value
         $cfsFile->writeLong($dataOffset);
         // Seek back to the end of file
         $cfsFile->seek($dataOffset);
         $dataFile = $this->_directory->getFileObject($fileName);
         $cfsFile->writeBytes($dataFile->readBytes($this->_directory->fileLength($fileName)));
         $this->_directory->deleteFile($fileName);
     }
 }
Ejemplo n.º 3
0
 /**
  * Update segments file by adding current segment to a list
  * @todo !!!!!Finish the implementation
  *
  * @throws Zend_Search_Lucene_Exception
  */
 private function _updateSegments()
 {
     $segmentsFile = $this->_directory->getFileObject('segments');
     $newSegmentFile = $this->_directory->createFile('segments.new');
     $newSegmentFile->writeInt((int) 0xffffffff);
     $newSegmentFile->writeLong($this->_version);
     $newSegmentFile->writeInt($this->_segmentNameCounter);
     $newSegmentFile->writeInt($this->_segments + count($this->_newSegments));
     $segmentsFile->seek(20);
     $newSegmentFile->writeBytes($segmentsFile->readBytes($segmentsFile->tell() - 20));
     foreach ($this->_newSegments as $segmentName => $segmentInfo) {
         $newSegmentFile->writeString($segmentName);
         $newSegmentFile->writeInt($segmentInfo->count());
     }
     $this->_directory->renameFile('segments.new', 'segments');
 }