1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 * @package   Zend_Search
9 */
10
11namespace ZendSearch\Lucene\Index\SegmentWriter;
12
13use ZendSearch\Lucene\Index as LuceneIndex;
14use ZendSearch\Lucene\Storage\Directory;
15
16/**
17 * @category   Zend
18 * @package    Zend_Search_Lucene
19 * @subpackage Index
20 */
21class StreamWriter extends AbstractSegmentWriter
22{
23    /**
24     * Object constructor.
25     *
26     * @param Directory\DirectoryInterface $directory
27     * @param string $name
28     */
29    public function __construct(Directory\DirectoryInterface $directory, $name)
30    {
31        parent::__construct($directory, $name);
32    }
33
34
35    /**
36     * Create stored fields files and open them for write
37     */
38    public function createStoredFieldsFiles()
39    {
40        $this->_fdxFile = $this->_directory->createFile($this->_name . '.fdx');
41        $this->_fdtFile = $this->_directory->createFile($this->_name . '.fdt');
42
43        $this->_files[] = $this->_name . '.fdx';
44        $this->_files[] = $this->_name . '.fdt';
45    }
46
47    public function addNorm($fieldName, $normVector)
48    {
49        if (isset($this->_norms[$fieldName])) {
50            $this->_norms[$fieldName] .= $normVector;
51        } else {
52            $this->_norms[$fieldName] = $normVector;
53        }
54    }
55
56    /**
57     * Close segment, write it to disk and return segment info
58     *
59     * @return \ZendSearch\Lucene\Index\SegmentInfo
60     */
61    public function close()
62    {
63        if ($this->_docCount == 0) {
64            return null;
65        }
66
67        $this->_dumpFNM();
68        $this->_generateCFS();
69
70        return new LuceneIndex\SegmentInfo($this->_directory,
71                                           $this->_name,
72                                           $this->_docCount,
73                                           -1,
74                                           null,
75                                           true,
76                                           true);
77    }
78}
79
80