diff options
author | Yuval Kogman <nothingmuch@woobling.org> | 2009-03-13 22:09:28 +0100 |
---|---|---|
committer | Yuval Kogman <nothingmuch@woobling.org> | 2009-03-13 22:09:28 +0100 |
commit | fbf6222fc9ee9f48ec34f323e099aa64c36a1ea4 (patch) | |
tree | 2ec27883ecb4609536ad62000d8bfeca3430eb72 | |
parent | fix stupid typo (diff) | |
download | kiokudb-backend-memcachedb-fbf6222fc9ee9f48ec34f323e099aa64c36a1ea4.tar.gz |
various cleanups
-rw-r--r-- | lib/KiokuDB/Backend/Memcachedb.pm | 46 |
1 files changed, 18 insertions, 28 deletions
diff --git a/lib/KiokuDB/Backend/Memcachedb.pm b/lib/KiokuDB/Backend/Memcachedb.pm index 1341c79..9d185ad 100644 --- a/lib/KiokuDB/Backend/Memcachedb.pm +++ b/lib/KiokuDB/Backend/Memcachedb.pm @@ -13,14 +13,7 @@ our $VERSION = "0.01"; with qw( KiokuDB::Backend KiokuDB::Backend::Serialize::Delegate - KiokuDB::Backend::Role::TXN::Memory - KiokuDB::Backend::Role::Concurrency::POSIX -); - -has create => ( - isa => "Bool", - is => "ro", - default => 0, + KiokuDB::Backend::Role::Clear ); sub BUILD { @@ -42,42 +35,35 @@ sub new_from_dsn_params { $self->new( %args, db => $db ); } -sub insert { - my ( $self, @entries ) = @_; - my $db = $self->db; - foreach my $entry ( @entries ) { - $db->set( $entry->id, $self->serialize( $entry ) ); - } -} - sub get { my ( $self, @ids ) = @_; my $db = $self->db; - my @objs = map { $self->deserialize( $db->get( $_ ) ) } @ids; - @objs; + my $entries = $self->db->get_multi( @ids ); + + my @objs; + + foreach my $id ( @ids ) { + return unless exists $entries->{$id}; + } + + return map { $self->deserialize($_) } @{ $entries }{@ids}; } -sub commit_entries { +sub insert { my ( $self, @entries ) = @_; + my $db = $self->db; - my @checks = $self->exists( map { $_->id } @entries ); foreach my $entry ( @entries ) { my $id = $entry->id; - my $status = shift @checks; if ( $entry->deleted ) { - if ( !$status ) { - croak "Entry $id doesn't exist in the database"; - } $db->delete( $id ); } else { - if ( $status and not $entry->has_prev ) { - croak "Entry $id already exists in the database"; - } - $db->set( $id, $self->serialize( $entry ) ); + my $method = $entry->has_prev ? "replace" : "add"; + die "error in $method $id" unless $db->$method( $id => $self->serialize($entry) ); } } } @@ -94,6 +80,10 @@ sub delete { $self->db->delete($_) foreach (@ids); } +sub clear { + shift->db->flush_all; +} + __PACKAGE__->meta->make_immutable; __PACKAGE__ |