2
0

Proxy.php 16 KB

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