4 * This is a port of D.J. Bernstein's CDB to PHP. It's based on the copy that
5 * appears in PHP 5.3. Changes are:
6 * * Error returns replaced with exceptions
7 * * Exception thrown if sizes or offsets are between 2GB and 4GB
8 * * Some variables renamed
12 * Common functions for readers and writers
16 * Take a modulo of a signed integer as if it were an unsigned integer.
17 * $b must be less than 0x40000000 and greater than 0
19 public static function unsignedMod( $a, $b ) {
20 if ( $a & 0x80000000 ) {
21 $m = ( $a & 0x7fffffff ) %
$b +
2 * ( 0x40000000 %
$b );
29 * Shift a signed integer right as if it were unsigned
31 public static function unsignedShiftRight( $a, $b ) {
35 if ( $a & 0x80000000 ) {
36 return ( ( $a & 0x7fffffff ) >> $b ) |
( 0x40000000 >> ( $b - 1 ) );
43 * The CDB hash function.
45 public static function hash( $s ) {
47 for ( $i = 0; $i < strlen( $s ); $i++
) {
48 $h5 = ($h << 5) & 0xffffffff;
50 // Inlined here for speed
51 $sum = ($h & 0x3fffffff) +
($h5 & 0x3fffffff);
54 ( $sum & 0x40000000 ?
1 : 0 )
55 +
( $h & 0x80000000 ?
2 : 0 )
56 +
( $h & 0x40000000 ?
1 : 0 )
57 +
( $h5 & 0x80000000 ?
2 : 0 )
58 +
( $h5 & 0x40000000 ?
1 : 0 )
60 |
( $sum & 0x3fffffff );
71 class CdbReader_PHP
extends CdbReader
{
72 /** The file handle */
75 /* number of hash slots searched under this key */
78 /* initialized if loop is nonzero */
81 /* initialized if loop is nonzero */
84 /* initialized if loop is nonzero */
87 /* initialized if loop is nonzero */
90 /* initialized if findNext() returns true */
93 /* initialized if cdb_findnext() returns 1 */
96 function __construct( $fileName ) {
97 $this->handle
= fopen( $fileName, 'rb' );
98 if ( !$this->handle
) {
99 throw new MWException( 'Unable to open DB file "' . $fileName . '"' );
105 fclose( $this->handle
);
106 unset( $this->handle
);
109 public function get( $key ) {
110 // strval is required
111 if ( $this->find( strval( $key ) ) ) {
112 return $this->read( $this->dlen
, $this->dpos
);
118 protected function match( $key, $pos ) {
119 $buf = $this->read( strlen( $key ), $pos );
120 return $buf === $key;
123 protected function findStart() {
127 protected function read( $length, $pos ) {
128 if ( fseek( $this->handle
, $pos ) == -1 ) {
129 // This can easily happen if the internal pointers are incorrect
130 throw new MWException( __METHOD__
.': seek failed, file may be corrupted.' );
133 if ( $length == 0 ) {
137 $buf = fread( $this->handle
, $length );
138 if ( $buf === false ||
strlen( $buf ) !== $length ) {
139 throw new MWException( __METHOD__
.': read from cdb file failed, file may be corrupted' );
145 * Unpack an unsigned integer and throw an exception if it needs more than 31 bits
147 protected function unpack31( $s ) {
148 $data = unpack( 'V', $s );
149 if ( $data[1] > 0x7fffffff ) {
150 throw new MWException( __METHOD__
.': error in CDB file, integer too big' );
156 * Unpack a 32-bit signed integer
158 protected function unpackSigned( $s ) {
159 $data = unpack( 'va/vb', $s );
160 return $data['a'] |
( $data['b'] << 16 );
163 protected function findNext( $key ) {
164 if ( !$this->loop
) {
165 $u = CdbFunctions
::hash( $key );
166 $buf = $this->read( 8, ( $u << 3 ) & 2047 );
167 $this->hslots
= $this->unpack31( substr( $buf, 4 ) );
168 if ( !$this->hslots
) {
171 $this->hpos
= $this->unpack31( substr( $buf, 0, 4 ) );
173 $u = CdbFunctions
::unsignedShiftRight( $u, 8 );
174 $u = CdbFunctions
::unsignedMod( $u, $this->hslots
);
176 $this->kpos
= $this->hpos +
$u;
179 while ( $this->loop
< $this->hslots
) {
180 $buf = $this->read( 8, $this->kpos
);
181 $pos = $this->unpack31( substr( $buf, 4 ) );
187 if ( $this->kpos
== $this->hpos +
( $this->hslots
<< 3 ) ) {
188 $this->kpos
= $this->hpos
;
190 $u = $this->unpackSigned( substr( $buf, 0, 4 ) );
191 if ( $u === $this->khash
) {
192 $buf = $this->read( 8, $pos );
193 $keyLen = $this->unpack31( substr( $buf, 0, 4 ) );
194 if ( $keyLen == strlen( $key ) && $this->match( $key, $pos +
8 ) ) {
196 $this->dlen
= $this->unpack31( substr( $buf, 4 ) );
197 $this->dpos
= $pos +
8 +
$keyLen;
205 protected function find( $key ) {
207 return $this->findNext( $key );
214 class CdbWriter_PHP
extends CdbWriter
{
215 var $handle, $realFileName, $tmpFileName;
218 var $numEntries, $pos;
220 function __construct( $fileName ) {
221 $this->realFileName
= $fileName;
222 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
223 $this->handle
= fopen( $this->tmpFileName
, 'wb' );
224 if ( !$this->handle
) {
225 throw new MWException( 'Unable to open DB file for write "' . $fileName . '"' );
227 $this->hplist
= array();
228 $this->numentries
= 0;
229 $this->pos
= 2048; // leaving space for the pointer array, 256 * 8
230 if ( fseek( $this->handle
, $this->pos
) == -1 ) {
231 throw new MWException( __METHOD__
.': fseek failed' );
235 function __destruct() {
236 if ( isset( $this->handle
) ) {
241 public function set( $key, $value ) {
242 if ( strval( $key ) === '' ) {
243 // DBA cross-check hack
246 $this->addbegin( strlen( $key ), strlen( $value ) );
247 $this->write( $key );
248 $this->write( $value );
249 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions
::hash( $key ) );
252 public function close() {
254 fclose( $this->handle
);
255 if ( wfIsWindows() && file_exists($this->realFileName
) ) {
256 unlink( $this->realFileName
);
258 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
259 throw new MWException( 'Unable to move the new CDB file into place.' );
261 unset( $this->handle
);
264 protected function write( $buf ) {
265 $len = fwrite( $this->handle
, $buf );
266 if ( $len !== strlen( $buf ) ) {
267 throw new MWException( 'Error writing to CDB file.' );
271 protected function posplus( $len ) {
272 $newpos = $this->pos +
$len;
273 if ( $newpos > 0x7fffffff ) {
274 throw new MWException( 'A value in the CDB file is too large' );
276 $this->pos
= $newpos;
279 protected function addend( $keylen, $datalen, $h ) {
280 $this->hplist
[] = array(
287 $this->posplus( $keylen );
288 $this->posplus( $datalen );
291 protected function addbegin( $keylen, $datalen ) {
292 if ( $keylen > 0x7fffffff ) {
293 throw new MWException( __METHOD__
.': key length too long' );
295 if ( $datalen > 0x7fffffff ) {
296 throw new MWException( __METHOD__
.': data length too long' );
298 $buf = pack( 'VV', $keylen, $datalen );
299 $this->write( $buf );
302 protected function finish() {
303 // Hack for DBA cross-check
304 $this->hplist
= array_reverse( $this->hplist
);
306 // Calculate the number of items that will be in each hashtable
307 $counts = array_fill( 0, 256, 0 );
308 foreach ( $this->hplist
as $item ) {
309 ++
$counts[ 255 & $item['h'] ];
312 // Fill in $starts with the *end* indexes
315 for ( $i = 0; $i < 256; ++
$i ) {
320 // Excessively clever and indulgent code to simultaneously fill $packedTables
321 // with the packed hashtables, and adjust the elements of $starts
322 // to actually point to the starts instead of the ends.
323 $packedTables = array_fill( 0, $this->numentries
, false );
324 foreach ( $this->hplist
as $item ) {
325 $packedTables[--$starts[255 & $item['h']]] = $item;
329 for ( $i = 0; $i < 256; ++
$i ) {
330 $count = $counts[$i];
332 // The size of the hashtable will be double the item count.
333 // The rest of the slots will be empty.
334 $len = $count +
$count;
335 $final .= pack( 'VV', $this->pos
, $len );
337 $hashtable = array();
338 for ( $u = 0; $u < $len; ++
$u ) {
339 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
342 // Fill the hashtable, using the next empty slot if the hashed slot
344 for ( $u = 0; $u < $count; ++
$u ) {
345 $hp = $packedTables[$starts[$i] +
$u];
346 $where = CdbFunctions
::unsignedMod(
347 CdbFunctions
::unsignedShiftRight( $hp['h'], 8 ), $len );
348 while ( $hashtable[$where]['p'] )
349 if ( ++
$where == $len )
351 $hashtable[$where] = $hp;
354 // Write the hashtable
355 for ( $u = 0; $u < $len; ++
$u ) {
357 $hashtable[$u]['h'] & 0xffff,
358 CdbFunctions
::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
359 $hashtable[$u]['p'] );
360 $this->write( $buf );
365 // Write the pointer array at the start of the file
366 rewind( $this->handle
);
367 if ( ftell( $this->handle
) != 0 ) {
368 throw new MWException( __METHOD__
.': Error rewinding to start of file' );
370 $this->write( $final );