summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-05-10 21:55:33 +0200
committerfranck cuny <franck@lumberjaph.net>2010-05-10 21:55:33 +0200
commitb5f8ecf61b083da72a6f56f89099fc098ffc2d7f (patch)
treed681ec789320bb84ed97294fa3b4c1237548867a
parentuse moose, keep compatibility with old interface (diff)
downloadanyevent-riak-b5f8ecf61b083da72a6f56f89099fc098ffc2d7f.tar.gz
class for bucket and object
-rw-r--r--lib/AnyEvent/Riak/Bucket.pm91
-rw-r--r--lib/AnyEvent/Riak/Object.pm20
2 files changed, 111 insertions, 0 deletions
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;