Mango/Bulk.pm

filter: Statementstmt 14.7% 15 / 102 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 19.0% 4 / 21 Podpod 100% 8 / 8 totaltotal 12.7% 27 / 212 CC 30 SCAR 36.2
CC
30
Cov
9%
_op
110.0
execute
42.0
_merge
42.0
CRAP 37.4
« Mango/Cursor/Query.pm ↑ Summary Mango/Collection.pm »
1 package Mango::Bulk;
2 9 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Mango::BSON qw(bson_doc bson_encode bson_oid bson_raw);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 has 'collection';
9 9 has ordered => 1;
10  
11 0 ▶sub execute {
Subroutine execute: not called CC=6 SCAR=37.4
Pod documented
12 0 my ($self, $cb) = @_;
13  
14 # Full results shared with all operations
15 0 my $full = {upserted => [], writeConcernErrors => [], writeErrors => []};
16 0 $full->{$_} = 0 for qw(nInserted nMatched nModified nRemoved nUpserted);
17  
18 # Non-blocking
19 0 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
20 0 ▶ return Mojo::IOLoop->next_tick(sub { shift; $self->$cb(undef, $full) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 0 ▶ unless my $group = shift @{$self->{ops}};
Branch: 1 branch
BranchTrueFalseTotal
unless my $group = shift @{$$self{"ops"};} 0 0 0
22 0 return $self->_next($group, $full, $cb);
23 }
24  
25 # Blocking
26 0 my $db = $self->collection->db;
27 0 my $protocol = $db->mango->protocol;
28 0 ▶ while (my $group = shift @{$self->{ops}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = shift @{$$self{"ops"};}) 0 0 0
29 0 my ($type, $offset, $command) = $self->_group($group);
30 0 _merge($type, $offset, $full, $db->command($command));
31 0 ▶ if (my $err = $protocol->write_error($full)) { croak $err }
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $protocol->write_error($full)) 0 0 0
32 }
33  
34 0 return $full;
35 }
36  
37 0 ▶sub find { shift->_set(query => shift) }
Subroutine find: not called CC=1 SCAR=6.9
Pod documented
38  
39 0 ▶sub insert {
Subroutine insert: not called CC=2 SCAR=17.9
Pod documented
40 0 my ($self, $doc) = @_;
41 0 ▶ $doc->{_id} //= bson_oid;
Condition0%0/3
$doc->{"_id"} //= bson_oid()
l!l&&r!l&&!r
000
MC/DC0%0/2
$doc->{"_id"} //= bson_oid()
$doc->{"_id"} bson_oid()
Truth table0%0/3
A //= B
A= $doc->{"_id"}B= bson_oid()
ABresult
1X1
011
000
42 0 return $self->_op(insert => $doc);
43 }
44  
45 0 ▶sub remove { shift->_remove(0) }
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
46 0 ▶sub remove_one { shift->_remove(1) }
Subroutine remove_one: not called CC=1 SCAR=6.9
Pod documented
47  
48 0 ▶sub update { shift->_update(\1, @_) }
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
49 0 ▶sub update_one { shift->_update(\0, @_) }
Subroutine update_one: not called CC=1 SCAR=6.9
Pod documented
50  
51 0 ▶sub upsert { shift->_set(upsert => 1) }
Subroutine upsert: not called CC=1 SCAR=6.9
Pod documented
52  
53 0 ▶sub _group {
Subroutine _group: not called CC=3 SCAR=24.8
54 0 my ($self, $group) = @_;
55  
56 0 my ($type, $offset) = splice @$group, 0, 2;
57 0 my $collection = $self->collection;
58 0 ▶ return $type, $offset, bson_doc $type => $collection->name,
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'insert' ? : 0 0 0
$self->ordered ? : 0 0 0
59 $type eq 'insert' ? 'documents' : "${type}s" => $group,
60 ordered => $self->ordered ? \1 : \0,
61 writeConcern => $collection->db->build_write_concern;
62 }
63  
64 0 ▶sub _merge {
Subroutine _merge: not called CC=6 SCAR=37.4
65 0 my ($type, $offset, $full, $result) = @_;
66  
67 # Insert
68 0 ▶ if ($type eq 'insert') { $full->{nInserted} += $result->{n} }
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'insert') { } 0 0 0
elsif ($type eq 'update') { } 0 0 0
elsif ($type eq 'delete') { } 0 0 0
69  
70 # Update
71 elsif ($type eq 'update') {
72 0 $full->{nModified} += $result->{n};
73  
74 # Upsert
75 0 ▶ if (my $upserted = $result->{upserted}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $upserted = $result->{'upserted'}) { } 0 0 0
76 0 push @{$full->{upserted}}, map { $_->{index} += $offset; $_ } @$upserted;
77 0 $full->{nUpserted} += @$upserted;
78 0 $full->{nMatched} += $result->{n} - @$upserted;
79 }
80  
81 0 else { $full->{nMatched} += $result->{n} }
82 }
83  
84 # Delete
85 0 elsif ($type eq 'delete') { $full->{nRemoved} += $result->{n} }
86  
87 # Errors
88 0 push @{$full->{writeConcernErrors}}, $result->{writeConcernError}
89 0 ▶ if $result->{writeConcernError};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"writeConcernError"} 0 0 0
90 0 push @{$full->{writeErrors}},
91 0 map { $_->{index} += $offset; $_ } @{$result->{writeErrors}};
92 }
93  
94 0 ▶sub _next {
Subroutine _next: not called CC=1 SCAR=6.9
95 0 my ($self, $group, $full, $cb) = @_;
96  
97 0 my ($type, $offset, $command) = $self->_group($group);
98 $self->collection->db->command(
99 0 ▶ $command => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
100 0 my ($db, $err, $result) = @_;
101  
102 0 ▶ _merge($type, $offset, $full, $result) unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
103 0 ▶ $err ||= $self->collection->db->mango->protocol->write_error($full);
Condition0%0/3
$err ||= $self->collection->db->mango->protocol->write_error($full)
l!l&&r!l&&!r
000
MC/DC0%0/2
$err ||= $self->collection->db->mango->protocol->write_error($full)
$err $self->collection->db->mango->protocol->write_error($full)
Truth table0%0/3
A ||= B
A= $errB= $self->collection->db->mango->protocol->write_error($full)
ABresult
1X1
011
000
104 0 ▶ return $self->$cb($err, $full) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
105  
106 0 ▶ return $self->$cb(undef, $full) unless my $next = shift @{$self->{ops}};
Branch: 1 branch
BranchTrueFalseTotal
unless my $next = shift @{$$self{"ops"};} 0 0 0
107 0 $self->_next($next, $full, $cb);
108 }
109 0 );
110 }
111  
112 0 ▶sub _op {
Subroutine _op: not called CC=10 SCAR=47.0
113 0 my ($self, $type, $doc) = @_;
114  
115 # Pre-encode documents
116 0 my $mango = $self->collection->db->mango;
117 0 my $bson_max = $mango->max_bson_size;
118 0 my $batch_max = $mango->max_write_batch_size;
119 0 ▶ my $ops = $self->{ops} ||= [];
Condition0%0/2
$self->{"ops"} ||= []
l!l
00
MC/DC0%0/1
$self->{"ops"} ||= []
$self->{"ops"}
Truth table0%0/2
A
A= $self->{"ops"}
Aresult
11
00
120 0 ▶ my $previous = @$ops ? $ops->[-1] : [];
Branch: 1 branch
BranchTrueFalseTotal
@$ops ? : 0 0 0
121 0 my $bson = bson_encode $doc;
122 0 my $size = length $bson;
123 0 ▶ my $new = ($self->{size} // 0) + $size;
Condition0%0/2
$self->{"size"} // 0
l!l
00
MC/DC0%0/1
$self->{"size"} // 0
$self->{"size"}
Truth table0%0/2
A
A= $self->{"size"}
Aresult
11
00
124 0 ▶ my $limit = $new > $bson_max || @$previous >= $batch_max + 2;
Condition0%0/3
$new > $bson_max || @$previous >= $batch_max + 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$new > $bson_max || @$previous >= $batch_max + 2
$new > $bson_max @$previous >= $batch_max + 2
Truth table0%0/3
A || B
A= $new > $bson_maxB= @$previous >= $batch_max + 2
ABresult
1X1
011
000
125  
126 # Group documents based on type and limits
127 push @$ops, [$type, $self->{offset} || 0] and delete $self->{size}
128 0 ▶ if !@$previous || $previous->[0] ne $type || $limit;
Branch: 1 branch
BranchTrueFalseTotal
if not @$previous or $previous->[0] ne $type or $limit 0 0 0
Condition0%0/14
(!@$previous || $previous->[0] ne $type || $limit) && (push(@$ops, [$type, $self->{'offset'} || 0]) && delete $self->{'size'})
!ll&&!rl&&r
000
not @$previous or $previous->[0] ne $type or $limit
l!l&&r!l&&!r
000
not @$previous or $previous->[0] ne $type
l!l&&r!l&&!r
000
push @$ops, [$type, $self->{"offset"} || 0] and delete $self->{"size"}
!ll&&!rl&&r
000
$self->{'offset'} || 0
l!l
00
MC/DC0%0/5
(!@$previous || $previous->[0] ne $type || $limit) && (push(@$ops, [$type, $self->{'offset'} || 0]) && delete $self->{'size'})
not @$previous $previous->[0] ne $type $limit push @$ops, [$type, $self->{"offset"} || 0] delete $self->{"size"}
MC/DC0%0/1
$self->{'offset'} || 0
$self->{'offset'}
Truth table0%0/10
not(A) or B or C && D and E
A= not @$previousB= $previous->[0] ne $typeC= $limitD= push @$ops, [$type, $self->{"offset"} || 0]E= delete $self->{"size"}
ABCDEresult
000XX0
1XX0X0
1XX100
01X0X0
01X100
0010X0
001100
1XX111
01X111
001111
Truth table0%0/2
A
A= $self->{'offset'}
Aresult
11
00
129 0 push @{$ops->[-1]}, bson_raw $bson;
130 0 $self->{size} += $size;
131 0 $self->{offset}++;
132  
133 0 return $self;
134 }
135  
136 0 ▶sub _remove {
Subroutine _remove: not called CC=2 SCAR=17.9
137 0 my ($self, $limit) = @_;
138 0 ▶ my $query = delete $self->{query} // {};
Condition0%0/2
delete $self->{"query"} // {}
l!l
00
MC/DC0%0/1
delete $self->{"query"} // {}
delete $self->{"query"}
Truth table0%0/2
A
A= delete $self->{"query"}
Aresult
11
00
139 0 return $self->_op(delete => {q => $query, limit => $limit});
140 }
141  
142 0 ▶sub _set {
Subroutine _set: not called CC=1 SCAR=6.9
143 0 my ($self, $key, $value) = @_;
144 0 $self->{$key} = $value;
145 0 return $self;
146 }
147  
148 0 ▶sub _update {
Subroutine _update: not called CC=3 SCAR=24.8
149 0 my ($self, $multi, $update) = @_;
150 0 ▶ my $query = delete $self->{query} // {};
Condition0%0/2
delete $self->{"query"} // {}
l!l
00
MC/DC0%0/1
delete $self->{"query"} // {}
delete $self->{"query"}
Truth table0%0/2
A
A= delete $self->{"query"}
Aresult
11
00
151 0 ▶ my $upsert = delete $self->{upsert} ? \1 : \0;
Branch: 1 branch
BranchTrueFalseTotal
delete $self->{'upsert'} ? : 0 0 0
152 0 return $self->_op(
153 update => {q => $query, u => $update, multi => $multi, upsert => $upsert});
154 }
155  
156 9 1;
157  
158 =encoding utf8
159  
160 =head1 NAME
161  
162 Mango::Bulk - MongoDB bulk operations
163  
164 =head1 SYNOPSIS
165  
166 use Mango::Bulk;
167  
168 my $bulk = Mango::Bulk->new(collection => $collection);
169 $bulk->insert({foo => 'bar'})->insert({foo => 'baz'})->execute;
170  
171 =head1 DESCRIPTION
172  
173 L<Mango::Bulk> is a container for MongoDB bulk operations, all operations will
174 be automatically grouped so they don't exceed L<Mango/"max_bson_size">.
175  
176 =head1 ATTRIBUTES
177  
178 L<Mango::Bulk> implements the following attributes.
179  
180 =head2 collection
181  
182 my $collection = $bulk->collection;
183 $bulk = $bulk->collection(Mango::Collection->new);
184  
185 L<Mango::Collection> object this bulk operation belongs to.
186  
187 =head2 ordered
188  
189 my $bool = $bulk->ordered;
190 $bulk = $bulk->ordered($bool);
191  
192 Bulk operations are ordered, defaults to a true value.
193  
194 =head1 METHODS
195  
196 L<Mango::Bulk> inherits all methods from L<Mojo::Base> and implements the
197 following new ones.
198  
199 =head2 execute
200  
201 my $results = $bulk->execute;
202  
203 Execute bulk operations. You can also append a callback to perform operation
204 non-blocking.
205  
206 $bulk->execute(sub {
207 my ($bulk, $err, $results) = @_;
208 ...
209 });
210 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
211  
212 =head2 find
213  
214 $bulk = $bulk->find({foo => 'bar'});
215  
216 Query for next update or remove operation.
217  
218 =head2 insert
219  
220 $bulk = $bulk->insert({foo => 'bar'});
221  
222 Insert document.
223  
224 =head2 remove
225  
226 $bulk = $bulk->remove;
227  
228 Remove multiple documents.
229  
230 =head2 remove_one
231  
232 $bulk = $bulk->remove_one;
233  
234 Remove one document.
235  
236 =head2 update
237  
238 $bulk = $bulk->update({foo => 'bar'});
239  
240 Update multiple documents.
241  
242 =head2 update_one
243  
244 $bulk = $bulk->update_one({foo => 'baz'});
245  
246 Update one document.
247  
248 =head2 upsert
249  
250 $bulk = $bulk->upsert;
251  
252 Next update operation will be an C<upsert>.
253  
254 =head1 SEE ALSO
255  
256 L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
257  
258 =cut
« Mango/Cursor/Query.pm ↑ Summary Mango/Collection.pm »