KiokuDB/LiveObjects.pm

filter: Statementstmt 96.2% 255 / 265 Branchbran 84.2% 96 / 114 Conditioncond 80.0% 32 / 40 MC/DCmcdc 56.0% 14 / 25 Subroutinesub 97.9% 47 / 48 Podpod 65.5% 19 / 29 totaltotal 88.8% 463 / 521 CC 74 SCAR 16.8
CC
74
Cov
91%
insert
13.3
register_object
11.4
update_object_entry
9.9
CRAP 5.4
« KiokuDB/Collapser/Buffer.pm ↑ Summary KiokuDB/TypeMap.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::LiveObjects;
4 22 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use Scalar::Util qw(weaken refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use KiokuDB::LiveObjects::Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Hash::Util::FieldHash::Compat qw(fieldhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶BEGIN { local $@; eval 'use Devel::PartialDump qw(croak)' };
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine BEGIN: called CC=1 SCAR=0.6
11 22 ▶use Set::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 22 ▶use KiokuDB::LiveObjects::Scope;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use KiokuDB::LiveObjects::TXNScope;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 22 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 22 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 34 ▶coerce __PACKAGE__, from "HashRef", via { __PACKAGE__->new($_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22 22 has clear_leaks => (
23 isa => "Bool",
24 is => "rw",
25 );
26  
27 22 has cache => (
28 isa => "Cache::Ref",
29 is => "ro",
30 );
31  
32 22 has leak_tracker => (
33 isa => "CodeRef|Object",
34 is => "rw",
35 clearer => "clear_leak_tracker",
36 );
37  
38 22 has keep_entries => (
39 isa => "Bool",
40 is => "ro",
41 default => 1,
42 );
43  
44 has [qw(_objects _entries _object_entries)] => (
45 isa => "HashRef",
46 is => "ro",
47 init_arg => undef,
48 303 ▶ default => sub { fieldhash my %hash },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 22 );
50  
51 has _ids => (
52 #metaclass => 'Collection::Hash',
53 isa => "HashRef",
54 is => "ro",
55 init_arg => undef,
56 101 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 22 );
58  
59 sub _id_info {
60 27131 ▶ my ( $self, @ids ) = @_;
Subroutine _id_info: called CC=2 SCAR=6.9
61  
62 22 ▶ no warnings 'uninitialized'; # @ids can contain undefs
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 27131 ▶ if ( @ids == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ids == 1) { } 26111 1020 27131
65 26111 return $self->_ids->{$ids[0]};
66 } else {
67 1020 return @{ $self->_ids }{@ids};
68 }
69 }
70  
71 sub _vivify_id_info {
72 20002 ▶ my ( $self, $id ) = @_;
Subroutine _vivify_id_info: called CC=2 SCAR=6.9
73  
74 20002 my $info;
75  
76 20002 my $i = $self->_ids;
77  
78 20002 ▶ unless ( $info = $i->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info = $i->{$id}) 7010 12992 20002
79 7010 $info = { guard => KiokuDB::LiveObjects::Guard->new( $i, $id ) };
80 7010 weaken( $i->{$id} = $info );
81 }
82  
83 20002 return $info;
84 }
85  
86 sub id_to_object {
87 8007 ▶ my ( $self, $id ) = @_;
Subroutine id_to_object: called CC=3 SCAR=11.0
Pod documented
88  
89 8007 ▶ if ( my $c = $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->cache) 3540 4467 8007
90 3540 $c->hit($id);
91 }
92  
93 8007 ▶ if ( my $data = $self->_id_info($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->_id_info($id)) 3524 4483 8007
94 3524 return $data->{object};
95 }
96 }
97  
98 sub ids_to_objects {
99 528 ▶ my ( $self, @ids ) = @_;
Subroutine ids_to_objects: called CC=3 SCAR=11.0
Pod documented
100  
101 528 ▶ if ( my $c = $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->cache) 194 334 528
102 194 $c->hit(@ids);
103 }
104  
105 528 ▶ map { $_ && $_->{object} } $self->_id_info(@ids);
Branch: 1 branch
BranchTrueFalseTotal
$_ and $_->{"object"} 1603 1418 3021
106 }
107  
108 sub known_ids {
109 19 ▶ keys %{ shift->_ids };
Subroutine known_ids: called CC=1 SCAR=0.0
Pod undocumented
110 }
111  
112 sub live_ids {
113 0 ▶ my $self = shift;
Subroutine live_ids: not called CC=1 SCAR=6.9
Pod documented
114  
115 0 grep { ref $self->_id_info($_)->{object} } $self->known_ids;
116 }
117  
118 sub live_objects {
119 5539 ▶ grep { ref } map { $_->{object} } values %{ shift->_ids };
Subroutine live_objects: called CC=1 SCAR=0.0
Pod documented
120 }
121  
122 sub id_to_entry {
123 3994 ▶ my ( $self, $id ) = @_;
Subroutine id_to_entry: called CC=2 SCAR=6.9
Pod undocumented
124  
125 3994 ▶ if ( my $data = $self->_id_info($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->_id_info($id)) 1565 2429 3994
126 1565 return $data->{entry};
127 }
128  
129 2429 return undef;
130 }
131  
132 sub ids_to_entries {
133 792 ▶ my ( $self, @ids ) = @_;
Subroutine ids_to_entries: called CC=3 SCAR=11.0
Pod documented
134  
135 792 ▶ return $self->id_to_entry($ids[0]) if @ids == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ids == 1 300 492 792
136  
137 492 ▶ map { $_ && $_->{entry} } $self->_id_info(@ids);
Branch: 1 branch
BranchTrueFalseTotal
$_ and $_->{"entry"} 9 1253 1262
138 }
139  
140 sub loaded_ids {
141 10 ▶ my $self = shift;
Subroutine loaded_ids: called CC=1 SCAR=0.0
Pod undocumented
142  
143 10 grep { $self->_id_info($_)->{entry} } $self->known_ids;
144 }
145  
146 sub live_entries {
147 2448 ▶ grep { ref } map { $_->{entry} } values %{ shift->_ids };
Subroutine live_entries: called CC=1 SCAR=0.0
Pod documented
148 }
149  
150 22 has current_scope => (
151 isa => "KiokuDB::LiveObjects::Scope",
152 is => "ro",
153 writer => "_set_current_scope",
154 clearer => "_clear_current_scope",
155 weak_ref => 1,
156 );
157  
158 has _known_scopes => (
159 isa => "Set::Object",
160 is => "ro",
161 101 ▶ default => sub { Set::Object::Weak->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
162 22 );
163  
164 sub detach_scope {
165 3278 ▶ my ( $self, $scope ) = @_;
Subroutine detach_scope: called CC=4 SCAR=13.9
Pod documented
166  
167 3278 my $current_scope = $self->current_scope;
168 3278 ▶ if ( defined($current_scope) and refaddr($current_scope) == refaddr($scope) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_scope and builtin::refaddr($current_scope) == builtin::refaddr($scope)) 3277 1 3278
Condition66%2/3
defined $current_scope and builtin::refaddr($current_scope) == builtin::refaddr($scope)
!ll&&!rl&&r
013277
MC/DC50%1/2
defined $current_scope and builtin::refaddr($current_scope) == builtin::refaddr($scope)
defined $current_scope builtin::refaddr($current_scope) == builtin::refaddr($scope)
Truth table66%2/3
A and B
A= defined $current_scopeB= builtin::refaddr($current_scope) == builtin::refaddr($scope)
ABresult
0X0
100
111
169 3277 ▶ if ( my $parent = $scope->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $scope->parent) { } 679 2598 3277
170 679 $self->_set_current_scope($parent);
171 } else {
172 2598 $self->_clear_current_scope;
173 }
174 }
175 }
176  
177 sub remove_scope {
178 3277 ▶ my ( $self, $scope ) = @_;
Subroutine remove_scope: called CC=2 SCAR=6.9
Pod documented
179  
180 3277 $self->detach_scope($scope);
181  
182 3277 $scope->clear;
183  
184 3277 my $known = $self->_known_scopes;
185  
186 3277 $known->remove($scope);
187  
188 3277 ▶ if ( $known->size == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($known->size == 0) 2597 680 3277
189 2597 $self->check_leaks;
190 }
191 }
192  
193 sub check_leaks {
194 2597 ▶ my $self = shift;
Subroutine check_leaks: called CC=8 SCAR=20.9
Pod undocumented
195  
196 2597 ▶ return if $self->_known_scopes->size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_known_scopes->size 0 2597 2597
197  
198 2597 ▶ if ( my @still_live = grep { defined } $self->live_objects ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@still_live) = grep({defined $_;} $self->live_objects)) 377 2220 2597
199 # immortal objects are still live but not considered leaks
200 377 my $o = $self->_objects;
201 my @leaked = grep {
202 377 my $i = $o->{$_};
203 not($i->{immortal} or $i->{cache})
204 646 ▶ } @still_live;
Condition100%3/3
$i->{'immortal'} || $i->{'cache'}
l!l&&r!l&&!r
136609
MC/DC100%2/2
$i->{'immortal'} || $i->{'cache'}
$i->{'immortal'} $i->{'cache'}
Truth table100%3/3
A || B
A= $i->{'immortal'}B= $i->{'cache'}
ABresult
1X1
011
000
205  
206 377 ▶ if ( $self->clear_leaks ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->clear_leaks) 2 375 377
207 2 $self->clear;
208 }
209  
210 377 ▶ if ( my $tracker = $self->leak_tracker and @leaked ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tracker = $self->leak_tracker and @leaked) 1 376 377
Condition100%3/3
my $tracker = $self->leak_tracker and @leaked
!ll&&!rl&&r
339371
MC/DC100%2/2
my $tracker = $self->leak_tracker and @leaked
my $tracker = $self->leak_tracker @leaked
Truth table100%3/3
A and B
A= my $tracker = $self->leak_trackerB= @leaked
ABresult
0X0
100
111
211 1 ▶ if ( ref($tracker) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tracker eq 'CODE') { } 1 0 1
212 1 $tracker->(@leaked);
213 } else {
214 0 $tracker->leaked_objects(@leaked);
215 }
216 }
217 }
218 }
219  
220 22 has txn_scope => (
221 isa => "KiokuDB::LiveObjects::TXNScope",
222 is => "ro",
223 writer => "_set_txn_scope",
224 clearer => "_clear_txn_scope",
225 weak_ref => 1,
226 );
227  
228 sub new_scope {
229 3277 ▶ my $self = shift;
Subroutine new_scope: called CC=2 SCAR=6.9
Pod documented
230  
231 3277 my $parent = $self->current_scope;
232  
233 3277 ▶ my $child = KiokuDB::LiveObjects::Scope->new(
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 679 2598 3277
234 ( $parent ? ( parent => $parent ) : () ),
235 live_objects => $self,
236 );
237  
238 3277 $self->_set_current_scope($child);
239  
240 3277 $self->_known_scopes->insert($child);
241  
242 3277 return $child;
243 }
244  
245 sub new_txn {
246 2090 ▶ my $self = shift;
Subroutine new_txn: called CC=3 SCAR=11.0
Pod documented
247  
248 2090 ▶ return unless $self->keep_entries;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keep_entries 996 1094 2090
249  
250 1094 my $parent = $self->txn_scope;
251  
252 1094 ▶ my $child = KiokuDB::LiveObjects::TXNScope->new(
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 64 1030 1094
253 ( $parent ? ( parent => $parent ) : () ),
254 live_objects => $self,
255 );
256  
257 1094 $self->_set_txn_scope($child);
258  
259 1094 return $child;
260 }
261  
262 sub objects_to_ids {
263 1753 ▶ my ( $self, @objects ) = @_;
Subroutine objects_to_ids: called CC=3 SCAR=11.0
Pod documented
264  
265 1753 ▶ return $self->object_to_id($objects[0])
Branch: 1 branch
BranchTrueFalseTotal
if @objects == 1 1257 496 1753
266 if @objects == 1;
267  
268 496 ▶ map { $_ && $_->{guard}->key } @{ $self->_objects }{@objects};
Branch: 1 branch
BranchTrueFalseTotal
$_ and $_->{"guard"}->key 1239 2 1241
269 }
270  
271 sub object_to_id {
272 34353 ▶ my ( $self, $obj ) = @_;
Subroutine object_to_id: called CC=2 SCAR=6.9
Pod documented
273  
274 34353 ▶ if ( my $info = $self->_objects->{$obj} ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $self->_objects->{$obj}) 6567 27786 34353
275 6567 return $info->{guard}->key;
276 }
277  
278 27786 return undef;
279 }
280  
281 sub objects_to_entries {
282 260 ▶ my ( $self, @objects ) = @_;
Subroutine objects_to_entries: called CC=1 SCAR=0.0
Pod documented
283  
284 260 return $self->ids_to_entries( $self->objects_to_ids(@objects) );
285 }
286  
287 sub object_to_entry {
288 9792 ▶ my ( $self, $obj ) = @_;
Subroutine object_to_entry: called CC=2 SCAR=6.9
Pod documented
289  
290 9792 ▶ return $self->id_to_entry( $self->object_to_id($obj) || return );
Condition100%2/2
$self->object_to_id($obj) || (return)
l!l
12778515
MC/DC100%1/1
$self->object_to_id($obj) || (return)
$self->object_to_id($obj)
Truth table100%2/2
A
A= $self->object_to_id($obj)
Aresult
11
00
291 }
292  
293 sub id_in_root_set {
294 691 ▶ my ( $self, $id ) = @_;
Subroutine id_in_root_set: called CC=2 SCAR=6.9
Pod undocumented
295  
296 691 ▶ if ( my $data = $self->_id_info($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->_id_info($id)) 659 32 691
297 659 return $data->{root};
298 }
299  
300 32 return undef;
301 }
302  
303 sub id_in_storage {
304 13033 ▶ my ( $self, $id ) = @_;
Subroutine id_in_storage: called CC=2 SCAR=6.9
Pod undocumented
305  
306 13033 ▶ if ( my $data = $self->_id_info($id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data = $self->_id_info($id)) 2843 10190 13033
307 2843 return $data->{in_storage};
308 }
309  
310 10190 return undef;
311 }
312  
313  
314 sub object_in_storage {
315 1064 ▶ my ( $self, $object ) = @_;
Subroutine object_in_storage: called CC=2 SCAR=6.9
Pod undocumented
316  
317 1064 ▶ $self->id_in_storage( $self->object_to_id($object) || return );
Condition100%2/2
$self->object_to_id($object) || (return)
l!l
709355
MC/DC100%1/1
$self->object_to_id($object) || (return)
$self->object_to_id($object)
Truth table100%2/2
A
A= $self->object_to_id($object)
Aresult
11
00
318 }
319  
320 sub update_object_entry {
321 1178 ▶ my ( $self, $object, $entry, %args ) = @_;
Subroutine update_object_entry: called CC=8 SCAR=22.9
Pod undocumented
322  
323  
324 1178 ▶ my $s = $self->current_scope or croak "no open live object scope";
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->current_scope or croak("no open live object scope") 0 1178 1178
325  
326 1178 ▶ my $info = $self->_objects->{$object} or croak "Object not yet registered";
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->_objects->{$object} or croak("Object not yet registered") 0 1178 1178
327 1178 $self->_entries->{$entry} = $info;
328  
329 1178 @{$info}{keys %args} = values %args;
330 1178 weaken($info->{entry} = $entry);
331  
332 1178 ▶ if ( $self->keep_entries ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keep_entries) 647 531 1178
333 647 $self->_object_entries->{$object} = $entry;
334  
335 647 ▶ if ( $args{in_storage} and my $txs = $self->txn_scope ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"in_storage"} and my $txs = $self->txn_scope) 0 647 647
Condition33%1/3
$args{"in_storage"} and my $txs = $self->txn_scope
!ll&&!rl&&r
64700
MC/DC0%0/2
$args{"in_storage"} and my $txs = $self->txn_scope
$args{"in_storage"} my $txs = $self->txn_scope
Truth table33%1/3
A and B
A= $args{"in_storage"}B= my $txs = $self->txn_scope
ABresult
0X0
100
111
336 0 $txs->push($entry);
337 }
338 }
339  
340 # break cycle for passthrough objects
341 1178 ▶ 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)) 0 1178 1178
Condition66%2/3
ref $entry->data and builtin::refaddr($object) == builtin::refaddr($entry->data)
!ll&&!rl&&r
811700
MC/DC0%0/2
ref $entry->data and builtin::refaddr($object) == builtin::refaddr($entry->data)
ref $entry->data builtin::refaddr($object) == builtin::refaddr($entry->data)
Truth table66%2/3
A and B
A= ref $entry->dataB= builtin::refaddr($object) == builtin::refaddr($entry->data)
ABresult
0X0
100
111
342 0 weaken($entry->{data}); # FIXME there should be a MOP way to do this
343 }
344 }
345  
346 sub register_object {
347 6804 ▶ my ( $self, $id, $object, %args ) = @_;
Subroutine register_object: called CC=11 SCAR=24.3
Pod undocumented
348  
349 6804 ▶ my $s = $self->current_scope or croak "no open live object scope";
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->current_scope or croak("no open live object scope") 0 6804 6804
350  
351 6804 ▶ croak($object, " is not a reference") unless ref($object);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $object 0 6804 6804
352 6804 ▶ croak($object, " is an entry") if blessed($object) && $object->isa("KiokuDB::Entry");
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($object) and $object->isa("KiokuDB::Entry") 0 6804 6804
Condition66%2/3
builtin::blessed($object) and $object->isa("KiokuDB::Entry")
!ll&&!rl&&r
52162830
MC/DC0%0/2
builtin::blessed($object) and $object->isa("KiokuDB::Entry")
builtin::blessed($object) $object->isa("KiokuDB::Entry")
Truth table66%2/3
A and B
A= builtin::blessed($object)B= $object->isa("KiokuDB::Entry")
ABresult
0X0
100
111
353  
354 6804 ▶ if ( my $id = $self->object_to_id($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->object_to_id($object)) 1 6803 6804
355 1 croak($object, " is already registered as $id")
356 }
357  
358 6803 my $info = $self->_vivify_id_info($id);
359  
360 6803 ▶ if ( ref $info->{object} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info->{"object"}) 0 6803 6803
361 0 croak "An object with the id '$id' is already registered ($info->{object} != $object)"
362 }
363  
364 6803 $self->_objects->{$object} = $info;
365  
366 6803 weaken($info->{object} = $object);
367  
368 6803 ▶ if ( $self->keep_entries and my $entry = $info->{entry} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keep_entries and my $entry = $info->{"entry"}) 3522 3281 6803
Condition100%3/3
$self->keep_entries and my $entry = $info->{"entry"}
!ll&&!rl&&r
30032783522
MC/DC100%2/2
$self->keep_entries and my $entry = $info->{"entry"}
$self->keep_entries my $entry = $info->{"entry"}
Truth table100%3/3
A and B
A= $self->keep_entriesB= my $entry = $info->{"entry"}
ABresult
0X0
100
111
369 3522 $self->_object_entries->{$object} = $entry;
370 }
371  
372 6803 @{$info}{keys %args} = values %args;
373  
374 6803 ▶ if ( $args{cache} and my $c = $self->cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"cache"} and my $c = $self->cache) 36 6767 6803
Condition100%3/3
$args{"cache"} and my $c = $self->cache
!ll&&!rl&&r
67283936
MC/DC100%2/2
$args{"cache"} and my $c = $self->cache
$args{"cache"} my $c = $self->cache
Truth table100%3/3
A and B
A= $args{"cache"}B= my $c = $self->cache
ABresult
0X0
100
111
375 36 $c->set( $id => $object );
376 }
377  
378 6803 $s->push($object);
379 }
380  
381 sub register_entry {
382 13199 ▶ my ( $self, $id, $entry, %args ) = @_;
Subroutine register_entry: called CC=5 SCAR=16.1
Pod undocumented
383  
384 13199 my $info = $self->_vivify_id_info($id);
385  
386 13199 $self->_entries->{$entry} = $info;
387  
388 13199 ▶ confess "$entry" unless $entry->isa("KiokuDB::Entry");
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->isa("KiokuDB::Entry") 0 13036 13036
389 13036 @{$info}{keys %args, 'root'} = ( values %args, $entry->root );
390  
391 13036 weaken($info->{entry} = $entry);
392  
393 13036 ▶ if ( $args{in_storage} and $self->keep_entries and my $txs = $self->txn_scope ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"in_storage"} and $self->keep_entries and my $txs = $self->txn_scope) 3595 9441 13036
Condition100%6/6
$args{"in_storage"} and $self->keep_entries and my $txs = $self->txn_scope
!ll&&!rl&&r
822712143595
$args{"in_storage"} and $self->keep_entries
!ll&&!rl&&r
433838894809
MC/DC100%3/3
$args{"in_storage"} and $self->keep_entries and my $txs = $self->txn_scope
$args{"in_storage"} $self->keep_entries my $txs = $self->txn_scope
Truth table100%4/4
A and B and C
A= $args{"in_storage"}B= $self->keep_entriesC= my $txs = $self->txn_scope
ABCresult
0XX0
10X0
1100
1111
394 3595 $txs->push($entry);
395 }
396 }
397  
398 sub insert {
399 230 ▶ my ( $self, @pairs ) = @_;
Subroutine insert: called CC=12 SCAR=25.9
Pod documented
400  
401 230 ▶ croak "The arguments must be an list of pairs of IDs/Entries to objects"
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs % 2 == 0 0 230 230
402 unless @pairs % 2 == 0;
403  
404 230 ▶ croak "no open live object scope" unless $self->current_scope;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->current_scope 0 230 230
405  
406 230 my @register;
407 230 ▶ while ( @pairs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 455 228 683
408 455 my ( $id, $object ) = splice @pairs, 0, 2;
409 455 my $entry;
410  
411 455 ▶ if ( ref $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $id) 2 453 455
412 2 $entry = $id;
413 2 $id = $entry->id;
414 }
415  
416 455 ▶ confess("blah") unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 455 455
417  
418 455 ▶ croak($object, " is not a reference") unless ref($object);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $object 1 454 455
419 454 ▶ croak($object, " is an entry") if blessed($object) && $object->isa("KiokuDB::Entry");
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($object) and $object->isa("KiokuDB::Entry") 0 454 454
Condition66%2/3
builtin::blessed($object) and $object->isa("KiokuDB::Entry")
!ll&&!rl&&r
2002540
MC/DC0%0/2
builtin::blessed($object) and $object->isa("KiokuDB::Entry")
builtin::blessed($object) $object->isa("KiokuDB::Entry")
Truth table66%2/3
A and B
A= builtin::blessed($object)B= $object->isa("KiokuDB::Entry")
ABresult
0X0
100
111
420  
421 454 ▶ if ( $entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry) { } 2 452 454
422 2 $self->register_entry( $id => $entry, in_storage => 1 );
423 2 $self->register_object( $id => $object );
424  
425 # break cycle for passthrough objects
426 2 ▶ 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)) 0 2 2
Condition33%1/3
ref $entry->data and builtin::refaddr($object) == builtin::refaddr($entry->data)
!ll&&!rl&&r
200
MC/DC0%0/2
ref $entry->data and builtin::refaddr($object) == builtin::refaddr($entry->data)
ref $entry->data builtin::refaddr($object) == builtin::refaddr($entry->data)
Truth table33%1/3
A and B
A= ref $entry->dataB= builtin::refaddr($object) == builtin::refaddr($entry->data)
ABresult
0X0
100
111
427 0 weaken($entry->{data}); # FIXME there should be a MOP way to do this
428 }
429 } else {
430 452 $self->register_object( $id => $object );
431 }
432 }
433 }
434  
435 sub update_entries {
436 77 ▶ my ( $self, @pairs ) = @_;
Subroutine update_entries: called CC=3 SCAR=11.0
Pod documented
437 77 my @entries;
438  
439 77 ▶ while ( @pairs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 77 77 154
440 77 my ( $object, $entry ) = splice @pairs, 0, 2;
441  
442 77 $self->register_entry( $entry->id => $entry, in_storage => 1 );
443  
444 77 ▶ unless ( $self->object_to_id($object) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->object_to_id($object)) { } 75 2 77
445 2 $self->register_object( $entry->id => $object );
446 } else {
447 75 $self->update_object_entry( $object, $entry );
448 }
449 }
450  
451 77 return;
452 }
453  
454 sub rollback_entries {
455 228 ▶ my ( $self, @entries ) = @_;
Subroutine rollback_entries: called CC=3 SCAR=11.0
Pod documented
456  
457 228 foreach my $entry ( reverse @entries ) {
458 380 my $info = $self->_id_info($entry->id);
459  
460 380 ▶ if ( my $prev = $entry->prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $prev = $entry->prev) { } 39 341 380
461 39 weaken($info->{entry} = $prev);
462 } else {
463 341 delete $info->{entry};
464 }
465 }
466 }
467  
468 sub remove {
469 139 ▶ my ( $self, @stuff ) = @_;
Subroutine remove: called CC=7 SCAR=19.5
Pod documented
470  
471 139 my ( $i, $o, $e, $oe ) = ( $self->_ids, $self->_objects, $self->_entries, $self->_object_entries );
472  
473 139 ▶ while ( @stuff ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stuff) 184 139 323
474 184 my $thing = shift @stuff;
475  
476 184 ▶ if ( ref $thing ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing) { } 98 86 184
477 # FIXME make this a bit less zealous?
478 98 my $info;
479 98 ▶ if ( $info = delete $o->{$thing} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info = delete $o->{$thing}) { } 56 42 98
elsif ($info = delete $e->{$thing}) { } 42 0 42
480 56 delete $info->{object};
481 56 delete $oe->{$thing};
482 56 ▶ push @stuff, $info->{entry} if $info->{entry};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"entry"} 42 14 56
483 } elsif ( $info = delete $e->{$thing} ) {
484 42 delete $info->{entry};
485 42 ▶ push @stuff, $info->{object} if ref $info->{object};
Branch: 1 branch
BranchTrueFalseTotal
if ref $info->{"object"} 0 42 42
486 }
487 } else {
488 86 my $info = delete $i->{$thing};
489 86 push @stuff, grep { ref } delete @{$info}{qw(entry object)};
490 }
491 }
492 }
493  
494 sub clear {
495 966 ▶ my $self = shift;
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
496  
497 # don't waste too much time in DESTROY
498 966 $_->{guard}->dismiss for values %{ $self->_ids };
499  
500 966 %{ $self->_ids } = ();
501 966 %{ $self->_objects } = ();
502 966 %{ $self->_object_entries } = ();
503 966 %{ $self->_entries } = ();
504  
505 966 $self->_clear_current_scope;
506 966 $self->_known_scopes->clear;
507 }
508  
509 22 __PACKAGE__->meta->make_immutable;
510  
511 22 __PACKAGE__
512  
513 __END__
514  
515 =pod
516  
517 =head1 NAME
518  
519 KiokuDB::LiveObjects - Live object set tracking
520  
521 =head1 SYNOPSIS
522  
523 $live_objects->insert( $entry => $object );
524  
525 $live_objects->insert( $id => $object );
526  
527 my $id = $live_objects->object_to_id( $object );
528  
529 my $obj = $live_objects->id_to_object( $id );
530  
531 my $scope = $live_objects->new_scope;
532  
533 =head1 DESCRIPTION
534  
535 This object keeps track of the set of live objects, their associated IDs, and
536 the storage entries.
537  
538 =head1 ATTRIBUTES
539  
540 =over 4
541  
542 =item clear_leaks
543  
544 Boolean. Defaults to false.
545  
546 If true, when the last known scope is removed but some objects are still live
547 they will be removed from the live object set.
548  
549 Note that this does B<NOT> prevent leaks (memory cannot be reclaimed), it
550 merely prevents stale objects from staying loaded.
551  
552 =item leak_tracker
553  
554 This is a coderef or object.
555  
556 If any objects ar eleaked (see C<clear_leaks>) then the this can be used to
557 report them, or to break the circular structure.
558  
559 When an object is provided the C<leaked_objects> method is called. The coderef
560 is simply invoked with the objects as arguments.
561  
562 Triggered after C<clear_leaks> causes C<clear> to be called.
563  
564 For example, to break cycles you can use L<Data::Structure::Util>'s
565 C<circular_off> function:
566  
567 use Data::Structure::Util qw(circular_off);
568  
569 $dir->live_objects->leak_tracker(sub {
570 my @leaked_objects = @_;
571 circular_off($_) for @leaked_objects;
572 });
573  
574 =item keep_entries
575  
576 B<EXPERIMENTAL>
577  
578 When true (the default), L<KiokuDB::Entries> loaded from the backend or created
579 by the collapser are kept around.
580  
581 This results in a considerable memory overhead, so it's no longer required.
582  
583 =back
584  
585 =head1 METHODS
586  
587 =over 4
588  
589 =item insert
590  
591 Takes pairs, id or entry as the key, and object as the value, registering the
592 objects.
593  
594 =item objects_to_ids
595  
596 =item object_to_id
597  
598 Given objects, returns their IDs, or undef for objects which not registered.
599  
600 =item objects_to_entries
601  
602 =item object_to_entry
603  
604 Given objects, find the corresponding entries.
605  
606 =item ids_to_objects
607  
608 =item id_to_object
609  
610 Given IDs, find the corresponding objects.
611  
612 =item ids_to_entries
613  
614 Given IDs, find the corresponding entries.
615  
616 =item update_entries
617  
618 Given entries, replaces the live entries of the corresponding objects with the
619 newly updated ones.
620  
621 The objects must already be in the live object set.
622  
623 This method is called on a successful transaction commit.
624  
625 =item new_scope
626  
627 Creates a new L<KiokuDB::LiveObjects::Scope>, with the current scope as its
628 parent.
629  
630 =item current_scope
631  
632 The current L<KiokuDB::LiveObjects::Scope> instance.
633  
634 This is the scope into which newly registered objects are pushed.
635  
636 =item new_txn
637  
638 Creates a new L<KiokuDB::LiveObjects::TXNScope>, with the current txn scope as
639 its parent.
640  
641 =item txn_scope
642  
643 The current L<KiokuDB::LiveObjects::TXNScope>.
644  
645 =item clear
646  
647 Forces a clear of the live object set.
648  
649 This removes all objects and entries, and can be useful in the case of leaks
650 (to prevent false positives on lookups).
651  
652 Note that this does not actually break the circular structures, so the leak is
653 unresolved, but the objects are no longer considered live by the L<KiokuDB> instance.
654  
655 =item live_entries
656  
657 =item live_objects
658  
659 =item live_ids
660  
661 Enumerates the live entries, objects or ids.
662  
663 =item rollback_entries
664  
665 Called by L<KiokuDB::LiveObjects::TXNScope/rollback>.
666  
667 =item remove
668  
669 Removes entries from the live object set.
670  
671 =item remove_scope $scope
672  
673 Removes a scope from the set of known scopes.
674  
675 Also calls C<detach_scope>, and calls C<KiokuDB::LiveObjects::Scope/clear> on
676 the scope itself.
677  
678 =item detach_scope $scope
679  
680 Detaches C<$scope> if it's the current scope.
681  
682 This prevents C<push> from being called on this scope object implicitly
683 anymore.
684  
685 =back
686  
687 =cut
« KiokuDB/Collapser/Buffer.pm ↑ Summary KiokuDB/TypeMap.pm »