cdb: One class per file
[lhc/web/wiklou.git] / includes / libs / cdb / CdbWriterDBA.php
1 <?php
2 /**
3 * DBA-based CDB reader/writer
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Writer class which uses the DBA extension
25 */
26 class CdbWriterDBA extends CdbWriter {
27 public function __construct( $fileName ) {
28 $this->realFileName = $fileName;
29 $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
30 $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' );
31 if ( !$this->handle ) {
32 throw new CdbException( 'Unable to open CDB file for write "' . $fileName . '"' );
33 }
34 }
35
36 public function set( $key, $value ) {
37 return dba_insert( $key, $value, $this->handle );
38 }
39
40 public function close() {
41 if ( isset( $this->handle ) ) {
42 dba_close( $this->handle );
43 }
44 if ( $this->isWindows() ) {
45 unlink( $this->realFileName );
46 }
47 if ( !rename( $this->tmpFileName, $this->realFileName ) ) {
48 throw new CdbException( 'Unable to move the new CDB file into place.' );
49 }
50 unset( $this->handle );
51 }
52 }