PageRenderTime 85ms CodeModel.GetById 25ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/vendor/Zend/Search/Lucene/TermStreamsPriorityQueue.php

https://bitbucket.org/anycode/sfluceneplugin
PHP | 172 lines | 67 code | 26 blank | 79 comment | 7 complexity | 17a4ad6ef0816e3e273c77107c48ffb5 MD5 | raw file
Possible License(s): BSD-3-Clause
  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: TermStreamsPriorityQueue.php 22987 2010-09-21 10:39:53Z alexander $
  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 the 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. $this->_termsStreamQueue = new Zend_Search_Lucene_Index_TermsPriorityQueue();
  88. foreach ($this->_termStreams as $termStream) {
  89. $termStream->skipTo($prefix);
  90. if ($termStream->currentTerm() !== null) {
  91. $this->_termsStreamQueue->put($termStream);
  92. }
  93. }
  94. return $this->nextTerm();
  95. }
  96. /**
  97. * Scans term streams and returns next term
  98. *
  99. * @return Zend_Search_Lucene_Index_Term|null
  100. */
  101. public function nextTerm()
  102. {
  103. while (($termStream = $this->_termsStreamQueue->pop()) !== null) {
  104. if ($this->_termsStreamQueue->top() === null ||
  105. $this->_termsStreamQueue->top()->currentTerm()->key() !=
  106. $termStream->currentTerm()->key()) {
  107. // We got new term
  108. $this->_lastTerm = $termStream->currentTerm();
  109. if ($termStream->nextTerm() !== null) {
  110. // Put segment back into the priority queue
  111. $this->_termsStreamQueue->put($termStream);
  112. }
  113. return $this->_lastTerm;
  114. }
  115. if ($termStream->nextTerm() !== null) {
  116. // Put segment back into the priority queue
  117. $this->_termsStreamQueue->put($termStream);
  118. }
  119. }
  120. // End of stream
  121. $this->_lastTerm = null;
  122. return null;
  123. }
  124. /**
  125. * Returns term in current position
  126. *
  127. * @return Zend_Search_Lucene_Index_Term|null
  128. */
  129. public function currentTerm()
  130. {
  131. return $this->_lastTerm;
  132. }
  133. /**
  134. * Close terms stream
  135. *
  136. * Should be used for resources clean up if stream is not read up to the end
  137. */
  138. public function closeTermsStream()
  139. {
  140. while (($termStream = $this->_termsStreamQueue->pop()) !== null) {
  141. $termStream->closeTermsStream();
  142. }
  143. $this->_termsStreamQueue = null;
  144. $this->_lastTerm = null;
  145. }
  146. }