summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/index/Zend/Search/Lucene/Index/Writer.php
blob: da4af0009652187b535edcffc41bc5144c1a20ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to version 1.0 of the Zend Framework
 * license, that is bundled with this package in the file LICENSE, and
 * is available through the world-wide-web at the following URL:
 * http://www.zend.com/license/framework/1_0.txt. If you did not receive
 * a copy of the Zend Framework license and are unable to obtain it
 * through the world-wide-web, please send a note to license@zend.com
 * so we can mail you a copy immediately.
 *
 * @package    Zend_Search_Lucene
 * @subpackage Index
 * @copyright  Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
 */


/** Zend_Search_Lucene_Index_SegmentWriter */
require_once 'Zend/Search/Lucene/Index/SegmentWriter.php';

/** Zend_Search_Lucene_Index_SegmentInfo */
require_once 'Zend/Search/Lucene/Index/SegmentInfo.php';


/**
 * @package    Zend_Search_Lucene
 * @subpackage Index
 * @copyright  Copyright (c) 2005-2006 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
 */
class Zend_Search_Lucene_Index_Writer
{
    /**
     * @todo Implement segment merger
     * @todo Implement mergeFactor, minMergeDocs, maxMergeDocs usage.
     * @todo Implement Analyzer substitution
     * @todo Implement Zend_Search_Lucene_Storage_DirectoryRAM and Zend_Search_Lucene_Storage_FileRAM to use it for
     *       temporary index files
     * @todo Directory lock processing
     */

    /**
     * File system adapter.
     *
     * @var Zend_Search_Lucene_Storage_Directory
     */
    private $_directory = null;


    /**
     * Index version
     * Counts how often the index has been changed by adding or deleting docs
     *
     * @var integer
     */
    private $_version;

    /**
     * Segment name counter.
     * Used to name new segments .
     *
     * @var integer
     */
    private $_segmentNameCounter;

    /**
     * Number of the segments in the index
     *
     * @var inteher
     */
    private $_segments;

    /**
     * Determines how often segment indices
     * are merged by addDocument().
     *
     * @var integer
     */
    public $mergeFactor;

    /**
     * Determines the minimal number of documents required before
     * the buffered in-memory documents are merging and a new Segment
     * is created.
     *
     * @var integer
     */
    public $minMergeDocs;

    /**
     * Determines the largest number of documents ever merged by addDocument().
     *
     * @var integer
     */
    public $maxMergeDocs;

    /**
     * List of the segments, created by index writer
     * Array of Zend_Search_Lucene_Index_SegmentInfo objects
     *
     * @var array
     */
    private $_newSegments;

    /**
     * Current segment to add documents
     *
     * @var Zend_Search_Lucene_Index_SegmentWriter
     */
    private $_currentSegment;

    /**
     * Opens the index for writing
     *
     * IndexWriter constructor needs Directory as a parameter. It should be
     * a string with a path to the index folder or a Directory object.
     * Second constructor parameter create is optional - true to create the
     * index or overwrite the existing one.
     *
     * @param Zend_Search_Lucene_Storage_Directory $directory
     * @param boolean $create
     */
    public function __construct(Zend_Search_Lucene_Storage_Directory $directory, $create = false)
    {
        $this->_directory = $directory;

        if ($create) {
            foreach ($this->_directory->fileList() as $file) {
                if ($file == 'deletable' ||
                    $file == 'segments'  ||
                    substr($file, strlen($file)-4) == '.cfs') {
                        $this->_directory->deleteFile($file);
                    }
            }
            $segmentsFile = $this->_directory->createFile('segments');
            $segmentsFile->writeInt((int)0xFFFFFFFF);
            // write version
            $segmentsFile->writeLong(0);
            // write name counter
            $segmentsFile->writeInt(0);
            // write segment counter
            $segmentsFile->writeInt(0);

            $deletableFile = $this->_directory->createFile('deletable');
            // write counter
            $deletableFile->writeInt(0);

            $this->_version            = 0;
            $this->_segmentNameCounter = 0;
            $this->_segments           = 0;
        } else {
            $segmentsFile = $this->_directory->getFileObject('segments');
            $format = $segmentsFile->readInt();
            if ($format != (int)0xFFFFFFFF) {
                throw new Zend_Search_Lucene_Exception('Wrong segments file format');
            }

            // read version
            $this->_version            = $segmentsFile->readLong();
            // read counter
            $this->_segmentNameCounter = $segmentsFile->readInt();
            // read segment counter
            $this->_segments           = $segmentsFile->readInt();
        }

        $this->_newSegments = array();
        $this->_currentSegment = null;
    }

    /**
     * Adds a document to this index.
     *
     * @param Zend_Search_Lucene_Document $document
     */
    public function addDocument(Zend_Search_Lucene_Document $document)
    {
        if ($this->_currentSegment === null) {
            $this->_currentSegment =
                new Zend_Search_Lucene_Index_SegmentWriter($this->_directory, $this->_newSegmentName());
        }
        $this->_currentSegment->addDocument($document);
        $this->_version++;
    }



    /**
     * 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($this->_directory->fileLength('segments') - 20));

        foreach ($this->_newSegments as $segmentName => $segmentInfo) {
            $newSegmentFile->writeString($segmentName);
            $newSegmentFile->writeInt($segmentInfo->count());
        }

        $this->_directory->renameFile('segments.new', 'segments');
    }


    /**
     * Commit current changes
     * returns array of new segments
     *
     * @return array
     */
    public function commit()
    {
        if ($this->_currentSegment !== null) {
            $newSegment = $this->_currentSegment->close();
            if ($newSegment !== null) {
                $this->_newSegments[$newSegment->getName()] = $newSegment;
            }
            $this->_currentSegment = null;
        }

        if (count($this->_newSegments) != 0) {
            $this->_updateSegments();
        }

        $result = $this->_newSegments;
        $this->_newSegments = array();

        return $result;
    }


    /**
     * Merges the provided indexes into this index.
     *
     * @param array $readers
     * @return void
     */
    public function addIndexes($readers)
    {
        /**
         * @todo implementation
         */
    }


    /**
     * Returns the number of documents currently in this index.
     *
     * @return integer
     */
    public function docCount($readers)
    {
        /**
         * @todo implementation
         */
    }


    /**
     * Flushes all changes to an index and closes all associated files.
     *
     */
    public function close()
    {
        /**
         * @todo implementation
         */
    }


    /**
     * Merges all segments together into a single segment, optimizing
     * an index for search.
     *
     * return void
     */
    public function optimize()
    {
        /**
         * @todo implementation
         */
    }

    /**
     * Get name for new segment
     *
     * @return string
     */
    private function _newSegmentName()
    {
        return '_' . base_convert($this->_segmentNameCounter++, 10, 36);
    }

}