PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Search/Lucene/Index/DictionaryLoader.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 266 lines | 167 code | 30 blank | 69 comment | 55 complexity | ff41c7736c98eee2730cba233e457dde MD5 | raw file
  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-2008 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_Exception */
  22. require_once 'Zend/Search/Lucene/Exception.php';
  23. /**
  24. * Dictionary loader
  25. *
  26. * It's a dummy class which is created to encapsulate non-good structured code.
  27. * Manual "method inlining" is performed to increase dictionary index loading operation
  28. * which is major bottelneck for search performance.
  29. *
  30. *
  31. * @category Zend
  32. * @package Zend_Search_Lucene
  33. * @subpackage Index
  34. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Search_Lucene_Index_DictionaryLoader
  38. {
  39. /**
  40. * Dictionary index loader.
  41. *
  42. * It takes a string which is actually <segment_name>.tii index file data and
  43. * returns two arrays - term and tremInfo lists.
  44. *
  45. * See Zend_Search_Lucene_Index_SegmintInfo class for details
  46. *
  47. * @param string $data
  48. * @return array
  49. * @throws Zend_Search_Lucene_Exception
  50. */
  51. public static function load($data)
  52. {
  53. $termDictionary = array();
  54. $termInfos = array();
  55. $pos = 0;
  56. // $tiVersion = $tiiFile->readInt();
  57. $tiVersion = ord($data[0]) << 24 | ord($data[1]) << 16 | ord($data[2]) << 8 | ord($data[3]);
  58. $pos += 4;
  59. if ($tiVersion != (int)0xFFFFFFFE /* pre-2.1 format */ &&
  60. $tiVersion != (int)0xFFFFFFFD /* 2.1+ format */) {
  61. throw new Zend_Search_Lucene_Exception('Wrong TermInfoIndexFile file format');
  62. }
  63. // $indexTermCount = $tiiFile->readLong();
  64. if (PHP_INT_SIZE > 4) {
  65. $indexTermCount = ord($data[$pos]) << 56 |
  66. ord($data[$pos+1]) << 48 |
  67. ord($data[$pos+2]) << 40 |
  68. ord($data[$pos+3]) << 32 |
  69. ord($data[$pos+4]) << 24 |
  70. ord($data[$pos+5]) << 16 |
  71. ord($data[$pos+6]) << 8 |
  72. ord($data[$pos+7]);
  73. } else {
  74. if ((ord($data[$pos]) != 0) ||
  75. (ord($data[$pos+1]) != 0) ||
  76. (ord($data[$pos+2]) != 0) ||
  77. (ord($data[$pos+3]) != 0) ||
  78. ((ord($data[$pos+4]) & 0x80) != 0)) {
  79. throw new Zend_Search_Lucene_Exception('Largest supported segment size (for 32-bit mode) is 2Gb');
  80. }
  81. $indexTermCount = ord($data[$pos+4]) << 24 |
  82. ord($data[$pos+5]) << 16 |
  83. ord($data[$pos+6]) << 8 |
  84. ord($data[$pos+7]);
  85. }
  86. $pos += 8;
  87. // $tiiFile->readInt(); // IndexInterval
  88. $pos += 4;
  89. // $skipInterval = $tiiFile->readInt();
  90. $skipInterval = ord($data[$pos]) << 24 | ord($data[$pos+1]) << 16 | ord($data[$pos+2]) << 8 | ord($data[$pos+3]);
  91. $pos += 4;
  92. if ($indexTermCount < 1) {
  93. throw new Zend_Search_Lucene_Exception('Wrong number of terms in a term dictionary index');
  94. }
  95. if ($tiVersion == (int)0xFFFFFFFD /* 2.1+ format */) {
  96. /* Skip MaxSkipLevels value */
  97. $pos += 4;
  98. }
  99. $prevTerm = '';
  100. $freqPointer = 0;
  101. $proxPointer = 0;
  102. $indexPointer = 0;
  103. for ($count = 0; $count < $indexTermCount; $count++) {
  104. //$termPrefixLength = $tiiFile->readVInt();
  105. $nbyte = ord($data[$pos++]);
  106. $termPrefixLength = $nbyte & 0x7F;
  107. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  108. $nbyte = ord($data[$pos++]);
  109. $termPrefixLength |= ($nbyte & 0x7F) << $shift;
  110. }
  111. // $termSuffix = $tiiFile->readString();
  112. $nbyte = ord($data[$pos++]);
  113. $len = $nbyte & 0x7F;
  114. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  115. $nbyte = ord($data[$pos++]);
  116. $len |= ($nbyte & 0x7F) << $shift;
  117. }
  118. if ($len == 0) {
  119. $termSuffix = '';
  120. } else {
  121. $termSuffix = substr($data, $pos, $len);
  122. $pos += $len;
  123. for ($count1 = 0; $count1 < $len; $count1++ ) {
  124. if (( ord($termSuffix[$count1]) & 0xC0 ) == 0xC0) {
  125. $addBytes = 1;
  126. if (ord($termSuffix[$count1]) & 0x20 ) {
  127. $addBytes++;
  128. // Never used for Java Lucene created index.
  129. // Java2 doesn't encode strings in four bytes
  130. if (ord($termSuffix[$count1]) & 0x10 ) {
  131. $addBytes++;
  132. }
  133. }
  134. $termSuffix .= substr($data, $pos, $addBytes);
  135. $pos += $addBytes;
  136. $len += $addBytes;
  137. // Check for null character. Java2 encodes null character
  138. // in two bytes.
  139. if (ord($termSuffix[$count1]) == 0xC0 &&
  140. ord($termSuffix[$count1+1]) == 0x80 ) {
  141. $termSuffix[$count1] = 0;
  142. $termSuffix = substr($termSuffix,0,$count1+1)
  143. . substr($termSuffix,$count1+2);
  144. }
  145. $count1 += $addBytes;
  146. }
  147. }
  148. }
  149. // $termValue = Zend_Search_Lucene_Index_Term::getPrefix($prevTerm, $termPrefixLength) . $termSuffix;
  150. $pb = 0; $pc = 0;
  151. while ($pb < strlen($prevTerm) && $pc < $termPrefixLength) {
  152. $charBytes = 1;
  153. if ((ord($prevTerm[$pb]) & 0xC0) == 0xC0) {
  154. $charBytes++;
  155. if (ord($prevTerm[$pb]) & 0x20 ) {
  156. $charBytes++;
  157. if (ord($prevTerm[$pb]) & 0x10 ) {
  158. $charBytes++;
  159. }
  160. }
  161. }
  162. if ($pb + $charBytes > strlen($data)) {
  163. // wrong character
  164. break;
  165. }
  166. $pc++;
  167. $pb += $charBytes;
  168. }
  169. $termValue = substr($prevTerm, 0, $pb) . $termSuffix;
  170. // $termFieldNum = $tiiFile->readVInt();
  171. $nbyte = ord($data[$pos++]);
  172. $termFieldNum = $nbyte & 0x7F;
  173. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  174. $nbyte = ord($data[$pos++]);
  175. $termFieldNum |= ($nbyte & 0x7F) << $shift;
  176. }
  177. // $docFreq = $tiiFile->readVInt();
  178. $nbyte = ord($data[$pos++]);
  179. $docFreq = $nbyte & 0x7F;
  180. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  181. $nbyte = ord($data[$pos++]);
  182. $docFreq |= ($nbyte & 0x7F) << $shift;
  183. }
  184. // $freqPointer += $tiiFile->readVInt();
  185. $nbyte = ord($data[$pos++]);
  186. $vint = $nbyte & 0x7F;
  187. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  188. $nbyte = ord($data[$pos++]);
  189. $vint |= ($nbyte & 0x7F) << $shift;
  190. }
  191. $freqPointer += $vint;
  192. // $proxPointer += $tiiFile->readVInt();
  193. $nbyte = ord($data[$pos++]);
  194. $vint = $nbyte & 0x7F;
  195. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  196. $nbyte = ord($data[$pos++]);
  197. $vint |= ($nbyte & 0x7F) << $shift;
  198. }
  199. $proxPointer += $vint;
  200. if( $docFreq >= $skipInterval ) {
  201. // $skipDelta = $tiiFile->readVInt();
  202. $nbyte = ord($data[$pos++]);
  203. $vint = $nbyte & 0x7F;
  204. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  205. $nbyte = ord($data[$pos++]);
  206. $vint |= ($nbyte & 0x7F) << $shift;
  207. }
  208. $skipDelta = $vint;
  209. } else {
  210. $skipDelta = 0;
  211. }
  212. // $indexPointer += $tiiFile->readVInt();
  213. $nbyte = ord($data[$pos++]);
  214. $vint = $nbyte & 0x7F;
  215. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  216. $nbyte = ord($data[$pos++]);
  217. $vint |= ($nbyte & 0x7F) << $shift;
  218. }
  219. $indexPointer += $vint;
  220. // $this->_termDictionary[] = new Zend_Search_Lucene_Index_Term($termValue, $termFieldNum);
  221. $termDictionary[] = array($termFieldNum, $termValue);
  222. $termInfos[] =
  223. // new Zend_Search_Lucene_Index_TermInfo($docFreq, $freqPointer, $proxPointer, $skipDelta, $indexPointer);
  224. array($docFreq, $freqPointer, $proxPointer, $skipDelta, $indexPointer);
  225. $prevTerm = $termValue;
  226. }
  227. // Check special index entry mark
  228. if ($termDictionary[0][0] != (int)0xFFFFFFFF) {
  229. throw new Zend_Search_Lucene_Exception('Wrong TermInfoIndexFile file format');
  230. } else if (PHP_INT_SIZE > 4){
  231. // Treat 64-bit 0xFFFFFFFF as -1
  232. $termDictionary[0][0] = -1;
  233. }
  234. return array(&$termDictionary, &$termInfos);
  235. }
  236. }