Skip to content

Commit c59f0f7

Browse files
committed
Merge pull request #1 from noony/add-missing-functions-1
Implements missing functions firstKey / nextKey / exists
2 parents 215841a + 2f8e11d commit c59f0f7

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

src/Reader.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,21 @@ abstract public function close();
8282
* @param string $key
8383
*/
8484
abstract public function get( $key );
85+
86+
/**
87+
* Check whether key exists
88+
*
89+
* @param string $key
90+
*/
91+
abstract public function exists( $key );
92+
93+
/**
94+
* Fetch first key
95+
*/
96+
abstract public function firstkey();
97+
98+
/**
99+
* Fetch next key
100+
*/
101+
abstract public function nextkey();
85102
}

src/Reader/DBA.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ public function close() {
4747
public function get( $key ) {
4848
return dba_fetch( $key, $this->handle );
4949
}
50+
51+
public function exists( $key ) {
52+
return dba_exists( $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+
}
5062
}

src/Reader/PHP.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class PHP extends Reader {
5959
/* initialized if cdb_findnext() returns 1 */
6060
protected $dlen;
6161

62+
/* initialized in firstkey() and nextkey() */
63+
protected $pos = 0;
64+
6265
/**
6366
* @param string $fileName
6467
* @throws Exception
@@ -217,5 +220,26 @@ protected function find( $key ) {
217220

218221
return $this->findNext( $key );
219222
}
223+
224+
public function exists( $key ) {
225+
if ( $this->find( strval( $key ) ) ) {
226+
return true;
227+
}
228+
229+
return false;
230+
}
231+
232+
public function firstkey() {
233+
$this->pos = 2048;
234+
return $this->nextkey();
235+
}
236+
237+
public function nextkey() {
238+
$buf = $this->read( 8, $this->pos );
239+
$klen = $this->unpack31( substr( $buf, 0, 4 ) );
240+
$dlen = $this->unpack31( substr( $buf, 4 ) );
241+
$this->pos += 8 + $klen + $dlen;
242+
return fread( $this->handle, $klen );
243+
}
220244
}
221245

test/CdbTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ protected function setUp() {
2727
$this->dbaCdbFile = tempnam( $temp, get_class( $this ) . '_' );
2828
}
2929

30+
protected function tearDown() {
31+
parent::tearDown();
32+
unlink($this->phpCdbFile);
33+
unlink($this->dbaCdbFile);
34+
}
35+
3036
/**
3137
* Make a random-ish string
3238
* @return string
@@ -57,6 +63,8 @@ public function testCdbWrite() {
5763
}
5864
}
5965

66+
unset($data['']);
67+
6068
$w1->close();
6169
$w2->close();
6270

@@ -84,6 +92,31 @@ public function testCdbWrite() {
8492
$this->cdbAssert( "PHP error", $key, $v1, $value );
8593
$this->cdbAssert( "DBA error", $key, $v2, $value );
8694
}
95+
96+
$r1->close();
97+
$r2->close();
98+
99+
$r1 = new Reader\PHP( $this->phpCdbFile );
100+
$r2 = new Reader\DBA( $this->dbaCdbFile );
101+
102+
$keys = array_keys($data);
103+
$firstKey = array_shift($keys);
104+
105+
$this->assertTrue($r1->exists($firstKey), 'PHP entry exists');
106+
$this->assertTrue($r2->exists($firstKey), 'DBA entry exists');
107+
$this->assertFalse($r1->exists(-1), 'PHP entry doesn\'t exists');
108+
$this->assertFalse($r2->exists(-1), 'DBA entry doesn\'t exists');
109+
110+
$firstKey1 = $r1->firstkey();
111+
$firstKey2 = $r2->firstkey();
112+
113+
$this->assertEquals($firstKey1, $firstKey, 'PHP Match first key');
114+
$this->assertEquals($firstKey2, $firstKey, 'DBA Match first key');
115+
116+
unset($data[$firstKey]);
117+
for ($j = 0, $max = count($data); $j < $max; $j++) {
118+
$this->assertEquals($r2->nextkey(), $r1->nextkey(), 'nextkey match');
119+
}
87120
}
88121

89122
private function cdbAssert( $msg, $key, $v1, $v2 ) {

0 commit comments

Comments
 (0)