KiokuDB/Entry.pm

filter: Statementstmt 100% 137 / 137 Branchbran 91.6% 66 / 72 Conditioncond 77.7% 7 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 7 totaltotal 92.6% 238 / 257 CC 40 SCAR 16.0
CC
40
Cov
96%
_unpack_old
10.5
_unpack
7.0
_build__references
7.0
CRAP 5.0
« KiokuDB/LinkChecker.pm ↑ Summary KiokuDB/Test/Fixture.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Entry;
4 27 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 27 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 27 with qw(MooseX::Clone);
11  
12 27 has id => (
13 isa => "Str",
14 is => "ro",
15 writer => "_id",
16 clearer => "clear_id",
17 predicate => "has_id",
18 );
19  
20 27 has root => (
21 isa => "Bool",
22 is => "rw",
23 lazy_build => 1,
24 );
25  
26 sub _build_root {
27 3542 ▶ my $self = shift;
Subroutine _build_root: called CC=3 SCAR=11.0
28  
29 3542 ▶ if ( $self->has_id and my $prev = $self->prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_id and my $prev = $self->prev) { } 644 2898 3542
Condition100%3/3
$self->has_id and my $prev = $self->prev
!ll&&!rl&&r
1042794644
MC/DC100%2/2
$self->has_id and my $prev = $self->prev
$self->has_id my $prev = $self->prev
Truth table100%3/3
A and B
A= $self->has_idB= my $prev = $self->prev
ABresult
0X0
100
111
30 644 return $prev->root;
31 } else {
32 2898 return 0;
33 }
34 }
35  
36 27 has deleted => (
37 isa => "Bool",
38 is => "ro",
39 writer => "_deleted",
40 );
41  
42 27 has data => (
43 is => "ro",
44 writer => "_data",
45 predicate => "has_data",
46 );
47  
48 27 has class => (
49 isa => "Str",
50 is => "ro",
51 writer => "_class",
52 predicate => "has_class",
53 );
54  
55 27 has class_meta => (
56 isa => "HashRef",
57 is => "ro",
58 writer => "_class_meta",
59 predicate => "has_class_meta",
60 );
61  
62 27 has class_version => (
63 isa => "Str",
64 is => "ro",
65 writer => "_class_version",
66 predicate => "has_class_version",
67 );
68  
69 27 my @tied = ( map { substr($_, 0, 1) } qw(HASH SCALAR ARRAY GLOB) );
70  
71 27 has tied => (
72 is => "ro",
73 writer => "_tied",
74 predicate => "has_tied",
75 );
76  
77 27 has backend_data => (
78 is => "rw",
79 predicate => "has_backend_data",
80 clearer => "clear_backend_data",
81 );
82  
83 27 has prev => (
84 isa => __PACKAGE__,
85 is => "rw",
86 predicate => "has_prev",
87 clearer => "clear_prev",
88 );
89  
90 sub root_prev {
91 10 ▶ my $self = shift;
Subroutine root_prev: called CC=2 SCAR=6.9
Pod undocumented
92  
93 10 ▶ if ( $self->has_prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_prev) { } 5 5 10
94 5 return $self->prev->root_prev;
95 } else {
96 5 return $self;
97 }
98 }
99  
100 27 has object => (
101 traits => [qw(NoClone)],
102 is => "rw",
103 weak_ref => 1,
104 predicate => "has_object",
105 clearer => "clear_object",
106 );
107  
108 sub deletion_entry {
109 131 ▶ my $self = shift;
Subroutine deletion_entry: called CC=3 SCAR=11.1
Pod undocumented
110  
111 131 ▶ ( ref $self )->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_object ? : 42 89 131
$self->has_backend_data ? : 0 131 131
112 id => $self->id,
113 prev => $self,
114 deleted => 1,
115 ( $self->has_object ? ( object => $self->object ) : () ),
116 ( $self->has_backend_data ? ( backend_data => $self->backend_data ) : () ),
117 );
118 }
119  
120 sub derive {
121 87 ▶ my ( $self, @args ) = @_;
Subroutine derive: called CC=1 SCAR=0.0
Pod undocumented
122  
123 87 $self->clone(
124 prev => $self,
125 @args,
126 );
127 }
128  
129 27 has _references => (
130 traits => [qw(NoClone)],
131 isa => "ArrayRef",
132 is => "ro",
133 lazy_build => 1,
134 );
135  
136 sub _build__references {
137 2089 ▶ my $self = shift;
Subroutine _build__references: called CC=7 SCAR=19.5
138  
139 27 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
140 2089 ▶ if ( $self->class eq 'KiokuDB::Set::Stored' ) { # FIXME should the typemap somehow handle this?
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class eq 'KiokuDB::Set::Stored') { } 1 2088 2089
141 1 return [ map { KiokuDB::Reference->new( id => $_ ) } @{ $self->data } ];
142 } else {
143 2088 my @refs;
144  
145 2088 my @queue = $self->data;
146  
147 2088 ▶ while ( @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 6480 2088 8568
148 6480 my $next = pop @queue;
149  
150 6480 my $ref = ref $next;
151 6480 ▶ if ( $ref eq 'HASH' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 67 6413 6480
elsif ($ref eq 'ARRAY') { } 2209 4204 6413
elsif ($ref eq 'KiokuDB::Entry') { } 2 4202 4204
elsif ($ref eq 'KiokuDB::Reference') { } 4201 1 4202
152 67 push @queue, grep { ref } values %$next;
153 } elsif ( $ref eq 'ARRAY' ) {
154 2209 push @queue, grep { ref } @$next;
155 } elsif ( $ref eq 'KiokuDB::Entry' ) {
156 2 push @refs, $next->references;
157 } elsif ( $ref eq 'KiokuDB::Reference' ) {
158 4201 push @refs, $next;
159 }
160 }
161  
162 2088 return \@refs;
163 }
164 }
165  
166 sub references {
167 2089 ▶ my $self = shift;
Subroutine references: called CC=1 SCAR=0.0
Pod undocumented
168  
169 2089 return @{ $self->_references };
170 }
171  
172 27 has _referenced_ids => (
173 traits => [qw(NoClone)],
174 isa => "ArrayRef",
175 is => "ro",
176 lazy_build => 1,
177 );
178  
179 sub _build__referenced_ids {
180 2083 ▶ my $self = shift;
Subroutine _build__referenced_ids: called CC=2 SCAR=6.9
181  
182 27 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
183 2083 ▶ if ( $self->class eq 'KiokuDB::Set::Stored' ) { # FIXME should the typemap somehow handle this?
Branch: 1 branch
BranchTrueFalseTotal
if ($self->class eq 'KiokuDB::Set::Stored') { } 1 2082 2083
184 1 return $self->data;
185 } else {
186 2082 return [ map { $_->id } $self->references ];
187 }
188 }
189  
190 sub referenced_ids {
191 2085 ▶ my $self = shift;
Subroutine referenced_ids: called CC=1 SCAR=0.0
Pod undocumented
192  
193 2085 @{ $self->_referenced_ids };
194 }
195  
196 27 ▶use constant _version => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
197  
198 27 ▶use constant _root_b => 0x01;
Subroutine BEGIN: called CC=1 SCAR=0.0
199 27 ▶use constant _deleted_b => 0x02;
Subroutine BEGIN: called CC=1 SCAR=0.0
200  
201 27 ▶use constant _tied_shift => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
202 27 ▶use constant _tied_mask => 0x03 << _tied_shift;
Subroutine BEGIN: called CC=1 SCAR=0.0
203  
204 27 my %tied; @tied{@tied} = ( 1 .. scalar(@tied) );
205  
206 sub _pack {
207 4571 ▶ my $self = shift;
Subroutine _pack: called CC=4 SCAR=13.9
208  
209 4571 my $flags = 0;
210  
211 4571 ▶ $flags |= _root_b if $self->root;
Branch: 1 branch
BranchTrueFalseTotal
if $self->root 1989 2582 4571
212 4571 ▶ $flags |= _deleted_b if $self->deleted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->deleted 2 4569 4571
213  
214 4571 ▶ if ( $self->has_tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_tied) 31 4540 4571
215 31 $flags |= $tied{$self->tied} << _tied_shift;
216 }
217  
218 27 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
219 4571 pack( "C C w/a* w/a*", _version, $flags, $self->id, $self->class );
220 }
221  
222 sub _unpack {
223 11087 ▶ my ( $self, $packed ) = @_;
Subroutine _unpack: called CC=7 SCAR=19.5
224  
225 11087 my ( $v, $body ) = unpack("C a*", $packed);
226  
227 11087 ▶ if ( $v == _version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == 1) { } 11075 12 11087
228 11075 my ( $flags, $id, $class, $extra ) = unpack("C w/a w/a a*", $body);
229  
230 11075 ▶ return $self->_unpack_old($packed) if length($extra);
Branch: 1 branch
BranchTrueFalseTotal
if length $extra 0 11075 11075
231  
232 11075 ▶ $self->_id($id) if length($id);
Branch: 1 branch
BranchTrueFalseTotal
if length $id 11022 53 11075
233  
234 11075 ▶ $self->_class($class) if length($class);
Branch: 1 branch
BranchTrueFalseTotal
if length $class 4794 6281 11075
235  
236 11075 $self->root($flags & _root_b);
237 11075 ▶ $self->_deleted(1) if $flags & _deleted_b;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 2 2 11073 11075
238  
239 11075 ▶ if ( my $tied = ( $flags & _tied_mask ) >> _tied_shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = ($flags & 12) >> 2) 31 11044 11075
240 31 $self->_tied( $tied[$tied - 1] );
241 }
242 } else {
243 12 $self->_unpack_old($packed);
244 }
245 }
246  
247  
248 sub _pack_old {
249 12 ▶ my $self = shift;
Subroutine _pack_old: called CC=1 SCAR=0.0
250  
251 27 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
252 12 join(",",
253 $self->id,
254 !!$self->root,
255 $self->class,
256 $self->tied,
257 !!$self->deleted,
258 );
259 }
260  
261 sub _unpack_old {
262 12 ▶ my ( $self, $packed ) = @_;
Subroutine _unpack_old: called CC=10 SCAR=23.5
263  
264 12 my ( $id, $root, $class, $tied, $deleted ) = split ',', $packed;
265  
266 12 ▶ die "bad entry format: $packed" if $root and $root ne '1';
Branch: 1 branch
BranchTrueFalseTotal
if $root and $root ne 1 0 12 12
Condition66%2/3
$root and $root ne 1
!ll&&!rl&&r
1110
MC/DC0%0/2
$root and $root ne 1
$root $root ne 1
Truth table66%2/3
A and B
A= $rootB= $root ne 1
ABresult
0X0
100
111
267 12 ▶ die "bad entry format: $packed" if $deleted and $deleted ne '1';
Branch: 1 branch
BranchTrueFalseTotal
if $deleted and $deleted ne 1 0 12 12
Condition66%2/3
$deleted and $deleted ne 1
!ll&&!rl&&r
1110
MC/DC0%0/2
$deleted and $deleted ne 1
$deleted $deleted ne 1
Truth table66%2/3
A and B
A= $deletedB= $deleted ne 1
ABresult
0X0
100
111
268  
269 12 ▶ $self->_id($id) if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 12 0 12
270 12 ▶ $self->root(1) if $root;
Branch: 1 branch
BranchTrueFalseTotal
if $root 1 11 12
271 12 ▶ $self->_class($class) if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 1 11 12
272 12 ▶ $self->_tied(substr($tied, 0, 1)) if $tied;
Branch: 1 branch
BranchTrueFalseTotal
if $tied 1 11 12
273 12 ▶ $self->_deleted(1) if $deleted;
Branch: 1 branch
BranchTrueFalseTotal
if $deleted 1 11 12
274 }
275  
276 sub STORABLE_freeze {
277 4571 ▶ my ( $self, $cloning ) = @_;
Subroutine STORABLE_freeze: called CC=5 SCAR=16.1
Pod undocumented
278  
279 return (
280 4571 ▶ $self->_pack,
Branch: 4 branches
BranchTrueFalseTotal
$self->has_data ? : 4569 2 4571
$self->has_backend_data ? : 2 4569 4571
$self->has_class_meta ? : 27 4544 4571
$self->has_class_version ? : 100 4471 4571
281 [
282 ( $self->has_data ? $self->data : undef ),
283 ( $self->has_backend_data ? $self->backend_data : undef ),
284 ( $self->has_class_meta ? $self->class_meta : undef ),
285 ( $self->has_class_version ? $self->class_version : undef ),
286 ],
287 );
288 }
289  
290 sub STORABLE_thaw {
291 11075 ▶ my ( $self, $cloning, $attrs, $refs ) = @_;
Subroutine STORABLE_thaw: called CC=6 SCAR=17.9
Pod undocumented
292  
293 11075 $self->_unpack($attrs);
294  
295 11075 ▶ if ( $refs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refs) 11075 0 11075
296 11075 my ( $data, $backend_data, $meta, $version ) = @$refs;
297 11075 ▶ $self->_data($data) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 11073 2 11075
298 11075 ▶ $self->backend_data($backend_data) if ref $backend_data;
Branch: 1 branch
BranchTrueFalseTotal
if ref $backend_data 2 11073 11075
299 11075 ▶ $self->_class_meta($meta) if ref $meta;
Branch: 1 branch
BranchTrueFalseTotal
if ref $meta 27 11048 11075
300 11075 ▶ $self->_class_version($version) if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 96 10979 11075
301 }
302 }
303  
304 27 __PACKAGE__->meta->make_immutable;
305  
306 27 __PACKAGE__
307  
308 __END__
309  
310 =pod
311  
312 =head1 NAME
313  
314 KiokuDB::Entry - An entry in the database
315  
316 =head1 SYNOPSIS
317  
318 KiokuDB::Entry->new(
319 id => ...,
320 data => ...
321 );
322  
323 =head1 DESCRIPTION
324  
325 This object provides the meta data for a single storage entry.
326  
327 =head1 ATTRIBUTES
328  
329 =over 4
330  
331 =item id
332  
333 The UUID for the entry.
334  
335 If there is no ID then the entry is intrinsic.
336  
337 =item root
338  
339 Whether or not this is a member of the root set (not subject to garbage
340 collection, because storage was explicitly requested).
341  
342 =item data
343  
344 A simplified data structure modeling this object/reference. This is a tree, not
345 a graph, and has no shared data (JSON compliant). All references are symbolic,
346 using a L<KiokuDB::Reference> object with UIDs as the
347 address space.
348  
349 =item class
350  
351 If the entry is blessed, this contains the class of that object.
352  
353 In the future this might be a complex structure for anonymous classes, e.g. the
354 class and the runtime roles.
355  
356 =item class_meta
357  
358 Optional information such as runtime roles to be applied to the object is
359 stored in this hashref.
360  
361 =item tied
362  
363 One of C<HASH>, C<ARRAY>, C<SCALAR> or C<GLOB>.
364  
365 C<data> is assumed to be a reference or an intrinsic entry for the object
366 driving the tied structure (e.g. the C<tied(%hash)>).
367  
368 =item prev
369  
370 Contains a link to a L<KiokuDB::Entry> objects that precedes this one.
371  
372 The last entry that was loaded from the store, or successfully written to the
373 store for a given UUID is kept in the live object set.
374  
375 The collapser creates transient Entry objects, which if written to the store
376 successfully replace the previous one.
377  
378 =item backend_data
379  
380 Backends can use this to store additional meta data as they see fit.
381  
382 For instance, this is used in the CouchDB backend to track entry revisions for
383 the opportunistic locking, and in L<KiokuDB::Backend::BDB::GIN> to to store
384 extracted keys.
385  
386 =item deleted
387  
388 Used for marking entries for deletion.
389  
390 Deletion entries can be generated using the C<deletion_entry> method, which
391 creates a new derived entry with no data but retaining the ID.
392  
393 =back
394  
395 =cut
« KiokuDB/LinkChecker.pm ↑ Summary KiokuDB/Test/Fixture.pm »