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
21 public static function unsignedMod( $a, $b ) {
22 if ( $a & 0x80000000 ) {
23 $m = ( $a & 0x7fffffff ) %
$b +
2 * ( 0x40000000 %
$b );
31 * Shift a signed integer right as if it were unsigned
36 public static function unsignedShiftRight( $a, $b ) {
40 if ( $a & 0x80000000 ) {
41 return ( ( $a & 0x7fffffff ) >> $b ) |
( 0x40000000 >> ( $b - 1 ) );
48 * The CDB hash function.
53 public static function hash( $s ) {
55 for ( $i = 0; $i < strlen( $s ); $i++
) {
56 $h5 = ($h << 5) & 0xffffffff;
58 // Inlined here for speed
59 $sum = ($h & 0x3fffffff) +
($h5 & 0x3fffffff);
62 ( $sum & 0x40000000 ?
1 : 0 )
63 +
( $h & 0x80000000 ?
2 : 0 )
64 +
( $h & 0x40000000 ?
1 : 0 )
65 +
( $h5 & 0x80000000 ?
2 : 0 )
66 +
( $h5 & 0x40000000 ?
1 : 0 )
68 |
( $sum & 0x3fffffff );
79 class CdbReader_PHP
extends CdbReader
{
80 /** The file handle */
83 /* number of hash slots searched under this key */
86 /* initialized if loop is nonzero */
89 /* initialized if loop is nonzero */
92 /* initialized if loop is nonzero */
95 /* initialized if loop is nonzero */
98 /* initialized if findNext() returns true */
101 /* initialized if cdb_findnext() returns 1 */
104 function __construct( $fileName ) {
105 $this->handle
= fopen( $fileName, 'rb' );
106 if ( !$this->handle
) {
107 throw new MWException( 'Unable to open CDB file "' . $fileName . '"' );
113 if( isset( $this->handle
) ) {
114 fclose( $this->handle
);
116 unset( $this->handle
);
119 public function get( $key ) {
120 // strval is required
121 if ( $this->find( strval( $key ) ) ) {
122 return $this->read( $this->dlen
, $this->dpos
);
133 protected function match( $key, $pos ) {
134 $buf = $this->read( strlen( $key ), $pos );
135 return $buf === $key;
138 protected function findStart() {
143 * @throws MWException
148 protected function read( $length, $pos ) {
149 if ( fseek( $this->handle
, $pos ) == -1 ) {
150 // This can easily happen if the internal pointers are incorrect
151 throw new MWException( __METHOD__
.': seek failed, file may be corrupted.' );
154 if ( $length == 0 ) {
158 $buf = fread( $this->handle
, $length );
159 if ( $buf === false ||
strlen( $buf ) !== $length ) {
160 throw new MWException( __METHOD__
.': read from CDB file failed, file may be corrupted' );
166 * Unpack an unsigned integer and throw an exception if it needs more than 31 bits
170 protected function unpack31( $s ) {
171 $data = unpack( 'V', $s );
172 if ( $data[1] > 0x7fffffff ) {
173 throw new MWException( __METHOD__
.': error in CDB file, integer too big' );
179 * Unpack a 32-bit signed integer
183 protected function unpackSigned( $s ) {
184 $data = unpack( 'va/vb', $s );
185 return $data['a'] |
( $data['b'] << 16 );
192 protected function findNext( $key ) {
193 if ( !$this->loop
) {
194 $u = CdbFunctions
::hash( $key );
195 $buf = $this->read( 8, ( $u << 3 ) & 2047 );
196 $this->hslots
= $this->unpack31( substr( $buf, 4 ) );
197 if ( !$this->hslots
) {
200 $this->hpos
= $this->unpack31( substr( $buf, 0, 4 ) );
202 $u = CdbFunctions
::unsignedShiftRight( $u, 8 );
203 $u = CdbFunctions
::unsignedMod( $u, $this->hslots
);
205 $this->kpos
= $this->hpos +
$u;
208 while ( $this->loop
< $this->hslots
) {
209 $buf = $this->read( 8, $this->kpos
);
210 $pos = $this->unpack31( substr( $buf, 4 ) );
216 if ( $this->kpos
== $this->hpos +
( $this->hslots
<< 3 ) ) {
217 $this->kpos
= $this->hpos
;
219 $u = $this->unpackSigned( substr( $buf, 0, 4 ) );
220 if ( $u === $this->khash
) {
221 $buf = $this->read( 8, $pos );
222 $keyLen = $this->unpack31( substr( $buf, 0, 4 ) );
223 if ( $keyLen == strlen( $key ) && $this->match( $key, $pos +
8 ) ) {
225 $this->dlen
= $this->unpack31( substr( $buf, 4 ) );
226 $this->dpos
= $pos +
8 +
$keyLen;
238 protected function find( $key ) {
240 return $this->findNext( $key );
247 class CdbWriter_PHP
extends CdbWriter
{
248 var $handle, $realFileName, $tmpFileName;
251 var $numEntries, $pos;
253 function __construct( $fileName ) {
254 $this->realFileName
= $fileName;
255 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
256 $this->handle
= fopen( $this->tmpFileName
, 'wb' );
257 if ( !$this->handle
) {
258 throw new MWException( 'Unable to open CDB file for write "' . $fileName . '"' );
260 $this->hplist
= array();
261 $this->numentries
= 0;
262 $this->pos
= 2048; // leaving space for the pointer array, 256 * 8
263 if ( fseek( $this->handle
, $this->pos
) == -1 ) {
264 throw new MWException( __METHOD__
.': fseek failed' );
268 function __destruct() {
269 if ( isset( $this->handle
) ) {
279 public function set( $key, $value ) {
280 if ( strval( $key ) === '' ) {
281 // DBA cross-check hack
284 $this->addbegin( strlen( $key ), strlen( $value ) );
285 $this->write( $key );
286 $this->write( $value );
287 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions
::hash( $key ) );
291 * @throws MWException
293 public function close() {
295 if( isset($this->handle
) ) {
296 fclose( $this->handle
);
298 if ( wfIsWindows() && file_exists($this->realFileName
) ) {
299 unlink( $this->realFileName
);
301 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
302 throw new MWException( 'Unable to move the new CDB file into place.' );
304 unset( $this->handle
);
308 * @throws MWException
311 protected function write( $buf ) {
312 $len = fwrite( $this->handle
, $buf );
313 if ( $len !== strlen( $buf ) ) {
314 throw new MWException( 'Error writing to CDB file.' );
319 * @throws MWException
322 protected function posplus( $len ) {
323 $newpos = $this->pos +
$len;
324 if ( $newpos > 0x7fffffff ) {
325 throw new MWException( 'A value in the CDB file is too large' );
327 $this->pos
= $newpos;
330 protected function addend( $keylen, $datalen, $h ) {
331 $this->hplist
[] = array(
338 $this->posplus( $keylen );
339 $this->posplus( $datalen );
343 * @throws MWException
347 protected function addbegin( $keylen, $datalen ) {
348 if ( $keylen > 0x7fffffff ) {
349 throw new MWException( __METHOD__
.': key length too long' );
351 if ( $datalen > 0x7fffffff ) {
352 throw new MWException( __METHOD__
.': data length too long' );
354 $buf = pack( 'VV', $keylen, $datalen );
355 $this->write( $buf );
358 protected function finish() {
359 // Hack for DBA cross-check
360 $this->hplist
= array_reverse( $this->hplist
);
362 // Calculate the number of items that will be in each hashtable
363 $counts = array_fill( 0, 256, 0 );
364 foreach ( $this->hplist
as $item ) {
365 ++
$counts[ 255 & $item['h'] ];
368 // Fill in $starts with the *end* indexes
371 for ( $i = 0; $i < 256; ++
$i ) {
376 // Excessively clever and indulgent code to simultaneously fill $packedTables
377 // with the packed hashtables, and adjust the elements of $starts
378 // to actually point to the starts instead of the ends.
379 $packedTables = array_fill( 0, $this->numentries
, false );
380 foreach ( $this->hplist
as $item ) {
381 $packedTables[--$starts[255 & $item['h']]] = $item;
385 for ( $i = 0; $i < 256; ++
$i ) {
386 $count = $counts[$i];
388 // The size of the hashtable will be double the item count.
389 // The rest of the slots will be empty.
390 $len = $count +
$count;
391 $final .= pack( 'VV', $this->pos
, $len );
393 $hashtable = array();
394 for ( $u = 0; $u < $len; ++
$u ) {
395 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
398 // Fill the hashtable, using the next empty slot if the hashed slot
400 for ( $u = 0; $u < $count; ++
$u ) {
401 $hp = $packedTables[$starts[$i] +
$u];
402 $where = CdbFunctions
::unsignedMod(
403 CdbFunctions
::unsignedShiftRight( $hp['h'], 8 ), $len );
404 while ( $hashtable[$where]['p'] )
405 if ( ++
$where == $len )
407 $hashtable[$where] = $hp;
410 // Write the hashtable
411 for ( $u = 0; $u < $len; ++
$u ) {
413 $hashtable[$u]['h'] & 0xffff,
414 CdbFunctions
::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
415 $hashtable[$u]['p'] );
416 $this->write( $buf );
421 // Write the pointer array at the start of the file
422 rewind( $this->handle
);
423 if ( ftell( $this->handle
) != 0 ) {
424 throw new MWException( __METHOD__
.': Error rewinding to start of file' );
426 $this->write( $final );