cdb: One class per file
[lhc/web/wiklou.git] / includes / libs / cdb / CdbWriterPHP.php
1 <?php
2 /**
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
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * CDB writer class
29 */
30 class CdbWriterPHP extends CdbWriter {
31 protected $hplist;
32
33 protected $numentries;
34
35 protected $pos;
36
37 /**
38 * @param string $fileName
39 */
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.' );
47 }
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 . '".' );
53 }
54 }
55
56 /**
57 * @param string $key
58 * @param string $value
59 */
60 public function set( $key, $value ) {
61 if ( strval( $key ) === '' ) {
62 // DBA cross-check hack
63 return;
64 }
65 $this->addbegin( strlen( $key ), strlen( $value ) );
66 $this->write( $key );
67 $this->write( $value );
68 $this->addend( strlen( $key ), strlen( $value ), CdbFunctions::hash( $key ) );
69 }
70
71 /**
72 * @throws CdbException
73 */
74 public function close() {
75 $this->finish();
76 if ( isset( $this->handle ) ) {
77 fclose( $this->handle );
78 }
79 if ( $this->isWindows() && file_exists( $this->realFileName ) ) {
80 unlink( $this->realFileName );
81 }
82 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
83 $this->throwException( 'Unable to move the new CDB file into place.' );
84 }
85 unset( $this->handle );
86 }
87
88 /**
89 * @throws CdbException
90 * @param string $buf
91 */
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 . '".' );
96 }
97 }
98
99 /**
100 * @throws CdbException
101 * @param int $len
102 */
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.' );
108 }
109 $this->pos = $newpos;
110 }
111
112 /**
113 * @param int $keylen
114 * @param int $datalen
115 * @param int $h
116 */
117 protected function addend( $keylen, $datalen, $h ) {
118 $this->hplist[] = array(
119 'h' => $h,
120 'p' => $this->pos
121 );
122
123 $this->numentries++;
124 $this->posplus( 8 );
125 $this->posplus( $keylen );
126 $this->posplus( $datalen );
127 }
128
129 /**
130 * @throws CdbException
131 * @param int $keylen
132 * @param int $datalen
133 */
134 protected function addbegin( $keylen, $datalen ) {
135 if ( $keylen > 0x7fffffff ) {
136 $this->throwException( 'Key length too long in file "' . $this->tmpFileName . '".' );
137 }
138 if ( $datalen > 0x7fffffff ) {
139 $this->throwException( 'Data length too long in file "' . $this->tmpFileName . '".' );
140 }
141 $buf = pack( 'VV', $keylen, $datalen );
142 $this->write( $buf );
143 }
144
145 /**
146 * @throws CdbException
147 */
148 protected function finish() {
149 // Hack for DBA cross-check
150 $this->hplist = array_reverse( $this->hplist );
151
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']];
156 }
157
158 // Fill in $starts with the *end* indexes
159 $starts = array();
160 $pos = 0;
161 for ( $i = 0; $i < 256; ++$i ) {
162 $pos += $counts[$i];
163 $starts[$i] = $pos;
164 }
165
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;
172 }
173
174 $final = '';
175 for ( $i = 0; $i < 256; ++$i ) {
176 $count = $counts[$i];
177
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 );
182
183 $hashtable = array();
184 for ( $u = 0; $u < $len; ++$u ) {
185 $hashtable[$u] = array( 'h' => 0, 'p' => 0 );
186 }
187
188 // Fill the hashtable, using the next empty slot if the hashed slot
189 // is taken.
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 ) {
196 $where = 0;
197 }
198 }
199 $hashtable[$where] = $hp;
200 }
201
202 // Write the hashtable
203 for ( $u = 0; $u < $len; ++$u ) {
204 $buf = pack( 'vvV',
205 $hashtable[$u]['h'] & 0xffff,
206 CdbFunctions::unsignedShiftRight( $hashtable[$u]['h'], 16 ),
207 $hashtable[$u]['p'] );
208 $this->write( $buf );
209 $this->posplus( 8 );
210 }
211 }
212
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 . '".' );
217 }
218 $this->write( $final );
219 }
220
221 /**
222 * Clean up the temp file and throw an exception
223 *
224 * @param string $msg
225 * @throws CdbException
226 */
227 protected function throwException( $msg ) {
228 if ( $this->handle ) {
229 fclose( $this->handle );
230 unlink( $this->tmpFileName );
231 }
232 throw new CdbException( $msg );
233 }
234 }