Proxy.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Search_Lucene_Interface */
  22. require_once 'Zend/Search/Lucene/Interface.php';
  23. /**
  24. * Proxy class intended to be used in userland.
  25. *
  26. * It tracks, when index object goes out of scope and forces ndex closing
  27. *
  28. * @category Zend
  29. * @package Zend_Search_Lucene
  30. * @copyright Copyright (c) 2005-2015 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_Proxy implements Zend_Search_Lucene_Interface
  34. {
  35. /**
  36. * Index object
  37. *
  38. * @var Zend_Search_Lucene_Interface
  39. */
  40. private $_index;
  41. /**
  42. * Object constructor
  43. *
  44. * @param Zend_Search_Lucene_Interface $index
  45. */
  46. public function __construct(Zend_Search_Lucene_Interface $index)
  47. {
  48. $this->_index = $index;
  49. $this->_index->addReference();
  50. }
  51. /**
  52. * Object destructor
  53. */
  54. public function __destruct()
  55. {
  56. if ($this->_index !== null) {
  57. // This code is invoked if Zend_Search_Lucene_Interface object constructor throws an exception
  58. $this->_index->removeReference();
  59. }
  60. $this->_index = null;
  61. }
  62. /**
  63. * Get current generation number
  64. *
  65. * Returns generation number
  66. * 0 means pre-2.1 index format
  67. * -1 means there are no segments files.
  68. *
  69. * @param Zend_Search_Lucene_Storage_Directory $directory
  70. * @return integer
  71. * @throws Zend_Search_Lucene_Exception
  72. */
  73. public static function getActualGeneration(Zend_Search_Lucene_Storage_Directory $directory)
  74. {
  75. Zend_Search_Lucene::getActualGeneration($directory);
  76. }
  77. /**
  78. * Get segments file name
  79. *
  80. * @param integer $generation
  81. * @return string
  82. */
  83. public static function getSegmentFileName($generation)
  84. {
  85. Zend_Search_Lucene::getSegmentFileName($generation);
  86. }
  87. /**
  88. * Get index format version
  89. *
  90. * @return integer
  91. */
  92. public function getFormatVersion()
  93. {
  94. return $this->_index->getFormatVersion();
  95. }
  96. /**
  97. * Set index format version.
  98. * Index is converted to this format at the nearest upfdate time
  99. *
  100. * @param int $formatVersion
  101. * @throws Zend_Search_Lucene_Exception
  102. */
  103. public function setFormatVersion($formatVersion)
  104. {
  105. $this->_index->setFormatVersion($formatVersion);
  106. }
  107. /**
  108. * Returns the Zend_Search_Lucene_Storage_Directory instance for this index.
  109. *
  110. * @return Zend_Search_Lucene_Storage_Directory
  111. */
  112. public function getDirectory()
  113. {
  114. return $this->_index->getDirectory();
  115. }
  116. /**
  117. * Returns the total number of documents in this index (including deleted documents).
  118. *
  119. * @return integer
  120. */
  121. public function count()
  122. {
  123. return $this->_index->count();
  124. }
  125. /**
  126. * Returns one greater than the largest possible document number.
  127. * This may be used to, e.g., determine how big to allocate a structure which will have
  128. * an element for every document number in an index.
  129. *
  130. * @return integer
  131. */
  132. public function maxDoc()
  133. {
  134. return $this->_index->maxDoc();
  135. }
  136. /**
  137. * Returns the total number of non-deleted documents in this index.
  138. *
  139. * @return integer
  140. */
  141. public function numDocs()
  142. {
  143. return $this->_index->numDocs();
  144. }
  145. /**
  146. * Checks, that document is deleted
  147. *
  148. * @param integer $id
  149. * @return boolean
  150. * @throws Zend_Search_Lucene_Exception Exception is thrown if $id is out of the range
  151. */
  152. public function isDeleted($id)
  153. {
  154. return $this->_index->isDeleted($id);
  155. }
  156. /**
  157. * Set default search field.
  158. *
  159. * Null means, that search is performed through all fields by default
  160. *
  161. * Default value is null
  162. *
  163. * @param string $fieldName
  164. */
  165. public static function setDefaultSearchField($fieldName)
  166. {
  167. Zend_Search_Lucene::setDefaultSearchField($fieldName);
  168. }
  169. /**
  170. * Get default search field.
  171. *
  172. * Null means, that search is performed through all fields by default
  173. *
  174. * @return string
  175. */
  176. public static function getDefaultSearchField()
  177. {
  178. return Zend_Search_Lucene::getDefaultSearchField();
  179. }
  180. /**
  181. * Set result set limit.
  182. *
  183. * 0 (default) means no limit
  184. *
  185. * @param integer $limit
  186. */
  187. public static function setResultSetLimit($limit)
  188. {
  189. Zend_Search_Lucene::setResultSetLimit($limit);
  190. }
  191. /**
  192. * Set result set limit.
  193. *
  194. * 0 means no limit
  195. *
  196. * @return integer
  197. */
  198. public static function getResultSetLimit()
  199. {
  200. return Zend_Search_Lucene::getResultSetLimit();
  201. }
  202. /**
  203. * Retrieve index maxBufferedDocs option
  204. *
  205. * maxBufferedDocs is a minimal number of documents required before
  206. * the buffered in-memory documents are written into a new Segment
  207. *
  208. * Default value is 10
  209. *
  210. * @return integer
  211. */
  212. public function getMaxBufferedDocs()
  213. {
  214. return $this->_index->getMaxBufferedDocs();
  215. }
  216. /**
  217. * Set index maxBufferedDocs option
  218. *
  219. * maxBufferedDocs is a minimal number of documents required before
  220. * the buffered in-memory documents are written into a new Segment
  221. *
  222. * Default value is 10
  223. *
  224. * @param integer $maxBufferedDocs
  225. */
  226. public function setMaxBufferedDocs($maxBufferedDocs)
  227. {
  228. $this->_index->setMaxBufferedDocs($maxBufferedDocs);
  229. }
  230. /**
  231. * Retrieve index maxMergeDocs option
  232. *
  233. * maxMergeDocs is a largest number of documents ever merged by addDocument().
  234. * Small values (e.g., less than 10,000) are best for interactive indexing,
  235. * as this limits the length of pauses while indexing to a few seconds.
  236. * Larger values are best for batched indexing and speedier searches.
  237. *
  238. * Default value is PHP_INT_MAX
  239. *
  240. * @return integer
  241. */
  242. public function getMaxMergeDocs()
  243. {
  244. return $this->_index->getMaxMergeDocs();
  245. }
  246. /**
  247. * Set index maxMergeDocs option
  248. *
  249. * maxMergeDocs is a largest number of documents ever merged by addDocument().
  250. * Small values (e.g., less than 10,000) are best for interactive indexing,
  251. * as this limits the length of pauses while indexing to a few seconds.
  252. * Larger values are best for batched indexing and speedier searches.
  253. *
  254. * Default value is PHP_INT_MAX
  255. *
  256. * @param integer $maxMergeDocs
  257. */
  258. public function setMaxMergeDocs($maxMergeDocs)
  259. {
  260. $this->_index->setMaxMergeDocs($maxMergeDocs);
  261. }
  262. /**
  263. * Retrieve index mergeFactor option
  264. *
  265. * mergeFactor determines how often segment indices are merged by addDocument().
  266. * With smaller values, less RAM is used while indexing,
  267. * and searches on unoptimized indices are faster,
  268. * but indexing speed is slower.
  269. * With larger values, more RAM is used during indexing,
  270. * and while searches on unoptimized indices are slower,
  271. * indexing is faster.
  272. * Thus larger values (> 10) are best for batch index creation,
  273. * and smaller values (< 10) for indices that are interactively maintained.
  274. *
  275. * Default value is 10
  276. *
  277. * @return integer
  278. */
  279. public function getMergeFactor()
  280. {
  281. return $this->_index->getMergeFactor();
  282. }
  283. /**
  284. * Set index mergeFactor option
  285. *
  286. * mergeFactor determines how often segment indices are merged by addDocument().
  287. * With smaller values, less RAM is used while indexing,
  288. * and searches on unoptimized indices are faster,
  289. * but indexing speed is slower.
  290. * With larger values, more RAM is used during indexing,
  291. * and while searches on unoptimized indices are slower,
  292. * indexing is faster.
  293. * Thus larger values (> 10) are best for batch index creation,
  294. * and smaller values (< 10) for indices that are interactively maintained.
  295. *
  296. * Default value is 10
  297. *
  298. * @param integer $maxMergeDocs
  299. */
  300. public function setMergeFactor($mergeFactor)
  301. {
  302. $this->_index->setMergeFactor($mergeFactor);
  303. }
  304. /**
  305. * Performs a query against the index and returns an array
  306. * of Zend_Search_Lucene_Search_QueryHit objects.
  307. * Input is a string or Zend_Search_Lucene_Search_Query.
  308. *
  309. * @param mixed $query
  310. * @return array Zend_Search_Lucene_Search_QueryHit
  311. * @throws Zend_Search_Lucene_Exception
  312. */
  313. public function find($query)
  314. {
  315. // actual parameter list
  316. $parameters = func_get_args();
  317. // invoke $this->_index->find() method with specified parameters
  318. return call_user_func_array(array(&$this->_index, 'find'), $parameters);
  319. }
  320. /**
  321. * Returns a list of all unique field names that exist in this index.
  322. *
  323. * @param boolean $indexed
  324. * @return array
  325. */
  326. public function getFieldNames($indexed = false)
  327. {
  328. return $this->_index->getFieldNames($indexed);
  329. }
  330. /**
  331. * Returns a Zend_Search_Lucene_Document object for the document
  332. * number $id in this index.
  333. *
  334. * @param integer|Zend_Search_Lucene_Search_QueryHit $id
  335. * @return Zend_Search_Lucene_Document
  336. */
  337. public function getDocument($id)
  338. {
  339. return $this->_index->getDocument($id);
  340. }
  341. /**
  342. * Returns true if index contain documents with specified term.
  343. *
  344. * Is used for query optimization.
  345. *
  346. * @param Zend_Search_Lucene_Index_Term $term
  347. * @return boolean
  348. */
  349. public function hasTerm(Zend_Search_Lucene_Index_Term $term)
  350. {
  351. return $this->_index->hasTerm($term);
  352. }
  353. /**
  354. * Returns IDs of all the documents containing term.
  355. *
  356. * @param Zend_Search_Lucene_Index_Term $term
  357. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  358. * @return array
  359. */
  360. public function termDocs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  361. {
  362. return $this->_index->termDocs($term, $docsFilter);
  363. }
  364. /**
  365. * Returns documents filter for all documents containing term.
  366. *
  367. * It performs the same operation as termDocs, but return result as
  368. * Zend_Search_Lucene_Index_DocsFilter object
  369. *
  370. * @param Zend_Search_Lucene_Index_Term $term
  371. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  372. * @return Zend_Search_Lucene_Index_DocsFilter
  373. */
  374. public function termDocsFilter(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  375. {
  376. return $this->_index->termDocsFilter($term, $docsFilter);
  377. }
  378. /**
  379. * Returns an array of all term freqs.
  380. * Return array structure: array( docId => freq, ...)
  381. *
  382. * @param Zend_Search_Lucene_Index_Term $term
  383. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  384. * @return integer
  385. */
  386. public function termFreqs(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  387. {
  388. return $this->_index->termFreqs($term, $docsFilter);
  389. }
  390. /**
  391. * Returns an array of all term positions in the documents.
  392. * Return array structure: array( docId => array( pos1, pos2, ...), ...)
  393. *
  394. * @param Zend_Search_Lucene_Index_Term $term
  395. * @param Zend_Search_Lucene_Index_DocsFilter|null $docsFilter
  396. * @return array
  397. */
  398. public function termPositions(Zend_Search_Lucene_Index_Term $term, $docsFilter = null)
  399. {
  400. return $this->_index->termPositions($term, $docsFilter);
  401. }
  402. /**
  403. * Returns the number of documents in this index containing the $term.
  404. *
  405. * @param Zend_Search_Lucene_Index_Term $term
  406. * @return integer
  407. */
  408. public function docFreq(Zend_Search_Lucene_Index_Term $term)
  409. {
  410. return $this->_index->docFreq($term);
  411. }
  412. /**
  413. * Retrive similarity used by index reader
  414. *
  415. * @return Zend_Search_Lucene_Search_Similarity
  416. */
  417. public function getSimilarity()
  418. {
  419. return $this->_index->getSimilarity();
  420. }
  421. /**
  422. * Returns a normalization factor for "field, document" pair.
  423. *
  424. * @param integer $id
  425. * @param string $fieldName
  426. * @return float
  427. */
  428. public function norm($id, $fieldName)
  429. {
  430. return $this->_index->norm($id, $fieldName);
  431. }
  432. /**
  433. * Returns true if any documents have been deleted from this index.
  434. *
  435. * @return boolean
  436. */
  437. public function hasDeletions()
  438. {
  439. return $this->_index->hasDeletions();
  440. }
  441. /**
  442. * Deletes a document from the index.
  443. * $id is an internal document id
  444. *
  445. * @param integer|Zend_Search_Lucene_Search_QueryHit $id
  446. * @throws Zend_Search_Lucene_Exception
  447. */
  448. public function delete($id)
  449. {
  450. return $this->_index->delete($id);
  451. }
  452. /**
  453. * Adds a document to this index.
  454. *
  455. * @param Zend_Search_Lucene_Document $document
  456. */
  457. public function addDocument(Zend_Search_Lucene_Document $document)
  458. {
  459. $this->_index->addDocument($document);
  460. }
  461. /**
  462. * Commit changes resulting from delete() or undeleteAll() operations.
  463. */
  464. public function commit()
  465. {
  466. $this->_index->commit();
  467. }
  468. /**
  469. * Optimize index.
  470. *
  471. * Merges all segments into one
  472. */
  473. public function optimize()
  474. {
  475. $this->_index->optimize();
  476. }
  477. /**
  478. * Returns an array of all terms in this index.
  479. *
  480. * @return array
  481. */
  482. public function terms()
  483. {
  484. return $this->_index->terms();
  485. }
  486. /**
  487. * Reset terms stream.
  488. */
  489. public function resetTermsStream()
  490. {
  491. $this->_index->resetTermsStream();
  492. }
  493. /**
  494. * Skip terms stream up to specified term preffix.
  495. *
  496. * Prefix contains fully specified field info and portion of searched term
  497. *
  498. * @param Zend_Search_Lucene_Index_Term $prefix
  499. */
  500. public function skipTo(Zend_Search_Lucene_Index_Term $prefix)
  501. {
  502. return $this->_index->skipTo($prefix);
  503. }
  504. /**
  505. * Scans terms dictionary and returns next term
  506. *
  507. * @return Zend_Search_Lucene_Index_Term|null
  508. */
  509. public function nextTerm()
  510. {
  511. return $this->_index->nextTerm();
  512. }
  513. /**
  514. * Returns term in current position
  515. *
  516. * @return Zend_Search_Lucene_Index_Term|null
  517. */
  518. public function currentTerm()
  519. {
  520. return $this->_index->currentTerm();
  521. }
  522. /**
  523. * Close terms stream
  524. *
  525. * Should be used for resources clean up if stream is not read up to the end
  526. */
  527. public function closeTermsStream()
  528. {
  529. $this->_index->closeTermsStream();
  530. }
  531. /**
  532. * Undeletes all documents currently marked as deleted in this index.
  533. */
  534. public function undeleteAll()
  535. {
  536. return $this->_index->undeleteAll();
  537. }
  538. /**
  539. * Add reference to the index object
  540. *
  541. * @internal
  542. */
  543. public function addReference()
  544. {
  545. return $this->_index->addReference();
  546. }
  547. /**
  548. * Remove reference from the index object
  549. *
  550. * When reference count becomes zero, index is closed and resources are cleaned up
  551. *
  552. * @internal
  553. */
  554. public function removeReference()
  555. {
  556. return $this->_index->removeReference();
  557. }
  558. }