diff options
author | franck cuny <franck@lumberjaph.net> | 2009-12-14 16:07:15 +0100 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2009-12-14 16:07:15 +0100 |
commit | 63550f0188cfc6c316acfe663b388268b64c4278 (patch) | |
tree | 3d29826b2f85120084c734dda8809e212a4d1b02 | |
parent | initial commit (diff) | |
download | kiokudb-backend-riak-master.tar.gz |
update code, stil 2 tests failing master
-rw-r--r-- | lib/KiokuDB/Backend/Riak.pm | 129 | ||||
-rw-r--r-- | t/10_riak.t | 35 |
2 files changed, 161 insertions, 3 deletions
diff --git a/lib/KiokuDB/Backend/Riak.pm b/lib/KiokuDB/Backend/Riak.pm index daa1f73..35af85c 100644 --- a/lib/KiokuDB/Backend/Riak.pm +++ b/lib/KiokuDB/Backend/Riak.pm @@ -1,14 +1,133 @@ package KiokuDB::Backend::Riak; +use Carp; use Moose; +use JSON::XS; +use AnyEvent::Riak; +use Try::Tiny; + +use namespace::clean -except => 'meta'; +use Data::Stream::Bulk::Util qw(bulk); + +with qw( + KiokuDB::Backend + KiokuDB::Backend::Role::Clear + KiokuDB::Backend::Serialize::JSPON +); + our $VERSION = '0.01'; +has bucket => (isa => 'Str', is => 'rw'); + +has db => ( + isa => "AnyEvent::Riak", + is => "ro", + handles => [qw(document)], +); + +sub new_from_dsn_params { + my ( $self, %args ) = @_; + + my $host = delete $args{host}; + my $path = delete $args{path}; + + $self->bucket( delete $args{bucket} ); + + croak "bucket is required" if !$self->bucket; + my $db = AnyEvent::Riak->new( { 'host' => $host, path => $path } ); + $self->new( %args, db => $db ); +} + +sub get { + my ( $self, @ids ) = @_; + return map {$self->get_entry($_)} @ids; +} + +sub get_entry { + my ($self, $id) = @_; + my $entry; + try { + my $obj = $self->db->fetch($self->bucket, $id)->recv; + $entry = $self->deserialize($obj->{object}); + }; + return $entry; +} + +sub deserialize { + my ( $self, $doc ) = @_; + my %doc = %{ $doc }; + return $self->expand_jspon(\%doc, backend_data => $doc ); +} + +sub exists { + my ( $self, @ids ) = @_; + + my @exists; + + foreach my $id (@ids) { + my $res; + try { + $res = $self->db->fetch($self->bucket, $_)->recv; + push @exists, 1 + }catch{ + push @exists, 0; + }; + } + return @exists; +} + +sub delete { + my ($self, @ids_or_entries) = @_; + my @ids = map { ref($_) ? $_->id : $_ } @ids_or_entries; + $self->db->delete($self->bucket, $_)->recv foreach (@ids); +} + +sub clear { + my ($self, ) = @_; + try { + my $res = $self->db->list_bucket($self->bucket); + return $self->delete(@{$res->{keys}}); + }; +} + +sub all_entries { + my ($self) = @_; + try { + my $res = $self->db->list_bucket($self->bucket); + return $self->get(@{$res->{keys}}); + }; +} + +sub insert { + my ($self, @entries) = @_; + for my $entry (@entries) { + $self->insert_entry($entry); + } + return; +} + +sub insert_entry { + my ( $self, $entry ) = @_; + my $check; + try { + my $res = $self->db->store( + { + bucket => $self->bucket, + key => $entry->id, + object => $self->collapse_jspon($entry), + links => [] + } + )->recv; + }; +} + 1; + __END__ =head1 NAME -KiokuDB::Backend::Riak - +KiokuDB::Backend::Riak - Riak backend for L<KiokuDB> =head1 SYNOPSIS @@ -16,16 +135,20 @@ KiokuDB::Backend::Riak - =head1 DESCRIPTION -KiokuDB::Backend::Riak is +This backend provides L<KiokuDB> support for Riak using <AnyEvent::Riak>. =head1 AUTHOR -franck cuny E<lt>franck.cuny {at} rtgi.frE<gt> +franck cuny E<lt>franck@lumberjaph.netE<gt> =head1 SEE ALSO =head1 LICENSE +Copyright 2009 by linkfluence. + +L<http://linkfluence.net> + This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. diff --git a/t/10_riak.t b/t/10_riak.t new file mode 100644 index 0000000..808a964 --- /dev/null +++ b/t/10_riak.t @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +use Scope::Guard; +use Test::More 'no_plan'; + +#BEGIN { +# plan skip_all => 'Please set KIOKU_RIAK_URI to a Riak instance URI' unless $ENV{KIOKU_RIAK_URI}; +# plan 'no_plan'; +#} + +use ok 'KiokuDB'; +use ok 'KiokuDB::Backend::Riak'; + +use KiokuDB::Test; + +use AnyEvent::Riak; + +my $db = AnyEvent::Riak->new( + host => 'http://localhost:8098', + path => 'jiak', +); + +my $bucket = $db->list_bucket('kiokudb')->recv; +foreach my $key (@{$bucket->{keys}}) { + $db->delete('kiokudb', $key)->recv; +} + +run_all_fixtures( + KiokuDB->new( + backend => KiokuDB::Backend::Riak->new( + db => $db, + bucket => 'kiokudb', + ), + ) +); |