From b5f8ecf61b083da72a6f56f89099fc098ffc2d7f Mon Sep 17 00:00:00 2001 From: franck cuny Date: Mon, 10 May 2010 21:55:33 +0200 Subject: class for bucket and object --- lib/AnyEvent/Riak/Bucket.pm | 91 +++++++++++++++++++++++++++++++++++++++++++++ lib/AnyEvent/Riak/Object.pm | 20 ++++++++++ 2 files changed, 111 insertions(+) create mode 100644 lib/AnyEvent/Riak/Bucket.pm create mode 100644 lib/AnyEvent/Riak/Object.pm diff --git a/lib/AnyEvent/Riak/Bucket.pm b/lib/AnyEvent/Riak/Bucket.pm new file mode 100644 index 0000000..0ab1e94 --- /dev/null +++ b/lib/AnyEvent/Riak/Bucket.pm @@ -0,0 +1,91 @@ +package AnyEvent::Riak::Bucket; + +use Moose; +use AnyEvent::HTTP; + +with qw/ + AnyEvent::Riak::Role::CVCB + AnyEvent::Riak::Role::HTTPUtils + /; + +has _client => (is => 'rw', isa => 'AnyEvent::Riak', required => 1); +has name => (is => 'rw', isa => 'Str', required => 1); +has _properties => + (is => 'rw', isa => 'HashRef', predicate => '_has_properties'); +has r => ( + is => 'rw', + isa => 'Int', + lazy => 1, + default => sub { my $self = shift; $self->_client->r } +); +has w => ( + is => 'rw', + isa => 'Int', + lazy => 1, + default => sub { my $self = shift; $self->_client->w } +); +has dw => ( + is => 'rw', + isa => 'Int', + lazy => 1, + default => sub { my $self = shift; $self->_client->dw } +); + +sub get_properties { + my ($self, %options) = @_; + + my ($cv, $cb) = $self->cvcb(\%options); + + if ($self->_has_properties) { + $cv->send($self->_properties); + } + else { + http_request( + GET => $self->_build_uri( + $self->_client->host, [$self->_client->path, $self->name], + $options{params} + ), + headers => $self->_build_headers($options{params}), + sub { + my ($body, $headers) = @_; + if ($body && $headers->{Status} == 200) { + my $prop = JSON::decode_json($body); + $self->_properties($prop); + $cv->send($cb->($self->_properties)); + } + else { + $cv->send(undef); + } + } + ); + } + return $cv; +} + +sub set_properties { +} + +sub create { + my ($self, $key, $content) = @_; + my $object = AnyEvent::Riak::Object->new( + _client => $self->_client, + key => $key, + content => $content, + bucket => $self, + ); + return $object; +} + +sub get { + my ($self, $key, $r) = @_; + my $obj = AnyEvent::Riak::Object->new( + client => $self->_client, + key => $key, + r => $r, + bucket => $self, + )->get; +} + +no Moose; + +1; diff --git a/lib/AnyEvent/Riak/Object.pm b/lib/AnyEvent/Riak/Object.pm new file mode 100644 index 0000000..f756af4 --- /dev/null +++ b/lib/AnyEvent/Riak/Object.pm @@ -0,0 +1,20 @@ +package AnyEvent::Riak::Object; + +use Moose; + +has _client => (is => 'rw', isa => 'AnyEvent::Riak', requid => 1); +has key => (is => 'rw', isa => 'Str'); +has content => (is => 'rw', isa => 'HashRef'); +has content_type => (is => 'rw', isa => 'Str', default => 'application/json'); +has bucket => (is => 'rw', isa => 'AnyEvent::Riak::Bucket', required => 1); +has status => (is => 'rw', isa => 'Int'); +has r => (is => 'rw', isa => 'Int'); + +sub get { + my ($self) = @_; + $self->_client->http_get($self->bucket_name, $self->key, $self->r); +} + +no Moose; + +1; -- cgit 1.4.1