Index/SimpleDB.pm

filter: Statementstmt 23.6% 38 / 161 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 25.6% 10 / 39 Podpod 0.0% 0 / 17 totaltotal 17.6% 48 / 272 CC 25 SCAR 33.0
CC
25
Cov
18%
translate_query
110.0
__ANON__
56.0
__ANON__
20.0
CRAP 27.1
↑ Summary Archive/S3.pm »
1 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Metabase::Index::SimpleDB;
6 3 our $VERSION = '1.000'; # VERSION
7  
8 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Data::Stream::Bulk::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use List::AllUtils qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use SimpleDB::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 with 'Metabase::Backend::AWS';
18 3 with 'Metabase::Index' => { -version => 1.000 };
19  
20 3 has 'domain' => (
21 is => 'ro',
22 isa => 'Str',
23 required => 1,
24 );
25  
26 has 'simpledb' => (
27 is => 'ro',
28 isa => 'SimpleDB::Client',
29 lazy => 1,
30 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 my $self = shift;
32 0 my $sdb = SimpleDB::Client->new(
33 access_key => $self->access_key_id,
34 secret_key => $self->secret_access_key
35 );
36 0 $sdb->send_request('CreateDomain', { DomainName => $self->domain });
37 0 return $sdb;
38 },
39 3 );
40  
41 has consistent => (
42 is => 'ro',
43 isa => 'Bool',
44 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 3 );
46  
47 0 ▶sub initialize {}
Subroutine initialize: not called CC=1 SCAR=0.0
Pod undocumented
48  
49 # XXX not currently used, but available for future
50 0 ▶sub _format_int {
Subroutine _format_int: not called CC=2 SCAR=17.9
51 0 my ($self, $value) = @_;
52 0 ▶ $value ||= 0; # init
Condition0%0/2
$value ||= 0
l!l
00
MC/DC0%0/1
$value ||= 0
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
53 0 return sprintf("%015d",$value+1000000000);
54 }
55  
56 0 ▶sub _get_search_sql {
Subroutine _get_search_sql: not called CC=1 SCAR=6.9
57 0 my ( $self, $select, $spec ) = @_;
58  
59 0 my ($where, $limit) = $self->get_native_query($spec);
60  
61 0 my $domain = $self->domain;
62 0 my $sql = qq{$select from `$domain` $where};
63  
64 0 return ($sql, $limit);
65 }
66  
67 0 ▶sub add {
Subroutine add: not called CC=3 SCAR=24.8
Pod undocumented
68 0 my ( $self, $fact ) = @_;
69  
70 0 ▶ Carp::confess("can't index a Fact without a GUID") unless $fact->guid;
Branch: 1 branch
BranchTrueFalseTotal
unless $fact->guid 0 0 0
71  
72 0 my $metadata = $self->clone_metadata( $fact );
73  
74 0 my $i = 0;
75 0 my @attributes;
76 0 foreach my $key ( sort keys %$metadata ) {
77 0 my $value = $metadata->{$key};
78 0 push @attributes,
79 "Attribute.$i.Name" => $key,
80 "Attribute.$i.Value" => $value;
81 # XXX not using replace is an optimization -- dagolden, 2010-04-29
82 # "Attribute.$i.Replace" => 'true'; # XXX optimization -- dagolden, 2010-04-29
83 0 $i++;
84 }
85  
86 0 my $response = $self->simpledb->send_request(
87 'PutAttributes',
88 { DomainName => $self->domain,
89 ItemName => lc $fact->guid,
90 @attributes,
91 }
92 );
93 }
94  
95 0 ▶my $_count_extractor = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
96 0 my $response = shift;
97 0 my $items = $response->{SelectResult}{Item};
98  
99 # the following may not be necessary as of SimpleDB::Class 1.0000
100 0 ▶ $items = [ $items ] unless ref $items eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $items eq "ARRAY" 0 0 0
101  
102 0 my $count = 0;
103 0 for my $i (@$items) {
104 0 ▶ next unless $i->{Name} eq 'Domain';
Branch: 1 branch
BranchTrueFalseTotal
unless $i->{"Name"} eq "Domain" 0 0 0
105 0 $count += $i->{Attribute}{Value};
106 }
107  
108 0 return [$count], $count;
109 3 };
110  
111 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my ( $self, %spec) = @_;
113 0 my $query = "select count(*)";
114 0 my ($sql, $limit) = $self->_get_search_sql($query, \%spec );
115 0 my $cb = Data::Stream::Bulk::Callback->new(
116 callback => $self->_generate_fetch_callback($sql, $limit, $_count_extractor)
117 );
118 0 return sum $cb->all;
119 }
120  
121 0 ▶my $_item_extractor = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
122 0 my $response = shift;
123 0 my $items = $response->{SelectResult}{Item};
124  
125 # the following may not be necessary as of SimpleDB::Class 1.0000
126 0 ▶ $items = [ $items ] unless ref $items eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $items eq "ARRAY" 0 0 0
127  
128 0 my $result = [ map { $_->{Name} } @$items ];
129 0 return $result, scalar @$result;
130 3 };
131  
132 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=6.9
Pod undocumented
133 0 my ( $self, %spec) = @_;
134 0 my $query = "select ItemName()";
135 0 my ($sql, $limit) = $self->_get_search_sql($query, \%spec );
136 0 return Data::Stream::Bulk::Callback->new(
137 callback => $self->_generate_fetch_callback($sql, $limit, $_item_extractor)
138 );
139 }
140  
141 0 ▶sub _generate_fetch_callback {
Subroutine _generate_fetch_callback: not called CC=2 SCAR=17.9
142 0 my ($self, $sql, $limit, $extractor) = @_;
143 # prepare request
144 0 ▶ my $request = {
Branch: 1 branch
BranchTrueFalseTotal
$self->consistent ? : 0 0 0
145 SelectExpression => $sql,
146 ConsistentRead => ($self->consistent ? "true" : "false"),
147 };
148 0 my $total_count = 0;
149 0 my $finished = 0;
150  
151 0 ▶ return sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
152 0 ▶ return if $finished;
Branch: 1 branch
BranchTrueFalseTotal
if $finished 0 0 0
153 FETCH: {
154 0 my ($response, $result, $query_count);
155 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
156 0 $response = $self->simpledb->send_request( 'Select', $request );
157 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 die("Got error '$_' from '$sql'");
159 0 };
160  
161 0 ▶ if ( exists $response->{SelectResult}{Item} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $response->{"SelectResult"}{"Item"}) 0 0 0
162 0 ($result, $query_count) = $extractor->($response);
163 0 $total_count += $query_count;
164 }
165  
166 # If SimpleDB promises more data, then update the request
167 0 ▶ if ( exists $response->{SelectResult}{NextToken} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $response->{'SelectResult'}{'NextToken'}) { } 0 0 0
168 0 $request->{NextToken} = $response->{SelectResult}{NextToken};
169 # if promised more, but have nothing now, repeat query right away
170 0 ▶ redo FETCH unless @$result;
Branch: 1 branch
BranchTrueFalseTotal
unless @$result 0 0 0
171 # if we have more than we need, flag that we're done
172 0 ▶ $finished++ if $limit && $total_count >= $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $total_count >= $limit 0 0 0
Condition0%0/3
$limit and $total_count >= $limit
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $total_count >= $limit
$limit $total_count >= $limit
Truth table0%0/3
A and B
A= $limitB= $total_count >= $limit
ABresult
0X0
100
111
173 }
174 # No promise of more, so we're done
175 else {
176 0 $finished++;
177 }
178  
179 # return whatever we got
180 0 return $result;
181 }
182 0 };
183 }
184  
185 # DO NOT lc() GUID
186 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod undocumented
187 0 my ( $self, $guid ) = @_;
188  
189 0 ▶ Carp::confess("can't delete without a GUID") unless $guid;
Branch: 1 branch
BranchTrueFalseTotal
unless $guid 0 0 0
190  
191 0 my $response = $self->simpledb->send_request(
192 'DeleteAttributes',
193 { DomainName => $self->domain,
194 ItemName => $guid,
195 }
196 );
197 }
198  
199 #--------------------------------------------------------------------------#
200 # required by Metabase::Query
201 #
202 # ops return closures that define the necessary logic when called
203 # with hash of index fields
204 #--------------------------------------------------------------------------#
205  
206 0 ▶sub _quote_field {
Subroutine _quote_field: not called CC=1 SCAR=6.9
207 0 my ($self, $field) = @_;
208 0 return qq{`$field`};
209 }
210  
211 0 ▶sub _quote_val {
Subroutine _quote_val: not called CC=1 SCAR=6.9
212 0 my ($self, $value) = @_;
213 0 $value =~ s{"}{""}g;
214 0 return qq{"$value"};
215 }
216  
217 0 ▶sub translate_query {
Subroutine translate_query: not called CC=10 SCAR=47.0
Pod undocumented
218 0 my ( $self, $spec ) = @_;
219  
220 0 my (@parts, $limit);
221  
222 # where
223 0 ▶ if ( defined $spec->{-where} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{"-where"}) 0 0 0
224 0 push @parts, "where " . $self->dispatch_query_op( $spec->{-where} );
225 }
226  
227 # order
228 0 ▶ if ( defined $spec->{-order} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $spec->{"-order"}) 0 0 0
229 0 my @order_pairs = @{$spec->{-order}};
230  
231 Carp::confess("Only one of '-asc' or '-desc' allowed")
232 0 ▶ if @{$spec->{-order}} > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @{$spec->{"-order"};} > 2 0 0 0
233  
234 0 my ($dir, $field) = @{$spec->{-order}};
235  
236 0 ▶ Carp::confess("'$field' must be used in WHERE predicates")
Branch: 1 branch
BranchTrueFalseTotal
unless $parts[0] and $parts[0] =~ /\Q$field\E/ 0 0 0
Condition0%0/3
$parts[0] and $parts[0] =~ /\Q$field\E/
!ll&&!rl&&r
000
MC/DC0%0/2
$parts[0] and $parts[0] =~ /\Q$field\E/
$parts[0] $parts[0] =~ /\Q$field\E/
Truth table0%0/3
A and B
A= $parts[0]B= $parts[0] =~ /\Q$field\E/
ABresult
0X0
100
111
237 unless $parts[0] && $parts[0] =~ /\Q$field\E/;
238  
239 0 $field = $self->_quote_field( $field );
240 0 $dir =~ s/^-//;
241 0 push @parts, qq{order by $field $dir};
242 }
243  
244 # limit
245 0 ▶ if ( $limit = $spec->{-limit} or $spec->{-order} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit = $spec->{"-limit"} or $spec->{"-order"}) 0 0 0
Condition0%0/3
$limit = $spec->{"-limit"} or $spec->{"-order"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$limit = $spec->{"-limit"} or $spec->{"-order"}
$limit = $spec->{"-limit"} $spec->{"-order"}
Truth table0%0/3
A or B
A= $limit = $spec->{"-limit"}B= $spec->{"-order"}
ABresult
1X1
011
000
246 Carp::confess("-limit requires -asc or -desc")
247 0 ▶ unless $spec->{-order};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"-order"} 0 0 0
248 0 ▶ $limit ||= 2500; # if ordered and no limit, use high default
Condition0%0/2
$limit ||= 2500
l!l
00
MC/DC0%0/1
$limit ||= 2500
$limit
Truth table0%0/2
A
A= $limit
Aresult
11
00
249 0 push @parts, qq{limit $limit};
250 }
251  
252 0 return join( q{ }, @parts ), $limit;
253 }
254  
255 0 ▶sub op_eq {
Subroutine op_eq: not called CC=1 SCAR=6.9
Pod undocumented
256 0 my ($self, $field, $val) = @_;
257 0 return $self->_quote_field($field) . " = " . $self->_quote_val($val);
258 }
259  
260 0 ▶sub op_ne {
Subroutine op_ne: not called CC=1 SCAR=6.9
Pod undocumented
261 0 my ($self, $field, $val) = @_;
262 0 return $self->_quote_field($field) . " != " . $self->_quote_val($val);
263 }
264  
265 0 ▶sub op_gt {
Subroutine op_gt: not called CC=1 SCAR=6.9
Pod undocumented
266 0 my ($self, $field, $val) = @_;
267 0 return $self->_quote_field($field) . " > " . $self->_quote_val($val);
268 }
269  
270 0 ▶sub op_lt {
Subroutine op_lt: not called CC=1 SCAR=6.9
Pod undocumented
271 0 my ($self, $field, $val) = @_;
272 0 return $self->_quote_field($field) . " < " . $self->_quote_val($val);
273 }
274  
275 0 ▶sub op_ge {
Subroutine op_ge: not called CC=1 SCAR=6.9
Pod undocumented
276 0 my ($self, $field, $val) = @_;
277 0 return $self->_quote_field($field) . " >= " . $self->_quote_val($val);
278 }
279  
280 0 ▶sub op_le {
Subroutine op_le: not called CC=1 SCAR=6.9
Pod undocumented
281 0 my ($self, $field, $val) = @_;
282 0 return $self->_quote_field($field) . " <= " . $self->_quote_val($val);
283 }
284  
285 0 ▶sub op_between {
Subroutine op_between: not called CC=1 SCAR=6.9
Pod undocumented
286 0 my ($self, $field, $low, $high) = @_;
287 0 return $self->_quote_field($field) . " between "
288 . $self->_quote_val($low) . " and " . $self->_quote_val($high);
289 }
290  
291 0 ▶sub op_like {
Subroutine op_like: not called CC=1 SCAR=6.9
Pod undocumented
292 0 my ($self, $field, $val) = @_;
293 # XXX really should quote/check $val
294 0 return $self->_quote_field($field) . " like " . $self->_quote_val($val);
295 }
296  
297 0 ▶sub op_not {
Subroutine op_not: not called CC=1 SCAR=6.9
Pod undocumented
298 0 my ($self, $pred) = @_;
299 0 my $clause = $self->dispatch_query_op($pred);
300 0 return "NOT ($clause)";
301 }
302  
303 0 ▶sub op_or {
Subroutine op_or: not called CC=1 SCAR=6.9
Pod undocumented
304 0 my ($self, @args) = @_;
305 0 my @predicates = map { $self->dispatch_query_op($_) } @args;
306 0 return join(" or ", map { "($_)" } @predicates);
307 }
308  
309 0 ▶sub op_and {
Subroutine op_and: not called CC=1 SCAR=6.9
Pod undocumented
310 0 my ($self, @args) = @_;
311 0 my @predicates = map { $self->dispatch_query_op($_) } @args;
312 0 return join(" and ", map { "($_)" } @predicates);
313 }
314  
315 3 1;
316  
317 # ABSTRACT: Metabase index on Amazon SimpleDB
318 #
319 # This file is part of Metabase-Backend-AWS
320 #
321 # This software is Copyright (c) 2011 by David Golden.
322 #
323 # This is free software, licensed under:
324 #
325 # The Apache License, Version 2.0, January 2004
326 #
327  
328  
329 __END__
↑ Summary Archive/S3.pm »