TermStreamsPriorityQueue.php 4.8 KB

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