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
{
85 /** The file handle */
88 /* number of hash slots searched under this key */
91 /* initialized if loop is nonzero */
94 /* initialized if loop is nonzero */
97 /* initialized if loop is nonzero */
100 /* initialized if loop is nonzero */
103 /* initialized if findNext() returns true */
106 /* initialized if cdb_findnext() returns 1 */
110 * @param $fileName string
112 function __construct( $fileName ) {
113 $this->handle
= fopen( $fileName, 'rb' );
114 if ( !$this->handle
) {
115 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
121 if( isset( $this->handle
) ) {
122 fclose( $this->handle
);
124 unset( $this->handle
);
129 * @return bool|string
131 public function get( $key ) {
132 // strval is required
133 if ( $this->find( strval( $key ) ) ) {
134 return $this->read( $this->dlen
, $this->dpos
);
145 protected function match( $key, $pos ) {
146 $buf = $this->read( strlen( $key ), $pos );
147 return $buf === $key;
150 protected function findStart() {
155 * @throws MWException
160 protected function read( $length, $pos ) {
161 if ( fseek( $this->handle
, $pos ) == -1 ) {
162 // This can easily happen if the internal pointers are incorrect
163 throw new MWException( __METHOD__
.': seek failed, file may be corrupted.' );
166 if ( $length == 0 ) {
170 $buf = fread( $this->handle
, $length );
171 if ( $buf === false ||
strlen( $buf ) !== $length ) {
172 throw new MWException( __METHOD__
.': read from CDB file failed, file may be corrupted' );
178 * Unpack an unsigned integer and throw an exception if it needs more than 31 bits
182 protected function unpack31( $s ) {
183 $data = unpack( 'V', $s );
184 if ( $data[1] > 0x7fffffff ) {
185 throw new MWException( __METHOD__
.': error in CDB file, integer too big' );
191 * Unpack a 32-bit signed integer
195 protected function unpackSigned( $s ) {
196 $data = unpack( 'va/vb', $s );
197 return $data['a'] |
( $data['b'] << 16 );
204 protected function findNext( $key ) {
205 if ( !$this->loop
) {
206 $u = CdbFunctions
::hash( $key );
207 $buf = $this->read( 8, ( $u << 3 ) & 2047 );
208 $this->hslots
= $this->unpack31( substr( $buf, 4 ) );
209 if ( !$this->hslots
) {
212 $this->hpos
= $this->unpack31( substr( $buf, 0, 4 ) );
214 $u = CdbFunctions
::unsignedShiftRight( $u, 8 );
215 $u = CdbFunctions
::unsignedMod( $u, $this->hslots
);
217 $this->kpos
= $this->hpos +
$u;
220 while ( $this->loop
< $this->hslots
) {
221 $buf = $this->read( 8, $this->kpos
);
222 $pos = $this->unpack31( substr( $buf, 4 ) );
228 if ( $this->kpos
== $this->hpos +
( $this->hslots
<< 3 ) ) {
229 $this->kpos
= $this->hpos
;
231 $u = $this->unpackSigned( substr( $buf, 0, 4 ) );
232 if ( $u === $this->khash
) {
233 $buf = $this->read( 8, $pos );
234 $keyLen = $this->unpack31( substr( $buf, 0, 4 ) );
235 if ( $keyLen == strlen( $key ) && $this->match( $key, $pos +
8 ) ) {
237 $this->dlen
= $this->unpack31( substr( $buf, 4 ) );
238 $this->dpos
= $pos +
8 +
$keyLen;
250 protected function find( $key ) {
252 return $this->findNext( $key );
259 class CdbWriter_PHP
extends CdbWriter
{
260 var $handle, $realFileName, $tmpFileName;
263 var $numentries, $pos;
266 * @param $fileName string
268 function __construct( $fileName ) {
269 $this->realFileName
= $fileName;
270 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
271 $this->handle
= fopen( $this->tmpFileName
, 'wb' );
272 if ( !$this->handle
) {
273 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
275 $this->hplist
= array();
276 $this->numentries
= 0;
277 $this->pos
= 2048; // leaving space for the pointer array, 256 * 8
278 if ( fseek( $this->handle
, $this->pos
) == -1 ) {
279 throw new MWException( __METHOD__
.': fseek failed' );
283 function __destruct() {
284 if ( isset( $this->handle
) ) {
294 public function set( $key, $value ) {
295 if ( strval( $key ) === '' ) {
296 // DBA cross-check hack
299 $this->addbegin( strlen( $key ), strlen( $value ) );
300 $this->write( $key );
301 $this->write( $value );
302 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions
::hash( $key ) );
306 * @throws MWException
308 public function close() {
310 if( isset($this->handle
) ) {
311 fclose( $this->handle
);
313 if ( wfIsWindows() && file_exists($this->realFileName
) ) {
314 unlink( $this->realFileName
);
316 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
317 throw new MWException( 'Unable to move the new CDB file into place.' );
319 unset( $this->handle
);
323 * @throws MWException
326 protected function write( $buf ) {
327 $len = fwrite( $this->handle
, $buf );
328 if ( $len !== strlen( $buf ) ) {
329 throw new MWException( 'Error writing to CDB file.' );
334 * @throws MWException
337 protected function posplus( $len ) {
338 $newpos = $this->pos +
$len;
339 if ( $newpos > 0x7fffffff ) {
340 throw new MWException( 'A value in the CDB file is too large' );
342 $this->pos
= $newpos;
350 protected function addend( $keylen, $datalen, $h ) {
351 $this->hplist
[] = array(
358 $this->posplus( $keylen );
359 $this->posplus( $datalen );
363 * @throws MWException
367 protected function addbegin( $keylen, $datalen ) {
368 if ( $keylen > 0x7fffffff ) {
369 throw new MWException( __METHOD__
.': key length too long' );
371 if ( $datalen > 0x7fffffff ) {
372 throw new MWException( __METHOD__
.': data length too long' );
374 $buf = pack( 'VV', $keylen, $datalen );
375 $this->write( $buf );
379 * @throws MWException
381 protected function finish() {
382 // Hack for DBA cross-check
383 $this->hplist
= array_reverse( $this->hplist
);
385 // Calculate the number of items that will be in each hashtable
386 $counts = array_fill( 0, 256, 0 );
387 foreach ( $this->hplist
as $item ) {
388 ++
$counts[ 255 & $item['h'] ];
391 // Fill in $starts with the *end* indexes
394 for ( $i = 0; $i < 256; ++
$i ) {
399 // Excessively clever and indulgent code to simultaneously fill $packedTables
400 // with the packed hashtables, and adjust the elements of $starts
401 // to actually point to the starts instead of the ends.
402 $packedTables = array_fill( 0, $this->numentries
, false );
403 foreach ( $this->hplist
as $item ) {
404 $packedTables[--$starts[255 & $item['h']]] = $item;
408 for ( $i = 0; $i < 256; ++
$i ) {
409 $count = $counts[$i];
411 // The size of the hashtable will be double the item count.
412 // The rest of the slots will be empty.
413 $len = $count +
$count;
414 $final .= pack( 'VV', $this->pos
, $len );
416 $hashtable = array();
417 for ( $u = 0; $u < $len; ++
$u ) {
418 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
421 // Fill the hashtable, using the next empty slot if the hashed slot
423 for ( $u = 0; $u < $count; ++
$u ) {
424 $hp = $packedTables[$starts[$i] +
$u];
425 $where = CdbFunctions
::unsignedMod(
426 CdbFunctions
::unsignedShiftRight( $hp['h'], 8 ), $len );
427 while ( $hashtable[$where]['p'] )
428 if ( ++
$where == $len )
430 $hashtable[$where] = $hp;
433 // Write the hashtable
434 for ( $u = 0; $u < $len; ++
$u ) {
436 $hashtable[$u]['h'] & 0xffff,
437 CdbFunctions
::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
438 $hashtable[$u]['p'] );
439 $this->write( $buf );
444 // Write the pointer array at the start of the file
445 rewind( $this->handle
);
446 if ( ftell( $this->handle
) != 0 ) {
447 throw new MWException( __METHOD__
.': Error rewinding to start of file' );
449 $this->write( $final );