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-2010 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. /** Zend_Search_Lucene_Index_TermsStream_Interface */
  23. require_once 'Zend/Search/Lucene/Index/TermsStream/Interface.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Search_Lucene
  27. * @subpackage Index
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Search_Lucene_TermStreamsPriorityQueue implements Zend_Search_Lucene_Index_TermsStream_Interface
  32. {
  33. /**
  34. * Array of term streams (Zend_Search_Lucene_Index_TermsStream_Interface objects)
  35. *
  36. * @var array
  37. */
  38. protected $_termStreams;
  39. /**
  40. * Terms stream queue
  41. *
  42. * @var Zend_Search_Lucene_Index_TermsPriorityQueue
  43. */
  44. protected $_termsStreamQueue = null;
  45. /**
  46. * Last Term in a terms stream
  47. *
  48. * @var Zend_Search_Lucene_Index_Term
  49. */
  50. protected $_lastTerm = null;
  51. /**
  52. * Object constructor
  53. *
  54. * @param array $termStreams array of term streams (Zend_Search_Lucene_Index_TermsStream_Interface objects)
  55. */
  56. public function __construct(array $termStreams)
  57. {
  58. $this->_termStreams = $termStreams;
  59. $this->resetTermsStream();
  60. }
  61. /**
  62. * Reset terms stream.
  63. */
  64. public function resetTermsStream()
  65. {
  66. /** Zend_Search_Lucene_Index_TermsPriorityQueue */
  67. require_once 'Zend/Search/Lucene/Index/TermsPriorityQueue.php';
  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. }