/library/Zend/Search/Lucene/TermStreamsPriorityQueue.php
PHP | 172 lines | 67 code | 26 blank | 79 comment | 7 complexity | 793eeca157ef082afa139dd9b9479a5c MD5 | raw file
Possible License(s): AGPL-1.0
1<?php
2/**
3 * Zend Framework
4 *
5 * LICENSE
6 *
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.
14 *
15 * @category Zend
16 * @package Zend_Search_Lucene
17 * @subpackage Index
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: TermStreamsPriorityQueue.php 24594 2012-01-05 21:27:01Z matthew $
21 */
22
23/** Zend_Search_Lucene_Index_TermsStream_Interface */
24require_once 'Zend/Search/Lucene/Index/TermsStream/Interface.php';
25
26
27/**
28 * @category Zend
29 * @package Zend_Search_Lucene
30 * @subpackage Index
31 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
33 */
34class Zend_Search_Lucene_TermStreamsPriorityQueue implements Zend_Search_Lucene_Index_TermsStream_Interface
35{
36 /**
37 * Array of term streams (Zend_Search_Lucene_Index_TermsStream_Interface objects)
38 *
39 * @var array
40 */
41 protected $_termStreams;
42
43 /**
44 * Terms stream queue
45 *
46 * @var Zend_Search_Lucene_Index_TermsPriorityQueue
47 */
48 protected $_termsStreamQueue = null;
49
50 /**
51 * Last Term in a terms stream
52 *
53 * @var Zend_Search_Lucene_Index_Term
54 */
55 protected $_lastTerm = null;
56
57
58 /**
59 * Object constructor
60 *
61 * @param array $termStreams array of term streams (Zend_Search_Lucene_Index_TermsStream_Interface objects)
62 */
63 public function __construct(array $termStreams)
64 {
65 $this->_termStreams = $termStreams;
66
67 $this->resetTermsStream();
68 }
69
70 /**
71 * Reset terms stream.
72 */
73 public function resetTermsStream()
74 {
75 /** Zend_Search_Lucene_Index_TermsPriorityQueue */
76 require_once 'Zend/Search/Lucene/Index/TermsPriorityQueue.php';
77
78 $this->_termsStreamQueue = new Zend_Search_Lucene_Index_TermsPriorityQueue();
79
80 foreach ($this->_termStreams as $termStream) {
81 $termStream->resetTermsStream();
82
83 // Skip "empty" containers
84 if ($termStream->currentTerm() !== null) {
85 $this->_termsStreamQueue->put($termStream);
86 }
87 }
88
89 $this->nextTerm();
90 }
91
92 /**
93 * Skip terms stream up to the specified term preffix.
94 *
95 * Prefix contains fully specified field info and portion of searched term
96 *
97 * @param Zend_Search_Lucene_Index_Term $prefix
98 */
99 public function skipTo(Zend_Search_Lucene_Index_Term $prefix)
100 {
101 $this->_termsStreamQueue = new Zend_Search_Lucene_Index_TermsPriorityQueue();
102
103 foreach ($this->_termStreams as $termStream) {
104 $termStream->skipTo($prefix);
105
106 if ($termStream->currentTerm() !== null) {
107 $this->_termsStreamQueue->put($termStream);
108 }
109 }
110
111 return $this->nextTerm();
112 }
113
114 /**
115 * Scans term streams and returns next term
116 *
117 * @return Zend_Search_Lucene_Index_Term|null
118 */
119 public function nextTerm()
120 {
121 while (($termStream = $this->_termsStreamQueue->pop()) !== null) {
122 if ($this->_termsStreamQueue->top() === null ||
123 $this->_termsStreamQueue->top()->currentTerm()->key() !=
124 $termStream->currentTerm()->key()) {
125 // We got new term
126 $this->_lastTerm = $termStream->currentTerm();
127
128 if ($termStream->nextTerm() !== null) {
129 // Put segment back into the priority queue
130 $this->_termsStreamQueue->put($termStream);
131 }
132
133 return $this->_lastTerm;
134 }
135
136 if ($termStream->nextTerm() !== null) {
137 // Put segment back into the priority queue
138 $this->_termsStreamQueue->put($termStream);
139 }
140 }
141
142 // End of stream
143 $this->_lastTerm = null;
144
145 return null;
146 }
147
148 /**
149 * Returns term in current position
150 *
151 * @return Zend_Search_Lucene_Index_Term|null
152 */
153 public function currentTerm()
154 {
155 return $this->_lastTerm;
156 }
157
158 /**
159 * Close terms stream
160 *
161 * Should be used for resources clean up if stream is not read up to the end
162 */
163 public function closeTermsStream()
164 {
165 while (($termStream = $this->_termsStreamQueue->pop()) !== null) {
166 $termStream->closeTermsStream();
167 }
168
169 $this->_termsStreamQueue = null;
170 $this->_lastTerm = null;
171 }
172}