Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,21 @@ abstract public function close();
* @param string $key
*/
abstract public function get( $key );

/**
* Check whether key exists
*
* @param string $key
*/
abstract public function exists( $key );

/**
* Fetch first key
*/
abstract public function firstkey();

/**
* Fetch next key
*/
abstract public function nextkey();
}
12 changes: 12 additions & 0 deletions src/Reader/DBA.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ public function close() {
public function get( $key ) {
return dba_fetch( $key, $this->handle );
}

public function exists( $key ) {
return dba_exists( $key, $this->handle );
}

public function firstkey() {
return dba_firstkey( $this->handle );
}

public function nextkey() {
return dba_nextkey( $this->handle );
}
}
24 changes: 24 additions & 0 deletions src/Reader/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class PHP extends Reader {
/* initialized if cdb_findnext() returns 1 */
protected $dlen;

/* initialized in firstkey() and nextkey() */
protected $pos = 0;

/**
* @param string $fileName
* @throws Exception
Expand Down Expand Up @@ -217,5 +220,26 @@ protected function find( $key ) {

return $this->findNext( $key );
}

public function exists( $key ) {
if ( $this->find( strval( $key ) ) ) {
return true;
}

return false;
}

public function firstkey() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your implementation is basically a transliteration of the PHP 5.3 implementation, but you could reduce duplication by having firstKey() set $this->pos = 2048 and then return $this->nextKey().

$this->pos = 2048;
return $this->nextkey();
}

public function nextkey() {
$buf = $this->read( 8, $this->pos );
$klen = $this->unpack31( substr( $buf, 0, 4 ) );
$dlen = $this->unpack31( substr( $buf, 4 ) );
$this->pos += 8 + $klen + $dlen;
return fread( $this->handle, $klen );
}
}

33 changes: 33 additions & 0 deletions test/CdbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ protected function setUp() {
$this->dbaCdbFile = tempnam( $temp, get_class( $this ) . '_' );
}

protected function tearDown() {
parent::tearDown();
unlink($this->phpCdbFile);
unlink($this->dbaCdbFile);
}

/**
* Make a random-ish string
* @return string
Expand Down Expand Up @@ -57,6 +63,8 @@ public function testCdbWrite() {
}
}

unset($data['']);

$w1->close();
$w2->close();

Expand Down Expand Up @@ -84,6 +92,31 @@ public function testCdbWrite() {
$this->cdbAssert( "PHP error", $key, $v1, $value );
$this->cdbAssert( "DBA error", $key, $v2, $value );
}

$r1->close();
$r2->close();

$r1 = new Reader\PHP( $this->phpCdbFile );
$r2 = new Reader\DBA( $this->dbaCdbFile );

$keys = array_keys($data);
$firstKey = array_shift($keys);

$this->assertTrue($r1->exists($firstKey), 'PHP entry exists');
$this->assertTrue($r2->exists($firstKey), 'DBA entry exists');
$this->assertFalse($r1->exists(-1), 'PHP entry doesn\'t exists');
$this->assertFalse($r2->exists(-1), 'DBA entry doesn\'t exists');

$firstKey1 = $r1->firstkey();
$firstKey2 = $r2->firstkey();

$this->assertEquals($firstKey1, $firstKey, 'PHP Match first key');
$this->assertEquals($firstKey2, $firstKey, 'DBA Match first key');

unset($data[$firstKey]);
for ($j = 0, $max = count($data); $j < $max; $j++) {
$this->assertEquals($r2->nextkey(), $r1->nextkey(), 'nextkey match');
}
}

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