File indexing completed on 2025-01-19 05:21:27

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Search_Lucene
0017  * @subpackage Index
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /** Zend_Search_Lucene_Index_TermsStream_Interface */
0024 // require_once 'Zend/Search/Lucene/Index/TermsStream/Interface.php';
0025 
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Search_Lucene
0030  * @subpackage Index
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_Search_Lucene_TermStreamsPriorityQueue implements Zend_Search_Lucene_Index_TermsStream_Interface
0035 {
0036     /**
0037      * Array of term streams (Zend_Search_Lucene_Index_TermsStream_Interface objects)
0038      *
0039      * @var array
0040      */
0041     protected $_termStreams;
0042 
0043     /**
0044      * Terms stream queue
0045      *
0046      * @var Zend_Search_Lucene_Index_TermsPriorityQueue
0047      */
0048     protected $_termsStreamQueue = null;
0049 
0050     /**
0051      * Last Term in a terms stream
0052      *
0053      * @var Zend_Search_Lucene_Index_Term
0054      */
0055     protected $_lastTerm = null;
0056 
0057 
0058     /**
0059      * Object constructor
0060      *
0061      * @param array $termStreams  array of term streams (Zend_Search_Lucene_Index_TermsStream_Interface objects)
0062      */
0063     public function __construct(array $termStreams)
0064     {
0065         $this->_termStreams = $termStreams;
0066 
0067         $this->resetTermsStream();
0068     }
0069 
0070     /**
0071      * Reset terms stream.
0072      */
0073     public function resetTermsStream()
0074     {
0075         /** Zend_Search_Lucene_Index_TermsPriorityQueue */
0076         // require_once 'Zend/Search/Lucene/Index/TermsPriorityQueue.php';
0077 
0078         $this->_termsStreamQueue = new Zend_Search_Lucene_Index_TermsPriorityQueue();
0079 
0080         foreach ($this->_termStreams as $termStream) {
0081             $termStream->resetTermsStream();
0082 
0083             // Skip "empty" containers
0084             if ($termStream->currentTerm() !== null) {
0085                 $this->_termsStreamQueue->put($termStream);
0086             }
0087         }
0088 
0089         $this->nextTerm();
0090     }
0091 
0092     /**
0093      * Skip terms stream up to the specified term preffix.
0094      *
0095      * Prefix contains fully specified field info and portion of searched term
0096      *
0097      * @param Zend_Search_Lucene_Index_Term $prefix
0098      */
0099     public function skipTo(Zend_Search_Lucene_Index_Term $prefix)
0100     {
0101         $this->_termsStreamQueue = new Zend_Search_Lucene_Index_TermsPriorityQueue();
0102 
0103         foreach ($this->_termStreams as $termStream) {
0104             $termStream->skipTo($prefix);
0105 
0106             if ($termStream->currentTerm() !== null) {
0107                 $this->_termsStreamQueue->put($termStream);
0108             }
0109         }
0110 
0111         return $this->nextTerm();
0112     }
0113 
0114     /**
0115      * Scans term streams and returns next term
0116      *
0117      * @return Zend_Search_Lucene_Index_Term|null
0118      */
0119     public function nextTerm()
0120     {
0121         while (($termStream = $this->_termsStreamQueue->pop()) !== null) {
0122             if ($this->_termsStreamQueue->top() === null ||
0123                 $this->_termsStreamQueue->top()->currentTerm()->key() !=
0124                             $termStream->currentTerm()->key()) {
0125                 // We got new term
0126                 $this->_lastTerm = $termStream->currentTerm();
0127 
0128                 if ($termStream->nextTerm() !== null) {
0129                     // Put segment back into the priority queue
0130                     $this->_termsStreamQueue->put($termStream);
0131                 }
0132 
0133                 return $this->_lastTerm;
0134             }
0135 
0136             if ($termStream->nextTerm() !== null) {
0137                 // Put segment back into the priority queue
0138                 $this->_termsStreamQueue->put($termStream);
0139             }
0140         }
0141 
0142         // End of stream
0143         $this->_lastTerm = null;
0144 
0145         return null;
0146     }
0147 
0148     /**
0149      * Returns term in current position
0150      *
0151      * @return Zend_Search_Lucene_Index_Term|null
0152      */
0153     public function currentTerm()
0154     {
0155         return $this->_lastTerm;
0156     }
0157 
0158     /**
0159      * Close terms stream
0160      *
0161      * Should be used for resources clean up if stream is not read up to the end
0162      */
0163     public function closeTermsStream()
0164     {
0165         while (($termStream = $this->_termsStreamQueue->pop()) !== null) {
0166             $termStream->closeTermsStream();
0167         }
0168 
0169         $this->_termsStreamQueue = null;
0170         $this->_lastTerm         = null;
0171     }
0172 }