Debian Codegraph @

vendor/wikimedia/cdb/src/Reader/DBA.php

raw
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 */
18
19 namespace Cdb\Reader;
20
21 use Cdb\Exception;
22 use Cdb\Reader;
23
24 /**
25 * Reader class which uses the DBA extension (php-dba)
26 */
27 class DBA extends Reader {
28 /**
29 * @var resource|false|null The file handle
30 */
31 protected $handle;
32
33 public function __construct( $fileName ) {
34 $this->handle = dba_open( $fileName, 'r-', 'cdb' );
35 if ( !$this->handle ) {
36 throw new Exception( 'Unable to open CDB file "' . $fileName . '"' );
37 }
38 }
39
40 public function close(): void {
41 if ( $this->handle ) {
42 dba_close( $this->handle );
43 }
44 $this->handle = null;
45 }
46
47 public function get( $key ) {
48 return dba_fetch( (string)$key, $this->handle );
49 }
50
51 public function exists( $key ): bool {
52 return dba_exists( (string)$key, $this->handle );
53 }
54
55 public function firstkey() {
56 return dba_firstkey( $this->handle );
57 }
58
59 public function nextkey() {
60 return dba_nextkey( $this->handle );
61 }
62 }