Data/Riak/Fast/MapReduce.pm

filter: Statementstmt 85.1% 23 / 27 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 78.3% 29 / 37 CC 2 SCAR 8.1
CC
2
Cov
79%
mapreduce
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.2
« Data/Riak/Fast/HTTP.pm ↑ Summary Data/Riak/Fast/Types.pm »
1 package Data::Riak::Fast::MapReduce;
2  
3 # ABSTRACT: A map/reduce query
4  
5 22 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use Data::Riak::Fast::MapReduce::Phase::Link;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Data::Riak::Fast::MapReduce::Phase::Map;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Data::Riak::Fast::MapReduce::Phase::Reduce;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use JSON::XS qw/encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 22 with 'Data::Riak::Fast::Role::HasRiak';
13  
14 =head1 DESCRIPTION
15  
16 A map/reduce query.
17  
18 =head1 SYNOPSIS
19  
20 my $riak = Data::Riak::Fast->new;
21  
22 my $mr = Data::Riak::Fast::MapReduce->new({
23 riak => $riak,
24 inputs => [ [ "products8", $arg ] ],
25 phases => [
26 Data::Riak::Fast::MapReduce::Phase::Map->new(
27 language => "javascript",
28 source => "
29 function(v) {
30 var m = v.values[0].data.toLowerCase().match(/\w*/g);
31 var r = [];
32 for(var i in m) {
33 if(m[i] != '') {
34 var o = {};
35 o[m[i]] = 1;
36 r.push(o);
37 }
38 }
39 return r;
40 }
41 ",
42 ),
43 Data::Riak::Fast::MapReduce::Phase::Reduce->new(
44 language => "javascript",
45 source => "
46 function(v) {
47 var r = {};
48 for(var i in v) {
49 for(var w in v[i]) {
50 if(w in r) r[w] += v[i][w];
51 else r[w] = v[i][w];
52 }
53 }
54 return [r];
55 }
56 ",
57 ),
58 ]
59 });
60  
61 my $results = $mr->mapreduce;
62  
63 =head2 inputs
64  
65 Inputs to this query. There are few allowable forms.
66  
67 For a single bucket:
68  
69 inputs => "bucketname"
70  
71 For a bucket and key (or many!):
72  
73 inputs => [ [ "bucketname", "keyname" ] ]
74  
75 inputs => [ [ "bucketname", "keyname" ], [ "bucketname", "keyname2" ] ]
76
77 And finally:
78  
79 inputs => [ [ "bucketname", "keyname", "keyData" ] ]
80  
81 =cut
82  
83 22 has inputs => (
84 is => 'ro',
85 isa => 'ArrayRef | Str | HashRef',
86 required => 1
87 );
88  
89 =head2 phases
90  
91 An arrayref of phases that will be executed in order. The phases should be
92 one of L<Data::Riak::Fast::MapReduce::Phase::Link>,
93 L<Data::Riak::Fast::MapReduce::Phase::Map>, or L<Data::Riak::Fast::MapReduce::Phase::Reduce>.
94  
95 =cut
96  
97 22 has phases => (
98 is => 'ro',
99 isa => 'ArrayRef[Data::Riak::Fast::MapReduce::Phase]',
100 required => 1
101 );
102  
103 =head1 METHOD
104 =head2 mapreduce
105  
106 Execute the mapreduce query.
107  
108 To enable streaming, do the following:
109  
110 my $results = $mr->mapreduce(chunked => 1);
111  
112 =cut
113  
114 0 ▶sub mapreduce {
Subroutine mapreduce: not called CC=2 SCAR=17.9
Pod undocumented
115 0 my ($self, %options) = @_;
116
117 return $self->riak->send_request({
118 content_type => 'application/json',
119 method => 'POST',
120 uri => 'mapred',
121 data => encode_json({
122 inputs => $self->inputs,
123 0 ▶ query => [ map { { $_->phase => $_->pack } } @{ $self->phases } ]
Branch: 1 branch
BranchTrueFalseTotal
$options{'chunked'} ? : 0 0 0
124 }),
125 0 ($options{'chunked'}
126 ? (query => { chunked => 'true' })
127 : ()),
128 });
129 }
130  
131 22 __PACKAGE__->meta->make_immutable;
132 22 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 22 1;
135  
136 __END__
« Data/Riak/Fast/HTTP.pm ↑ Summary Data/Riak/Fast/Types.pm »