PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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