3 * This is a port of D.J. Bernstein's CDB to PHP. It's based on the copy that
4 * appears in PHP 5.3. Changes are:
5 * * Error returns replaced with exceptions
6 * * Exception thrown if sizes or offsets are between 2GB and 4GB
7 * * Some variables renamed
13 * Common functions for readers and writers
17 * Take a modulo of a signed integer as if it were an unsigned integer.
18 * $b must be less than 0x40000000 and greater than 0
25 public static function unsignedMod( $a, $b ) {
26 if ( $a & 0x80000000 ) {
27 $m = ( $a & 0x7fffffff ) %
$b +
2 * ( 0x40000000 %
$b );
35 * Shift a signed integer right as if it were unsigned
40 public static function unsignedShiftRight( $a, $b ) {
44 if ( $a & 0x80000000 ) {
45 return ( ( $a & 0x7fffffff ) >> $b ) |
( 0x40000000 >> ( $b - 1 ) );
52 * The CDB hash function.
58 public static function hash( $s ) {
60 for ( $i = 0; $i < strlen( $s ); $i++
) {
61 $h5 = ($h << 5) & 0xffffffff;
63 // Inlined here for speed
64 $sum = ($h & 0x3fffffff) +
($h5 & 0x3fffffff);
67 ( $sum & 0x40000000 ?
1 : 0 )
68 +
( $h & 0x80000000 ?
2 : 0 )
69 +
( $h & 0x40000000 ?
1 : 0 )
70 +
( $h5 & 0x80000000 ?
2 : 0 )
71 +
( $h5 & 0x40000000 ?
1 : 0 )
73 |
( $sum & 0x3fffffff );
84 class CdbReader_PHP
extends CdbReader
{
88 /** The file handle */
91 /* number of hash slots searched under this key */
94 /* initialized if loop is nonzero */
97 /* initialized if loop is nonzero */
100 /* initialized if loop is nonzero */
103 /* initialized if loop is nonzero */
106 /* initialized if findNext() returns true */
109 /* initialized if cdb_findnext() returns 1 */
113 * @param $fileName string
115 function __construct( $fileName ) {
116 $this->fileName
= $fileName;
117 $this->handle
= fopen( $fileName, 'rb' );
118 if ( !$this->handle
) {
119 throw new MWException( 'Unable to open CDB file "' . $this->fileName
. '".' );
125 if( isset( $this->handle
) ) {
126 fclose( $this->handle
);
128 unset( $this->handle
);
133 * @return bool|string
135 public function get( $key ) {
136 // strval is required
137 if ( $this->find( strval( $key ) ) ) {
138 return $this->read( $this->dlen
, $this->dpos
);
149 protected function match( $key, $pos ) {
150 $buf = $this->read( strlen( $key ), $pos );
151 return $buf === $key;
154 protected function findStart() {
159 * @throws MWException
164 protected function read( $length, $pos ) {
165 if ( fseek( $this->handle
, $pos ) == -1 ) {
166 // This can easily happen if the internal pointers are incorrect
167 throw new MWException(
168 'Seek failed, file "' . $this->fileName
. '" may be corrupted.' );
171 if ( $length == 0 ) {
175 $buf = fread( $this->handle
, $length );
176 if ( $buf === false ||
strlen( $buf ) !== $length ) {
177 throw new MWException(
178 'Read from CDB file failed, file "' . $this->fileName
. '" may be corrupted.' );
184 * Unpack an unsigned integer and throw an exception if it needs more than 31 bits
188 protected function unpack31( $s ) {
189 $data = unpack( 'V', $s );
190 if ( $data[1] > 0x7fffffff ) {
191 throw new MWException(
192 'Error in CDB file "' . $this->fileName
. '", integer too big.' );
198 * Unpack a 32-bit signed integer
202 protected function unpackSigned( $s ) {
203 $data = unpack( 'va/vb', $s );
204 return $data['a'] |
( $data['b'] << 16 );
211 protected function findNext( $key ) {
212 if ( !$this->loop
) {
213 $u = CdbFunctions
::hash( $key );
214 $buf = $this->read( 8, ( $u << 3 ) & 2047 );
215 $this->hslots
= $this->unpack31( substr( $buf, 4 ) );
216 if ( !$this->hslots
) {
219 $this->hpos
= $this->unpack31( substr( $buf, 0, 4 ) );
221 $u = CdbFunctions
::unsignedShiftRight( $u, 8 );
222 $u = CdbFunctions
::unsignedMod( $u, $this->hslots
);
224 $this->kpos
= $this->hpos +
$u;
227 while ( $this->loop
< $this->hslots
) {
228 $buf = $this->read( 8, $this->kpos
);
229 $pos = $this->unpack31( substr( $buf, 4 ) );
235 if ( $this->kpos
== $this->hpos +
( $this->hslots
<< 3 ) ) {
236 $this->kpos
= $this->hpos
;
238 $u = $this->unpackSigned( substr( $buf, 0, 4 ) );
239 if ( $u === $this->khash
) {
240 $buf = $this->read( 8, $pos );
241 $keyLen = $this->unpack31( substr( $buf, 0, 4 ) );
242 if ( $keyLen == strlen( $key ) && $this->match( $key, $pos +
8 ) ) {
244 $this->dlen
= $this->unpack31( substr( $buf, 4 ) );
245 $this->dpos
= $pos +
8 +
$keyLen;
257 protected function find( $key ) {
259 return $this->findNext( $key );
266 class CdbWriter_PHP
extends CdbWriter
{
267 var $handle, $realFileName, $tmpFileName;
270 var $numentries, $pos;
273 * @param $fileName string
275 function __construct( $fileName ) {
276 $this->realFileName
= $fileName;
277 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
278 $this->handle
= fopen( $this->tmpFileName
, 'wb' );
279 if ( !$this->handle
) {
280 $this->throwException(
281 'Unable to open CDB file "' . $this->tmpFileName
. '" for write.' );
283 $this->hplist
= array();
284 $this->numentries
= 0;
285 $this->pos
= 2048; // leaving space for the pointer array, 256 * 8
286 if ( fseek( $this->handle
, $this->pos
) == -1 ) {
287 $this->throwException( 'fseek failed in file "' . $this->tmpFileName
. '".' );
291 function __destruct() {
292 if ( isset( $this->handle
) ) {
302 public function set( $key, $value ) {
303 if ( strval( $key ) === '' ) {
304 // DBA cross-check hack
307 $this->addbegin( strlen( $key ), strlen( $value ) );
308 $this->write( $key );
309 $this->write( $value );
310 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions
::hash( $key ) );
314 * @throws MWException
316 public function close() {
318 if( isset($this->handle
) ) {
319 fclose( $this->handle
);
321 if ( wfIsWindows() && file_exists($this->realFileName
) ) {
322 unlink( $this->realFileName
);
324 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
325 $this->throwException( 'Unable to move the new CDB file into place.' );
327 unset( $this->handle
);
331 * @throws MWException
334 protected function write( $buf ) {
335 $len = fwrite( $this->handle
, $buf );
336 if ( $len !== strlen( $buf ) ) {
337 $this->throwException( 'Error writing to CDB file "'.$this->tmpFileName
.'".' );
342 * @throws MWException
345 protected function posplus( $len ) {
346 $newpos = $this->pos +
$len;
347 if ( $newpos > 0x7fffffff ) {
348 $this->throwException(
349 'A value in the CDB file "'.$this->tmpFileName
.'" is too large.' );
351 $this->pos
= $newpos;
359 protected function addend( $keylen, $datalen, $h ) {
360 $this->hplist
[] = array(
367 $this->posplus( $keylen );
368 $this->posplus( $datalen );
372 * @throws MWException
376 protected function addbegin( $keylen, $datalen ) {
377 if ( $keylen > 0x7fffffff ) {
378 $this->throwException( 'Key length too long in file "'.$this->tmpFileName
.'".' );
380 if ( $datalen > 0x7fffffff ) {
381 $this->throwException( 'Data length too long in file "'.$this->tmpFileName
.'".' );
383 $buf = pack( 'VV', $keylen, $datalen );
384 $this->write( $buf );
388 * @throws MWException
390 protected function finish() {
391 // Hack for DBA cross-check
392 $this->hplist
= array_reverse( $this->hplist
);
394 // Calculate the number of items that will be in each hashtable
395 $counts = array_fill( 0, 256, 0 );
396 foreach ( $this->hplist
as $item ) {
397 ++
$counts[ 255 & $item['h'] ];
400 // Fill in $starts with the *end* indexes
403 for ( $i = 0; $i < 256; ++
$i ) {
408 // Excessively clever and indulgent code to simultaneously fill $packedTables
409 // with the packed hashtables, and adjust the elements of $starts
410 // to actually point to the starts instead of the ends.
411 $packedTables = array_fill( 0, $this->numentries
, false );
412 foreach ( $this->hplist
as $item ) {
413 $packedTables[--$starts[255 & $item['h']]] = $item;
417 for ( $i = 0; $i < 256; ++
$i ) {
418 $count = $counts[$i];
420 // The size of the hashtable will be double the item count.
421 // The rest of the slots will be empty.
422 $len = $count +
$count;
423 $final .= pack( 'VV', $this->pos
, $len );
425 $hashtable = array();
426 for ( $u = 0; $u < $len; ++
$u ) {
427 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
430 // Fill the hashtable, using the next empty slot if the hashed slot
432 for ( $u = 0; $u < $count; ++
$u ) {
433 $hp = $packedTables[$starts[$i] +
$u];
434 $where = CdbFunctions
::unsignedMod(
435 CdbFunctions
::unsignedShiftRight( $hp['h'], 8 ), $len );
436 while ( $hashtable[$where]['p'] )
437 if ( ++
$where == $len )
439 $hashtable[$where] = $hp;
442 // Write the hashtable
443 for ( $u = 0; $u < $len; ++
$u ) {
445 $hashtable[$u]['h'] & 0xffff,
446 CdbFunctions
::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
447 $hashtable[$u]['p'] );
448 $this->write( $buf );
453 // Write the pointer array at the start of the file
454 rewind( $this->handle
);
455 if ( ftell( $this->handle
) != 0 ) {
456 $this->throwException( 'Error rewinding to start of file "'.$this->tmpFileName
.'".' );
458 $this->write( $final );
462 * Clean up the temp file and throw an exception
465 * @throws MWException
467 protected function throwException( $msg ) {
468 if ( $this->handle
) {
469 fclose( $this->handle
);
470 unlink( $this->tmpFileName
);
472 throw new MWException( $msg );