Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / search / Zend / Search / Lucene / Storage / File / Memory.php
blobe3567c66dab57b1a599809cf8f7c9f67b25e5cda
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Search_Lucene
17 * @subpackage Storage
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';
30 /**
31 * @category Zend
32 * @package Zend_Search_Lucene
33 * @subpackage Storage
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
39 /**
40 * FileData
42 * @var string
44 private $_data;
46 /**
47 * File Position
49 * @var integer
51 private $_position = 0;
54 /**
55 * Object constractor
57 * @param string $data
59 public function __construct($data)
61 $this->_data = $data;
64 /**
65 * Reads $length number of bytes at the current position in the
66 * file and advances the file pointer.
68 * @param integer $length
69 * @return string
71 protected function _fread($length = 1)
73 $returnValue = substr($this->_data, $this->_position, $length);
74 $this->_position += $length;
75 return $returnValue;
79 /**
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
88 * in offset.)
89 * Upon success, returns 0; otherwise, returns -1
91 * @param integer $offset
92 * @param integer $whence
93 * @return integer
95 public function seek($offset, $whence=SEEK_SET)
97 switch ($whence) {
98 case SEEK_SET:
99 $this->_position = $offset;
100 break;
102 case SEEK_CUR:
103 $this->_position += $offset;
104 break;
106 case SEEK_END:
107 $this->_position = strlen($this->_data);
108 $this->_position += $offset;
109 break;
111 default:
112 break;
117 * Get file position.
119 * @return integer
121 public function tell()
123 return $this->_position;
127 * Flush output.
129 * Returns true on success or false on failure.
131 * @return boolean
133 public function flush()
135 // Do nothing
137 return true;
141 * Writes $length number of bytes (all, if $length===null) to the end
142 * of the file.
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);
154 } else {
155 $this->_data .= $data;
158 $this->_position = strlen($this->_data);
162 * Lock file
164 * Lock type may be a LOCK_SH (shared lock) or a LOCK_EX (exclusive lock)
166 * @param integer $lockType
167 * @return boolean
169 public function lock($lockType, $nonBlockinLock = false)
171 // Memory files can't be shared
172 // do nothing
174 return true;
178 * Unlock file
180 public function unlock()
182 // Memory files can't be shared
183 // do nothing
187 * Reads a byte from the current position in the file
188 * and advances the file pointer.
190 * @return integer
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);
210 return 1;
214 * Read num bytes from the current position in the file
215 * and advances the file pointer.
217 * @param integer $num
218 * @return string
220 public function readBytes($num)
222 $returnValue = substr($this->_data, $this->_position, $num);
223 $this->_position += $num;
225 return $returnValue;
229 * Writes num bytes of data (all, if $num===null) to the end
230 * of the string.
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
240 if ($num !== null) {
241 $this->_data .= substr($data, 0, $num);
242 } else {
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.
254 * @return integer
256 public function readInt()
258 $str = substr($this->_data, $this->_position, 4);
259 $this->_position += 4;
261 return ord($str{0}) << 24 |
262 ord($str{1}) << 16 |
263 ord($str{2}) << 8 |
264 ord($str{3});
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) .
282 chr($value & 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.
292 * @return integer
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 |
306 ord($str{1}) << 48 |
307 ord($str{2}) << 40 |
308 ord($str{3}) << 32 |
309 ord($str{4}) << 24 |
310 ord($str{5}) << 16 |
311 ord($str{6}) << 8 |
312 ord($str{7});
313 } else {
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 |
323 ord($str{5}) << 16 |
324 ord($str{6}) << 8 |
325 ord($str{7});
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) .
353 chr($value & 0xFF);
354 } else {
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) .
363 chr($value & 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.
375 * @return integer
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;
386 return $val;
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 );
402 $value >>= 7;
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.
414 * @return string
416 public function readString()
418 $strlen = $this->readVInt();
419 if ($strlen == 0) {
420 return '';
421 } else {
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
432 * characters.
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) {
440 $addBytes = 1;
441 if (ord($str_val{$count}) & 0x20 ) {
442 $addBytes++;
444 // Never used. Java2 doesn't encode strings in four bytes
445 if (ord($str_val{$count}) & 0x10 ) {
446 $addBytes++;
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
454 // in two bytes.
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);
461 $count += $addBytes;
465 return $str_val;
470 * Writes a string to the end of file.
472 * @param string $str
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
487 * characters.
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
503 * \x00 by \xC0\x80
505 if ((ord($str{$count}) & 0xC0) == 0xC0) {
506 $addBytes = 1;
507 if (ord($str{$count}) & 0x20 ) {
508 $addBytes++;
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 ) {
513 $addBytes++;
516 $chars -= $addBytes;
518 if (ord($str{$count}) == 0 ) {
519 $containNullChars = true;
521 $count += $addBytes;
525 if ($chars < 0) {
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");
533 } else {
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.
545 * @return string
547 public function readBinary()
549 $length = $this->readVInt();
550 $returnValue = substr($this->_data, $this->_position, $length);
551 $this->_position += $length;
552 return $returnValue;