summaryrefslogtreecommitdiff
path: root/lib/AnyEvent/Riak/Bucket.pm
blob: 0c690dd99260d6b27262af2e5c360b277566e2cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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;