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.
16 * @package Zend_Search_Lucene
18 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /** Zend_Search_Lucene_Storage_File */
24 require_once $CFG->dirroot
.'/search/Zend/Search/Lucene/Storage/File.php';
26 /** Zend_Search_Lucene_Exception */
27 require_once $CFG->dirroot
.'/search/Zend/Search/Lucene/Exception.php';
32 * @package Zend_Search_Lucene
34 * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license http://framework.zend.com/license/new-bsd New BSD License
37 class Zend_Search_Lucene_Storage_File_Memory
extends Zend_Search_Lucene_Storage_File
51 private $_position = 0;
59 public function __construct($data)
65 * Reads $length number of bytes at the current position in the
66 * file and advances the file pointer.
68 * @param integer $length
71 protected function _fread($length = 1)
73 $returnValue = substr($this->_data
, $this->_position
, $length);
74 $this->_position +
= $length;
80 * Sets the file position indicator and advances the file pointer.
81 * The new position, measured in bytes from the beginning of the file,
82 * is obtained by adding offset to the position specified by whence,
83 * whose values are defined as follows:
84 * SEEK_SET - Set position equal to offset bytes.
85 * SEEK_CUR - Set position to current location plus offset.
86 * SEEK_END - Set position to end-of-file plus offset. (To move to
87 * a position before the end-of-file, you need to pass a negative value
89 * Upon success, returns 0; otherwise, returns -1
91 * @param integer $offset
92 * @param integer $whence
95 public function seek($offset, $whence=SEEK_SET
)
99 $this->_position
= $offset;
103 $this->_position +
= $offset;
107 $this->_position
= strlen($this->_data
);
108 $this->_position +
= $offset;
121 public function tell()
123 return $this->_position
;
129 * Returns true on success or false on failure.
133 public function flush()
141 * Writes $length number of bytes (all, if $length===null) to the end
144 * @param string $data
145 * @param integer $length
147 protected function _fwrite($data, $length=null)
149 // We do not need to check if file position points to the end of "file".
150 // Only append operation is supported now
152 if ($length !== null) {
153 $this->_data
.= substr($data, 0, $length);
155 $this->_data
.= $data;
158 $this->_position
= strlen($this->_data
);
164 * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
166 * @param integer $lockType
169 public function lock($lockType, $nonBlockinLock = false)
171 // Memory files can't be shared
180 public function unlock()
182 // Memory files can't be shared
187 * Reads a byte from the current position in the file
188 * and advances the file pointer.
192 public function readByte()
194 return ord($this->_data
[$this->_position++
]);
198 * Writes a byte to the end of the file.
200 * @param integer $byte
202 public function writeByte($byte)
204 // We do not need to check if file position points to the end of "file".
205 // Only append operation is supported now
207 $this->_data
.= chr($byte);
208 $this->_position
= strlen($this->_data
);
214 * Read num bytes from the current position in the file
215 * and advances the file pointer.
217 * @param integer $num
220 public function readBytes($num)
222 $returnValue = substr($this->_data
, $this->_position
, $num);
223 $this->_position +
= $num;
229 * Writes num bytes of data (all, if $num===null) to the end
232 * @param string $data
233 * @param integer $num
235 public function writeBytes($data, $num=null)
237 // We do not need to check if file position points to the end of "file".
238 // Only append operation is supported now
241 $this->_data
.= substr($data, 0, $num);
243 $this->_data
.= $data;
246 $this->_position
= strlen($this->_data
);
251 * Reads an integer from the current position in the file
252 * and advances the file pointer.
256 public function readInt()
258 $str = substr($this->_data
, $this->_position
, 4);
259 $this->_position +
= 4;
261 return ord($str{0}) << 24 |
269 * Writes an integer to the end of file.
271 * @param integer $value
273 public function writeInt($value)
275 // We do not need to check if file position points to the end of "file".
276 // Only append operation is supported now
278 settype($value, 'integer');
279 $this->_data
.= chr($value>>24 & 0xFF) .
280 chr($value>>16 & 0xFF) .
281 chr($value>>8 & 0xFF) .
284 $this->_position
= strlen($this->_data
);
289 * Returns a long integer from the current position in the file
290 * and advances the file pointer.
293 * @throws Zend_Search_Lucene_Exception
295 public function readLong()
297 $str = substr($this->_data
, $this->_position
, 8);
298 $this->_position +
= 8;
301 * Check, that we work in 64-bit mode.
302 * fseek() uses long for offset. Thus, largest index segment file size in 32bit mode is 2Gb
304 if (PHP_INT_SIZE
> 4) {
305 return ord($str{0}) << 56 |
314 if ((ord($str{0}) != 0) ||
315 (ord($str{1}) != 0) ||
316 (ord($str{2}) != 0) ||
317 (ord($str{3}) != 0) ||
318 ((ord($str{0}) & 0x80) != 0)) {
319 throw new Zend_Search_Lucene_Exception('Largest supported segment size (for 32-bit mode) is 2Gb');
322 return ord($str{4}) << 24 |
330 * Writes long integer to the end of file
332 * @param integer $value
333 * @throws Zend_Search_Lucene_Exception
335 public function writeLong($value)
337 // We do not need to check if file position points to the end of "file".
338 // Only append operation is supported now
341 * Check, that we work in 64-bit mode.
342 * fseek() and ftell() use long for offset. Thus, largest index segment file size in 32bit mode is 2Gb
344 if (PHP_INT_SIZE
> 4) {
345 settype($value, 'integer');
346 $this->_data
.= chr($value>>56 & 0xFF) .
347 chr($value>>48 & 0xFF) .
348 chr($value>>40 & 0xFF) .
349 chr($value>>32 & 0xFF) .
350 chr($value>>24 & 0xFF) .
351 chr($value>>16 & 0xFF) .
352 chr($value>>8 & 0xFF) .
355 if ($value > 0x7FFFFFFF) {
356 throw new Zend_Search_Lucene_Exception('Largest supported segment size (for 32-bit mode) is 2Gb');
359 $this->_data
.= chr(0) . chr(0) . chr(0) . chr(0) .
360 chr($value>>24 & 0xFF) .
361 chr($value>>16 & 0xFF) .
362 chr($value>>8 & 0xFF) .
366 $this->_position
= strlen($this->_data
);
372 * Returns a variable-length integer from the current
373 * position in the file and advances the file pointer.
377 public function readVInt()
379 $nextByte = ord($this->_data
[$this->_position++
]);
380 $val = $nextByte & 0x7F;
382 for ($shift=7; ($nextByte & 0x80) != 0; $shift +
= 7) {
383 $nextByte = ord($this->_data
[$this->_position++
]);
384 $val |
= ($nextByte & 0x7F) << $shift;
390 * Writes a variable-length integer to the end of file.
392 * @param integer $value
394 public function writeVInt($value)
396 // We do not need to check if file position points to the end of "file".
397 // Only append operation is supported now
399 settype($value, 'integer');
400 while ($value > 0x7F) {
401 $this->_data
.= chr( ($value & 0x7F)|
0x80 );
404 $this->_data
.= chr($value);
406 $this->_position
= strlen($this->_data
);
411 * Reads a string from the current position in the file
412 * and advances the file pointer.
416 public function readString()
418 $strlen = $this->readVInt();
423 * This implementation supports only Basic Multilingual Plane
424 * (BMP) characters (from 0x0000 to 0xFFFF) and doesn't support
425 * "supplementary characters" (characters whose code points are
426 * greater than 0xFFFF)
427 * Java 2 represents these characters as a pair of char (16-bit)
428 * values, the first from the high-surrogates range (0xD800-0xDBFF),
429 * the second from the low-surrogates range (0xDC00-0xDFFF). Then
430 * they are encoded as usual UTF-8 characters in six bytes.
431 * Standard UTF-8 representation uses four bytes for supplementary
435 $str_val = substr($this->_data
, $this->_position
, $strlen);
436 $this->_position +
= $strlen;
438 for ($count = 0; $count < $strlen; $count++
) {
439 if (( ord($str_val{$count}) & 0xC0 ) == 0xC0) {
441 if (ord($str_val{$count}) & 0x20 ) {
444 // Never used. Java2 doesn't encode strings in four bytes
445 if (ord($str_val{$count}) & 0x10 ) {
449 $str_val .= substr($this->_data
, $this->_position
, $addBytes);
450 $this->_position +
= $addBytes;
451 $strlen +
= $addBytes;
453 // Check for null character. Java2 encodes null character
455 if (ord($str_val{$count}) == 0xC0 &&
456 ord($str_val{$count+
1}) == 0x80 ) {
457 $str_val{$count} = 0;
458 $str_val = substr($str_val,0,$count+
1)
459 . substr($str_val,$count+
2);
470 * Writes a string to the end of file.
473 * @throws Zend_Search_Lucene_Exception
475 public function writeString($str)
478 * This implementation supports only Basic Multilingual Plane
479 * (BMP) characters (from 0x0000 to 0xFFFF) and doesn't support
480 * "supplementary characters" (characters whose code points are
481 * greater than 0xFFFF)
482 * Java 2 represents these characters as a pair of char (16-bit)
483 * values, the first from the high-surrogates range (0xD800-0xDBFF),
484 * the second from the low-surrogates range (0xDC00-0xDFFF). Then
485 * they are encoded as usual UTF-8 characters in six bytes.
486 * Standard UTF-8 representation uses four bytes for supplementary
490 // We do not need to check if file position points to the end of "file".
491 // Only append operation is supported now
493 // convert input to a string before iterating string characters
494 settype($str, 'string');
496 $chars = $strlen = strlen($str);
497 $containNullChars = false;
499 for ($count = 0; $count < $strlen; $count++
) {
501 * String is already in Java 2 representation.
502 * We should only calculate actual string length and replace
505 if ((ord($str{$count}) & 0xC0) == 0xC0) {
507 if (ord($str{$count}) & 0x20 ) {
510 // Never used. Java2 doesn't encode strings in four bytes
511 // and we dont't support non-BMP characters
512 if (ord($str{$count}) & 0x10 ) {
518 if (ord($str{$count}) == 0 ) {
519 $containNullChars = true;
526 throw new Zend_Search_Lucene_Exception('Invalid UTF-8 string');
529 $this->writeVInt($chars);
530 if ($containNullChars) {
531 $this->_data
.= str_replace($str, "\x00", "\xC0\x80");
534 $this->_data
.= $str;
537 $this->_position
= strlen($this->_data
);
542 * Reads binary data from the current position in the file
543 * and advances the file pointer.
547 public function readBinary()
549 $length = $this->readVInt();
550 $returnValue = substr($this->_data
, $this->_position
, $length);
551 $this->_position +
= $length;