Data/Riak/Role/Bucket.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 242 CC 9 SCAR 21.6
CC
9
Cov
0%
create_link
30.0
get
12.0
_build_linklist
6.0
CRAP 8.6
1 package Data::Riak::Role::Bucket;
2 {
3 $Data::Riak::Role::Bucket::VERSION = '2.0';
4 }
5  
6 use Moose::Role;
7 use JSON 'decode_json';
8 use Data::Riak::Link;
9 use Data::Riak::MapReduce;
10 use HTTP::Headers::ActionPack::LinkList;
11 use namespace::autoclean;
12  
13 with 'Data::Riak::Role::HasRiak';
14  
15 has name => (
16 is => 'ro',
17 isa => 'Str',
18 required => 1
19 );
20  
21 sub _build_linklist {
22 my ($self, $links) = @_;
23  
24 my $pack = HTTP::Headers::ActionPack::LinkList->new;
25  
26 for my $link (@{ $links || [] }) {
27 if(blessed $link && $link->isa('Data::Riak::Link')) {
28 $pack->add($link->as_link_header);
29 }
30 else {
31 confess "Bad link type ($link)";
32 }
33 }
34  
35 return $pack;
36 }
37  
38 sub create_link {
39 my $self = shift;
40 my %opts = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
41 confess "You must provide a key for a link" unless exists $opts{key};
42 confess "You must provide a riaktag for a link" unless exists $opts{riaktag};
43 return Data::Riak::Link->new({
44 bucket => $self->name,
45 key => $opts{key},
46 riaktag => $opts{riaktag},
47 (exists $opts{params} ? (params => $opts{params}) : ())
48 });
49 }
50  
51 sub add {
52 my ($self, $key, $value, $opts) = @_;
53  
54 my $pack = $self->_build_linklist($opts->{links});
55  
56 # TODO:
57 # need to support other headers
58 # X-Riak-Vclock if the object already exists, the vector clock attached to the object when read.
59 # X-Riak-Meta-* - any additional metadata headers that should be stored with the object.
60 # see http://wiki.basho.com/HTTP-Store-Object.html
61 # - SL
62  
63 return $self->riak->send_request({
64 %{ $opts || {} },
65 type => 'StoreObject',
66 bucket_name => $self->name,
67 key => $key,
68 value => $value,
69 links => $pack,
70 });
71 }
72  
73 sub remove {
74 my ($self, $key, $opts) = @_;
75  
76 return $self->riak->send_request({
77 %{ $opts || {} },
78 type => 'RemoveObject',
79 bucket_name => $self->name,
80 key => $key,
81 });
82 }
83  
84 sub get {
85 my ($self, $key, $opts) = @_;
86  
87 confess "This method requires a key" unless $key;
88  
89 $opts ||= {};
90  
91 confess "This method does not support multipart/mixed responses"
92 if exists $opts->{'accept'} && $opts->{'accept'} eq 'multipart/mixed';
93  
94 return $self->riak->send_request({
95 %{ $opts },
96 type => 'GetObject',
97 bucket_name => $self->name,
98 key => $key,
99 });
100 }
101 sub list_keys {
102 my ($self, $opts) = @_;
103  
104 return $self->riak->send_request({
105 %{ $opts || {} },
106 type => 'ListBucketKeys',
107 bucket_name => $self->name,
108 });
109 }
110  
111 sub count {
112 my ($self, $opts) = @_;
113  
114 my $map_reduce = Data::Riak::MapReduce->new({
115 riak => $self->riak,
116 inputs => $self->name,
117 phases => [
118 Data::Riak::Util::MapCount->new,
119 Data::Riak::Util::ReduceCount->new,
120 ],
121 });
122  
123 return $map_reduce->mapreduce(
124 %{ $opts || {} },
125 retval_mangler => sub {
126 my ($map_reduce_results) = @_;
127 my ($result) = $map_reduce_results->results->[0];
128 my ($count) = decode_json($result->value) || 0;
129 return $count->[0];
130 },
131 );
132 }
133  
134 sub linkwalk {
135 my ($self, $object, $params, $opts) = @_;
136  
137 return $self->riak->linkwalk({
138 %{ $opts || {} },
139 bucket => $self->name,
140 object => $object,
141 params => $params,
142 });
143 }
144  
145 sub props {
146 my ($self, $opts) = @_;
147  
148 return $self->riak->send_request({
149 %{ $opts || {} },
150 type => 'GetBucketProps',
151 bucket_name => $self->name,
152 });
153 }
154  
155 sub set_props {
156 my ($self, $props, $opts) = @_;
157  
158 return $self->riak->send_request({
159 %{ $opts || {} },
160 type => 'SetBucketProps',
161 bucket_name => $self->name,
162 props => $props,
163 });
164 }
165  
166 sub create_alias {
167 my ($self, $opts) = @_;
168 my $bucket = delete $opts->{in} || $self;
169  
170 return $bucket->add($opts->{as}, $opts->{key}, {
171 %{ $opts },
172 links => [
173 Data::Riak::Link->new(
174 bucket => $bucket->name,
175 riaktag => 'perl-data-riak-alias',
176 key => $opts->{key},
177 ),
178 ],
179 });
180 }
181  
182 sub resolve_alias {
183 my ($self, $alias, $opts) = @_;
184  
185 return $self->linkwalk($alias, [[ 'perl-data-riak-alias', '_' ]], {
186 %{ $opts || {} },
187 retval_mangler => sub { shift->first },
188 });
189 }
190  
191 sub search_index {
192 my ($self, $opts) = @_;
193 my $field = $opts->{'field'} || confess 'You must specify a field for searching Secondary indexes';
194 my $values = $opts->{'values'} || confess 'You must specify values for searching Secondary indexes';
195  
196 my $inputs = { bucket => $self->name, index => $field };
197 if(ref($values) eq 'ARRAY') {
198 $inputs->{'start'} = $values->[0];
199 $inputs->{'end'} = $values->[1];
200 } else {
201 $inputs->{'key'} = $values;
202 }
203  
204 my $search_mr = Data::Riak::MapReduce->new({
205 riak => $self->riak,
206 inputs => $inputs,
207 phases => [
208 Data::Riak::MapReduce::Phase::Reduce->new({
209 language => 'erlang',
210 module => 'riak_kv_mapreduce',
211 function => 'reduce_identity',
212 keep => 1
213 })
214 ],
215 });
216  
217 # honour the passed in mangler so pretty_search_index can be easier
218 $opts ||= {};
219 my $retval_mangler = delete $opts->{retval_mangler} || sub { $_[0] };
220  
221 return $search_mr->mapreduce(
222 %{ $opts },
223 retval_mangler => sub { $retval_mangler->(shift->results->[0]->value) },
224 );
225 }
226  
227 # returns JUST the list of keys. human readable, not designed for MapReduce inputs.
228 sub pretty_search_index {
229 my ($self, $opts) = @_;
230  
231 return $self->search_index({
232 %{ $opts || {} },
233 retval_mangler => sub { [sort map { $_->[1] } @{ decode_json shift }] },
234 });
235 }
236  
237 1;
238  
239 __END__