2009-06-20 15:59:56 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2011-10-29 01:53:28 +00:00
|
|
|
* This is a port of D.J. Bernstein's CDB to PHP. It's based on the copy that
|
2009-06-20 15:59:56 +00:00
|
|
|
* appears in PHP 5.3. Changes are:
|
|
|
|
|
* * Error returns replaced with exceptions
|
|
|
|
|
* * Exception thrown if sizes or offsets are between 2GB and 4GB
|
|
|
|
|
* * Some variables renamed
|
2010-08-14 17:42:40 +00:00
|
|
|
*
|
2012-05-21 19:56:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-08-14 17:42:40 +00:00
|
|
|
* @file
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Common functions for readers and writers
|
|
|
|
|
*/
|
|
|
|
|
class CdbFunctions {
|
|
|
|
|
/**
|
|
|
|
|
* Take a modulo of a signed integer as if it were an unsigned integer.
|
|
|
|
|
* $b must be less than 0x40000000 and greater than 0
|
2011-05-28 17:51:33 +00:00
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $a
|
|
|
|
|
* @param int $b
|
2011-05-28 17:51:33 +00:00
|
|
|
*
|
2011-05-28 17:18:50 +00:00
|
|
|
* @return int
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
public static function unsignedMod( $a, $b ) {
|
2009-07-24 22:15:23 +00:00
|
|
|
if ( $a & 0x80000000 ) {
|
2009-06-20 15:59:56 +00:00
|
|
|
$m = ( $a & 0x7fffffff ) % $b + 2 * ( 0x40000000 % $b );
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return $m % $b;
|
|
|
|
|
} else {
|
|
|
|
|
return $a % $b;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-05-28 17:18:50 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
/**
|
|
|
|
|
* Shift a signed integer right as if it were unsigned
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $a
|
|
|
|
|
* @param int $b
|
2011-05-28 17:18:50 +00:00
|
|
|
* @return int
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
public static function unsignedShiftRight( $a, $b ) {
|
|
|
|
|
if ( $b == 0 ) {
|
|
|
|
|
return $a;
|
|
|
|
|
}
|
2009-07-24 22:15:23 +00:00
|
|
|
if ( $a & 0x80000000 ) {
|
2009-06-20 15:59:56 +00:00
|
|
|
return ( ( $a & 0x7fffffff ) >> $b ) | ( 0x40000000 >> ( $b - 1 ) );
|
|
|
|
|
} else {
|
|
|
|
|
return $a >> $b;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-22 06:41:48 +00:00
|
|
|
/**
|
|
|
|
|
* The CDB hash function.
|
2011-10-29 01:53:28 +00:00
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $s
|
2011-05-28 17:51:33 +00:00
|
|
|
*
|
2013-11-07 22:23:29 +00:00
|
|
|
* @return int
|
2009-06-22 06:41:48 +00:00
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
public static function hash( $s ) {
|
|
|
|
|
$h = 5381;
|
2013-11-04 10:02:48 +00:00
|
|
|
$len = strlen( $s );
|
|
|
|
|
for ( $i = 0; $i < $len; $i++ ) {
|
2013-05-17 14:47:00 +00:00
|
|
|
$h5 = ( $h << 5 ) & 0xffffffff;
|
2009-06-22 06:41:48 +00:00
|
|
|
// Do a 32-bit sum
|
|
|
|
|
// Inlined here for speed
|
2013-05-17 14:47:00 +00:00
|
|
|
$sum = ( $h & 0x3fffffff ) + ( $h5 & 0x3fffffff );
|
2011-10-29 01:53:28 +00:00
|
|
|
$h =
|
2009-06-22 06:41:48 +00:00
|
|
|
(
|
|
|
|
|
( $sum & 0x40000000 ? 1 : 0 )
|
2009-07-24 22:15:23 +00:00
|
|
|
+ ( $h & 0x80000000 ? 2 : 0 )
|
2009-06-22 06:41:48 +00:00
|
|
|
+ ( $h & 0x40000000 ? 1 : 0 )
|
2009-07-24 22:15:23 +00:00
|
|
|
+ ( $h5 & 0x80000000 ? 2 : 0 )
|
2009-06-22 06:41:48 +00:00
|
|
|
+ ( $h5 & 0x40000000 ? 1 : 0 )
|
|
|
|
|
) << 30
|
|
|
|
|
| ( $sum & 0x3fffffff );
|
|
|
|
|
$h ^= ord( $s[$i] );
|
2009-07-24 22:15:23 +00:00
|
|
|
$h &= 0xffffffff;
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return $h;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CDB reader class
|
|
|
|
|
*/
|
Remove underscore from CdbReader_[DBA|PHP] classes and related file
Also gets rid of some CodeSniffer errors. There are more cases in core
(cache, pool counter, installer, database, load balancer, diff, CSS
Janus, less (argh!), media, parser, revdel, ...), that have class names
with underscores I'd be trying to get rid of later.
Change-Id: I33709c05e597978a5574a445fa43c583cbd7e12b
2013-11-04 10:02:48 +00:00
|
|
|
class CdbReaderPHP extends CdbReader {
|
2012-02-15 20:06:47 +00:00
|
|
|
/** The filename */
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $fileName;
|
2012-02-15 20:06:47 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
/* number of hash slots searched under this key */
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $loop;
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/* initialized if loop is nonzero */
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $khash;
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/* initialized if loop is nonzero */
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $kpos;
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/* initialized if loop is nonzero */
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $hpos;
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/* initialized if loop is nonzero */
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $hslots;
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/* initialized if findNext() returns true */
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $dpos;
|
2009-06-20 15:59:56 +00:00
|
|
|
|
|
|
|
|
/* initialized if cdb_findnext() returns 1 */
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $dlen;
|
2009-06-20 15:59:56 +00:00
|
|
|
|
2011-10-29 01:53:28 +00:00
|
|
|
/**
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $fileName
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2011-10-29 01:53:28 +00:00
|
|
|
*/
|
2013-11-07 22:03:08 +00:00
|
|
|
public function __construct( $fileName ) {
|
2012-02-15 20:06:47 +00:00
|
|
|
$this->fileName = $fileName;
|
2009-06-20 15:59:56 +00:00
|
|
|
$this->handle = fopen( $fileName, 'rb' );
|
|
|
|
|
if ( !$this->handle ) {
|
2013-11-07 22:03:08 +00:00
|
|
|
throw new CdbException( 'Unable to open CDB file "' . $this->fileName . '".' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
$this->findStart();
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-07 22:03:08 +00:00
|
|
|
public function close() {
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( isset( $this->handle ) ) {
|
2009-08-13 20:42:19 +00:00
|
|
|
fclose( $this->handle );
|
2011-05-28 17:18:50 +00:00
|
|
|
}
|
2009-06-20 15:59:56 +00:00
|
|
|
unset( $this->handle );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:51:33 +00:00
|
|
|
/**
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param mixed $key
|
2011-05-28 17:51:33 +00:00
|
|
|
* @return bool|string
|
|
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
public function get( $key ) {
|
|
|
|
|
// strval is required
|
|
|
|
|
if ( $this->find( strval( $key ) ) ) {
|
|
|
|
|
return $this->read( $this->dlen, $this->dpos );
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @param int $pos
|
2011-05-28 17:18:50 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function match( $key, $pos ) {
|
|
|
|
|
$buf = $this->read( strlen( $key ), $pos );
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return $buf === $key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function findStart() {
|
|
|
|
|
$this->loop = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $length
|
|
|
|
|
* @param int $pos
|
2011-05-28 17:18:50 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function read( $length, $pos ) {
|
|
|
|
|
if ( fseek( $this->handle, $pos ) == -1 ) {
|
|
|
|
|
// This can easily happen if the internal pointers are incorrect
|
2013-11-07 22:03:08 +00:00
|
|
|
throw new CdbException(
|
2012-02-15 20:06:47 +00:00
|
|
|
'Seek failed, file "' . $this->fileName . '" may be corrupted.' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $length == 0 ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$buf = fread( $this->handle, $length );
|
|
|
|
|
if ( $buf === false || strlen( $buf ) !== $length ) {
|
2013-11-07 22:03:08 +00:00
|
|
|
throw new CdbException(
|
2012-02-15 20:06:47 +00:00
|
|
|
'Read from CDB file failed, file "' . $this->fileName . '" may be corrupted.' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return $buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unpack an unsigned integer and throw an exception if it needs more than 31 bits
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $s
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2012-10-07 23:35:26 +00:00
|
|
|
* @return mixed
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
protected function unpack31( $s ) {
|
|
|
|
|
$data = unpack( 'V', $s );
|
|
|
|
|
if ( $data[1] > 0x7fffffff ) {
|
2013-11-07 22:03:08 +00:00
|
|
|
throw new CdbException(
|
2012-02-15 20:06:47 +00:00
|
|
|
'Error in CDB file "' . $this->fileName . '", integer too big.' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return $data[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unpack a 32-bit signed integer
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $s
|
2011-05-28 17:18:50 +00:00
|
|
|
* @return int
|
2009-06-20 15:59:56 +00:00
|
|
|
*/
|
|
|
|
|
protected function unpackSigned( $s ) {
|
|
|
|
|
$data = unpack( 'va/vb', $s );
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return $data['a'] | ( $data['b'] << 16 );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $key
|
2011-05-28 17:18:50 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function findNext( $key ) {
|
|
|
|
|
if ( !$this->loop ) {
|
|
|
|
|
$u = CdbFunctions::hash( $key );
|
|
|
|
|
$buf = $this->read( 8, ( $u << 3 ) & 2047 );
|
|
|
|
|
$this->hslots = $this->unpack31( substr( $buf, 4 ) );
|
|
|
|
|
if ( !$this->hslots ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->hpos = $this->unpack31( substr( $buf, 0, 4 ) );
|
|
|
|
|
$this->khash = $u;
|
|
|
|
|
$u = CdbFunctions::unsignedShiftRight( $u, 8 );
|
|
|
|
|
$u = CdbFunctions::unsignedMod( $u, $this->hslots );
|
|
|
|
|
$u <<= 3;
|
|
|
|
|
$this->kpos = $this->hpos + $u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while ( $this->loop < $this->hslots ) {
|
|
|
|
|
$buf = $this->read( 8, $this->kpos );
|
|
|
|
|
$pos = $this->unpack31( substr( $buf, 4 ) );
|
|
|
|
|
if ( !$pos ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->loop += 1;
|
|
|
|
|
$this->kpos += 8;
|
|
|
|
|
if ( $this->kpos == $this->hpos + ( $this->hslots << 3 ) ) {
|
|
|
|
|
$this->kpos = $this->hpos;
|
|
|
|
|
}
|
|
|
|
|
$u = $this->unpackSigned( substr( $buf, 0, 4 ) );
|
|
|
|
|
if ( $u === $this->khash ) {
|
|
|
|
|
$buf = $this->read( 8, $pos );
|
|
|
|
|
$keyLen = $this->unpack31( substr( $buf, 0, 4 ) );
|
|
|
|
|
if ( $keyLen == strlen( $key ) && $this->match( $key, $pos + 8 ) ) {
|
|
|
|
|
// Found
|
|
|
|
|
$this->dlen = $this->unpack31( substr( $buf, 4 ) );
|
|
|
|
|
$this->dpos = $pos + 8 + $keyLen;
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param mixed $key
|
2011-05-28 17:18:50 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function find( $key ) {
|
|
|
|
|
$this->findStart();
|
2013-11-04 09:29:25 +00:00
|
|
|
|
2009-06-20 15:59:56 +00:00
|
|
|
return $this->findNext( $key );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CDB writer class
|
|
|
|
|
*/
|
Remove underscore from CdbReader_[DBA|PHP] classes and related file
Also gets rid of some CodeSniffer errors. There are more cases in core
(cache, pool counter, installer, database, load balancer, diff, CSS
Janus, less (argh!), media, parser, revdel, ...), that have class names
with underscores I'd be trying to get rid of later.
Change-Id: I33709c05e597978a5574a445fa43c583cbd7e12b
2013-11-04 10:02:48 +00:00
|
|
|
class CdbWriterPHP extends CdbWriter {
|
2014-05-09 12:46:29 +00:00
|
|
|
protected $hplist;
|
|
|
|
|
|
|
|
|
|
protected $numentries;
|
|
|
|
|
|
|
|
|
|
protected $pos;
|
2009-06-20 15:59:56 +00:00
|
|
|
|
2011-10-29 01:53:28 +00:00
|
|
|
/**
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $fileName
|
2011-10-29 01:53:28 +00:00
|
|
|
*/
|
2013-11-07 22:03:08 +00:00
|
|
|
public function __construct( $fileName ) {
|
2009-06-20 15:59:56 +00:00
|
|
|
$this->realFileName = $fileName;
|
|
|
|
|
$this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
|
|
|
|
|
$this->handle = fopen( $this->tmpFileName, 'wb' );
|
|
|
|
|
if ( !$this->handle ) {
|
2012-02-15 20:24:15 +00:00
|
|
|
$this->throwException(
|
2012-02-15 20:06:47 +00:00
|
|
|
'Unable to open CDB file "' . $this->tmpFileName . '" for write.' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
$this->hplist = array();
|
|
|
|
|
$this->numentries = 0;
|
|
|
|
|
$this->pos = 2048; // leaving space for the pointer array, 256 * 8
|
|
|
|
|
if ( fseek( $this->handle, $this->pos ) == -1 ) {
|
2012-02-15 20:24:15 +00:00
|
|
|
$this->throwException( 'fseek failed in file "' . $this->tmpFileName . '".' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2013-11-07 22:23:29 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @param string $value
|
2011-05-28 17:18:50 +00:00
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
public function set( $key, $value ) {
|
|
|
|
|
if ( strval( $key ) === '' ) {
|
|
|
|
|
// DBA cross-check hack
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->addbegin( strlen( $key ), strlen( $value ) );
|
|
|
|
|
$this->write( $key );
|
|
|
|
|
$this->write( $value );
|
|
|
|
|
$this->addend( strlen( $key ), strlen( $value ), CdbFunctions::hash( $key ) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2011-05-28 17:18:50 +00:00
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
public function close() {
|
|
|
|
|
$this->finish();
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( isset( $this->handle ) ) {
|
2009-08-13 20:42:19 +00:00
|
|
|
fclose( $this->handle );
|
2011-05-28 17:18:50 +00:00
|
|
|
}
|
2013-11-07 22:26:54 +00:00
|
|
|
if ( $this->isWindows() && file_exists( $this->realFileName ) ) {
|
2009-06-20 15:59:56 +00:00
|
|
|
unlink( $this->realFileName );
|
|
|
|
|
}
|
|
|
|
|
if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
|
2012-02-15 20:24:15 +00:00
|
|
|
$this->throwException( 'Unable to move the new CDB file into place.' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
unset( $this->handle );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $buf
|
2011-05-28 17:18:50 +00:00
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function write( $buf ) {
|
|
|
|
|
$len = fwrite( $this->handle, $buf );
|
|
|
|
|
if ( $len !== strlen( $buf ) ) {
|
2013-02-03 20:05:24 +00:00
|
|
|
$this->throwException( 'Error writing to CDB file "' . $this->tmpFileName . '".' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $len
|
2011-05-28 17:18:50 +00:00
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function posplus( $len ) {
|
|
|
|
|
$newpos = $this->pos + $len;
|
|
|
|
|
if ( $newpos > 0x7fffffff ) {
|
2012-02-15 20:24:15 +00:00
|
|
|
$this->throwException(
|
2013-02-03 20:05:24 +00:00
|
|
|
'A value in the CDB file "' . $this->tmpFileName . '" is too large.' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
$this->pos = $newpos;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:51:33 +00:00
|
|
|
/**
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $keylen
|
|
|
|
|
* @param int $datalen
|
|
|
|
|
* @param int $h
|
2011-05-28 17:51:33 +00:00
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function addend( $keylen, $datalen, $h ) {
|
|
|
|
|
$this->hplist[] = array(
|
|
|
|
|
'h' => $h,
|
|
|
|
|
'p' => $this->pos
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->numentries++;
|
|
|
|
|
$this->posplus( 8 );
|
|
|
|
|
$this->posplus( $keylen );
|
|
|
|
|
$this->posplus( $datalen );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:18:50 +00:00
|
|
|
/**
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param int $keylen
|
|
|
|
|
* @param int $datalen
|
2011-05-28 17:18:50 +00:00
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function addbegin( $keylen, $datalen ) {
|
|
|
|
|
if ( $keylen > 0x7fffffff ) {
|
2013-02-03 20:05:24 +00:00
|
|
|
$this->throwException( 'Key length too long in file "' . $this->tmpFileName . '".' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
if ( $datalen > 0x7fffffff ) {
|
2013-02-03 20:05:24 +00:00
|
|
|
$this->throwException( 'Data length too long in file "' . $this->tmpFileName . '".' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
$buf = pack( 'VV', $keylen, $datalen );
|
|
|
|
|
$this->write( $buf );
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:51:33 +00:00
|
|
|
/**
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2011-05-28 17:51:33 +00:00
|
|
|
*/
|
2009-06-20 15:59:56 +00:00
|
|
|
protected function finish() {
|
|
|
|
|
// Hack for DBA cross-check
|
|
|
|
|
$this->hplist = array_reverse( $this->hplist );
|
|
|
|
|
|
|
|
|
|
// Calculate the number of items that will be in each hashtable
|
|
|
|
|
$counts = array_fill( 0, 256, 0 );
|
|
|
|
|
foreach ( $this->hplist as $item ) {
|
2013-11-04 09:29:25 +00:00
|
|
|
++$counts[255 & $item['h']];
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fill in $starts with the *end* indexes
|
|
|
|
|
$starts = array();
|
|
|
|
|
$pos = 0;
|
|
|
|
|
for ( $i = 0; $i < 256; ++$i ) {
|
|
|
|
|
$pos += $counts[$i];
|
|
|
|
|
$starts[$i] = $pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Excessively clever and indulgent code to simultaneously fill $packedTables
|
2011-10-29 01:53:28 +00:00
|
|
|
// with the packed hashtables, and adjust the elements of $starts
|
2009-06-20 15:59:56 +00:00
|
|
|
// to actually point to the starts instead of the ends.
|
|
|
|
|
$packedTables = array_fill( 0, $this->numentries, false );
|
|
|
|
|
foreach ( $this->hplist as $item ) {
|
|
|
|
|
$packedTables[--$starts[255 & $item['h']]] = $item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$final = '';
|
|
|
|
|
for ( $i = 0; $i < 256; ++$i ) {
|
|
|
|
|
$count = $counts[$i];
|
|
|
|
|
|
|
|
|
|
// The size of the hashtable will be double the item count.
|
|
|
|
|
// The rest of the slots will be empty.
|
|
|
|
|
$len = $count + $count;
|
|
|
|
|
$final .= pack( 'VV', $this->pos, $len );
|
|
|
|
|
|
|
|
|
|
$hashtable = array();
|
|
|
|
|
for ( $u = 0; $u < $len; ++$u ) {
|
|
|
|
|
$hashtable[$u] = array( 'h' => 0, 'p' => 0 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fill the hashtable, using the next empty slot if the hashed slot
|
|
|
|
|
// is taken.
|
|
|
|
|
for ( $u = 0; $u < $count; ++$u ) {
|
|
|
|
|
$hp = $packedTables[$starts[$i] + $u];
|
2011-10-29 01:53:28 +00:00
|
|
|
$where = CdbFunctions::unsignedMod(
|
2009-06-20 15:59:56 +00:00
|
|
|
CdbFunctions::unsignedShiftRight( $hp['h'], 8 ), $len );
|
2013-04-20 22:49:30 +00:00
|
|
|
while ( $hashtable[$where]['p'] ) {
|
|
|
|
|
if ( ++$where == $len ) {
|
2009-06-20 15:59:56 +00:00
|
|
|
$where = 0;
|
2013-04-20 22:49:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-06-20 15:59:56 +00:00
|
|
|
$hashtable[$where] = $hp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the hashtable
|
|
|
|
|
for ( $u = 0; $u < $len; ++$u ) {
|
2011-10-29 01:53:28 +00:00
|
|
|
$buf = pack( 'vvV',
|
2009-06-20 15:59:56 +00:00
|
|
|
$hashtable[$u]['h'] & 0xffff,
|
|
|
|
|
CdbFunctions::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
|
|
|
|
|
$hashtable[$u]['p'] );
|
|
|
|
|
$this->write( $buf );
|
|
|
|
|
$this->posplus( 8 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the pointer array at the start of the file
|
|
|
|
|
rewind( $this->handle );
|
|
|
|
|
if ( ftell( $this->handle ) != 0 ) {
|
2013-02-03 20:05:24 +00:00
|
|
|
$this->throwException( 'Error rewinding to start of file "' . $this->tmpFileName . '".' );
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|
|
|
|
|
$this->write( $final );
|
|
|
|
|
}
|
2012-02-15 20:24:15 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clean up the temp file and throw an exception
|
2012-10-10 18:13:40 +00:00
|
|
|
*
|
2014-07-21 18:58:14 +00:00
|
|
|
* @param string $msg
|
2013-11-07 22:03:08 +00:00
|
|
|
* @throws CdbException
|
2012-02-15 20:24:15 +00:00
|
|
|
*/
|
|
|
|
|
protected function throwException( $msg ) {
|
|
|
|
|
if ( $this->handle ) {
|
|
|
|
|
fclose( $this->handle );
|
|
|
|
|
unlink( $this->tmpFileName );
|
|
|
|
|
}
|
2013-11-07 22:03:08 +00:00
|
|
|
throw new CdbException( $msg );
|
2012-02-15 20:24:15 +00:00
|
|
|
}
|
2009-06-20 15:59:56 +00:00
|
|
|
}
|