PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Search/Lucene/Storage/File.php

https://bitbucket.org/mercysam/zfs
PHP | 424 lines | 179 code | 45 blank | 200 comment | 35 complexity | 382d2af14aa123c7f73850fe08ca9687 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 Storage
  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. /**
  22. * @category Zend
  23. * @package Zend_Search_Lucene
  24. * @subpackage Storage
  25. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. abstract class Zend_Search_Lucene_Storage_File
  29. {
  30. /**
  31. * Reads $length number of bytes at the current position in the
  32. * file and advances the file pointer.
  33. *
  34. * @param integer $length
  35. * @return string
  36. */
  37. abstract protected function _fread($length=1);
  38. /**
  39. * Sets the file position indicator and advances the file pointer.
  40. * The new position, measured in bytes from the beginning of the file,
  41. * is obtained by adding offset to the position specified by whence,
  42. * whose values are defined as follows:
  43. * SEEK_SET - Set position equal to offset bytes.
  44. * SEEK_CUR - Set position to current location plus offset.
  45. * SEEK_END - Set position to end-of-file plus offset. (To move to
  46. * a position before the end-of-file, you need to pass a negative value
  47. * in offset.)
  48. * Upon success, returns 0; otherwise, returns -1
  49. *
  50. * @param integer $offset
  51. * @param integer $whence
  52. * @return integer
  53. */
  54. abstract public function seek($offset, $whence=SEEK_SET);
  55. /**
  56. * Get file position.
  57. *
  58. * @return integer
  59. */
  60. abstract public function tell();
  61. /**
  62. * Flush output.
  63. *
  64. * Returns true on success or false on failure.
  65. *
  66. * @return boolean
  67. */
  68. abstract public function flush();
  69. /**
  70. * Writes $length number of bytes (all, if $length===null) to the end
  71. * of the file.
  72. *
  73. * @param string $data
  74. * @param integer $length
  75. */
  76. abstract protected function _fwrite($data, $length=null);
  77. /**
  78. * Lock file
  79. *
  80. * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
  81. *
  82. * @param integer $lockType
  83. * @return boolean
  84. */
  85. abstract public function lock($lockType, $nonBlockinLock = false);
  86. /**
  87. * Unlock file
  88. */
  89. abstract public function unlock();
  90. /**
  91. * Reads a byte from the current position in the file
  92. * and advances the file pointer.
  93. *
  94. * @return integer
  95. */
  96. public function readByte()
  97. {
  98. return ord($this->_fread(1));
  99. }
  100. /**
  101. * Writes a byte to the end of the file.
  102. *
  103. * @param integer $byte
  104. */
  105. public function writeByte($byte)
  106. {
  107. return $this->_fwrite(chr($byte), 1);
  108. }
  109. /**
  110. * Read num bytes from the current position in the file
  111. * and advances the file pointer.
  112. *
  113. * @param integer $num
  114. * @return string
  115. */
  116. public function readBytes($num)
  117. {
  118. return $this->_fread($num);
  119. }
  120. /**
  121. * Writes num bytes of data (all, if $num===null) to the end
  122. * of the string.
  123. *
  124. * @param string $data
  125. * @param integer $num
  126. */
  127. public function writeBytes($data, $num=null)
  128. {
  129. $this->_fwrite($data, $num);
  130. }
  131. /**
  132. * Reads an integer from the current position in the file
  133. * and advances the file pointer.
  134. *
  135. * @return integer
  136. */
  137. public function readInt()
  138. {
  139. $str = $this->_fread(4);
  140. return ord($str[0]) << 24 |
  141. ord($str[1]) << 16 |
  142. ord($str[2]) << 8 |
  143. ord($str[3]);
  144. }
  145. /**
  146. * Writes an integer to the end of file.
  147. *
  148. * @param integer $value
  149. */
  150. public function writeInt($value)
  151. {
  152. settype($value, 'integer');
  153. $this->_fwrite( chr($value>>24 & 0xFF) .
  154. chr($value>>16 & 0xFF) .
  155. chr($value>>8 & 0xFF) .
  156. chr($value & 0xFF), 4 );
  157. }
  158. /**
  159. * Returns a long integer from the current position in the file
  160. * and advances the file pointer.
  161. *
  162. * @return integer
  163. * @throws Zend_Search_Lucene_Exception
  164. */
  165. public function readLong()
  166. {
  167. $str = $this->_fread(8);
  168. /**
  169. * Check, that we work in 64-bit mode.
  170. * fseek() uses long for offset. Thus, largest index segment file size in 32bit mode is 2Gb
  171. */
  172. if (PHP_INT_SIZE > 4) {
  173. return ord($str[0]) << 56 |
  174. ord($str[1]) << 48 |
  175. ord($str[2]) << 40 |
  176. ord($str[3]) << 32 |
  177. ord($str[4]) << 24 |
  178. ord($str[5]) << 16 |
  179. ord($str[6]) << 8 |
  180. ord($str[7]);
  181. } else {
  182. if ((ord($str[0]) != 0) ||
  183. (ord($str[1]) != 0) ||
  184. (ord($str[2]) != 0) ||
  185. (ord($str[3]) != 0) ||
  186. ((ord($str[0]) & 0x80) != 0)) {
  187. require_once 'Zend/Search/Lucene/Exception.php';
  188. throw new Zend_Search_Lucene_Exception('Largest supported segment size (for 32-bit mode) is 2Gb');
  189. }
  190. return ord($str[4]) << 24 |
  191. ord($str[5]) << 16 |
  192. ord($str[6]) << 8 |
  193. ord($str[7]);
  194. }
  195. }
  196. /**
  197. * Writes long integer to the end of file
  198. *
  199. * @param integer $value
  200. * @throws Zend_Search_Lucene_Exception
  201. */
  202. public function writeLong($value)
  203. {
  204. /**
  205. * Check, that we work in 64-bit mode.
  206. * fseek() and ftell() use long for offset. Thus, largest index segment file size in 32bit mode is 2Gb
  207. */
  208. if (PHP_INT_SIZE > 4) {
  209. settype($value, 'integer');
  210. $this->_fwrite( chr($value>>56 & 0xFF) .
  211. chr($value>>48 & 0xFF) .
  212. chr($value>>40 & 0xFF) .
  213. chr($value>>32 & 0xFF) .
  214. chr($value>>24 & 0xFF) .
  215. chr($value>>16 & 0xFF) .
  216. chr($value>>8 & 0xFF) .
  217. chr($value & 0xFF), 8 );
  218. } else {
  219. if ($value > 0x7FFFFFFF) {
  220. require_once 'Zend/Search/Lucene/Exception.php';
  221. throw new Zend_Search_Lucene_Exception('Largest supported segment size (for 32-bit mode) is 2Gb');
  222. }
  223. $this->_fwrite( "\x00\x00\x00\x00" .
  224. chr($value>>24 & 0xFF) .
  225. chr($value>>16 & 0xFF) .
  226. chr($value>>8 & 0xFF) .
  227. chr($value & 0xFF), 8 );
  228. }
  229. }
  230. /**
  231. * Returns a variable-length integer from the current
  232. * position in the file and advances the file pointer.
  233. *
  234. * @return integer
  235. */
  236. public function readVInt()
  237. {
  238. $nextByte = ord($this->_fread(1));
  239. $val = $nextByte & 0x7F;
  240. for ($shift=7; ($nextByte & 0x80) != 0; $shift += 7) {
  241. $nextByte = ord($this->_fread(1));
  242. $val |= ($nextByte & 0x7F) << $shift;
  243. }
  244. return $val;
  245. }
  246. /**
  247. * Writes a variable-length integer to the end of file.
  248. *
  249. * @param integer $value
  250. */
  251. public function writeVInt($value)
  252. {
  253. settype($value, 'integer');
  254. while ($value > 0x7F) {
  255. $this->_fwrite(chr( ($value & 0x7F)|0x80 ));
  256. $value >>= 7;
  257. }
  258. $this->_fwrite(chr($value));
  259. }
  260. /**
  261. * Reads a string from the current position in the file
  262. * and advances the file pointer.
  263. *
  264. * @return string
  265. */
  266. public function readString()
  267. {
  268. $strlen = $this->readVInt();
  269. if ($strlen == 0) {
  270. return '';
  271. } else {
  272. /**
  273. * This implementation supports only Basic Multilingual Plane
  274. * (BMP) characters (from 0x0000 to 0xFFFF) and doesn't support
  275. * "supplementary characters" (characters whose code points are
  276. * greater than 0xFFFF)
  277. * Java 2 represents these characters as a pair of char (16-bit)
  278. * values, the first from the high-surrogates range (0xD800-0xDBFF),
  279. * the second from the low-surrogates range (0xDC00-0xDFFF). Then
  280. * they are encoded as usual UTF-8 characters in six bytes.
  281. * Standard UTF-8 representation uses four bytes for supplementary
  282. * characters.
  283. */
  284. $str_val = $this->_fread($strlen);
  285. for ($count = 0; $count < $strlen; $count++ ) {
  286. if (( ord($str_val[$count]) & 0xC0 ) == 0xC0) {
  287. $addBytes = 1;
  288. if (ord($str_val[$count]) & 0x20 ) {
  289. $addBytes++;
  290. // Never used. Java2 doesn't encode strings in four bytes
  291. if (ord($str_val[$count]) & 0x10 ) {
  292. $addBytes++;
  293. }
  294. }
  295. $str_val .= $this->_fread($addBytes);
  296. $strlen += $addBytes;
  297. // Check for null character. Java2 encodes null character
  298. // in two bytes.
  299. if (ord($str_val[$count]) == 0xC0 &&
  300. ord($str_val[$count+1]) == 0x80 ) {
  301. $str_val[$count] = 0;
  302. $str_val = substr($str_val,0,$count+1)
  303. . substr($str_val,$count+2);
  304. }
  305. $count += $addBytes;
  306. }
  307. }
  308. return $str_val;
  309. }
  310. }
  311. /**
  312. * Writes a string to the end of file.
  313. *
  314. * @param string $str
  315. * @throws Zend_Search_Lucene_Exception
  316. */
  317. public function writeString($str)
  318. {
  319. /**
  320. * This implementation supports only Basic Multilingual Plane
  321. * (BMP) characters (from 0x0000 to 0xFFFF) and doesn't support
  322. * "supplementary characters" (characters whose code points are
  323. * greater than 0xFFFF)
  324. * Java 2 represents these characters as a pair of char (16-bit)
  325. * values, the first from the high-surrogates range (0xD800-0xDBFF),
  326. * the second from the low-surrogates range (0xDC00-0xDFFF). Then
  327. * they are encoded as usual UTF-8 characters in six bytes.
  328. * Standard UTF-8 representation uses four bytes for supplementary
  329. * characters.
  330. */
  331. // convert input to a string before iterating string characters
  332. settype($str, 'string');
  333. $chars = $strlen = strlen($str);
  334. $containNullChars = false;
  335. for ($count = 0; $count < $strlen; $count++ ) {
  336. /**
  337. * String is already in Java 2 representation.
  338. * We should only calculate actual string length and replace
  339. * \x00 by \xC0\x80
  340. */
  341. if ((ord($str[$count]) & 0xC0) == 0xC0) {
  342. $addBytes = 1;
  343. if (ord($str[$count]) & 0x20 ) {
  344. $addBytes++;
  345. // Never used. Java2 doesn't encode strings in four bytes
  346. // and we dont't support non-BMP characters
  347. if (ord($str[$count]) & 0x10 ) {
  348. $addBytes++;
  349. }
  350. }
  351. $chars -= $addBytes;
  352. if (ord($str[$count]) == 0 ) {
  353. $containNullChars = true;
  354. }
  355. $count += $addBytes;
  356. }
  357. }
  358. if ($chars < 0) {
  359. require_once 'Zend/Search/Lucene/Exception.php';
  360. throw new Zend_Search_Lucene_Exception('Invalid UTF-8 string');
  361. }
  362. $this->writeVInt($chars);
  363. if ($containNullChars) {
  364. $this->_fwrite(str_replace($str, "\x00", "\xC0\x80"));
  365. } else {
  366. $this->_fwrite($str);
  367. }
  368. }
  369. /**
  370. * Reads binary data from the current position in the file
  371. * and advances the file pointer.
  372. *
  373. * @return string
  374. */
  375. public function readBinary()
  376. {
  377. return $this->_fread($this->readVInt());
  378. }
  379. }