summary refs log tree commit diff
path: root/lib/Net/Riak/Bucket.pm
blob: ec17f5b3d51129dd3289062452d680e45e98d7af (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package Net::Riak::Bucket;

# ABSTRACT: Access and change information about a Riak bucket

use JSON;
use Moose;
use Net::Riak::Object;

has name => (
    is       => 'ro',
    isa      => 'Str',
    required => 1
);
has client => (
    is       => 'ro',
    isa      => 'Net::Riak',
    required => 1
);
has content_type => (
    is      => 'rw',
    isa     => 'Str',
    default => 'application/json'
);

has r => (
    is      => 'rw',
    isa     => 'Int',
    lazy    => 1,
    default => sub { (shift)->client->r }
);
has w => (
    is      => 'rw',
    isa     => 'Int',
    lazy    => 1,
    default => sub { (shift)->client->w }
);
has dw => (
    is      => 'rw',
    isa     => 'Int',
    lazy    => 1,
    default => sub { (shift)->client->dw }
);

sub n_val {
    my $self = shift;
    if (my $val = shift) {
        $self->set_property('n_val', $val);
    }
    else {
        $self->get_property('n_val');
    }
}

sub allow_multiples {
    my $self = shift;
    # XXX use JSON::false / true ?
    if (my $val = shift) {
        $self->set_property('allow_mult', $val);
    }
    else {
        return $self->get_property('allow_mult');
    }
}

sub get {
    my ($self, $key, $r) = @_;
    my $obj = Net::Riak::Object->new(
        client => $self->client,
        bucket => $self,
        key    => $key
    );
    $r ||= $self->r;
    $obj->load($r);
    $obj;
}

sub set_property {
    my ($self, $key, $value) = @_;
    $self->set_properties({$key => $value});
}

sub get_property {
    my ($self, $key) = @_;
    my $props = $self->get_properties;
    return $props->{$key};
}

sub get_properties {
    my $self = shift;

    my $params = {props => 'True', keys => 'False'};

    my $request =
      $self->client->request('GET', [$self->client->prefix, $self->name],
        $params);

    my $response = $self->client->useragent->request($request);

    my $props = {};
    if ($response->is_success) {
        $props = JSON::decode_json($response->content);
        $props = $props->{props};
    }
    return $props;
}

sub set_properties {
    my ($self, $props) = @_;

    my $request = $self->client->request('PUT', [$self->client->prefix, $self->name]);
    $request->header('Content-Type' => $self->content_type);
    $request->content(JSON::encode_json({props => $props}));
    my $response = $self->client->useragent->request($request);

    if (!$response->is_success) {
        # XXX
    }

    if ($response->code != 204) {
        # XXX
    }
}

sub new_object {
    my ($self, $key, $data) = @_;
    my $object = Net::Riak::Object->new(
        key    => $key,
        data   => $data,
        bucket => $self,
        client => $self->client
    );
    $object;
}

1;

=head1 SYNOPSIS

The L<Net::Riak::Bucket> object allows you to access and change information about a Riak bucket, and provides methods to create or retrieve objects within the bucket.

=head1 DESCRIPTION

=head2 ATTRIBUTES

=item B<name>

Get the bucket name

=item B<r>

R value setting for this client (default 2)

=item B<w>

W value setting for this client (default 2)

=item B<dw>

DW value setting for this client (default 2)

=head2 METHODS

=method new_object

Create a new L<Net::Riak::Object> object that will be stored as JSON.

=method get

Retrieve a JSON-encoded object from Riak

=method n_val

Get/set the N-value for this bucket, which is the number of replicas that will be written of each object in the bucket. Set this once before you write any data to the bucket, and never change it again, otherwise unpredictable things could happen. This should only be used if you know what you are doing.

=method allow_multiples

If set to True, then writes with conflicting data will be stored and returned to the client. This situation can be detected by calling has_siblings() and get_siblings(). This should only be used if you know what you are doing.

=method set_property

Set a bucket property. This should only be used if you know what you are doing.

=method get_property

Retrieve a bucket property

=method set_properties

Set multiple bucket properties in one call. This should only be used if you know what you are doing.

=method get_properties

Retrieve an associative array of all bucket properties.