| 1 | package Net::Riak::MapReduce; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 21 | $Net::Riak::MapReduce::VERSION = '0.1702'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Allows you to build up and run a map/reduce operation on Riak | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use JSON; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 21 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 21 | ▶ | use Net::Riak::LinkPhase; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 21 | ▶ | use Net::Riak::MapReducePhase; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 21 | with 'Net::Riak::Role::Base' => | |||||||||||||||||||||||||||||||||||||||||
| 18 | {classes => [{name => 'client', required => 1}]}; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | has phases => ( | ||||||||||||||||||||||||||||||||||||||||||
| 21 | traits => ['Array'], | ||||||||||||||||||||||||||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | isa => 'ArrayRef[Object]', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | auto_deref => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 21 | handles => { | |||||||||||||||||||||||||||||||||||||||||
| 28 | get_phases => 'elements', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | add_phase => 'push', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | num_phases => 'count', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | get_phase => 'get', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 21 | has inputs_bucket => ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | predicate => 'has_inputs_bucket', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | has inputs => ( | ||||||||||||||||||||||||||||||||||||||||||
| 40 | traits => ['Array'], | ||||||||||||||||||||||||||||||||||||||||||
| 41 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | isa => 'ArrayRef[ArrayRef]', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | handles => {add_input => 'push',}, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 21 | ); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 21 | has input_mode => ( | |||||||||||||||||||||||||||||||||||||||||
| 47 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 48 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 49 | predicate => 'has_input_mode', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub add { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | my $arg = shift; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if (ref $arg eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | do{ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $self->add_input($arg); | |||||||||||||||||||||||||||||||||||||||||
| 59 | }while($arg = shift @_); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (!scalar @_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($arg->isa('Net::Riak::Object')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $self->add_object($arg); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } elsif ($arg->isa('Net::Riak::Bucket')) { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $self->add_bucket($arg->name); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $self->add_bucket($arg); | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $self->add_bucket_key_data($arg, @_); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | sub add_object { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_object: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my ($self, $obj) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $self->add_bucket_key_data($obj->bucket->name, $obj->key); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub add_bucket_key_data { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_bucket_key_data: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my ($self, $bucket, $key, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($self->has_input_mode && $self->input_mode eq 'bucket') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->has_input_mode and $self->input_mode eq "bucket"
MC/DC0%0/2
$self->has_input_mode and $self->input_mode eq "bucket"
$self->has_input_mode
$self->input_mode eq "bucket"
Truth table0%0/3
A and B
A= $self->has_input_modeB= $self->input_mode eq "bucket"
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | croak("Already added a bucket, can't add an object"); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | $self->add_input([$bucket, $key, $data]); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub add_bucket { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_bucket: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my ($self, $bucket) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | $self->input_mode('bucket'); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | $self->inputs_bucket($bucket); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub link { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
link: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my ($self, $bucket, $tag, $keep) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $bucket ||= '_'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$bucket ||= "_"
MC/DC0%0/1
$bucket ||= "_"
$bucket
Truth table0%0/2
A
A= $bucket
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $tag ||= '_'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$tag ||= "_"
MC/DC0%0/1
$tag ||= "_"
$tag
Truth table0%0/2
A
A= $tag
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $keep ||= JSON::false; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$keep ||= JSON::false()
MC/DC0%0/2
$keep ||= JSON::false()
$keep
JSON::false()
Truth table0%0/3
A ||= B
A= $keepB= JSON::false()
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $self->add_phase( | |||||||||||||||||||||||||||||||||||||||||
| 108 | Net::Riak::LinkPhase->new( | ||||||||||||||||||||||||||||||||||||||||||
| 109 | bucket => $bucket, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | tag => $tag, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | keep => $keep | ||||||||||||||||||||||||||||||||||||||||||
| 112 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub map { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
map: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | my ($self, $function, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | my $map_reduce = Net::Riak::MapReducePhase->new( | ||||||||||||||||||||||||||||||||||||||||||
| 120 | type => 'map', | ||||||||||||||||||||||||||||||||||||||||||
| 121 | function => $function, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | keep => $options{keep} ? JSON::true : JSON::false, | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | arg => $options{arg} || [], | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$options{'arg'} || []
MC/DC0%0/1
$options{'arg'} || []
$options{'arg'}
Truth table0%0/2
A
A= $options{'arg'}
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | $self->add_phase($map_reduce); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub reduce { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reduce: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my ($self, $function, %options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | my $map_reduce = Net::Riak::MapReducePhase->new( | ||||||||||||||||||||||||||||||||||||||||||
| 133 | type => 'reduce', | ||||||||||||||||||||||||||||||||||||||||||
| 134 | function => $function, | ||||||||||||||||||||||||||||||||||||||||||
| 135 | keep => $options{keep} || JSON::false, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | arg => $options{arg} || [], | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
$options{'keep'} || JSON::false()
$options{'arg'} || []
MC/DC0%0/2
$options{'keep'} || JSON::false()
$options{'keep'}
JSON::false()
MC/DC0%0/1
$options{'arg'} || []
$options{'arg'}
Truth table0%0/3
A || B
A= $options{'keep'}B= JSON::false()
Truth table0%0/2
A
A= $options{'arg'}
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $self->add_phase($map_reduce); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my ($self, $timeout) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $num_phases = $self->num_phases; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $keep_flag = 0; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my $query = []; | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my $total_phase = $self->num_phases; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | foreach my $i (0 .. ($total_phase - 1)) { | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my $phase = $self->get_phase($i); | |||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($i == ($total_phase - 1) && !$keep_flag) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$i == $total_phase - 1 and not $keep_flag
MC/DC0%0/2
$i == $total_phase - 1 and not $keep_flag
$i == $total_phase - 1
not $keep_flag
Truth table0%0/3
A and not(B)
A= $i == $total_phase - 1B= not $keep_flag
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | $phase->keep(JSON::true); | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | $keep_flag = 1 if ($phase->{keep}->isa(JSON::true)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | push @$query, $phase->to_array; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my $inputs; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if ($self->has_input_mode && $self->input_mode eq 'bucket' && $self->has_inputs_bucket) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$self->has_input_mode and $self->input_mode eq "bucket" and $self->has_inputs_bucket
$self->has_input_mode and $self->input_mode eq "bucket"
MC/DC0%0/3
$self->has_input_mode and $self->input_mode eq "bucket" and $self->has_inputs_bucket
$self->has_input_mode
$self->input_mode eq "bucket"
$self->has_inputs_bucket
Truth table0%0/4
A and B and C
A= $self->has_input_modeB= $self->input_mode eq "bucket"C= $self->has_inputs_bucket
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | $inputs = $self->inputs_bucket; | |||||||||||||||||||||||||||||||||||||||||
| 162 | }else{ | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | $inputs = $self->inputs; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my $job = {inputs => $inputs, query => $query}; | |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | # how phases set to 'keep'. | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my $p = scalar ( grep { $_->keep } $self->phases); | |||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | my $result = $self->client->execute_job($job, $timeout, $p); | |||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my @phases = $self->phases; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if (ref $phases[-1] ne 'Net::Riak::LinkPhase') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | my $a = []; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | foreach (@$result) { | |||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | my $l = Net::Riak::Link->new( | |||||||||||||||||||||||||||||||||||||||||
| 181 | bucket => Net::Riak::Bucket->new(name => $_->[0], client => $self->client), | ||||||||||||||||||||||||||||||||||||||||||
| 182 | key => $_->[1], | ||||||||||||||||||||||||||||||||||||||||||
| 183 | tag => $_->[2], | ||||||||||||||||||||||||||||||||||||||||||
| 184 | client => $self->client | ||||||||||||||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | push @$a, $l; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | return $a; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 21 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | __END__ | ||||||||||||||||||||||||||||||||||||||||||