PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 260 lines | 164 code | 29 blank | 67 comment | 54 complexity | d8869b154e075d655f602f0f6dd8a663 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. }
  129. $termSuffix .= substr($data, $pos, $addBytes);
  130. $pos += $addBytes;
  131. $len += $addBytes;
  132. // Check for null character. Java2 encodes null character
  133. // in two bytes.
  134. if (ord($termSuffix[$count1]) == 0xC0 &&
  135. ord($termSuffix[$count1+1]) == 0x80 ) {
  136. $termSuffix[$count1] = 0;
  137. $termSuffix = substr($termSuffix,0,$count1+1)
  138. . substr($termSuffix,$count1+2);
  139. }
  140. $count1 += $addBytes;
  141. }
  142. }
  143. }
  144. // $termValue = Zend_Search_Lucene_Index_Term::getPrefix($prevTerm, $termPrefixLength) . $termSuffix;
  145. $pb = 0; $pc = 0;
  146. while ($pb < strlen($prevTerm) && $pc < $termPrefixLength) {
  147. $charBytes = 1;
  148. if ((ord($prevTerm[$pb]) & 0xC0) == 0xC0) {
  149. $charBytes++;
  150. if (ord($prevTerm[$pb]) & 0x20 ) {
  151. $charBytes++;
  152. if (ord($prevTerm[$pb]) & 0x10 ) {
  153. $charBytes++;
  154. }
  155. }
  156. }
  157. if ($pb + $charBytes > strlen($data)) {
  158. // wrong character
  159. break;
  160. }
  161. $pc++;
  162. $pb += $charBytes;
  163. }
  164. $termValue = substr($prevTerm, 0, $pb) . $termSuffix;
  165. // $termFieldNum = $tiiFile->readVInt();
  166. $nbyte = ord($data[$pos++]);
  167. $termFieldNum = $nbyte & 0x7F;
  168. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  169. $nbyte = ord($data[$pos++]);
  170. $termFieldNum |= ($nbyte & 0x7F) << $shift;
  171. }
  172. // $docFreq = $tiiFile->readVInt();
  173. $nbyte = ord($data[$pos++]);
  174. $docFreq = $nbyte & 0x7F;
  175. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  176. $nbyte = ord($data[$pos++]);
  177. $docFreq |= ($nbyte & 0x7F) << $shift;
  178. }
  179. // $freqPointer += $tiiFile->readVInt();
  180. $nbyte = ord($data[$pos++]);
  181. $vint = $nbyte & 0x7F;
  182. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  183. $nbyte = ord($data[$pos++]);
  184. $vint |= ($nbyte & 0x7F) << $shift;
  185. }
  186. $freqPointer += $vint;
  187. // $proxPointer += $tiiFile->readVInt();
  188. $nbyte = ord($data[$pos++]);
  189. $vint = $nbyte & 0x7F;
  190. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  191. $nbyte = ord($data[$pos++]);
  192. $vint |= ($nbyte & 0x7F) << $shift;
  193. }
  194. $proxPointer += $vint;
  195. if( $docFreq >= $skipInterval ) {
  196. // $skipDelta = $tiiFile->readVInt();
  197. $nbyte = ord($data[$pos++]);
  198. $vint = $nbyte & 0x7F;
  199. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  200. $nbyte = ord($data[$pos++]);
  201. $vint |= ($nbyte & 0x7F) << $shift;
  202. }
  203. $skipDelta = $vint;
  204. } else {
  205. $skipDelta = 0;
  206. }
  207. // $indexPointer += $tiiFile->readVInt();
  208. $nbyte = ord($data[$pos++]);
  209. $vint = $nbyte & 0x7F;
  210. for ($shift=7; ($nbyte & 0x80) != 0; $shift += 7) {
  211. $nbyte = ord($data[$pos++]);
  212. $vint |= ($nbyte & 0x7F) << $shift;
  213. }
  214. $indexPointer += $vint;
  215. // $this->_termDictionary[] = new Zend_Search_Lucene_Index_Term($termValue, $termFieldNum);
  216. $termDictionary[] = array($termFieldNum, $termValue);
  217. $termInfos[] =
  218. // new Zend_Search_Lucene_Index_TermInfo($docFreq, $freqPointer, $proxPointer, $skipDelta, $indexPointer);
  219. array($docFreq, $freqPointer, $proxPointer, $skipDelta, $indexPointer);
  220. $prevTerm = $termValue;
  221. }
  222. // Check special index entry mark
  223. if ($termDictionary[0][0] != (int)0xFFFFFFFF) {
  224. throw new Zend_Search_Lucene_Exception('Wrong TermInfoIndexFile file format');
  225. } else if (PHP_INT_SIZE > 4){
  226. // Treat 64-bit 0xFFFFFFFF as -1
  227. $termDictionary[0][0] = -1;
  228. }
  229. return array(&$termDictionary, &$termInfos);
  230. }
  231. }