3 * Native CDB file reader and writer
9 * Read from a CDB file.
10 * Native and pure PHP implementations are provided.
11 * http://cr.yp.to/cdb.html
13 abstract class CdbReader
{
15 * Open a file and return a subclass instance
17 public static function open( $fileName ) {
18 if ( self
::haveExtension() ) {
19 return new CdbReader_DBA( $fileName );
21 wfDebug( "Warning: no dba extension found, using emulation.\n" );
22 return new CdbReader_PHP( $fileName );
27 * Returns true if the native extension is available
29 public static function haveExtension() {
30 if ( !function_exists( 'dba_handlers' ) ) {
33 $handlers = dba_handlers();
34 if ( !in_array( 'cdb', $handlers ) ||
!in_array( 'cdb_make', $handlers ) ) {
41 * Construct the object and open the file
43 abstract function __construct( $fileName );
46 * Close the file. Optional, you can just let the variable go out of scope.
48 abstract function close();
51 * Get a value with a given key. Only string values are supported.
53 abstract public function get( $key );
57 * Write to a CDB file.
58 * Native and pure PHP implementations are provided.
60 abstract class CdbWriter
{
62 * Open a writer and return a subclass instance.
63 * The user must have write access to the directory, for temporary file creation.
65 public static function open( $fileName ) {
66 if ( CdbReader
::haveExtension() ) {
67 return new CdbWriter_DBA( $fileName );
69 wfDebug( "Warning: no dba extension found, using emulation.\n" );
70 return new CdbWriter_PHP( $fileName );
75 * Create the object and open the file
77 abstract function __construct( $fileName );
80 * Set a key to a given value. The value will be converted to string.
82 abstract public function set( $key, $value );
85 * Close the writer object. You should call this function before the object
86 * goes out of scope, to write out the final hashtables.
88 abstract public function close();
93 * Reader class which uses the DBA extension
98 function __construct( $fileName ) {
99 $this->handle
= dba_open( $fileName, 'r-', 'cdb' );
100 if ( !$this->handle
) {
101 throw new MWException( 'Unable to open DB file "' . $fileName . '"' );
106 if( isset($this->handle
) )
107 dba_close( $this->handle
);
108 unset( $this->handle
);
111 function get( $key ) {
112 return dba_fetch( $key, $this->handle
);
118 * Writer class which uses the DBA extension
120 class CdbWriter_DBA
{
121 var $handle, $realFileName, $tmpFileName;
123 function __construct( $fileName ) {
124 $this->realFileName
= $fileName;
125 $this->tmpFileName
= $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff );
126 $this->handle
= dba_open( $this->tmpFileName
, 'n', 'cdb_make' );
127 if ( !$this->handle
) {
128 throw new MWException( 'Unable to open DB file for write "' . $fileName . '"' );
132 function set( $key, $value ) {
133 return dba_insert( $key, $value, $this->handle
);
137 if( isset($this->handle
) )
138 dba_close( $this->handle
);
139 if ( wfIsWindows() ) {
140 unlink( $this->realFileName
);
142 if ( !rename( $this->tmpFileName
, $this->realFileName
) ) {
143 throw new MWException( 'Unable to move the new CDB file into place.' );
145 unset( $this->handle
);
148 function __destruct() {
149 if ( isset( $this->handle
) ) {