summaryrefslogtreecommitdiff
path: root/lib/AnyEvent/Riak
diff options
context:
space:
mode:
Diffstat (limited to 'lib/AnyEvent/Riak')
-rw-r--r--lib/AnyEvent/Riak/Bucket.pm113
-rw-r--r--lib/AnyEvent/Riak/Object.pm52
-rw-r--r--lib/AnyEvent/Riak/Role/CVCB.pm24
-rw-r--r--lib/AnyEvent/Riak/Role/Client.pm12
-rw-r--r--lib/AnyEvent/Riak/Role/HTTPUtils.pm16
5 files changed, 20 insertions, 197 deletions
diff --git a/lib/AnyEvent/Riak/Bucket.pm b/lib/AnyEvent/Riak/Bucket.pm
deleted file mode 100644
index 0c690dd..0000000
--- a/lib/AnyEvent/Riak/Bucket.pm
+++ /dev/null
@@ -1,113 +0,0 @@
-package AnyEvent::Riak::Bucket;
-
-use Moose;
-use AnyEvent::HTTP;
-
-use AnyEvent::Riak::Object;
-
-with qw/
- AnyEvent::Riak::Role::CVCB
- AnyEvent::Riak::Role::HTTPUtils
- AnyEvent::Riak::Role::Client
- /;
-
-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->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 {
- my ($self, $schema, %options) = @_;
-
- my ($cv, $cb) = $self->cvcb(\%options);
-
- http_request(
- PUT =>
- $self->_build_uri([$self->{path}, $self->name], $options{params}),
- headers => $self->_build_headers($options{params}),
- body => JSON::encode_json({props => $schema}),
- sub {
- my ($body, $headers) = @_;
- if ($headers->{Status} == 204) {
- $cv->send($cb->(1));
- }
- else {
- $cv->send($cb->(0));
- }
- }
- );
- return $cv;
-}
-
-sub create {
- my ($self, $key, $content) = @_;
- my $object = AnyEvent::Riak::Object->new(
- _client => $self->_client,
- key => $key,
- content => $content,
- bucket => $self,
- );
- return $object;
-}
-
-sub object {
- my ($self, $key, $r) = @_;
- my $obj = AnyEvent::Riak::Object->new(
- _client => $self->_client,
- key => $key,
- r => $r,
- bucket => $self,
- );
-}
-
-no Moose;
-
-1;
diff --git a/lib/AnyEvent/Riak/Object.pm b/lib/AnyEvent/Riak/Object.pm
deleted file mode 100644
index d106254..0000000
--- a/lib/AnyEvent/Riak/Object.pm
+++ /dev/null
@@ -1,52 +0,0 @@
-package AnyEvent::Riak::Object;
-
-use Moose;
-use AnyEvent::HTTP;
-
-with qw/
- AnyEvent::Riak::Role::Client
- AnyEvent::Riak::Role::HTTPUtils
- AnyEvent::Riak::Role::CVCB
- /;
-
-has key => (is => 'rw', isa => 'Str');
-has _content => (is => 'rw', isa => 'HashRef', predicate => '_has_content');
-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, %options) = @_;
-
- my ($cv, $cb) = $self->cvcb(\%options);
-
- if ($self->_has_content) {
- $cv->send($self->_content);
- }
- else {
- http_request(
- GET => $self->_build_uri(
- [$self->_client->path, $self->bucket->name, $self->key],
- $options{params}
- ),
- headers => $self->_build_headers($options{params}),
- sub {
- my ($body, $headers) = @_;
- if ($body && $headers->{Status} == 200) {
- my $content = JSON::decode_json($body);
- $self->_content($content);
- $cv->send($cb->($self->_content));
- }
- else {
- $cv->send(undef);
- }
- }
- );
- }
- return $cv;
-}
-
-no Moose;
-
-1;
diff --git a/lib/AnyEvent/Riak/Role/CVCB.pm b/lib/AnyEvent/Riak/Role/CVCB.pm
index 74684c2..73812c2 100644
--- a/lib/AnyEvent/Riak/Role/CVCB.pm
+++ b/lib/AnyEvent/Riak/Role/CVCB.pm
@@ -1,27 +1,19 @@
package AnyEvent::Riak::Role::CVCB;
-use Moose::Role;
+# ABSTRACT: return a default condvar and callback if none defined
-sub default_cb {
- my ($self, $options) = @_;
- return sub {
- my $res = shift;
- return $res;
- };
-}
+use Moose::Role;
-sub cvcb {
+sub _cvcb {
my ($self, $options) = @_;
- my ($cv, $cb);
- $cv = AE::cv;
- if ($options->{callback}) {
- $cb = delete $options->{callback};
- }
- else {
- $cb = $self->default_cb();
+ my ($cv, $cb) = (AnyEvent->condvar, sub { return @_ });
+ if ($options && @$options) {
+ $cv = pop @$options if UNIVERSAL::isa($options->[-1], 'AnyEvent::CondVar');
+ $cb = pop @$options if ref $options->[-1] eq 'CODE';
}
($cv, $cb);
}
1;
+
diff --git a/lib/AnyEvent/Riak/Role/Client.pm b/lib/AnyEvent/Riak/Role/Client.pm
deleted file mode 100644
index 0623e71..0000000
--- a/lib/AnyEvent/Riak/Role/Client.pm
+++ /dev/null
@@ -1,12 +0,0 @@
-package AnyEvent::Riak::Role::Client;
-
-use Moose::Role;
-
-has _client => (
- is => 'rw',
- isa => 'AnyEvent::Riak',
- required => 1,
- handles => {host => 'host', client_id => 'client_id'}
-);
-
-1;
diff --git a/lib/AnyEvent/Riak/Role/HTTPUtils.pm b/lib/AnyEvent/Riak/Role/HTTPUtils.pm
index 399f369..701af5d 100644
--- a/lib/AnyEvent/Riak/Role/HTTPUtils.pm
+++ b/lib/AnyEvent/Riak/Role/HTTPUtils.pm
@@ -1,15 +1,23 @@
package AnyEvent::Riak::Role::HTTPUtils;
+# ABSTRACT: HTTP methods
+
use Moose::Role;
use AnyEvent;
use AnyEvent::HTTP;
use URI;
-
use MIME::Base64;
+has client_id => (is => 'rw', isa => 'Str', lazy_build => 1,);
+
+sub _build_client_id {
+ "perl_anyevent_riak" . encode_base64(int(rand(10737411824)), '');
+}
+
sub _build_uri {
my ($self, $path, $options) = @_;
+
my $uri = URI->new($self->host);
$uri->path(join("/", @$path));
$uri->query_form($self->_build_query($options));
@@ -17,8 +25,8 @@ sub _build_uri {
}
sub _build_headers {
- my ($self, $options) = @_;
- my $headers = delete $options->{headers} || {};
+ my $self = shift;
+ my $headers = shift || {};
$headers->{'X-Riak-ClientId'} = $self->client_id;
$headers->{'Content-Type'} = 'application/json'
@@ -28,7 +36,7 @@ sub _build_headers {
sub _build_query {
my ($self, $options) = @_;
- my $valid_options = [qw/props keys returnbody/];
+ my $valid_options = [qw/props keys returnbody w r dw/];
my $query;
foreach (@$valid_options) {
$query->{$_} = $options->{$_} if exists $options->{$_};