KiokuDB/Linker.pm

filter: Statementstmt 92.2% 178 / 193 Branchbran 83.3% 60 / 72 Conditioncond 75.0% 9 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 28 / 28 Podpod 0.0% 0 / 18 totaltotal 83.9% 278 / 331 CC 51 SCAR 23.3
CC
51
Cov
89%
inflate_data
26.8
load_queue
10.0
load_entries
5.4
CRAP 10.3
« KiokuDB/Set.pm ↑ Summary KiokuDB/Test.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Linker;
4 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # perf improvements:
7 # use a queue of required objects, queue up references, and bulk fetch
8 # bulk fetch arrays
9 # could support a Backend::Queueing which allows queuing of IDs for fetching,
10 # to help clump or start a request and only read it when it's actually needed
11  
12  
13 18 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Scalar::Util qw(reftype weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use Tie::ToObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 18 ▶use KiokuDB::Error::MissingObjects;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 18 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 18 has live_objects => (
23 isa => "KiokuDB::LiveObjects",
24 is => "ro",
25 required => 1,
26 handles => [qw(id_to_object ids_to_objects object_to_id objects_to_ids id_to_entry ids_to_entries)],
27 );
28  
29 18 has backend => (
30 does => "KiokuDB::Backend",
31 is => "ro",
32 required => 1,
33 );
34  
35 18 has typemap_resolver => (
36 isa => "KiokuDB::TypeMap::Resolver",
37 is => "ro",
38 handles => [qw(expand_method refresh_method)],
39 required => 1,
40 );
41  
42 18 has queue => (
43 isa => "Bool",
44 is => "ro",
45 default => 1,
46 );
47  
48 has _queue => (
49 isa => "ArrayRef",
50 is => "ro",
51 65 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 18 );
53  
54 has _deferred => (
55 isa => "ArrayRef",
56 is => "ro",
57 65 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 18 );
59  
60 sub register_object {
61 4414 ▶ my ( $self, $entry, $object, @args ) = @_;
Subroutine register_object: called CC=4 SCAR=13.9
Pod undocumented
62  
63 4414 ▶ if ( my $id = $entry->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $entry->id) 4325 89 4414
64 4325 my $l = $self->live_objects;
65  
66 4325 $l->register_entry( $id => $entry );
67 4325 $l->register_object( $id => $object, @args );
68  
69 18 ▶ use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
70 # break cycle for passthrough objects
71 4325 ▶ if ( ref($entry->data) and refaddr($object) == refaddr($entry->data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry->data and builtin::refaddr($object) == builtin::refaddr($entry->data)) 1 4324 4325
Condition100%3/3
ref $entry->data and builtin::refaddr($object) == builtin::refaddr($entry->data)
!ll&&!rl&&r
143231
MC/DC100%2/2
ref $entry->data and builtin::refaddr($object) == builtin::refaddr($entry->data)
ref $entry->data builtin::refaddr($object) == builtin::refaddr($entry->data)
Truth table100%3/3
A and B
A= ref $entry->dataB= builtin::refaddr($object) == builtin::refaddr($entry->data)
ABresult
0X0
100
111
72 1 weaken($entry->{data}); # FIXME there should be a MOP way to do this
73 }
74 }
75 }
76  
77 sub expand_objects {
78 938 ▶ my ( $self, @entries ) = @_;
Subroutine expand_objects: called CC=3 SCAR=11.0
Pod undocumented
79  
80 938 my $l = $self->live_objects;
81  
82 938 my @objects;
83  
84 938 foreach my $entry ( @entries ) {
85 # if the object was referred to in some other entry in @entries, it may
86 # have already been loaded.
87 2325 ▶ if ( defined ( my $obj = $l->id_to_object($entry->id) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj = $l->id_to_object($entry->id))) { } 178 2147 2325
88 178 push @objects, $obj;
89 } else {
90 2147 $self->inflate_data( $entry, \($objects[@objects]) );
91 }
92 }
93  
94 938 $self->load_queue;
95  
96 938 return @objects;
97 }
98  
99 sub expand_object {
100 2220 ▶ my ( $self, $entry ) = @_;
Subroutine expand_object: called CC=1 SCAR=0.0
Pod undocumented
101  
102 2220 $self->inflate_data( $entry, \(my $obj) );
103  
104 2217 $self->load_queue;
105  
106 2217 return $obj;
107 }
108  
109 sub queue_ref {
110 5031 ▶ my ( $self, $ref, $into ) = @_;
Subroutine queue_ref: called CC=4 SCAR=14.0
Pod undocumented
111  
112 5031 ▶ if ( $self->queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->queue) { } 3075 1956 5031
113  
114 #my $b = $self->backend;
115  
116 #if ( $b->can("prefetch") ) {
117 # $b->prefetch($ref->id);
118 #}
119  
120 3075 push @{ $self->_queue }, [ $ref, $into ];
121 } else {
122 1956 ▶ if ( ref $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref) { } 1956 0 1956
123 1956 $$into = $self->get_or_load_object($ref->id);
124 1956 ▶ weaken($$into) if $ref->is_weak;
Branch: 1 branch
BranchTrueFalseTotal
if $ref->is_weak 816 1140 1956
125 } else {
126 0 $$into = $self->get_or_load_object($ref);
127 }
128 }
129 }
130  
131 sub queue_finalizer {
132 4177 ▶ my ( $self, @hooks ) = @_;
Subroutine queue_finalizer: called CC=3 SCAR=11.0
Pod undocumented
133  
134 4177 ▶ if ( $self->queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->queue) { } 2385 1792 4177
135 2385 push @{ $self->_deferred }, @hooks;
136 } else {
137 1792 foreach my $hook ( @hooks ) {
138 1792 $self->$hook();
139 }
140 }
141 }
142  
143 sub load_queue {
144 3386 ▶ my $self = shift;
Subroutine load_queue: called CC=10 SCAR=23.0
Pod undocumented
145  
146 3386 ▶ return unless $self->queue;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->queue 1681 1705 3386
147  
148 1705 my $queue = $self->_queue;
149 1705 my $deferred = $self->_deferred;
150  
151 1705 my @queue = @$queue;
152 1705 my @deferred = @$deferred;
153  
154 1705 @$queue = ();
155 1705 @$deferred = ();
156  
157 1705 ▶ if ( @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 732 973 1705
158 732 my @ids;
159  
160 732 foreach my $entry ( @queue ) {
161 3075 my $ref = $entry->[0];
162 3075 ▶ push @ids, ref($ref) ? $ref->id : $ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $ref ? : 3075 0 3075
163 }
164  
165 732 my @objects = $self->get_or_load_objects(@ids);
166  
167 732 foreach my $item ( @queue ) {
168 3075 my ( $data, $into ) = @$item;
169 3075 my $obj = shift @objects;
170  
171 3075 $$into = $obj;
172  
173 3075 ▶ weaken $$into if ref $data and $data->is_weak;
Branch: 1 branch
BranchTrueFalseTotal
if ref $data and $data->is_weak 1352 1723 3075
Condition66%2/3
ref $data and $data->is_weak
!ll&&!rl&&r
017231352
MC/DC50%1/2
ref $data and $data->is_weak
ref $data $data->is_weak
Truth table66%2/3
A and B
A= ref $dataB= $data->is_weak
ABresult
0X0
100
111
174 }
175 }
176  
177 1705 ▶ if ( @deferred ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@deferred) 1323 382 1705
178 1323 foreach my $item ( @deferred ) {
179 2385 $self->$item;
180 }
181 }
182 }
183  
184 sub inflate_data {
185 20940 ▶ my ( $self, $data, $into, $entry ) = @_;
Subroutine inflate_data: called CC=22 SCAR=32.9
Pod undocumented
186  
187 # Kinda ugly... inflates $data into the scalar ref in $into
188 # but this allows us to handle weakening properly.
189 # god I hate perl's reftypes, why couldn't they be a little more consistent
190  
191 20940 ▶ unless ( ref $data ) {
Branch: 7 branches
BranchTrueFalseTotal
if (ref $data) { } 20733 207 20940
if (do { ref $data eq 'KiokuDB::Reference' }) { } 5031 15702 20733
elsif (ref $data eq 'KiokuDB::Entry') { } 4459 11243 15702
elsif (ref $data eq 'HASH') { } 220 11023 11243
elsif (ref $data eq 'ARRAY') { } 10941 82 11023
elsif (ref $data eq 'SCALAR') { } 25 57 82
elsif (ref $data eq 'REF') { } 4 53 57
192 207 $$into = $data;
193 } elsif ( ref $data eq 'KiokuDB::Reference' ) {
194 5031 $self->queue_ref( $data, $into );
195 } elsif ( ref $data eq 'KiokuDB::Entry' ) {
196 4459 ▶ if ( my $class = $data->class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $data->class) { } 4260 199 4459
197 4260 my $expand_method = $self->expand_method($class);
198 4260 $$into = $self->$expand_method($data);
199 } else {
200 199 my $obj;
201  
202 199 $self->inflate_data($data->data, \$obj, $data);
203  
204 199 $self->load_queue; # force vivification of $obj
205  
206 199 ▶ if ( my $tie = $data->tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tie = $data->tied) 27 172 199
207 27 ▶ if ( $tie eq 'H' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($tie eq 'H') { } 27 0 27
elsif ($tie eq 'A') { } 0 0 0
elsif ($tie eq 'G') { } 0 0 0
elsif ($tie eq 'S') { } 0 0 0
208 27 tie my %h, "Tie::ToObject" => $obj;
209 27 $obj = \%h;
210 } elsif ( $tie eq 'A' ) {
211 0 tie my @a, "Tie::ToObject" => $obj;
212 0 $obj = \@a;
213 } elsif ( $tie eq 'G' ) {
214 0 my $glob = gensym();
215 0 tie *$glob, "Tie::ToObject" => $obj,
216 $obj = $glob;
217 } elsif ( $tie eq 'S' ) {
218 0 my $scalar;
219 0 tie $scalar, "Tie::ToObject" => $obj;
220 0 $obj = \$scalar;
221 } else {
222 0 die "Don't know how to tie $tie";
223 }
224 }
225  
226 199 $$into = $obj;
227 }
228  
229 4456 $data->object($$into);
230 } elsif ( ref($data) eq 'HASH' ) {
231 220 my %targ;
232 220 ▶ $self->register_object( $entry => \%targ ) if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 112 108 220
233 220 foreach my $key ( keys %$data ) {
234 223 $self->inflate_data( $data->{$key}, \$targ{$key} );
235 }
236 220 $$into = \%targ;
237 } elsif ( ref($data) eq 'ARRAY' ) {
238 10941 my @targ;
239 10941 ▶ $self->register_object( $entry => \@targ ) if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 83 10858 10941
240 10941 for (@$data ) {
241 4214 push @targ, undef;
242 4214 $self->inflate_data( $_, \$targ[-1] );
243 }
244 10941 $$into = \@targ;
245 } elsif ( ref($data) eq 'SCALAR' ) {
246 25 my $targ = $$data;
247 25 ▶ $self->register_object( $entry => \$targ ) if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 17 8 25
248 25 $$into = \$targ;
249 } elsif ( ref($data) eq 'REF' ) {
250 4 my $targ;
251 4 ▶ $self->register_object( $entry => \$targ ) if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 0 4 4
252 4 $self->inflate_data( $$data, \$targ );
253 4 $$into = \$targ;
254 } else {
255 53 ▶ if ( blessed($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data)) { } 53 0 53
256 # this branch is for passthrough intrinsic values
257 53 ▶ $self->register_object( $entry => $data ) if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 1 52 53
258 53 $$into = $data;
259 } else {
260 0 die "unsupported reftype: " . ref $data;
261 }
262 }
263 }
264  
265 sub get_or_load_objects {
266 2099 ▶ my ( $self, @ids ) = @_;
Subroutine get_or_load_objects: called CC=2 SCAR=6.9
Pod undocumented
267  
268 2099 ▶ return $self->get_or_load_object($ids[0]) if @ids == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ids == 1 1572 527 2099
269  
270 527 my %objects;
271 527 @objects{@ids} = $self->live_objects->ids_to_objects(@ids);
272  
273 527 my @missing = grep { not defined $objects{$_} } keys %objects; # @ids may contain duplicates
274  
275 527 @objects{@missing} = $self->load_objects(@missing);
276  
277 503 return @objects{@ids};
278 }
279  
280 sub load_objects {
281 527 ▶ my ( $self, @ids ) = @_;
Subroutine load_objects: called CC=1 SCAR=0.0
Pod undocumented
282  
283 527 return $self->expand_objects( $self->get_or_load_entries(@ids) );
284 }
285  
286 sub get_or_load_entries {
287 527 ▶ my ( $self, @ids ) = @_;
Subroutine get_or_load_entries: called CC=2 SCAR=6.9
Pod undocumented
288  
289 527 my %entries;
290 527 @entries{@ids} = $self->ids_to_entries(@ids);
291  
292 527 ▶ if ( my @load = grep { !$entries{$_} } @ids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@load) = grep({not $entries{$_};} @ids)) 447 80 527
293 447 @entries{@load} = $self->load_entries(@load);
294 }
295  
296 503 return @entries{@ids};
297 }
298  
299 sub load_entries {
300 447 ▶ my ( $self, @ids ) = @_;
Subroutine load_entries: called CC=4 SCAR=16.9
Pod undocumented
301  
302 447 my @entries = $self->backend->get(@ids);
303  
304 447 ▶ if ( @entries != @ids or grep { !$_ } @entries ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@entries != @ids or grep {not $_;} @entries) 0 447 447
Condition33%1/3
@entries != @ids or grep {not $_;} @entries
l!l&&r!l&&!r
00447
MC/DC0%0/2
@entries != @ids or grep {not $_;} @entries
@entries != @ids grep {not $_;} @entries
Truth table33%1/3
A or B
A= @entries != @idsB= grep {not $_;} @entries
ABresult
1X1
011
000
305 0 my %entries;
306 0 @entries{@ids} = @entries;
307 0 my @missing = grep { !$entries{$_} } @ids;
308  
309 0 KiokuDB::Error::MissingObjects->throw( ids => \@missing );
310 }
311  
312 447 my $l = $self->live_objects;
313 447 foreach my $entry ( @entries ) {
314 1259 $l->register_entry( $entry->id, $entry, in_storage => 1 );
315 }
316  
317 423 return @entries;
318 }
319  
320 sub register_and_expand_entries {
321 435 ▶ my ( $self, @entries ) = @_;
Subroutine register_and_expand_entries: called CC=2 SCAR=6.9
Pod undocumented
322  
323 435 my $l = $self->live_objects;
324 435 foreach my $entry ( @entries ) {
325 1090 $l->register_entry( $entry->id, $entry, in_storage => 1 );
326 }
327  
328 435 $self->expand_objects(@entries);
329 }
330  
331 sub get_or_load_object {
332 3533 ▶ my ( $self, $id ) = @_;
Subroutine get_or_load_object: called CC=2 SCAR=6.9
Pod undocumented
333  
334 3533 ▶ if ( defined( my $obj = $self->live_objects->id_to_object($id) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj = $self->live_objects->id_to_object($id))) { } 1120 2413 3533
335 1120 return $obj;
336 } else {
337 2413 return $self->load_object($id);
338 }
339 }
340  
341 sub refresh_objects {
342 25 ▶ my ( $self, @objects ) = @_;
Subroutine refresh_objects: called CC=2 SCAR=6.9
Pod undocumented
343  
344 25 $self->refresh_object($_) for @objects;
345 }
346  
347 sub refresh_object {
348 27 ▶ my ( $self, $object ) = @_;
Subroutine refresh_object: called CC=1 SCAR=0.0
Pod undocumented
349  
350 27 my $id = $self->object_to_id($object);
351  
352 27 my $entry = $self->load_entry($id);
353  
354 27 my $refresh = $self->refresh_method( $entry->class );
355  
356 27 $self->$refresh($object, $entry);
357 27 $self->load_queue;
358  
359 27 return $object;
360 }
361  
362 sub get_or_load_entry {
363 2413 ▶ my ( $self, $id ) = @_;
Subroutine get_or_load_entry: called CC=2 SCAR=6.9
Pod undocumented
364  
365 2413 ▶ return $self->id_to_entry($id) || $self->load_entry($id);
Condition100%3/3
$self->id_to_entry($id) || $self->load_entry($id)
l!l&&r!l&&!r
222228163
MC/DC0%0/2
$self->id_to_entry($id) || $self->load_entry($id)
$self->id_to_entry($id) $self->load_entry($id)
Truth table66%2/3
A || B
A= $self->id_to_entry($id)B= $self->load_entry($id)
ABresult
1X1
011
000
366 }
367  
368 sub load_entry {
369 2418 ▶ my ( $self, $id ) = @_;
Subroutine load_entry: called CC=2 SCAR=6.9
Pod undocumented
370  
371 2418 ▶ my $entry = ( $self->backend->get($id) )[0]
Branch: 1 branch
BranchTrueFalseTotal
my $entry = ($self->backend->get($id))[0] or "KiokuDB::Error::MissingObjects"->throw("ids", [$id]) 53 2365 2418
372 or KiokuDB::Error::MissingObjects->throw( ids => [ $id ] );
373  
374 2365 $self->live_objects->register_entry( $id => $entry, in_storage => 1 );
375  
376 2202 return $entry;
377 }
378  
379 sub load_object {
380 2413 ▶ my ( $self, $id ) = @_;
Subroutine load_object: called CC=1 SCAR=0.0
Pod undocumented
381  
382 2413 my $entry = $self->get_or_load_entry($id);
383  
384 2197 return $self->expand_object($entry);
385 }
386  
387 18 __PACKAGE__->meta->make_immutable;
388  
389 18 __PACKAGE__
390  
391 __END__
392  
393 =pod
394  
395 =head1 NAME
396  
397 KiokuDB::Linker - Relinks live objects from storage entries
398  
399 =head1 SYNOPSIS
400  
401 # mostly internal
402  
403 =head1 DESCRIPTION
404  
405 The linker reconnects entry data, recreating the connected object graph in
406 memory.
407  
408 The linkage process starts with a an ID (or several IDs) to be loaded passed to
409 the C<get_or_load_objects> method.
410  
411 This ID will first be searched for in the live object set
412 (L<KiokuDB::LiveObjects>). If the object is already live, then it will be
413 returned as is.
414  
415 If the object is not live, then the corresponding entry is fetched from the
416 backend, and expanded into an actual instance.
417  
418 Expansion consults the L<KiokuDB::TypeMap> using L<KiokuDB::TypeMap::Resolver>,
419 to find the correct typemap entry (see
420 L<KiokuDB::Collapser/"COLLAPSING STRATEGIES"> and L<KiokuDB::TypeMap>), and
421 that is used for the actual expansion.
422  
423 Most of the grunt work is delegated by the entries back to the linker using the
424 C<inflate_data> method, which handles circular structures, retrying of tied
425 structures, etc.
426  
427 Inflated objects are registered with L<KiokuDB::LiveObjects>, and get inserted
428 into the current live object scope (L<KiokuDB::LiveObjects::Scope>). The
429 scope's job is to maintain a reference count of at least 1 for any loaded
430 object, until it is destroyed itself. This ensures that weak references are not
431 destroyed prematurely, but allows their use in order to avoid memory leaks.
432  
433 =cut
434  
« KiokuDB/Set.pm ↑ Summary KiokuDB/Test.pm »