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
20 public static function unsignedMod( $a, $b ) {
21 if ( $a & 0x80000000 ) {
22 $m = ( $a & 0x7fffffff ) %
$b +
2 * ( 0x40000000 %
$b );
30 * Shift a signed integer right as if it were unsigned
32 public static function unsignedShiftRight( $a, $b ) {
36 if ( $a & 0x80000000 ) {
37 return ( ( $a & 0x7fffffff ) >> $b ) |
( 0x40000000 >> ( $b - 1 ) );
44 * The CDB hash function.
46 public static function hash( $s ) {
48 for ( $i = 0; $i < strlen( $s ); $i++
) {
49 $h5 = ($h << 5) & 0xffffffff;
51 // Inlined here for speed
52 $sum = ($h & 0x3fffffff) +
($h5 & 0x3fffffff);
55 ( $sum & 0x40000000 ?
1 : 0 )
56 +
( $h & 0x80000000 ?
2 : 0 )
57 +
( $h & 0x40000000 ?
1 : 0 )
58 +
( $h5 & 0x80000000 ?
2 : 0 )
59 +
( $h5 & 0x40000000 ?
1 : 0 )
61 |
( $sum & 0x3fffffff );
72 class CdbReader_PHP
extends CdbReader
{
73 /** The file handle */
76 /* number of hash slots searched under this key */
79 /* initialized if loop is nonzero */
82 /* initialized if loop is nonzero */
85 /* initialized if loop is nonzero */
88 /* initialized if loop is nonzero */
91 /* initialized if findNext() returns true */
94 /* initialized if cdb_findnext() returns 1 */
97 function __construct( $fileName ) {
98 $this->handle
= fopen( $fileName, 'rb' );
99 if ( !$this->handle
) {
100 throw new MWException( 'Unable to open DB file "' . $fileName . '"' );
106 if( isset($this->handle
) )
107 fclose( $this->handle
);
108 unset( $this->handle
);
111 public function get( $key ) {
112 // strval is required
113 if ( $this->find( strval( $key ) ) ) {
114 return $this->read( $this->dlen
, $this->dpos
);
120 protected function match( $key, $pos ) {
121 $buf = $this->read( strlen( $key ), $pos );
122 return $buf === $key;
125 protected function findStart() {
129 protected function read( $length, $pos ) {
130 if ( fseek( $this->handle
, $pos ) == -1 ) {
131 // This can easily happen if the internal pointers are incorrect
132 throw new MWException( __METHOD__
.': seek failed, file may be corrupted.' );
135 if ( $length == 0 ) {
139 $buf = fread( $this->handle
, $length );
140 if ( $buf === false ||
strlen( $buf ) !== $length ) {
141 throw new MWException( __METHOD__
.': read from cdb file failed, file may be corrupted' );
147 * Unpack an unsigned integer and throw an exception if it needs more than 31 bits
149 protected function unpack31( $s ) {
150 $data = unpack( 'V', $s );
151 if ( $data[1] > 0x7fffffff ) {
152 throw new MWException( __METHOD__
.': error in CDB file, integer too big' );
158 * Unpack a 32-bit signed integer
160 protected function unpackSigned( $s ) {
161 $data = unpack( 'va/vb', $s );
162 return $data['a'] |
( $data['b'] << 16 );
165 protected function findNext( $key ) {
166 if ( !$this->loop
) {
167 $u = CdbFunctions
::hash( $key );
168 $buf = $this->read( 8, ( $u << 3 ) & 2047 );
169 $this->hslots
= $this->unpack31( substr( $buf, 4 ) );
170 if ( !$this->hslots
) {
173 $this->hpos
= $this->unpack31( substr( $buf, 0, 4 ) );
175 $u = CdbFunctions
::unsignedShiftRight( $u, 8 );
176 $u = CdbFunctions
::unsignedMod( $u, $this->hslots
);
178 $this->kpos
= $this->hpos +
$u;
181 while ( $this->loop
< $this->hslots
) {
182 $buf = $this->read( 8, $this->kpos
);
183 $pos = $this->unpack31( substr( $buf, 4 ) );
189 if ( $this->kpos
== $this->hpos +
( $this->hslots
<< 3 ) ) {
190 $this->kpos
= $this->hpos
;
192 $u = $this->unpackSigned( substr( $buf, 0, 4 ) );
193 if ( $u === $this->khash
) {
194 $buf = $this->read( 8, $pos );
195 $keyLen = $this->unpack31( substr( $buf, 0, 4 ) );
196 if ( $keyLen == strlen( $key ) && $this->match( $key, $pos +
8 ) ) {
198 $this->dlen
= $this->unpack31( substr( $buf, 4 ) );
199 $this->dpos
= $pos +
8 +
$keyLen;
207 protected function find( $key ) {
209 return $this->findNext( $key );
216 class CdbWriter_PHP
extends CdbWriter
{
217 var $handle, $realFileName, $tmpFileName;
220 var $numEntries, $pos;
222 function __construct( $fileName ) {
223 $this->realFileName
= $fileName;
224 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
225 $this->handle
= fopen( $this->tmpFileName
, 'wb' );
226 if ( !$this->handle
) {
227 throw new MWException( 'Unable to open DB file for write "' . $fileName . '"' );
229 $this->hplist
= array();
230 $this->numentries
= 0;
231 $this->pos
= 2048; // leaving space for the pointer array, 256 * 8
232 if ( fseek( $this->handle
, $this->pos
) == -1 ) {
233 throw new MWException( __METHOD__
.': fseek failed' );
237 function __destruct() {
238 if ( isset( $this->handle
) ) {
243 public function set( $key, $value ) {
244 if ( strval( $key ) === '' ) {
245 // DBA cross-check hack
248 $this->addbegin( strlen( $key ), strlen( $value ) );
249 $this->write( $key );
250 $this->write( $value );
251 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions
::hash( $key ) );
254 public function close() {
256 if( isset($this->handle
) )
257 fclose( $this->handle
);
258 if ( wfIsWindows() && file_exists($this->realFileName
) ) {
259 unlink( $this->realFileName
);
261 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
262 throw new MWException( 'Unable to move the new CDB file into place.' );
264 unset( $this->handle
);
267 protected function write( $buf ) {
268 $len = fwrite( $this->handle
, $buf );
269 if ( $len !== strlen( $buf ) ) {
270 throw new MWException( 'Error writing to CDB file.' );
274 protected function posplus( $len ) {
275 $newpos = $this->pos +
$len;
276 if ( $newpos > 0x7fffffff ) {
277 throw new MWException( 'A value in the CDB file is too large' );
279 $this->pos
= $newpos;
282 protected function addend( $keylen, $datalen, $h ) {
283 $this->hplist
[] = array(
290 $this->posplus( $keylen );
291 $this->posplus( $datalen );
294 protected function addbegin( $keylen, $datalen ) {
295 if ( $keylen > 0x7fffffff ) {
296 throw new MWException( __METHOD__
.': key length too long' );
298 if ( $datalen > 0x7fffffff ) {
299 throw new MWException( __METHOD__
.': data length too long' );
301 $buf = pack( 'VV', $keylen, $datalen );
302 $this->write( $buf );
305 protected function finish() {
306 // Hack for DBA cross-check
307 $this->hplist
= array_reverse( $this->hplist
);
309 // Calculate the number of items that will be in each hashtable
310 $counts = array_fill( 0, 256, 0 );
311 foreach ( $this->hplist
as $item ) {
312 ++
$counts[ 255 & $item['h'] ];
315 // Fill in $starts with the *end* indexes
318 for ( $i = 0; $i < 256; ++
$i ) {
323 // Excessively clever and indulgent code to simultaneously fill $packedTables
324 // with the packed hashtables, and adjust the elements of $starts
325 // to actually point to the starts instead of the ends.
326 $packedTables = array_fill( 0, $this->numentries
, false );
327 foreach ( $this->hplist
as $item ) {
328 $packedTables[--$starts[255 & $item['h']]] = $item;
332 for ( $i = 0; $i < 256; ++
$i ) {
333 $count = $counts[$i];
335 // The size of the hashtable will be double the item count.
336 // The rest of the slots will be empty.
337 $len = $count +
$count;
338 $final .= pack( 'VV', $this->pos
, $len );
340 $hashtable = array();
341 for ( $u = 0; $u < $len; ++
$u ) {
342 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
345 // Fill the hashtable, using the next empty slot if the hashed slot
347 for ( $u = 0; $u < $count; ++
$u ) {
348 $hp = $packedTables[$starts[$i] +
$u];
349 $where = CdbFunctions
::unsignedMod(
350 CdbFunctions
::unsignedShiftRight( $hp['h'], 8 ), $len );
351 while ( $hashtable[$where]['p'] )
352 if ( ++
$where == $len )
354 $hashtable[$where] = $hp;
357 // Write the hashtable
358 for ( $u = 0; $u < $len; ++
$u ) {
360 $hashtable[$u]['h'] & 0xffff,
361 CdbFunctions
::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
362 $hashtable[$u]['p'] );
363 $this->write( $buf );
368 // Write the pointer array at the start of the file
369 rewind( $this->handle
);
370 if ( ftell( $this->handle
) != 0 ) {
371 throw new MWException( __METHOD__
.': Error rewinding to start of file' );
373 $this->write( $final );