TermStreamsPriorityQueue.php 4.9 KB

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