Merge pull request #1 from charsbar/tie

tie interface
This commit is contained in:
Tokuhiro Matsuno 2013-07-03 21:53:57 -07:00
commit 89c490c504
2 changed files with 114 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package UnQLite;
use 5.008005; use 5.008005;
use strict; use strict;
use warnings; use warnings;
use Carp ();
our $VERSION = "0.01"; our $VERSION = "0.01";
our $rc = 0; our $rc = 0;
@ -9,6 +10,12 @@ our $rc = 0;
use XSLoader; use XSLoader;
XSLoader::load(__PACKAGE__, $VERSION); XSLoader::load(__PACKAGE__, $VERSION);
sub TIEHASH {
my $class = shift;
my $self = $class->open(@_) or Carp::croak $class->errstr;
$self->cursor_init;
}
sub rc { $UnQLite::rc } sub rc { $UnQLite::rc }
sub errstr { sub errstr {
@ -96,6 +103,62 @@ sub DESTROY {
_release($self->[0], $self->[1]); _release($self->[0], $self->[1]);
} }
# tie interface
sub FETCH {
my ($self, $key) = @_;
$self->[1]->kv_fetch($key);
}
sub STORE {
my ($self, $key, $value) = @_;
$self->[1]->kv_store($key, $value) or Carp::croak $self->[1]->errstr;
$value;
}
sub DELETE {
my ($self, $key) = @_;
my $prev = $self->[1]->kv_fetch($key);
my $errstr = $self->[1]->errstr;
return unless $errstr && $errstr eq 'UNQLITE_OK';
$self->[1]->kv_delete($key) or Carp::croak $self->[1]->errstr;
$prev;
}
sub FIRSTKEY {
my $self = shift;
$self->first_entry or return;
$self->key;
}
sub NEXTKEY {
my $self = shift;
$self->next_entry or return;
$self->key;
}
sub EXISTS {
my ($self, $key) = @_;
$self->[1]->kv_fetch($key) and return 1;
my $errstr = $self->[1]->errstr;
return $errstr && $errstr eq 'UNQLITE_OK' ? 1 : 0;
}
sub CLEAR {
my $self = shift;
$self->first_entry or return;
$self->delete_entry while $self->valid_entry;
return;
}
sub SCALAR {
my $self = shift;
$self->first_entry or return;
my $ct = 1;
$ct++ while $self->next_entry && $self->valid_entry;
return $ct;
}
1; 1;
__END__ __END__
@ -117,6 +180,11 @@ UnQLite - Perl bindings for UnQLite
$db->kv_delete('foo'); $db->kv_delete('foo');
undef $db; # close database undef $db; # close database
# tie interface
tie my %hash, 'UnQLite', 'foo.db';
$hash{foo} = 'bar';
say $hash{foo}; # => bar
=head1 DESCRIPTION =head1 DESCRIPTION
UnQLite is a in-process software library which implements a self-contained, serverless, zero-configuration, transactional NoSQL database engine. UnQLite is a document store database similar to MongoDB, Redis, CouchDB etc. as well a standard Key/Value store similar to BerkeleyDB, LevelDB, etc. UnQLite is a in-process software library which implements a self-contained, serverless, zero-configuration, transactional NoSQL database engine. UnQLite is a document store database similar to MongoDB, Redis, CouchDB etc. as well a standard Key/Value store similar to BerkeleyDB, LevelDB, etc.

46
t/03_tie.t Normal file
View file

@ -0,0 +1,46 @@
use strict;
use Test::More;
use File::Temp qw(tempdir);
use UnQLite;
my $tmp = tempdir( CLEANUP => 1 );
{
my $db = UnQLite->open("$tmp/foo.db");
isa_ok($db, 'UnQLite');
ok($db->kv_store("foo", "bar"));
is($db->kv_fetch('foo'), 'bar');
ok($db->kv_delete('foo'));
is($db->kv_fetch('foo'), undef);
$db->kv_store('yay', 'yap');
$db->kv_append('yay', 'po');
is($db->kv_fetch('yay'), 'yappo');
is($db->rc,0);
}
{
ok -e "$tmp/foo.db", "foo.db exists";
tie my %hash, 'UnQLite', "$tmp/foo.db";
# stored data can be retrieved again?
is($hash{yay}, 'yappo');
$hash{foo} = 'baz';
is($hash{foo}, 'baz');
$hash{delete} = 'delete';
is(delete $hash{delete}, 'delete');
is(delete $hash{delete}, undef);
ok(exists $hash{foo});
ok(!exists $hash{delete});
is(join(" ", sort keys %hash), "foo yay");
is(join(" ", sort values %hash), "baz yappo");
is(scalar %hash, 2);
%hash = ();
is(join(" ", sort keys %hash), "");
is(join(" ", sort values %hash), "");
is(scalar %hash, undef);
}
done_testing;