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
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
30 class CdbWriterPHP
extends CdbWriter
{
33 protected $numentries;
38 * @param string $fileName
40 public function __construct( $fileName ) {
41 $this->realFileName
= $fileName;
42 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
43 $this->handle
= fopen( $this->tmpFileName
, 'wb' );
44 if ( !$this->handle
) {
45 $this->throwException(
46 'Unable to open CDB file "' . $this->tmpFileName
. '" for write.' );
48 $this->hplist
= array();
49 $this->numentries
= 0;
50 $this->pos
= 2048; // leaving space for the pointer array, 256 * 8
51 if ( fseek( $this->handle
, $this->pos
) == -1 ) {
52 $this->throwException( 'fseek failed in file "' . $this->tmpFileName
. '".' );
58 * @param string $value
60 public function set( $key, $value ) {
61 if ( strval( $key ) === '' ) {
62 // DBA cross-check hack
65 $this->addbegin( strlen( $key ), strlen( $value ) );
67 $this->write( $value );
68 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions
::hash( $key ) );
72 * @throws CdbException
74 public function close() {
76 if ( isset( $this->handle
) ) {
77 fclose( $this->handle
);
79 if ( $this->isWindows() && file_exists( $this->realFileName
) ) {
80 unlink( $this->realFileName
);
82 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
83 $this->throwException( 'Unable to move the new CDB file into place.' );
85 unset( $this->handle
);
89 * @throws CdbException
92 protected function write( $buf ) {
93 $len = fwrite( $this->handle
, $buf );
94 if ( $len !== strlen( $buf ) ) {
95 $this->throwException( 'Error writing to CDB file "' . $this->tmpFileName
. '".' );
100 * @throws CdbException
103 protected function posplus( $len ) {
104 $newpos = $this->pos +
$len;
105 if ( $newpos > 0x7fffffff ) {
106 $this->throwException(
107 'A value in the CDB file "' . $this->tmpFileName
. '" is too large.' );
109 $this->pos
= $newpos;
114 * @param int $datalen
117 protected function addend( $keylen, $datalen, $h ) {
118 $this->hplist
[] = array(
125 $this->posplus( $keylen );
126 $this->posplus( $datalen );
130 * @throws CdbException
132 * @param int $datalen
134 protected function addbegin( $keylen, $datalen ) {
135 if ( $keylen > 0x7fffffff ) {
136 $this->throwException( 'Key length too long in file "' . $this->tmpFileName
. '".' );
138 if ( $datalen > 0x7fffffff ) {
139 $this->throwException( 'Data length too long in file "' . $this->tmpFileName
. '".' );
141 $buf = pack( 'VV', $keylen, $datalen );
142 $this->write( $buf );
146 * @throws CdbException
148 protected function finish() {
149 // Hack for DBA cross-check
150 $this->hplist
= array_reverse( $this->hplist
);
152 // Calculate the number of items that will be in each hashtable
153 $counts = array_fill( 0, 256, 0 );
154 foreach ( $this->hplist
as $item ) {
155 ++
$counts[255 & $item['h']];
158 // Fill in $starts with the *end* indexes
161 for ( $i = 0; $i < 256; ++
$i ) {
166 // Excessively clever and indulgent code to simultaneously fill $packedTables
167 // with the packed hashtables, and adjust the elements of $starts
168 // to actually point to the starts instead of the ends.
169 $packedTables = array_fill( 0, $this->numentries
, false );
170 foreach ( $this->hplist
as $item ) {
171 $packedTables[--$starts[255 & $item['h']]] = $item;
175 for ( $i = 0; $i < 256; ++
$i ) {
176 $count = $counts[$i];
178 // The size of the hashtable will be double the item count.
179 // The rest of the slots will be empty.
180 $len = $count +
$count;
181 $final .= pack( 'VV', $this->pos
, $len );
183 $hashtable = array();
184 for ( $u = 0; $u < $len; ++
$u ) {
185 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
188 // Fill the hashtable, using the next empty slot if the hashed slot
190 for ( $u = 0; $u < $count; ++
$u ) {
191 $hp = $packedTables[$starts[$i] +
$u];
192 $where = CdbFunctions
::unsignedMod(
193 CdbFunctions
::unsignedShiftRight( $hp['h'], 8 ), $len );
194 while ( $hashtable[$where]['p'] ) {
195 if ( ++
$where == $len ) {
199 $hashtable[$where] = $hp;
202 // Write the hashtable
203 for ( $u = 0; $u < $len; ++
$u ) {
205 $hashtable[$u]['h'] & 0xffff,
206 CdbFunctions
::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
207 $hashtable[$u]['p'] );
208 $this->write( $buf );
213 // Write the pointer array at the start of the file
214 rewind( $this->handle
);
215 if ( ftell( $this->handle
) != 0 ) {
216 $this->throwException( 'Error rewinding to start of file "' . $this->tmpFileName
. '".' );
218 $this->write( $final );
222 * Clean up the temp file and throw an exception
225 * @throws CdbException
227 protected function throwException( $msg ) {
228 if ( $this->handle
) {
229 fclose( $this->handle
);
230 unlink( $this->tmpFileName
);
232 throw new CdbException( $msg );