line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Model::Riak; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
2023
|
$Catalyst::Model::Riak::AUTHORITY = 'cpan:NLTBO'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
19
|
$Catalyst::Model::Riak::VERSION = '0.07'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
524
|
use Net::Riak; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Moose; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
BEGIN { extends 'Catalyst::Model' } |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has host => ( |
15
|
|
|
|
|
|
|
isa => 'Str', |
16
|
|
|
|
|
|
|
is => 'ro', |
17
|
|
|
|
|
|
|
required => 1, |
18
|
|
|
|
|
|
|
default => sub { 'http://localhost:8098' } |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has ua_timeout => ( |
22
|
|
|
|
|
|
|
isa => 'Int', |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
required => 1, |
25
|
|
|
|
|
|
|
default => 900 |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has dw => ( |
29
|
|
|
|
|
|
|
isa => 'Int', |
30
|
|
|
|
|
|
|
is => 'rw', |
31
|
|
|
|
|
|
|
default => 1, |
32
|
|
|
|
|
|
|
trigger => \&_dw_set |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
has w => ( |
36
|
|
|
|
|
|
|
isa => 'Int', |
37
|
|
|
|
|
|
|
is => 'rw', |
38
|
|
|
|
|
|
|
default => 1, |
39
|
|
|
|
|
|
|
trigger => \&_w_set |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
has r => ( |
43
|
|
|
|
|
|
|
isa => 'Int', |
44
|
|
|
|
|
|
|
is => 'rw', |
45
|
|
|
|
|
|
|
default => 1, |
46
|
|
|
|
|
|
|
trigger => \&_r_set |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
has container => ( |
50
|
|
|
|
|
|
|
isa => 'Net::Riak::Bucket', |
51
|
|
|
|
|
|
|
is => 'rw' |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has 'client' => ( |
55
|
|
|
|
|
|
|
isa => 'Net::Riak', |
56
|
|
|
|
|
|
|
is => 'rw', |
57
|
|
|
|
|
|
|
lazy_build => 1, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
has 'object' => ( |
61
|
|
|
|
|
|
|
isa => 'Net::Riak::Object|Undef', |
62
|
|
|
|
|
|
|
is => 'rw' |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub _build_client { |
66
|
|
|
|
|
|
|
my($self) = @_; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $conn = Net::Riak->new( |
69
|
|
|
|
|
|
|
host => $self->host, |
70
|
|
|
|
|
|
|
ua_timeout => $self->ua_timeout, |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
if ( $self->dw != $conn->client->dw ) { $conn->client->dw($self->dw); } |
73
|
|
|
|
|
|
|
if ( $self->w != $conn->client->w ) { $conn->client->w($self->w); } |
74
|
|
|
|
|
|
|
if ( $self->r != $conn->client->r ) { $conn->client->r($self->r); } |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
return $conn; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub bucket { |
80
|
|
|
|
|
|
|
my($self, $data) = @_; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
if ( defined($data) ) { |
83
|
|
|
|
|
|
|
$self->container($self->client->bucket($data)); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
return $self->container; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub buckets { |
90
|
|
|
|
|
|
|
my($self) = @_; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
return $self->client->all_buckets; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub create { |
96
|
|
|
|
|
|
|
my($self, $data) = @_; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
if ( defined($data->{key}) && defined($data->{value}) ) |
100
|
|
|
|
|
|
|
{ |
101
|
|
|
|
|
|
|
my $object = $self->bucket->new_object($data->{key}, $data->{value}); |
102
|
|
|
|
|
|
|
return $object->store; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub delete { |
107
|
|
|
|
|
|
|
my($self, $data) = @_; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
if ( defined($data->{key}) ) { |
110
|
|
|
|
|
|
|
my $object = $self->get($data); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
if ( defined($object) ) { |
113
|
|
|
|
|
|
|
return $object->delete; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub get { |
119
|
|
|
|
|
|
|
my($self, $data) = @_; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
if ( defined($data->{key}) ) { |
122
|
|
|
|
|
|
|
my $object = $self->bucket->get($data->{key}); |
123
|
|
|
|
|
|
|
if ( $object->exists ) { |
124
|
|
|
|
|
|
|
$self->object( $object ); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
return $self->object; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub read { |
132
|
|
|
|
|
|
|
my($self, $data) = @_; |
133
|
|
|
|
|
|
|
return $self->get($data); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub update { |
137
|
|
|
|
|
|
|
my($self, $data) = @_; |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
if ( defined($data->{key}) ) { |
140
|
|
|
|
|
|
|
my $object = $self->get({ key => $data->{key} }); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
if ( defined($object) ) { |
143
|
|
|
|
|
|
|
$object->data($data->{value}); |
144
|
|
|
|
|
|
|
return $object->store($self->w, $self->dw); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub links { |
150
|
|
|
|
|
|
|
my($self, $data) = @_; |
151
|
|
|
|
|
|
|
if ( defined($data) && defined($data->{key}) ) |
152
|
|
|
|
|
|
|
{ |
153
|
|
|
|
|
|
|
my $object = $self->get($data->{key}); |
154
|
|
|
|
|
|
|
if ( defined($object) ) |
155
|
|
|
|
|
|
|
{ |
156
|
|
|
|
|
|
|
return $object->links(); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub _dw_set |
162
|
|
|
|
|
|
|
{ |
163
|
|
|
|
|
|
|
my($self, $nr) = @_; |
164
|
|
|
|
|
|
|
return $self->client->client->dw($nr); |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub _w_set |
168
|
|
|
|
|
|
|
{ |
169
|
|
|
|
|
|
|
my($self, $nr) = @_; |
170
|
|
|
|
|
|
|
return $self->client->client->w($nr); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub _r_set |
174
|
|
|
|
|
|
|
{ |
175
|
|
|
|
|
|
|
my($self, $nr) = @_; |
176
|
|
|
|
|
|
|
return $self->client->client->r($nr); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
__END__ |
182
|
|
|
|
|
|
|
=pod |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=head1 NAME |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Catalyst::Model::Riak - Basho/Riak model class for Catalyst |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 VERSION |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
version 0.01 |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head1 SYNOPSYS |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
# Use this to create a new model |
195
|
|
|
|
|
|
|
script/myapp_create.pl model ModelName Riak http:/192.168.0.1:8089 900 |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# In you controller use |
199
|
|
|
|
|
|
|
my $coder = JSON::XS->new->utf8->pretty->allow_nonref; |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# |
202
|
|
|
|
|
|
|
# Set bucket |
203
|
|
|
|
|
|
|
# |
204
|
|
|
|
|
|
|
$c->model("ModelName")->bucket('Bucket'); |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# |
207
|
|
|
|
|
|
|
# Create a key/value pair in the bucket |
208
|
|
|
|
|
|
|
$c->model('ModelName')->create( { key => 'key', value => $coder->encode($data) } ); |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# |
211
|
|
|
|
|
|
|
# Read key/value pair from the 'Bucket' |
212
|
|
|
|
|
|
|
my $object = $c->model('ModelName')->get({ key => 'key' }); |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
# |
215
|
|
|
|
|
|
|
# Update a key/value pair in the bucket |
216
|
|
|
|
|
|
|
$c->model('ModelName')->update( { key => 'key', value => $code->encode($newdata) } ); |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# |
219
|
|
|
|
|
|
|
# Delete a key/value pair from the bucket |
220
|
|
|
|
|
|
|
$c->model('ModelName')->delete( { key => 'key' } ); |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# |
223
|
|
|
|
|
|
|
# Get linked objects |
224
|
|
|
|
|
|
|
$c->model('ModelName')->links( { key => 'key' } ); |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# |
227
|
|
|
|
|
|
|
# Or |
228
|
|
|
|
|
|
|
# |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
# |
231
|
|
|
|
|
|
|
# Create a key/value pair |
232
|
|
|
|
|
|
|
my $object = $c->model("ModelName")->bucket('Container')->new_object('key', $coder->encode($data) ); |
233
|
|
|
|
|
|
|
$object->store; |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# |
236
|
|
|
|
|
|
|
# Get a key/value pair |
237
|
|
|
|
|
|
|
my $object = $c->model("ModelName")->bucket('Container')->get('key'); |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
# |
240
|
|
|
|
|
|
|
# Update a key/value pair |
241
|
|
|
|
|
|
|
$object->data($coder->encode($newdata)); |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
# |
244
|
|
|
|
|
|
|
# Delete a key/value pair |
245
|
|
|
|
|
|
|
$object->delete; |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 DESCRIPTION |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Use this model set create a new L<Catalyst::Model::Riak> model for your Catalyst application. |
251
|
|
|
|
|
|
|
Check the L<Net::Riak> documentation for addtional information. Also visit L<http://www.basho.com> |
252
|
|
|
|
|
|
|
for more information on Riak. |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head1 METHODS |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
=head2 bucket |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
Set the bucket and returns a Net::Riak::Bucket object. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
$c->model("ModelName")->bucket("Container"); |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head2 buckets |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
Returns an array of all available buckets. |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=head2 create |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
Creates a new key/value pair |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
$c->model("ModelName")->create({ key => 'keyname', value => $json_data }); |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head2 delete |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Deletes a key/value pair |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head2 get |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Get a key/value pair from the riak server. It returns a L<Net::Riak::Object>. |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head2 read |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
Synonym for get |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head2 update |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Update a key/value pair |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
$c->model('ModelName')->update( { key => 'key', value => $json_data } ); |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head2 dw |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Get or set the number of partitions to wait for write confirmation |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=head2 w |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
Get or set the number of responding partitions to wait for while writing or updating a value |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=head2 r |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
Get or set the number of responding partitions to wait for while retrieving an object |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=head1 SUPPORT |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
Repository |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
https://github.com/Mainframe2008/CatRiak |
308
|
|
|
|
|
|
|
Pull request and additional contributors are welcome |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
Issue Tracker |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
https://github.com/Mainframe2008/CatRiak/issues |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=head1 AUTHOR |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
Theo Bot <nltbo@cpan.org> L<http://www.proxy.nl> |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
This software is copyright (c) 2013 by Theo Bot |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
323
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
=cut |