Data/Riak/Fast/Bucket.pm

filter: Statementstmt 29.8% 31 / 104 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 39.1% 9 / 23 Podpod 57.1% 8 / 14 totaltotal 22.1% 48 / 217 CC 32 SCAR 35.6
CC
32
Cov
19%
add
110.0
get
56.0
create_link
30.0
CRAP 35.2
« Data/Riak/Fast/Result.pm ↑ Summary Data/Riak/Fast/Link.pm »
1 package Data::Riak::Fast::Bucket;
2 # ABSTRACT: A Data::Riak::Fast bucket, used for storing keys and values.
3  
4 22 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use Data::Riak::Fast::Link;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Data::Riak::Fast::Util::MapCount;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Data::Riak::Fast::Util::ReduceCount;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use Data::Riak::Fast::MapReduce;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Data::Riak::Fast::MapReduce::Phase::Reduce;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 ▶use HTTP::Headers::ActionPack::LinkList;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 22 ▶use JSON::XS qw/decode_json encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 with 'Data::Riak::Fast::Role::HasRiak';
18  
19 =head1 DESCRIPTION
20  
21 Data::Riak::Fast::Bucket is the primary interface that most people will use for Riak.
22 Adding and removing keys and values, adding links, querying keys; all of those
23 happen here.
24  
25 =head1 SYNOPSIS
26  
27 my $bucket = Data::Riak::Fast::Bucket->new({
28 name => 'my_bucket',
29 riak => $riak
30 });
31  
32 # Sets the value of "foo" to "bar", in my_bucket.
33 $bucket->add('foo', 'bar');
34  
35 # Gets the Result object for "foo" in my_bucket.
36 my $foo = $bucket->get('foo');
37  
38 # Returns "bar"
39 my $value = $foo->value;
40  
41 $bucket->create_alias({ key => 'foo', as => 'alias_to_foo' });
42 $bucket->create_alias({ key => 'foo', as => 'alias_to_foo', in => $another_bucket });
43  
44 # Returns "bar"
45 my $value = $bucket->resolve_alias('alias_to_foo');
46 my $value = $another_bucket->resolve_alias('alias_to_foo');
47  
48 $bucket->add('baz, 'value of baz', { links => [$bucket->create_link( riaktag => 'buddy', key =>'foo' )] });
49 my $resultset = $bucket->linkwalk('baz', [[ 'buddy', '_' ]]);
50 my $value = $resultset->first->value; # Will be "bar", the value of foo
51  
52 =cut
53  
54 22 has name => (
55 is => 'ro',
56 isa => 'Str',
57 required => 1
58 );
59  
60 =head1 METHOD
61 =head2 add ($key, $value, $opts)
62  
63 This will insert a key C<$key> into the bucket, with value C<$value>. The C<$opts>
64 can include links, allowed content types, or queries.
65  
66 =cut
67  
68 0 ▶sub add {
Subroutine add: not called CC=10 SCAR=47.0
Pod undocumented
69 0 my ($self, $key, $value, $opts) = @_;
70  
71 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
72  
73 0 my $pack = HTTP::Headers::ActionPack::LinkList->new;
74 0 ▶ if($opts->{'links'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"links"}) 0 0 0
75 0 foreach my $link (@{$opts->{'links'}}) {
76 0 ▶ if(blessed $link && $link->isa('Data::Riak::Fast::Link')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($link) and $link->isa('Data::Riak::Fast::Link')) { } 0 0 0
Condition0%0/3
builtin::blessed($link) and $link->isa("Data::Riak::Fast::Link")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($link) and $link->isa("Data::Riak::Fast::Link")
builtin::blessed($link) $link->isa("Data::Riak::Fast::Link")
Truth table0%0/3
A and B
A= builtin::blessed($link)B= $link->isa("Data::Riak::Fast::Link")
ABresult
0X0
100
111
77 0 $pack->add($link->as_link_header);
78 }
79 else {
80 0 confess "Bad link type ($link)";
81 }
82 }
83 }
84  
85 # TODO:
86 # need to support other headers
87 # X-Riak-Vclock if the object already exists, the vector clock attached to the object when read.
88 # X-Riak-Meta-* - any additional metadata headers that should be stored with the object.
89 # see http://wiki.basho.com/HTTP-Store-Object.html
90 # - SL
91  
92 my $resultset = $self->riak->send_request({
93 method => 'PUT',
94 uri => sprintf('buckets/%s/keys/%s', $self->name, $key),
95 data => $value,
96 links => $pack,
97 (exists $opts->{'indexes'}
98 ? (indexes => $opts->{'indexes'})
99 : ()),
100 (exists $opts->{'content_type'}
101 ? (content_type => $opts->{'content_type'})
102 : ()),
103 (exists $opts->{'query'}
104 0 ▶ ? (query => $opts->{'query'})
Branch: 3 branches
BranchTrueFalseTotal
exists $opts->{'indexes'} ? : 0 0 0
exists $opts->{'content_type'} ? : 0 0 0
exists $opts->{'query'} ? : 0 0 0
105 : ()),
106 });
107  
108 0 ▶ return $resultset->first if $resultset;
Branch: 1 branch
BranchTrueFalseTotal
if $resultset 0 0 0
109 0 return;
110 }
111  
112 =head2 remove ($key, $opts)
113  
114 This will remove a key C<$key> from the bucket.
115  
116 =cut
117  
118 0 ▶sub remove {
Subroutine remove: not called CC=3 SCAR=24.8
Pod documented
119 0 my ($self, $key, $opts) = @_;
120  
121 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
122  
123 return $self->riak->send_request({
124 method => 'DELETE',
125 uri => sprintf('buckets/%s/keys/%s', $self->name, $key),
126 (exists $opts->{'query'}
127 0 ▶ ? (query => $opts->{'query'})
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'query'} ? : 0 0 0
128 : ()),
129 });
130 }
131  
132 =head2 get ($key, $opts)
133  
134 This will get a key C<$key> from the bucket, returning a L<Data::Riak::Fast::Result> object.
135  
136 =cut
137  
138  
139 0 ▶sub get {
Subroutine get: not called CC=7 SCAR=40.3
Pod documented
140 0 my ($self, $key, $opts) = @_;
141  
142 0 ▶ die("This method requires a key") unless($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
143  
144 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
145  
146 confess "This method does not support multipart/mixed responses"
147 0 ▶ if exists $opts->{'accept'} && $opts->{'accept'} eq 'multipart/mixed';
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"accept"} and $opts->{"accept"} eq "multipart/mixed" 0 0 0
Condition0%0/3
exists $opts->{"accept"} and $opts->{"accept"} eq "multipart/mixed"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"accept"} and $opts->{"accept"} eq "multipart/mixed"
exists $opts->{"accept"} $opts->{"accept"} eq "multipart/mixed"
Truth table0%0/3
A and B
A= exists $opts->{"accept"}B= $opts->{"accept"} eq "multipart/mixed"
ABresult
0X0
100
111
148  
149 return $self->riak->send_request({
150 method => 'GET',
151 uri => sprintf('buckets/%s/keys/%s', $self->name, $key),
152 (exists $opts->{'accept'}
153 ? (accept => $opts->{'accept'})
154 : ()),
155 (exists $opts->{'query'}
156 0 ▶ ? (query => $opts->{'query'})
Branch: 2 branches
BranchTrueFalseTotal
exists $opts->{'accept'} ? : 0 0 0
exists $opts->{'query'} ? : 0 0 0
157 : ()),
158 })->first;
159 }
160  
161 =head2 list_keys
162  
163 List all the keys in the bucket. Warning: This is expensive, as it has to scan
164 every key in the system, so don't use it unless you mean it, and know what you're
165 doing.
166  
167 =cut
168  
169  
170 0 ▶sub list_keys {
Subroutine list_keys: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172  
173 0 my $result = $self->riak->send_request({
174 method => 'GET',
175 uri => sprintf('buckets/%s/keys', $self->name),
176 query => { keys => 'true' }
177 })->first;
178  
179 0 return decode_json( $result->value )->{'keys'};
180 }
181  
182 =head2 count
183  
184 Count all the keys in a bucket. This uses MapReduce to figure out the answer, so
185 it's expensive; Riak does not keep metadata on buckets for reasons that are beyond
186 the scope of this module (but are well documented, so if you are interested, read up).
187  
188 =cut
189  
190 0 ▶sub count {
Subroutine count: not called CC=2 SCAR=17.9
Pod documented
191 0 my $self = shift;
192 0 my $map_reduce = Data::Riak::Fast::MapReduce->new({
193 riak => $self->riak,
194 inputs => $self->name,
195 phases => [
196 Data::Riak::Fast::Util::MapCount->new,
197 Data::Riak::Fast::Util::ReduceCount->new
198 ]
199 });
200 0 my $map_reduce_results = $map_reduce->mapreduce;
201 0 my ( $result ) = $map_reduce_results->results->[0];
202 0 ▶ my ( $count ) = decode_json($result->value) || 0;
Condition0%0/2
&decode_json(scalar $result->value) || 0
l!l
00
MC/DC0%0/1
&decode_json(scalar $result->value) || 0
&decode_json(scalar $result->value)
Truth table0%0/2
A
A= &decode_json(scalar $result->value)
Aresult
11
00
203 0 return $count->[0];
204 }
205  
206 =head2 remove_all
207  
208 Remove all the keys from a bucket. This involves a list_keys call, so it will be
209 slow on larger systems.
210  
211 =cut
212  
213 0 ▶sub remove_all {
Subroutine remove_all: not called CC=4 SCAR=30.0
Pod documented
214 0 my $self = shift;
215 0 my $keys = $self->list_keys;
216 0 ▶ return unless ref $keys eq 'ARRAY' && @$keys;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keys eq "ARRAY" and @$keys 0 0 0
Condition0%0/3
ref $keys eq "ARRAY" and @$keys
!ll&&!rl&&r
000
MC/DC0%0/2
ref $keys eq "ARRAY" and @$keys
ref $keys eq "ARRAY" @$keys
Truth table0%0/3
A and B
A= ref $keys eq "ARRAY"B= @$keys
ABresult
0X0
100
111
217 0 foreach my $key ( @$keys ) {
218 0 $self->remove( $key );
219 }
220 }
221  
222 0 ▶sub create_link {
Subroutine create_link: not called CC=5 SCAR=34.0
Pod undocumented
223 0 my $self = shift;
224 0 ▶ my %opts = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
225 0 ▶ confess "You must provide a key for a link" unless exists $opts{key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"key"} 0 0 0
226 0 ▶ confess "You must provide a riaktag for a link" unless exists $opts{riaktag};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"riaktag"} 0 0 0
227 return Data::Riak::Fast::Link->new({
228 bucket => $self->name,
229 key => $opts{key},
230 riaktag => $opts{riaktag},
231 0 ▶ (exists $opts{params} ? (params => $opts{params}) : ())
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'params'} ? : 0 0 0
232 });
233 }
234  
235 0 ▶sub linkwalk {
Subroutine linkwalk: not called CC=2 SCAR=17.9
Pod undocumented
236 0 my ($self, $object, $params) = @_;
237 0 ▶ return unless $params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params 0 0 0
238 0 return $self->riak->linkwalk({
239 bucket => $self->name,
240 object => $object,
241 params => $params
242 });
243 }
244  
245 =head2 search_index
246  
247 Searches a Secondary Index to find results.
248  
249 =cut
250  
251 0 ▶sub search_index {
Subroutine search_index: not called CC=4 SCAR=30.0
Pod documented
252 0 my ($self, $opts) = @_;
253 0 ▶ my $field = $opts->{'field'} || die 'You must specify a field for searching Secondary indexes';
Condition0%0/2
$opts->{'field'} || die('You must specify a field for searching Secondary indexes')
l!l
00
MC/DC0%0/1
$opts->{'field'} || die('You must specify a field for searching Secondary indexes')
$opts->{'field'}
Truth table0%0/2
A
A= $opts->{'field'}
Aresult
11
00
254 0 ▶ my $values = $opts->{'values'} || die 'You must specify values for searching Secondary indexes';
Condition0%0/2
$opts->{'values'} || die('You must specify values for searching Secondary indexes')
l!l
00
MC/DC0%0/1
$opts->{'values'} || die('You must specify values for searching Secondary indexes')
$opts->{'values'}
Truth table0%0/2
A
A= $opts->{'values'}
Aresult
11
00
255  
256 0 my $inputs = { bucket => $self->name, index => $field };
257 0 ▶ if(ref($values) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $values eq 'ARRAY') { } 0 0 0
258 0 $inputs->{'start'} = $values->[0];
259 0 $inputs->{'end'} = $values->[1];
260 } else {
261 0 $inputs->{'key'} = $values;
262 }
263  
264 0 my $search_mr = Data::Riak::Fast::MapReduce->new({
265 riak => $self->riak,
266 inputs => $inputs,
267 phases => [
268 Data::Riak::Fast::MapReduce::Phase::Reduce->new({
269 language => 'erlang',
270 module => 'riak_kv_mapreduce',
271 function => 'reduce_identity',
272 keep => 1
273 })
274 ]
275 });
276 0 return $search_mr->mapreduce->results->[0]->value;
277 }
278  
279 # returns JUST the list of keys. human readable, not designed for MapReduce inputs.
280 0 ▶sub pretty_search_index {
Subroutine pretty_search_index: not called CC=1 SCAR=6.9
Pod undocumented
281 0 my ($self, $opts) = @_;
282 0 return [ sort map { $_->[1] } @{decode_json($self->search_index($opts))} ];
283 }
284  
285 0 ▶sub props {
Subroutine props: not called CC=1 SCAR=6.9
Pod undocumented
286 0 my $self = shift;
287  
288 0 my $result = $self->riak->send_request({
289 method => 'GET',
290 uri => sprintf('buckets/%s/props', $self->name)
291 })->first;
292  
293 0 return decode_json( $result->value )->{'props'};
294 }
295  
296 0 ▶sub indexing {
Subroutine indexing: not called CC=2 SCAR=17.9
Pod undocumented
297 0 my ($self, $enable) = @_;
298  
299 0 my $data;
300  
301 0 ▶ if($enable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enable) { } 0 0 0
302 0 $data->{props}->{precommit}->{mod} = 'riak_search_kv_hook';
303 0 $data->{props}->{precommit}->{fun} = 'precommit';
304 } else {
305 0 $data->{props}->{precommit}->{mod} = undef;
306 0 $data->{props}->{precommit}->{fun} = undef;
307 };
308  
309 0 return $self->riak->send_request({
310 method => 'PUT',
311 content_type => 'application/json',
312 uri => $self->name,
313 data => encode_json($data)
314 });
315 }
316  
317 =head2 create_alias ($opts)
318  
319 Creates an alias for a record using links. Helpful if your primary ID is a UUID or
320 some other automatically generated identifier. Can cross buckets, as well.
321  
322 $bucket->create_alias({ key => '123456', as => 'foo' });
323 $bucket->create_alias({ key => '123456', as => 'foo', in => $other_bucket });
324  
325 =cut
326  
327 0 ▶sub create_alias {
Subroutine create_alias: not called CC=2 SCAR=17.9
Pod documented
328 0 my ($self, $opts) = @_;
329 0 ▶ my $bucket = $opts->{in} || $self;
Condition0%0/3
$opts->{'in'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'in'} || $self
$opts->{'in'} $self
Truth table0%0/3
A || B
A= $opts->{'in'}B= $self
ABresult
1X1
011
000
330 0 $bucket->add($opts->{as}, $opts->{key}, { links => [ Data::Riak::Fast::Link->new( bucket => $bucket->name, riaktag => 'perl-data-riak-alias', key => $opts->{key} )] });
331 }
332  
333 =head2 resolve_alias ($alias)
334  
335 Returns the L<Data::Riak::Fast::Result> that $alias points to.
336  
337 =cut
338  
339 0 ▶sub resolve_alias {
Subroutine resolve_alias: not called CC=1 SCAR=6.9
Pod documented
340 0 my ($self, $alias) = @_;
341 0 return $self->linkwalk($alias, [[ 'perl-data-riak-alias', '_' ]])->first;
342 }
343  
344 22 __PACKAGE__->meta->make_immutable;
345 22 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
346  
347 22 1;
348  
349 __END__
« Data/Riak/Fast/Result.pm ↑ Summary Data/Riak/Fast/Link.pm »