blib/lib/Yote.pm

filter: Statementstmt 15.0% 180 / 1194 Branchbran 0.4% 2 / 456 Conditioncond 1.0% 1 / 100 MC/DCmcdc 0.0% 0 / 64 Subroutinesub 33.7% 57 / 169 Podpod 100% 1 / 1 totaltotal 12.1% 241 / 1984 CC 292 SCAR 50.3
CC
292
Cov
10%
_stow
1056.0
_raw_data
462.0
_generate_keep_db
342.0
CRAP 153.5
↑ Summary Build »
1 package Yote;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 $VERSION = '2.02';
10  
11 =head1 NAME
12  
13 Yote - Persistant Perl container objects in a directed graph of lazilly loaded nodes.
14  
15 =head1 DESCRIPTION
16  
17 This is for anyone who wants to store arbitrary structured state data and doesn't have
18 the time or inclination to write a schema or configure some framework. This can be used
19 orthagonally to any other storage system.
20  
21 Yote only loads data as it needs too. It does not load all stored containers at once.
22 Data is stored in a data directory and is stored using the Data::RecordStore module. A Yote
23 container is a key/value store where the values can be strings, numbers, arrays, hashes
24 or other Yote containers.
25  
26 The entry point for all Yote data stores is the root node. All objects in the store are
27 unreachable if they cannot trace a reference path back to this node. If they cannot, running
28 compress_store will remove them.
29  
30 There are lots of potential uses for Yote, and a few come to mind :
31  
32 * configuration data
33 * data modeling
34 * user preference data
35 * user account data
36 * game data
37 * shopping carts
38 * product information
39  
40 =head1 SYNOPSIS
41  
42 use Yote;
43  
44 my $store = Yote::open_store( '/path/to/data-directory' );
45  
46 my $root_node = $store->fetch_root;
47  
48 $root_node->add_to_myList( $store->newobj( {
49 someval => 123.53,
50 somehash => { A => 1 },
51 someobj => $store->newobj( { foo => "Bar" },
52 'Optional-Yote-Subclass-Package' );
53 } );
54  
55 # the root node now has a list 'myList' attached to it with the single
56 # value of a yote object that yote object has two fields,
57 # one of which is an other yote object.
58  
59 $root_node->add_to_myList( 42 );
60  
61 #
62 # New Yote container objects are created with $store->newobj. Note that
63 # they must find a reference path to the root to be protected from
64 # being deleted from the record store upon compression.
65 #
66 my $newObj = $store->newobj;
67  
68 $root_node->set_field( "Value" );
69  
70 my $val = $root_node->get_value( "default" );
71 # $val eq 'default'
72  
73 $val = $root_node->get_value( "Somethign Else" );
74 # $val eq 'default' (old value not overridden by a new default value)
75  
76  
77 my $otherval = $root_node->get( 'ot3rv@l', 'other default' );
78 # $otherval eq 'other default'
79  
80 $root_node->set( 'ot3rv@l', 'newy valuye' );
81 $otherval2 = $root_node->get( 'ot3rv@l', 'yet other default' );
82 # $otherval2 eq 'newy valuye'
83  
84 $root_node->set_value( "Something Else" );
85  
86 my $val = $root_node->get_value( "default" );
87 # $val eq 'Something Else'
88  
89 my $myList = $root_node->get_myList;
90  
91 for my $example (@$myList) {
92 print ">$example\n";
93 }
94  
95 #
96 # Each object gets a unique ID which can be used to fetch that
97 # object directly from the store.
98 #
99 my $someid = $root_node->get_someobj->{ID};
100  
101 my $someref = $store->fetch( $someid );
102  
103 #
104 # Even hashes and array have unique yote IDS. These can be
105 # determined by calling the _get_id method of the store.
106 #
107 my $hash = $root_node->set_ahash( { zoo => "Zar" } );
108 my $hash_id = $store->_get_id( $hash );
109 my $other_ref_to_hash = $store->fetch( $hash_id );
110  
111 #
112 # Anything that cannot trace a reference path to the root
113 # is eligable for being removed upon compression.
114 #
115  
116 =head1 PUBLIC METHODS
117  
118 =cut
119  
120  
121 =head2 open_store( '/path/to/directory' )
122  
123 Starts up a persistance engine and returns it.
124  
125 =cut
126  
127 sub open_store {
128 1 ▶ my $path = pop;
Subroutine open_store: called CC=1 SCAR=1.2
Pod documented
129 1 my $store = Yote::ObjStore->_new( { store => $path } );
130 0 $store->_init;
131 0 $store;
132 }
133  
134 # ---------------------------------------------------------------------------------------------------------------------
135  
136 package Yote::ObjStore;
137  
138 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 1 ▶no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
141 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 1 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
143  
144 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
146 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 1 ▶use Module::Loaded;
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 =head1 NAME
151  
152 Yote::ObjStore - manages Yote::Obj objects in a graph.
153  
154 =head1 DESCRIPTION
155  
156 The Yote::ObjStore does the following things :
157  
158 * fetches the root object
159 * creates new objects
160 * fetches existing objects by id
161 * saves all new or changed objects
162 * finds objects that cannot connect to the root node and removes them
163  
164 =cut
165  
166 # ------------------------------------------------------------------------------------------
167 # * PUBLIC CLASS METHODS *
168 # ------------------------------------------------------------------------------------------
169  
170 =head2 fetch_root
171  
172 Returns the root node of the graph. All things that can be
173 trace a reference path back to the root node are considered active
174 and are not removed when the object store is compressed.
175  
176 =cut
177 sub fetch_root {
178 0 ▶ my $self = shift;
Subroutine fetch_root: not called CC=3 SCAR=24.8
179 0 ▶ die "fetch_root must be called on Yote store object" unless ref( $self );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
180 0 my $root = $self->fetch( $self->_first_id );
181 0 ▶ unless( $root ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($root) 0 0 0
182 0 $root = $self->_newroot;
183 0 $root->{ID} = $self->_first_id;
184 0 $self->_stow( $root );
185 }
186 0 $root;
187 } #fetch_root
188  
189 =head2 newobj( { ... data .... }, optionalClass )
190  
191 Creates a container object initialized with the
192 incoming hash ref data. The class of the object must be either
193 Yote::Obj or a subclass of it. Yote::Obj is the default.
194  
195 Once created, the object will be saved in the data store when
196 $store->stow_all has been called. If the object is not attached
197 to the root or an object that can be reached by the root, it will be
198 remove when Yote::ObjStore::Compress is called.
199  
200 =cut
201 sub newobj {
202 0 ▶ my( $self, $data, $class ) = @_;
Subroutine newobj: not called CC=2 SCAR=17.9
203 0 ▶ $class ||= 'Yote::Obj';
Condition0%0/2
$class ||= "Yote::Obj"
l!l
00
MC/DC0%0/1
$class ||= "Yote::Obj"
$class
Truth table0%0/2
A
A= $class
Aresult
11
00
204 0 $class->_new( $self, $data );
205 }
206  
207 sub _newroot {
208 0 ▶ my $self = shift;
Subroutine _newroot: not called CC=1 SCAR=6.9
209 0 Yote::Obj->_new( $self, {}, $self->_first_id );
210 }
211  
212 =head2 copy_from_remote_store( $obj )
213  
214 This takes an object that belongs to a seperate store and makes
215 a deep copy of it.
216  
217 =cut
218 sub copy_from_remote_store {
219 0 ▶ my( $self, $obj ) = @_;
Subroutine copy_from_remote_store: not called CC=4 SCAR=30.0
220 0 my $r = ref( $obj );
221 0 ▶ return $obj unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
222 0 ▶ if( $r eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 0 0 0
elsif ($r eq 'HASH') { } 0 0 0
223 0 return [ map { $self->copy_from_remote_store($_) } @$obj ];
224 } elsif( $r eq 'HASH' ) {
225 0 return { map { $_ => $self->copy_from_remote_store($obj->{$_}) } keys %$obj };
226 } else {
227 0 my $data = { map { $_ => $self->copy_from_remote_store($obj->{DATA}{$_}) } keys %{$obj->{DATA}} };
228 0 return $self->newobj( $data, $r );
229 }
230 }
231  
232 =head2 cache_all()
233  
234 This turns on caching for the store. Any objects loaded will
235 remain cached until clear_cache is called. Normally, they
236 would be DESTROYed once their last reference was removed unless
237 they are in a state that needs stowing.
238  
239 =cut
240 sub cache_all {
241 0 ▶ my $self = shift;
Subroutine cache_all: not called CC=1 SCAR=6.9
242 0 $self->{CACHE_ALL} = 1;
243 }
244  
245 =head2 uncache( obj )
246  
247 This removes the object from the cache if it was in the cache
248  
249 =cut
250 sub uncache {
251 0 ▶ my( $self, $obj ) = @_;
Subroutine uncache: not called CC=2 SCAR=17.9
252 0 ▶ if( ref( $obj ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj) 0 0 0
253 0 delete $self->{CACHE}{$self->_get_id( $obj )};
254 }
255 }
256  
257  
258  
259 =head2 pause_cache()
260  
261 When called, no new objects will be added to the cache until
262 cache_all is called.
263  
264 =cut
265 sub pause_cache {
266 0 ▶ my $self = shift;
Subroutine pause_cache: not called CC=1 SCAR=6.9
267 0 $self->{CACHE_ALL} = 0;
268 }
269  
270 =head2 clear_cache()
271  
272 When called, this dumps the object cache. Objects that
273 references or have changes that need to be stowed will
274 not be cleared.
275  
276 =cut
277 sub clear_cache {
278 0 ▶ my $self = shift;
Subroutine clear_cache: not called CC=1 SCAR=6.9
279 0 $self->{_CACHE} = {};
280 }
281  
282  
283  
284 =head2 fetch( $id )
285  
286 Returns the object with the given id.
287  
288 =cut
289 sub fetch {
290 0 ▶ my( $self, $id ) = @_;
Subroutine fetch: not called CC=12 SCAR=50.5
291 0 ▶ return undef unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
292 #
293 # Return the object if we have a reference to its dirty state.
294 #
295 0 my $ref = $self->{_DIRTY}{$id};
296 0 ▶ if( defined $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) { } 0 0 0
297 0 return $ref;
298 } else {
299 0 $ref = $self->{_WEAK_REFS}{$id};
300 0 ▶ if( $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 0 0
301 0 return $ref;
302 }
303 0 undef $ref;
304 }
305 0 my $obj_arry = $self->{_DATASTORE}->_fetch( $id );
306  
307 0 ▶ if( $obj_arry ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj_arry) 0 0 0
308 0 my( $id, $class, $data ) = @$obj_arry;
309 0 ▶ if( $class eq 'ARRAY' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($class eq 'ARRAY') { } 0 0 0
elsif ($class eq 'Yote::ArrayGatekeeper') { } 0 0 0
elsif ($class eq 'HASH') { } 0 0 0
elsif ($class eq 'Yote::BigHash') { } 0 0 0
310 0 my( @arry );
311 0 tie @arry, 'Yote::Array', $self, $id, @$data;
312 0 $self->_store_weak( $id, \@arry );
313 0 return \@arry;
314 }
315 elsif( $class eq 'Yote::ArrayGatekeeper' ) {
316 0 my( @arry );
317 0 tie @arry, 'Yote::ArrayGatekeeper', $self, $id, @$data;
318 0 $self->_store_weak( $id, \@arry );
319 0 return \@arry;
320 }
321 elsif( $class eq 'HASH' ) {
322 0 my( %hash );
323 0 tie %hash, 'Yote::Hash', $self, $id, @$data;
324 0 $self->_store_weak( $id, \%hash );
325 0 return \%hash;
326 }
327 elsif( $class eq 'Yote::BigHash' ) {
328 0 my( %hash );
329 0 tie %hash, 'Yote::BigHash', $self, $id, @$data;
330 0 $self->_store_weak( $id, \%hash );
331 0 return \%hash;
332 }
333 else {
334 0 my $obj;
335 0 eval {
336 0 my $path = $class;
337 0 ▶ unless( $INC{ $class } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$class}) 0 0 0
338 0 eval("use $class");
339 }
340 0 ▶ $obj = $self->{_WEAK_REFS}{$id} || $class->_instantiate( $id, $self );
Condition0%0/3
$self->{'_WEAK_REFS'}{$id} || $class->_instantiate($id, $self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_WEAK_REFS'}{$id} || $class->_instantiate($id, $self)
$self->{'_WEAK_REFS'}{$id} $class->_instantiate($id, $self)
Truth table0%0/3
A || B
A= $self->{'_WEAK_REFS'}{$id}B= $class->_instantiate($id, $self)
ABresult
1X1
011
000
341 };
342 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
343 0 $obj->{DATA} = $data;
344 0 $obj->{ID} = $id;
345 0 $self->_store_weak( $id, $obj );
346 0 $obj->_load();
347 0 return $obj;
348 }
349 }
350 0 return undef;
351 } #fetch
352  
353 =head2 run_purger
354  
355 =cut
356 sub run_purger {
357 0 ▶ my( $self, $make_tally, $copy_only ) = @_;
Subroutine run_purger: not called CC=5 SCAR=34.0
358 0 $self->stow_all();
359  
360 0 my $keep_db = $self->{_DATASTORE}->_generate_keep_db();
361  
362 # analyze to see what percentage would be kept
363 0 my $total = $keep_db->entry_count;
364 0 my $keep = 0;
365 0 for my $tid (1..$total) {
366 0 my( $has_keep ) = $keep_db->get_record( $tid )->[0];
367 0 ▶ $keep++ if $has_keep;
Branch: 1 branch
BranchTrueFalseTotal
if $has_keep 0 0 0
368 }
369  
370 #
371 # If there are more things to keep than not, do a db purge,
372 # otherwise, rebuild the db.
373 #
374 0 ▶ my $do_purge = $keep > ( $total/2 ) && ! $copy_only;
Condition0%0/3
$keep > $total / 2 && !$copy_only
!ll&&!rl&&r
000
MC/DC0%0/2
$keep > $total / 2 && !$copy_only
$keep > $total / 2 !$copy_only
Truth table0%0/3
A && not(B)
A= $keep > $total / 2B= !$copy_only
ABresult
0X0
100
111
375 0 my $purged;
376 0 ▶ if( $do_purge ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_purge) { } 0 0 0
377 0 $purged = $self->{_DATASTORE}->_purge_objects( $keep_db, $make_tally );
378 0 $self->{_DATASTORE}->_update_recycle_ids( $keep_db );
379 0 $keep_db->unlink_store;
380 } else {
381 0 $purged = $self->_copy_active_ids( $keep_db );
382 # keep db is not copied over
383 }
384  
385 0 $purged;
386 } #run_purger
387  
388 sub _copy_active_ids {
389 0 ▶ my( $self, $copy_db ) = @_;
Subroutine _copy_active_ids: not called CC=10 SCAR=47.0
390 0 $self->stow_all();
391  
392 0 my $original_dir = $self->{args}{store};
393 0 my $backdir = $original_dir . '_COMPRESS_BACK_RECENT';
394 0 my $newdir = $original_dir . '_NEW_RECYC';
395  
396 0 ▶ if( -e $backdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $backdir) 0 0 0
397 0 my $oldback = $original_dir . '_COMPRESS_BACK_OLD';
398 0 ▶ if( -d $oldback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $oldback) 0 0 0
399 0 warn "Removing old compression backup directory";
400 0 remove_tree( $oldback );
401 }
402 0 ▶ move( $backdir, $oldback ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
move($backdir, $oldback) or die $! 0 0 0
403 }
404  
405 0 ▶ if( -x $newdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $newdir) 0 0 0
406 0 die "Unable to run compress store, temp directory '$newdir' already exists.";
407 }
408 0 my $newstore = Yote::ObjStore->_new( { store => $newdir } );
409  
410 0 my( @purges );
411 0 for my $keep_id ( 1..$copy_db->entry_count ) {
412  
413 0 my( $has_keep ) = $copy_db->get_record( $keep_id )->[0];
414 0 ▶ if( $has_keep ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($has_keep) { } 0 0 0
elsif ($self->{'_DATASTORE'}{'DATA_STORE'}->has_id($keep_id)) { } 0 0 0
415 0 my $obj = $self->fetch( $keep_id );
416  
417 0 $newstore->{_DATASTORE}{DATA_STORE}->ensure_entry_count( $keep_id - 1 );
418 0 $newstore->_dirty( $obj, $keep_id );
419 0 $newstore->_stow( $obj, $keep_id );
420 } elsif( $self->{_DATASTORE}{DATA_STORE}->has_id( $keep_id ) ) {
421 0 push @purges, $keep_id;
422 }
423 } #each entry id
424  
425 # reopen data store
426 0 $self->{_DATASTORE} = Yote::YoteDB->open( $self, $self->{args} );
427 0 ▶ move( $original_dir, $backdir ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
move($original_dir, $backdir) or die $! 0 0 0
428 0 ▶ move( $newdir, $original_dir ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
move($newdir, $original_dir) or die $! 0 0 0
429  
430 0 \@purges;
431  
432 } #_copy_active_ids
433  
434 =head2 has_id
435  
436 Returns true if there is a valid reference linked to the id
437  
438 =cut
439 sub has_id {
440 0 ▶ my( $self, $id ) = @_;
Subroutine has_id: not called CC=1 SCAR=6.9
441 0 return $self->{_DATASTORE}{DATA_STORE}->has_id( $id );
442 }
443  
444 =head2 stow_all
445  
446 Saves all newly created or dirty objects.
447  
448 =cut
449 sub stow_all {
450 0 ▶ my $self = shift;
Subroutine stow_all: not called CC=6 SCAR=37.4
451 0 my @odata;
452 0 for my $obj (values %{$self->{_DIRTY}} ) {
453 0 my $cls;
454 0 my $ref = ref( $obj );
455 0 ▶ if( $ref eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
456 0 ▶ $cls = ref(tied @$obj) eq 'Yote::ArrayGatekeeper' ? 'Yote::ArrayGatekeeper' : 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
ref tied @$obj eq 'Yote::ArrayGatekeeper' ? : 0 0 0
457 } elsif( $ref eq 'HASH' ) {
458 0 ▶ $cls = ref(tied %$obj) eq 'Yote::BigHash' ? 'Yote::BigHash' : 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref tied %$obj eq 'Yote::BigHash' ? : 0 0 0
459 } else {
460 0 $cls = $ref;
461 }
462 0 my( $text_rep ) = $self->_raw_data( $obj );
463 0 push( @odata, [ $self->_get_id( $obj ), $cls, $text_rep ] );
464 }
465 0 $self->{_DATASTORE}->_stow_all( \@odata );
466 0 $self->{_DIRTY} = {};
467 } #stow_all
468  
469  
470 =head2 stow( $obj )
471  
472 Saves that object to the database
473  
474 =cut
475 sub stow {
476 0 ▶ my( $self, $obj ) = @_;
Subroutine stow: not called CC=5 SCAR=34.0
477 0 my $cls;
478 0 my $ref = ref( $obj );
479 0 ▶ if( $ref eq 'ARRAY' || $ref eq 'Yote::Array' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY' or $ref eq 'Yote::Array') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
Condition0%0/3
$ref eq "ARRAY" or $ref eq "Yote::Array"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref eq "ARRAY" or $ref eq "Yote::Array"
$ref eq "ARRAY" $ref eq "Yote::Array"
Truth table0%0/3
A or B
A= $ref eq "ARRAY"B= $ref eq "Yote::Array"
ABresult
1X1
011
000
480 0 $cls = 'ARRAY';
481 } elsif( $ref eq 'HASH' ) {
482 0 ▶ $cls = ref(tied %$ref) eq 'Yote::BigHash' ? 'Yote::BigHash' : 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref tied %$ref eq 'Yote::BigHash' ? : 0 0 0
483 } else {
484 0 $cls = $ref;
485 }
486 0 my $id = $self->_get_id( $obj );
487 0 my( $text_rep ) = $self->_raw_data( $obj );
488 0 $self->{_DATASTORE}->_stow( $id, $cls, $text_rep );
489 0 delete $self->{_DIRTY}{$id};
490 } #stow
491  
492  
493  
494 # -------------------------------
495 # * PRIVATE METHODS *
496 # -------------------------------
497 sub _new { #Yote::ObjStore
498 1 ▶ my( $pkg, $args ) = @_;
Subroutine _new: called CC=1 SCAR=0.2
499 1 my $self = bless {
500 _DIRTY => {},
501 _WEAK_REFS => {},
502 args => $args,
503 }, $pkg;
504 1 $self->{_DATASTORE} = Yote::YoteDB->open( $self, $args );
505 0 $self;
506 } #_new
507  
508 sub _init {
509 0 ▶ my $self = shift;
Subroutine _init: not called CC=3 SCAR=24.8
510 0 for my $pkg ( qw( Yote::Obj Yote::Array Yote::Hash Yote::ArrayGatekeeper Yote::BigHash ) ) {
511 0 ▶ $INC{ $pkg } or eval("use $pkg");
Branch: 1 branch
BranchTrueFalseTotal
$INC{$pkg} or eval "use $pkg" 0 0 0
512 }
513 0 $self->fetch_root;
514 0 $self->stow_all;
515 0 $self;
516 }
517  
518  
519 sub dirty_count {
520 0 ▶ my $self = shift;
Subroutine dirty_count: not called CC=1 SCAR=6.9
521 0 return scalar( keys %{$self->{_DIRTY}} );
522 }
523  
524 #
525 # Markes given object as dirty.
526 #
527 sub _dirty {
528 # ( $self, $ref, $id
529 0 ▶ $_[0]->{_DIRTY}->{$_[2]} = $_[1];
Subroutine _dirty: not called CC=1 SCAR=6.9
530 } #_dirty
531  
532 #
533 # Returns the first ID that is associated with the root Root object
534 #
535 sub _first_id {
536 0 ▶ shift->{_DATASTORE}->_first_id();
Subroutine _first_id: not called CC=1 SCAR=6.9
537 } #_first_id
538  
539 sub _get_id {
540 # for debugging I think?
541 0 ▶ shift->__get_id( shift );
Subroutine _get_id: not called CC=1 SCAR=6.9
542 }
543  
544 sub __get_id {
545 0 ▶ my( $self, $ref ) = @_;
Subroutine __get_id: not called CC=17 SCAR=57.2
546  
547 0 my $class = ref( $ref );
548 0 ▶ die "__get_id requires reference. got '$ref'" unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
549  
550 0 ▶ if( $class eq 'Yote::Array') {
Branch: 4 branches
BranchTrueFalseTotal
if ($class eq 'Yote::Array') { } 0 0 0
elsif ($class eq 'ARRAY') { } 0 0 0
elsif ($class eq 'Yote::Hash' or $class eq 'Yote::BigHash') { } 0 0 0
elsif ($class eq 'HASH') { } 0 0 0
551 0 return $ref->[0];
552 }
553 elsif( $class eq 'ARRAY' ) {
554 0 my $tied = tied @$ref;
555 0 ▶ if( $tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied) 0 0 0
556 0 ▶ $tied->[0] ||= $self->{_DATASTORE}->_get_id( "ARRAY" );
Condition0%0/3
$tied->[0] ||= $self->{"_DATASTORE"}->_get_id("ARRAY")
l!l&&r!l&&!r
000
MC/DC0%0/2
$tied->[0] ||= $self->{"_DATASTORE"}->_get_id("ARRAY")
$tied->[0] $self->{"_DATASTORE"}->_get_id("ARRAY")
Truth table0%0/3
A ||= B
A= $tied->[0]B= $self->{"_DATASTORE"}->_get_id("ARRAY")
ABresult
1X1
011
000
557 0 return $tied->[0];
558 }
559 0 my( @data ) = @$ref;
560 0 my $id = $self->{_DATASTORE}->_get_id( $class );
561 0 tie @$ref, 'Yote::ArrayGatekeeper', $self, $id;
562 0 ▶ push( @$ref, @data ) if @data;
Branch: 1 branch
BranchTrueFalseTotal
if @data 0 0 0
563 0 $self->_dirty( $ref, $id );
564 0 $self->_store_weak( $id, $ref );
565 0 return $id;
566 }
567 ▶ elsif( $class eq 'Yote::Hash' || $class eq 'Yote::BigHash' ) {
Condition0%0/3
$class eq "Yote::Hash" or $class eq "Yote::BigHash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$class eq "Yote::Hash" or $class eq "Yote::BigHash"
$class eq "Yote::Hash" $class eq "Yote::BigHash"
Truth table0%0/3
A or B
A= $class eq "Yote::Hash"B= $class eq "Yote::BigHash"
ABresult
1X1
011
000
568 0 return $ref->[0];
569 }
570 elsif( $class eq 'HASH' ) {
571 0 my $tied = tied %$ref;
572 0 ▶ if( $tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied) { } 0 0 0
573 0 ▶ my $useclass = ref($tied) eq 'Yote::BigHash' ? 'Yote::BigHash' : 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref $tied eq 'Yote::BigHash' ? : 0 0 0
574 0 ▶ $tied->[0] ||= $self->{_DATASTORE}->_get_id( $useclass );
Condition0%0/3
$tied->[0] ||= $self->{"_DATASTORE"}->_get_id($useclass)
l!l&&r!l&&!r
000
MC/DC0%0/2
$tied->[0] ||= $self->{"_DATASTORE"}->_get_id($useclass)
$tied->[0] $self->{"_DATASTORE"}->_get_id($useclass)
Truth table0%0/3
A ||= B
A= $tied->[0]B= $self->{"_DATASTORE"}->_get_id($useclass)
ABresult
1X1
011
000
575 0 return $tied->[0];
576 } else {
577 0 $class = 'Yote::BigHash';
578 }
579 0 my $id = $self->{_DATASTORE}->_get_id( $class );
580  
581 0 my( %vals ) = %$ref;
582  
583 0 tie %$ref, 'Yote::BigHash', $self, $id;
584 0 for my $key (keys %vals) {
585 0 $ref->{$key} = $vals{$key};
586 }
587 0 $self->_dirty( $ref, $id );
588 0 $self->_store_weak( $id, $ref );
589 0 return $id;
590 }
591 else {
592 0 ▶ return $ref->{ID} if $ref->{ID};
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"ID"} 0 0 0
593 0 ▶ if( $class eq 'Yote::Root' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'Yote::Root') { } 0 0 0
594 0 $ref->{ID} = $self->{_DATASTORE}->_first_id( $class );
595 } else {
596 0 ▶ $ref->{ID} ||= $self->{_DATASTORE}->_get_id( $class );
Condition0%0/3
$ref->{"ID"} ||= $self->{"_DATASTORE"}->_get_id($class)
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref->{"ID"} ||= $self->{"_DATASTORE"}->_get_id($class)
$ref->{"ID"} $self->{"_DATASTORE"}->_get_id($class)
Truth table0%0/3
A ||= B
A= $ref->{"ID"}B= $self->{"_DATASTORE"}->_get_id($class)
ABresult
1X1
011
000
597 }
598 0 return $ref->{ID};
599 }
600  
601 } #_get_id
602  
603 sub _stow {
604 0 ▶ my( $self, $obj, $id ) = @_;
Subroutine _stow: not called CC=32 SCAR=69.6
605  
606 0 my $class = ref( $obj );
607 0 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
608 0 ▶ $id //= $self->_get_id( $obj );
Condition0%0/3
$id //= $self->_get_id($obj)
l!l&&r!l&&!r
000
MC/DC0%0/2
$id //= $self->_get_id($obj)
$id $self->_get_id($obj)
Truth table0%0/3
A //= B
A= $idB= $self->_get_id($obj)
ABresult
1X1
011
000
609 0 ▶ die unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
610  
611 0 my( $text_rep, $data ) = $self->_raw_data( $obj );
612  
613 0 ▶ if( $class eq 'ARRAY' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($class eq 'ARRAY') { } 0 0 0
elsif ($class eq 'HASH') { } 0 0 0
elsif ($class eq 'Yote::Array') { } 0 0 0
elsif ($class eq 'Yote::ArrayGatekeeper') { } 0 0 0
elsif ($class eq 'Yote::Hash') { } 0 0 0
elsif ($class eq 'Yote::BigHash') { } 0 0 0
614 0 ▶ my $useClass = ref( tied( @$obj ) ) eq 'Yote::ArrayGatekeeper' ? 'Yote::ArrayGatekeeper' : 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
ref tied @$obj eq 'Yote::ArrayGatekeeper' ? : 0 0 0
615 0 $self->{_DATASTORE}->_stow( $id,$useClass, $text_rep );
616 0 $self->_clean( $id );
617 }
618 elsif( $class eq 'HASH' ) {
619 0 ▶ my $useClass = ref( tied( %$obj ) ) eq 'Yote::BigHash' ? 'Yote::BigHash' : 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
ref tied %$obj eq 'Yote::BigHash' ? : 0 0 0
620 0 $self->{_DATASTORE}->_stow( $id,$useClass,$text_rep );
621 0 $self->_clean( $id );
622 }
623 elsif( $class eq 'Yote::Array' ) {
624 0 ▶ if( $self->_is_dirty( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_dirty($id)) 0 0 0
625 0 $self->{_DATASTORE}->_stow( $id,'ARRAY',$text_rep );
626 0 $self->_clean( $id );
627 }
628 0 for my $child (@$data) {
629 0 ▶ if( $child =~ /^[0-9]/ && $self->{_DIRTY}->{$child} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}) 0 0 0
Condition0%0/3
$child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}
!ll&&!rl&&r
000
MC/DC0%0/2
$child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}
$child =~ /^[0-9]/ $self->{"_DIRTY"}{$child}
Truth table0%0/3
A and B
A= $child =~ /^[0-9]/B= $self->{"_DIRTY"}{$child}
ABresult
0X0
100
111
630 0 $self->_stow( $child, $self->{_DIRTY}->{$child} );
631 }
632 }
633 }
634 elsif( $class eq 'Yote::ArrayGatekeeper' ) {
635 0 ▶ if( $self->_is_dirty( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_dirty($id)) 0 0 0
636 0 $self->{_DATASTORE}->_stow( $id,'Yote::ArrayGatekeeper',$text_rep );
637 0 $self->_clean( $id );
638 }
639 0 for my $child (@$data) {
640 0 ▶ if( $child =~ /^[0-9]/ && $self->{_DIRTY}->{$child} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}) 0 0 0
Condition0%0/3
$child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}
!ll&&!rl&&r
000
MC/DC0%0/2
$child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}
$child =~ /^[0-9]/ $self->{"_DIRTY"}{$child}
Truth table0%0/3
A and B
A= $child =~ /^[0-9]/B= $self->{"_DIRTY"}{$child}
ABresult
0X0
100
111
641 0 $self->_stow( $child, $self->{_DIRTY}->{$child} );
642 }
643 }
644 }
645 elsif( $class eq 'Yote::Hash' ) {
646 0 ▶ if( $self->_is_dirty( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_dirty($id)) 0 0 0
647 0 $self->{_DATASTORE}->_stow( $id, 'HASH', $text_rep );
648 }
649 0 $self->_clean( $id );
650 0 for my $child (values %$data) {
651 0 ▶ if( $child =~ /^[0-9]/ && $self->{_DIRTY}->{$child} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}) 0 0 0
Condition0%0/3
$child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}
!ll&&!rl&&r
000
MC/DC0%0/2
$child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}
$child =~ /^[0-9]/ $self->{"_DIRTY"}{$child}
Truth table0%0/3
A and B
A= $child =~ /^[0-9]/B= $self->{"_DIRTY"}{$child}
ABresult
0X0
100
111
652 0 $self->_stow( $child, $self->{_DIRTY}->{$child} );
653 }
654 }
655 }
656 elsif( $class eq 'Yote::BigHash' ) {
657 0 ▶ if( $self->_is_dirty( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_dirty($id)) 0 0 0
658 0 $self->{_DATASTORE}->_stow( $id, 'Yote::BigHash', $text_rep );
659 }
660 0 $self->_clean( $id );
661 0 for my $child (values %$data) {
662 0 ▶ if( $child =~ /^[0-9]/ && $self->{_DIRTY}->{$child} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}) 0 0 0
Condition0%0/3
$child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}
!ll&&!rl&&r
000
MC/DC0%0/2
$child =~ /^[0-9]/ and $self->{"_DIRTY"}{$child}
$child =~ /^[0-9]/ $self->{"_DIRTY"}{$child}
Truth table0%0/3
A and B
A= $child =~ /^[0-9]/B= $self->{"_DIRTY"}{$child}
ABresult
0X0
100
111
663 0 $self->_stow( $child, $self->{_DIRTY}->{$child} );
664 }
665 }
666 }
667 else {
668 0 ▶ if( $self->_is_dirty( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_dirty($id)) 0 0 0
669 0 $self->{_DATASTORE}->_stow( $id, $class, $text_rep );
670 0 $self->_clean( $id );
671 }
672 0 for my $val (values %$data) {
673 0 ▶ if( $val =~ /^[0-9]/ && $self->{_DIRTY}->{$val} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^[0-9]/ and $self->{"_DIRTY"}{$val}) 0 0 0
Condition0%0/3
$val =~ /^[0-9]/ and $self->{"_DIRTY"}{$val}
!ll&&!rl&&r
000
MC/DC0%0/2
$val =~ /^[0-9]/ and $self->{"_DIRTY"}{$val}
$val =~ /^[0-9]/ $self->{"_DIRTY"}{$val}
Truth table0%0/3
A and B
A= $val =~ /^[0-9]/B= $self->{"_DIRTY"}{$val}
ABresult
0X0
100
111
674 0 $self->_stow( $val, $self->{_DIRTY}->{$val} );
675 }
676 }
677 }
678 0 $id;
679 } #_stow
680  
681 sub _xform_in {
682 0 ▶ my( $self, $val ) = @_;
Subroutine _xform_in: not called CC=3 SCAR=24.8
683 0 ▶ if( ref( $val ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 0 0 0
684 0 return $self->_get_id( $val );
685 }
686 0 ▶ return defined $val ? "v$val" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
687 }
688  
689 sub _xform_out {
690 0 ▶ my( $self, $val ) = @_;
Subroutine _xform_out: not called CC=3 SCAR=24.8
691 0 ▶ return undef unless defined( $val );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
692 0 ▶ if( index($val,'v') == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($val, "v") == 0) 0 0 0
693 0 return substr( $val, 1 );
694 }
695 0 return $self->fetch( $val );
696 }
697  
698 sub _clean {
699 0 ▶ my( $self, $id ) = @_;
Subroutine _clean: not called CC=1 SCAR=6.9
700 0 delete $self->{_DIRTY}{$id};
701 } #_clean
702  
703 sub _is_dirty {
704 0 ▶ my( $self, $obj ) = @_;
Subroutine _is_dirty: not called CC=2 SCAR=17.9
705 0 ▶ my $id = ref($obj) ? _get_id($obj) : $obj;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj ? : 0 0 0
706 0 my $ans = $self->{_DIRTY}{$id};
707 0 $ans;
708 } #_is_dirty
709  
710 #
711 # Returns data structure representing object. References are integers. Values start with 'v'.
712 #
713 sub _raw_data {
714 0 ▶ my( $self, $obj ) = @_;
Subroutine _raw_data: not called CC=21 SCAR=61.4
715 0 my $class = ref( $obj );
716 0 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
717 0 my $id = $self->_get_id( $obj );
718 0 ▶ die unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
719 # TODO : clean this up nice
720 0 my( $r, $is_array, $is_hash, $hash_type, $tied );
721 0 ▶ if( $class eq 'ARRAY' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($class eq 'ARRAY') { } 0 0 0
elsif ($class eq 'HASH') { } 0 0 0
elsif ($class eq 'Yote::Array') { } 0 0 0
elsif ($class eq 'Yote::ArrayGatekeeper') { } 0 0 0
elsif ($class eq 'Yote::Hash') { } 0 0 0
elsif ($class eq 'Yote::BigHash') { } 0 0 0
722 0 $tied = tied @$obj;
723 0 ▶ if( $tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied) { } 0 0 0
724 0 $r = $tied->[1];
725 0 $is_array = ref( $tied );
726 } else {
727 0 die;
728 }
729 }
730 elsif( $class eq 'HASH' ) {
731 0 my $tied = tied %$obj;
732 0 ▶ if( ref( $tied ) eq 'Yote::BigHash' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $tied eq 'Yote::BigHash') { } 0 0 0
elsif (ref $tied eq 'Yote::Hash') { } 0 0 0
733 0 $r = $tied->[1];
734 0 $is_hash = $tied->[6];
735 0 $hash_type = $tied->[8];
736 } elsif( ref( $tied ) eq 'Yote::Hash' ) {
737 # is okey, the old hash is around for backwards compatability
738 } else {
739 0 die;
740 }
741 }
742 elsif( $class eq 'Yote::Array' ) {
743 0 $r = $obj->[1];
744 0 $is_array = 'Yote::Array';
745 }
746 elsif( $class eq 'Yote::ArrayGatekeeper' ) {
747 0 $tied = $obj;
748 0 $r = $obj->[1];
749 0 $is_array = 'Yote::ArrayGatekeeper';
750 }
751 elsif( $class eq 'Yote::Hash' ) {
752 # not seeing is_hash for the old hashes as there is no extra data for them
753 0 $r = $obj->[1];
754 }
755 elsif( $class eq 'Yote::BigHash' ) {
756 0 $r = $obj->[1];
757 0 $is_hash = $obj->[6];
758 0 $hash_type = $obj->[8];
759 }
760 else {
761 0 $r = $obj->{DATA};
762 }
763  
764 0 ▶ if( $is_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hash) 0 0 0
765 0 ▶ if( $hash_type eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_type eq "S") 0 0 0
766 0 ▶ return join( "`", $hash_type, $is_hash, map { if( defined($_) ) { s/[\\]/\\\\/gs; s/`/\\`/gs; } $_ } %$r ), $r;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
767 }
768 0 ▶ return join( "`", $hash_type, $is_hash, map { if( defined($_) ) { s/[\\]/\\\\/gs; s/`/\\`/gs; } $_ } @$r ), $r;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
769 }
770 0 ▶ if( $is_array eq 'Yote::ArrayGatekeeper' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_array eq 'Yote::ArrayGatekeeper') { } 0 0 0
elsif ($is_array) { } 0 0 0
771 0 ▶ return join( "`", $tied->[6],$tied->[4],$tied->[5],$tied->[7], map { if( defined($_) ) { s/[\\]/\\\\/gs; s/`/\\`/gs; } $_ } @$r ), $r;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
772 } elsif( $is_array ) {
773 0 ▶ return join( "`", map { if( defined($_) ) { s/[\\]/\\\\/gs; s/`/\\`/gs; } $_ } @$r ), $r;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
774 }
775 0 ▶ return join( "`", map { if( defined($_) ) { s/[\\]/\\\\/gs; s/`/\\`/gs; } $_ } %$r ), $r;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
776  
777 } #_raw_data
778  
779  
780 sub _store_weak {
781 0 ▶ my( $self, $id, $ref ) = @_;
Subroutine _store_weak: not called CC=2 SCAR=17.9
782 0 ▶ die unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 0 0
783 0 $self->{_WEAK_REFS}{$id} = $ref;
784  
785 0 weaken( $self->{_WEAK_REFS}{$id} );
786 } #_store_weak
787  
788 # ---------------------------------------------------------------------------------------------------------------------
789  
790 =head1 NAME
791  
792 Yote::Obj - Generic container object for graph.
793  
794 =head1 DESCRIPTION
795  
796 A Yote::Obj is a container class that as a specific idiom for getters
797 and setters. This idiom is set up to avoid confusion and collision
798 with any method names.
799  
800 # sets the 'foo' field to the given value.
801 $obj->set_foo( { value => $store->newobj } );
802  
803 # returns the value for bar, and if none, sets it to 'default'
804 my $bar = $obj->get_bar( "default" );
805  
806 $obj->add_to_somelist( "Freddish" );
807 my $list = $obj->get_somelist;
808 $list->[ 0 ] == "Freddish";
809  
810  
811 $obj->remove_from_somelist( "Freddish" );
812  
813 =cut
814 package Yote::Obj;
815  
816 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
817 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
818 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
819  
820 #
821 # The string version of the yote object is simply its id. This allows
822 # objet ids to easily be stored as hash keys.
823 #
824 use overload
825 0 ▶ '""' => sub { shift->{ID} }, # for hash keys
Subroutine __ANON__: not called CC=1 SCAR=6.9
826 0 ▶ eq => sub { ref($_[1]) && $_[1]->{ID} == $_[0]->{ID} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and $_[1]{"ID"} == $_[0]{"ID"} 0 0 0
827 0 ▶ ne => sub { ! ref($_[1]) || $_[1]->{ID} != $_[0]->{ID} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[1] or $_[1]{"ID"} != $_[0]{"ID"} 0 0 0
828 0 ▶ '==' => sub { ref($_[1]) && $_[1]->{ID} == $_[0]->{ID} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and $_[1]{"ID"} == $_[0]{"ID"} 0 0 0
829 0 ▶ '!=' => sub { ! ref($_[1]) || $_[1]->{ID} != $_[0]->{ID} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[1] or $_[1]{"ID"} != $_[0]{"ID"} 0 0 0
830 1 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
831  
832 =head2 absorb( hashref )
833  
834 pulls the hash data into this object.
835  
836 =cut
837 sub absorb {
838 0 ▶ my( $self, $data ) = @_;
Subroutine absorb: not called CC=2 SCAR=17.9
839 0 my $obj_store = $self->{STORE};
840 0 for my $key ( sort keys %$data ) {
841 0 my $item = $data->{ $key };
842 0 $self->{DATA}{$key} = $obj_store->_xform_in( $item );
843 }
844 0 $obj_store->_dirty( $self, $self->{ID} );
845  
846 } #absorb
847  
848 sub id {
849 0 ▶ shift->{ID};
Subroutine id: not called CC=1 SCAR=6.9
850 }
851  
852 =head2 set( $field, $value )
853  
854 Assigns the given value to the field in this object and returns the
855 assigned value.
856  
857 =cut
858 sub set {
859 0 ▶ my( $self, $fld, $val ) = @_;
Subroutine set: not called CC=3 SCAR=24.8
860  
861 0 my $inval = $self->{STORE}->_xform_in( $val );
862 0 ▶ if( $self->{DATA}{$fld} ne $inval ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DATA"}{$fld} ne $inval) 0 0 0
863 0 $self->{STORE}->_dirty( $self, $self->{ID} );
864 }
865  
866 0 ▶ unless( defined $inval ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inval) 0 0 0
867 0 delete $self->{DATA}{$fld};
868 0 return;
869 }
870 0 $self->{DATA}{$fld} = $inval;
871 0 return $self->{STORE}->_xform_out( $self->{DATA}{$fld} );
872 } #set
873  
874  
875 =head2 get( $field, $default-value )
876  
877 Returns the value assigned to the field, assinging the default
878 value to it if the value is currently not defined.
879  
880 =cut
881 sub get {
882 0 ▶ my( $self, $fld, $default ) = @_;
Subroutine get: not called CC=4 SCAR=30.0
883 0 my $cur = $self->{DATA}{$fld};
884 0 my $store = $self->{STORE};
885 0 ▶ if( ! defined( $cur ) && defined( $default ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cur and defined $default) 0 0 0
Condition0%0/3
not defined $cur and defined $default
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $cur and defined $default
not defined $cur defined $default
Truth table0%0/3
not(A) and B
A= not defined $curB= defined $default
ABresult
0X0
100
111
886 0 ▶ if( ref( $default ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default) 0 0 0
887 # this must be done to make sure the reference is saved for cases where the reference has not yet made it to the store of things to save
888 0 $store->_dirty( $store->_get_id( $default ) );
889 }
890 0 $store->_dirty( $self, $self->{ID} );
891 0 $self->{DATA}{$fld} = $store->_xform_in( $default );
892 }
893 0 return $store->_xform_out( $self->{DATA}{$fld} );
894 } #get
895  
896  
897 # -----------------------
898 #
899 # Public Methods
900 # -----------------------
901 #
902 # Defines get_foo, set_foo, add_to_list, remove_from_list
903 #
904 sub AUTOLOAD {
905 0 ▶ my( $s, $arg ) = @_;
Subroutine AUTOLOAD: not called CC=7 SCAR=33.3
906 0 my $func = our $AUTOLOAD;
907  
908 0 ▶ if( $func =~/:add_to_(.*)/ ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($func =~ /:add_to_(.*)/) { } 0 0 0
elsif ($func =~ /:add_once_to_(.*)/) { } 0 0 0
elsif ($func =~ /:remove_from_(.*)/) { } 0 0 0
elsif ($func =~ /:remove_all_from_(.*)/) { } 0 0 0
elsif ($func =~ /:set_(.*)/) { } 0 0 0
elsif ($func =~ /:get_(.*)/) { } 0 0 0
909 0 my( $fld ) = $1;
910 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
911 *$AUTOLOAD = sub {
912 0 ▶ my( $self, @vals ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
913 0 my $get = "get_$fld";
914 0 my $arry = $self->$get([]); # init array if need be
915 0 push( @$arry, @vals );
916 0 };
917 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
918 0 goto &$AUTOLOAD;
919 } #add_to
920 elsif( $func =~/:add_once_to_(.*)/ ) {
921 0 my( $fld ) = $1;
922 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
923 *$AUTOLOAD = sub {
924 0 ▶ my( $self, @vals ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
925 0 my $get = "get_$fld";
926 0 my $arry = $self->$get([]); # init array if need be
927 0 for my $val ( @vals ) {
928 0 ▶ unless( grep { $val eq $_ } @$arry ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$val eq $_;} @$arry) 0 0 0
929 0 push @$arry, $val;
930 }
931 }
932 0 };
933 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
934 0 goto &$AUTOLOAD;
935 } #add_once_to
936 elsif( $func =~ /:remove_from_(.*)/ ) { #removes the first instance of the target thing from the list
937 0 my $fld = $1;
938 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
939 *$AUTOLOAD = sub {
940 0 ▶ my( $self, @vals ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
941 0 my $get = "get_$fld";
942 0 my $arry = $self->$get([]); # init array if need be
943 0 for my $val (@vals ) {
944 0 for my $i (0..$#$arry) {
945 0 ▶ if( $arry->[$i] eq $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arry->[$i] eq $val) 0 0 0
946 0 splice @$arry, $i, 1;
947 0 last;
948 }
949 }
950 }
951 0 };
952 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
953 0 goto &$AUTOLOAD;
954 }
955 elsif( $func =~ /:remove_all_from_(.*)/ ) { #removes the first instance of the target thing from the list
956 0 my $fld = $1;
957 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
958 *$AUTOLOAD = sub {
959 0 ▶ my( $self, @vals ) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
960 0 my $get = "get_$fld";
961 0 my $arry = $self->$get([]); # init array if need be
962 0 for my $val (@vals) {
963 0 my $count = grep { $_ eq $val } @$arry;
964 0 ▶ while( $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
965 0 for my $i (0..$#$arry) {
966 0 ▶ if( $arry->[$i] eq $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arry->[$i] eq $val) 0 0 0
967 0 --$count;
968 0 splice @$arry, $i, 1;
969 0 ▶ last unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 0 0
970 }
971 }
972 }
973 }
974 0 };
975 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
976 0 goto &$AUTOLOAD;
977 }
978 elsif ( $func =~ /:set_(.*)/ ) {
979 0 my $fld = $1;
980 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
981 *$AUTOLOAD = sub {
982 0 ▶ my( $self, $val ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
983 0 my $inval = $self->{STORE}->_xform_in( $val );
984 0 ▶ $self->{STORE}->_dirty( $self, $self->{ID} ) if $self->{DATA}{$fld} ne $inval;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DATA"}{$fld} ne $inval 0 0 0
985 0 ▶ unless( defined $inval ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inval) 0 0 0
986 0 delete $self->{DATA}{$fld};
987 0 return;
988 }
989 0 $self->{DATA}{$fld} = $inval;
990 0 return $self->{STORE}->_xform_out( $self->{DATA}{$fld} );
991 0 };
992 0 goto &$AUTOLOAD;
993 }
994 elsif( $func =~ /:get_(.*)/ ) {
995 0 my $fld = $1;
996 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
997 *$AUTOLOAD = sub {
998 0 ▶ my( $self, $init_val ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
999 0 ▶ if( ! defined( $self->{DATA}{$fld} ) && defined($init_val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"DATA"}{$fld} and defined $init_val) 0 0 0
Condition0%0/3
not defined $self->{"DATA"}{$fld} and defined $init_val
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self->{"DATA"}{$fld} and defined $init_val
not defined $self->{"DATA"}{$fld} defined $init_val
Truth table0%0/3
not(A) and B
A= not defined $self->{"DATA"}{$fld}B= defined $init_val
ABresult
0X0
100
111
1000 0 ▶ if( ref( $init_val ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $init_val) 0 0 0
1001 # this must be done to make sure the reference is saved for cases where the reference has not yet made it to the store of things to save
1002 0 $self->{STORE}->_dirty( $init_val, $self->{STORE}->_get_id( $init_val ) );
1003 }
1004 0 $self->{STORE}->_dirty( $self, $self->{ID} );
1005 0 $self->{DATA}{$fld} = $self->{STORE}->_xform_in( $init_val );
1006 }
1007 0 return $self->{STORE}->_xform_out( $self->{DATA}{$fld} );
1008 0 };
1009 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1010 0 goto &$AUTOLOAD;
1011 }
1012 else {
1013 0 die "Unknown Yote::Obj function '$func'";
1014 }
1015  
1016 } #AUTOLOAD
1017  
1018 # -----------------------
1019 #
1020 # Overridable Methods
1021 # -----------------------
1022  
1023 =head2 _init
1024  
1025 This is called the first time an object is created. It is not
1026 called when the object is loaded from storage. This can be used
1027 to set up defaults. This is meant to be overridden.
1028  
1029 =cut
1030 ▶sub _init {}
Subroutine _init: not called CC=1 SCAR=0.0
1031  
1032 =head2 _init
1033  
1034 This is called each time the object is loaded from the data store.
1035 This is meant to be overridden.
1036  
1037 =cut
1038 ▶sub _load {}
Subroutine _load: not called CC=1 SCAR=0.0
1039  
1040  
1041  
1042 # -----------------------
1043 #
1044 # Private Methods
1045 #
1046 # -----------------------
1047  
1048  
1049 sub _new { #new Yote::Obj
1050 0 ▶ my( $pkg, $obj_store, $data, $_id ) = @_;
Subroutine _new: not called CC=5 SCAR=34.0
1051  
1052 0 ▶ my $class = ref($pkg) || $pkg;
Condition0%0/3
ref $pkg || $pkg
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table0%0/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
1053 0 my $obj = bless {
1054 DATA => {},
1055 STORE => $obj_store,
1056 }, $class;
1057 0 ▶ $obj->{ID} = $_id || $obj_store->_get_id( $obj );
Condition0%0/3
$_id || $obj_store->_get_id($obj)
l!l&&r!l&&!r
000
MC/DC0%0/2
$_id || $obj_store->_get_id($obj)
$_id $obj_store->_get_id($obj)
Truth table0%0/3
A || B
A= $_idB= $obj_store->_get_id($obj)
ABresult
1X1
011
000
1058 0 $obj_store->_dirty( $obj, $obj->{ID} );
1059 0 $obj->_init(); #called the first time the object is created.
1060  
1061 0 ▶ if( ref( $data ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 0 0 0
elsif ($data) { } 0 0 0
1062 0 $obj->absorb( $data );
1063 } elsif( $data ) {
1064 0 die "Yote::Obj::new must be called with hash or undef. Was called with '". ref( $data ) . "'";
1065 }
1066 0 return $obj;
1067 } #_new
1068  
1069 sub _store {
1070 0 ▶ return shift->{STORE};
Subroutine _store: not called CC=1 SCAR=6.9
1071 }
1072  
1073 #
1074 # Called by the object provider; returns a Yote::Obj the object
1075 # provider will stuff data into. Takes the class and id as arguments.
1076 #
1077 sub _instantiate {
1078 0 ▶ bless { ID => $_[1], DATA => {}, STORE => $_[2] }, $_[0];
Subroutine _instantiate: not called CC=1 SCAR=6.9
1079 } #_instantiate
1080  
1081 sub DESTROY {
1082 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
1083  
1084 0 delete $self->{STORE}{_WEAK_REFS}{$self->{ID}};
1085 }
1086  
1087  
1088 # ---------------------------------------------------------------------------------------------------------------------
1089  
1090 package Yote::Array;
1091  
1092 ############################################################################################################
1093 # This module is used transparently by Yote to link arrays into its graph structure. This is not meant to #
1094 # be called explicitly or modified. #
1095 ############################################################################################################
1096  
1097 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1098 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1099  
1100 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1101 1 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
1102  
1103 sub TIEARRAY {
1104 0 ▶ my( $class, $obj_store, $id, @list ) = @_;
Subroutine TIEARRAY: not called CC=2 SCAR=17.9
1105 0 my $storage = [];
1106  
1107 # once the array is tied, an additional data field will be added
1108 # so obj will be [ $id, $storage, $obj_store ]
1109 0 my $obj = bless [$id,$storage,$obj_store], $class;
1110 0 for my $item (@list) {
1111 0 push( @$storage, $item );
1112 }
1113 0 return $obj;
1114 }
1115  
1116 sub FETCH {
1117 0 ▶ my( $self, $idx ) = @_;
Subroutine FETCH: not called CC=1 SCAR=6.9
1118 0 return $self->[2]->_xform_out ( $self->[1][$idx] );
1119 }
1120  
1121 sub FETCHSIZE {
1122 0 ▶ my $self = shift;
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
1123 0 return scalar(@{$self->[1]});
1124 }
1125  
1126 sub STORE {
1127 0 ▶ my( $self, $idx, $val ) = @_;
Subroutine STORE: not called CC=1 SCAR=6.9
1128 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1129 0 $self->[1][$idx] = $self->[2]->_xform_in( $val );
1130 }
1131 sub STORESIZE {
1132 0 ▶ my( $self, $size ) = @_;
Subroutine STORESIZE: not called CC=1 SCAR=6.9
1133 0 my $aref = $self->[1];
1134 0 $#$aref = $size-1;
1135 }
1136  
1137 sub EXISTS {
1138 0 ▶ my( $self, $idx ) = @_;
Subroutine EXISTS: not called CC=1 SCAR=6.9
1139 0 return defined( $self->[1][$idx] );
1140 }
1141 sub DELETE {
1142 0 ▶ my( $self, $idx ) = @_;
Subroutine DELETE: not called CC=1 SCAR=6.9
1143 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1144 0 delete $self->[1][$idx];
1145 }
1146  
1147 sub CLEAR {
1148 0 ▶ my $self = shift;
Subroutine CLEAR: not called CC=1 SCAR=6.9
1149 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1150 0 @{$self->[1]} = ();
1151 }
1152 sub PUSH {
1153 0 ▶ my( $self, @vals ) = @_;
Subroutine PUSH: not called CC=1 SCAR=6.9
1154 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1155 0 push( @{$self->[1]}, map { $self->[2]->_xform_in($_) } @vals );
1156 }
1157 sub POP {
1158 0 ▶ my $self = shift;
Subroutine POP: not called CC=1 SCAR=6.9
1159 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1160 0 return $self->[2]->_xform_out( pop @{$self->[1]} );
1161 }
1162 sub SHIFT {
1163 0 ▶ my( $self ) = @_;
Subroutine SHIFT: not called CC=1 SCAR=6.9
1164 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1165 0 my $val = splice @{$self->[1]}, 0, 1;
1166 0 return $self->[2]->_xform_out( $val );
1167 }
1168 sub UNSHIFT {
1169 0 ▶ my( $self, @vals ) = @_;
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
1170 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1171 0 unshift @{$self->[1]}, map {$self->[2]->_xform_in($_)} @vals;
1172 }
1173 sub SPLICE {
1174 0 ▶ my( $self, $offset, $length, @vals ) = @_;
Subroutine SPLICE: not called CC=1 SCAR=6.9
1175 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1176 0 return map { $self->[2]->_xform_out($_) } splice @{$self->[1]}, $offset, $length, map {$self->[2]->_xform_in($_)} @vals;
1177 }
1178 ▶sub EXTEND {}
Subroutine EXTEND: not called CC=1 SCAR=0.0
1179  
1180 sub DESTROY {
1181 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
1182 0 delete $self->[2]->{_WEAK_REFS}{$self->[0]};
1183 }
1184  
1185 # ---------------------------------------------------------------------------------------------------------------------
1186  
1187 package Yote::ArrayGatekeeper;
1188  
1189 ############################################################################################################
1190 # This module is used transparently by Yote to link arrays into its graph structure. This is not meant to #
1191 # be called explicitly or modified. #
1192 ############################################################################################################
1193  
1194 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1195 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1196  
1197 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1198 1 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
1199  
1200 1 $Yote::ArrayGatekeeper::BLOCK_SIZE = 1024;
1201 1 $Yote::ArrayGatekeeper::BLOCK_COUNT = 1024;
1202  
1203 use constant {
1204 1 ID => 0,
1205 BLOCKS => 1,
1206 DSTORE => 2,
1207 CAPACITY => 3,
1208 BLOCK_COUNT => 4,
1209 BLOCK_SIZE => 5,
1210 ITEM_COUNT => 6,
1211 LEVEL => 7,
1212 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
1213  
1214 sub TIEARRAY {
1215 0 ▶ my( $class, $obj_store, $id, $item_count, $block_count, $block_size, $level, @list ) = @_;
Subroutine TIEARRAY: not called CC=5 SCAR=34.0
1216  
1217 0 ▶ $block_size ||= $Yote::ArrayGatekeeper::BLOCK_SIZE;
Condition0%0/3
$block_size ||= $Yote::ArrayGatekeeper::BLOCK_SIZE
l!l&&r!l&&!r
000
MC/DC0%0/2
$block_size ||= $Yote::ArrayGatekeeper::BLOCK_SIZE
$block_size $Yote::ArrayGatekeeper::BLOCK_SIZE
Truth table0%0/3
A ||= B
A= $block_sizeB= $Yote::ArrayGatekeeper::BLOCK_SIZE
ABresult
1X1
011
000
1218 0 ▶ $block_count ||= $Yote::ArrayGatekeeper::BLOCK_COUNT;
Condition0%0/3
$block_count ||= $Yote::ArrayGatekeeper::BLOCK_COUNT
l!l&&r!l&&!r
000
MC/DC0%0/2
$block_count ||= $Yote::ArrayGatekeeper::BLOCK_COUNT
$block_count $Yote::ArrayGatekeeper::BLOCK_COUNT
Truth table0%0/3
A ||= B
A= $block_countB= $Yote::ArrayGatekeeper::BLOCK_COUNT
ABresult
1X1
011
000
1219 0 ▶ $item_count ||= 0;
Condition0%0/2
$item_count ||= 0
l!l
00
MC/DC0%0/1
$item_count ||= 0
$item_count
Truth table0%0/2
A
A= $item_count
Aresult
11
00
1220 0 ▶ $level ||= 1;
Condition0%0/2
$level ||= 1
l!l
00
MC/DC0%0/1
$level ||= 1
$level
Truth table0%0/2
A
A= $level
Aresult
11
00
1221 0 my $capacity = $block_size * $block_count;
1222  
1223 0 my $blocks = [@list];
1224  
1225 # once the array is tied, an additional data field will be added
1226 # so obj will be [ $id, $storage, $obj_store ]
1227 0 my $obj = bless [$id,$blocks,$obj_store,$capacity,$block_count,$block_size, $item_count, $level], $class;
1228 0 return $obj;
1229 } #TIEARRAY
1230  
1231 sub _ensure_capacity {
1232 0 ▶ my( $self, $size ) = @_;
Subroutine _ensure_capacity: not called CC=2 SCAR=17.9
1233  
1234 0 ▶ if( $size > $self->[CAPACITY] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size > $self->[3]) 0 0 0
1235 0 my $store = $self->[DSTORE];
1236 #
1237 # make a new gatekeeper and moves the buckets of this
1238 # one into the new gatekeeper
1239 #
1240 0 my $new_id = $store->{_DATASTORE}->_get_id( 'Yote::ArrayGatekeeper' );
1241  
1242 0 my $newkeeper = [];
1243 0 tie @$newkeeper, 'Yote::ArrayGatekeeper', $store, $new_id, $self->[ITEM_COUNT], $self->[BLOCK_COUNT], $self->[BLOCK_SIZE], $self->[LEVEL], @{$self->[BLOCKS]};
1244  
1245 0 $store->_store_weak( $new_id, $newkeeper );
1246 0 $store->_dirty( $store->{_WEAK_REFS}{$new_id}, $new_id );
1247  
1248 0 $self->[LEVEL]++;
1249 0 $self->[BLOCKS] = [ $new_id ];
1250 0 $self->[BLOCK_SIZE] = $self->[CAPACITY];
1251 0 $self->[CAPACITY] = $self->[BLOCK_SIZE] * $self->[BLOCK_COUNT];
1252 0 $store->_dirty( $store->{_WEAK_REFS}{$self->[ID]}, $self->[ID] );
1253 }
1254  
1255 } #_ensure_capacity
1256  
1257 sub _dirty {
1258 0 ▶ my $self = shift;
Subroutine _dirty: not called CC=1 SCAR=6.9
1259 0 $self->[DSTORE]->_dirty( $self->[DSTORE]{_WEAK_REFS}{$self->[ID]}, $self->[ID] );
1260 }
1261  
1262 # returns the block object ( behind the tied ), the block index
1263 # and the first item index in the block
1264 sub _block {
1265 0 ▶ my( $self, $idx ) = @_;
Subroutine _block: not called CC=3 SCAR=24.8
1266 0 $self->_ensure_capacity( $idx + 1 );
1267  
1268  
1269 0 my $block_idx = int($idx / $self->[BLOCK_SIZE]); #block size
1270  
1271 0 my $store = $self->[DSTORE];
1272  
1273 0 my $block_id = $self->[BLOCKS][$block_idx];
1274 0 my $block;
1275 0 ▶ if( $block_id ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($block_id) { } 0 0 0
elsif ($self->[7] == 1) { } 0 0 0
1276 0 $block = $store->fetch($block_id);
1277 } elsif( $self->[LEVEL] == 1 ) {
1278 0 $block = [];
1279 0 my $block_id = $store->{_DATASTORE}->_get_id( "ARRAY" );
1280 0 tie @$block, 'Yote::Array', $store, $block_id;
1281 0 $store->_store_weak( $block_id, $block );
1282 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$block_id}, $block_id );
1283  
1284 0 $self->[BLOCKS][$block_idx] = $block_id;
1285 0 $self->_dirty;
1286 } else {
1287 0 my $firstblock = tied( @{$store->fetch($self->[BLOCKS][0])} );
1288  
1289 0 $block = [];
1290 0 my $block_id = $store->{_DATASTORE}->_get_id( "Yote::ArrayGatekeeper" );
1291 0 tie @$block, 'Yote::ArrayGatekeeper', $store, $block_id, 0, $firstblock->[BLOCK_COUNT], $firstblock->[BLOCK_SIZE], $firstblock->[LEVEL];
1292 0 $store->_store_weak( $block_id, $block );
1293 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$block_id}, $block_id );
1294  
1295 0 $self->[BLOCKS][$block_idx] = $block_id;
1296 0 $self->_dirty;
1297 }
1298 0 return ( $block, tied( @$block), $block_idx, $block_idx * $self->[BLOCK_SIZE] );
1299 }
1300  
1301 sub FETCH {
1302 0 ▶ my( $self, $idx ) = @_;
Subroutine FETCH: not called CC=1 SCAR=6.9
1303 0 my( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( $idx );
1304 0 return $tied_block->FETCH( $idx - $block_start_idx );
1305 }
1306  
1307 sub FETCHSIZE {
1308 0 ▶ shift->[ITEM_COUNT];
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
1309 }
1310  
1311 sub STORE {
1312 0 ▶ my( $self, $idx, $val ) = @_;
Subroutine STORE: not called CC=2 SCAR=17.9
1313 0 my( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( $idx );
1314 0 my $last_block_idx = $self->[ITEM_COUNT] - 1;
1315 0 $tied_block->STORE( $idx - $block_start_idx, $val );
1316 0 ▶ if( $idx > $last_block_idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx > $last_block_idx) 0 0 0
1317 0 $self->[ITEM_COUNT] = $idx + 1;
1318 }
1319 }
1320 sub STORESIZE {
1321 0 ▶ my( $self, $size ) = @_;
Subroutine STORESIZE: not called CC=2 SCAR=17.9
1322 # fixes the size of the array
1323 0 ▶ if( $size < $self->[ITEM_COUNT] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size < $self->[6]) 0 0 0
1324 0 my( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( $self->[BLOCK_SIZE] * int($size/$self->[BLOCK_SIZE]) );
1325 0 my $blocks = $self->[BLOCKS];
1326 0 $#$blocks = $block_idx; #removes further blocks
1327 0 $tied_block->STORESIZE( $size - $block_start_idx );
1328 0 $self->_dirty;
1329 0 $self->[ITEM_COUNT] = $size;
1330 }
1331 }
1332  
1333 sub EXISTS {
1334 0 ▶ my( $self, $idx ) = @_;
Subroutine EXISTS: not called CC=1 SCAR=6.9
1335 0 my( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( $idx );
1336 0 return $tied_block->EXISTS( $idx - $block_start_idx );
1337 }
1338 sub DELETE {
1339 0 ▶ my( $self, $idx ) = @_;
Subroutine DELETE: not called CC=5 SCAR=34.0
1340 0 my( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( $idx );
1341  
1342 0 ▶ if( (1+$idx) == $self->[ITEM_COUNT] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 + $idx == $self->[6]) 0 0 0
1343 # in this case, it is removing the last item here. so shorten the
1344 # array until the
1345 0 my $curr_block_start_idx = $block_start_idx;
1346 0 my $blocks = $self->[BLOCKS];
1347 0 my $prev_idx = $idx - 1;
1348 0 ▶ while( ! $self->EXISTS( $prev_idx ) && $prev_idx >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->EXISTS($prev_idx) and $prev_idx >= 0) 0 0 0
Condition0%0/3
not $self->EXISTS($prev_idx) and $prev_idx >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->EXISTS($prev_idx) and $prev_idx >= 0
not $self->EXISTS($prev_idx) $prev_idx >= 0
Truth table0%0/3
not(A) and B
A= not $self->EXISTS($prev_idx)B= $prev_idx >= 0
ABresult
0X0
100
111
1349 0 ▶ if( $curr_block_start_idx == $prev_idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_block_start_idx == $prev_idx) 0 0 0
1350 0 pop @$blocks;
1351 }
1352 0 $self->[ITEM_COUNT]--;
1353 0 $prev_idx--;
1354 }
1355 0 $self->_dirty;
1356 }
1357 0 return $tied_block->DELETE( $idx - $block_start_idx );
1358 }
1359  
1360 sub CLEAR {
1361 0 ▶ my $self = shift;
Subroutine CLEAR: not called CC=1 SCAR=6.9
1362 0 $self->_dirty;
1363 0 $self->[ITEM_COUNT] = 0;
1364 0 @{$self->[BLOCKS]} = ();
1365 }
1366 sub PUSH {
1367 0 ▶ my( $self, @vals ) = @_;
Subroutine PUSH: not called CC=4 SCAR=30.0
1368  
1369 0 ▶ return unless @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 0 0 0
1370  
1371 0 $self->_ensure_capacity( $self->[ITEM_COUNT] + @vals );
1372  
1373 0 my( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( $self->[ITEM_COUNT] );
1374  
1375 0 $self->[ITEM_COUNT] += @vals;
1376 0 $self->_dirty;
1377 0 my $idx_at = 0;
1378 0 ▶ while( @vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 0 0 0
1379  
1380 0 $idx_at += $self->[BLOCK_SIZE];
1381 0 my $room = $self->[BLOCK_SIZE] - ($tied_block->FETCHSIZE);
1382 0 my( @part ) = splice @vals, 0, $room;
1383 0 $tied_block->PUSH( @part );
1384 0 ▶ if( @vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 0 0 0
1385 0 ($block,$tied_block,undef,undef) = $self->_block( $idx_at );
1386 }
1387 }
1388 }
1389 sub POP {
1390 0 ▶ my $self = shift;
Subroutine POP: not called CC=3 SCAR=24.8
1391  
1392 0 my $blocks = $self->[BLOCKS];
1393 0 ▶ if( @$blocks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$blocks) 0 0 0
1394 0 my $lastblock = tied @{$self->[DSTORE]->fetch($blocks->[$#$blocks])};
1395 0 my $val = $lastblock->POP;
1396 0 ▶ if( @$lastblock == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lastblock == 0) 0 0 0
1397 0 pop @$blocks;
1398 }
1399 0 $self->[ITEM_COUNT]--;
1400 0 $self->_dirty;
1401 0 return $val;
1402 }
1403 0 return;
1404 }
1405 sub SHIFT {
1406 0 ▶ my( $self ) = @_;
Subroutine SHIFT: not called CC=4 SCAR=30.0
1407 0 my $blocks = $self->[1];
1408 0 my $store = $self->[DSTORE];
1409 0 ▶ if( @$blocks ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$blocks) 0 0 0
1410 0 my $block = tied( @{$store->fetch( $blocks->[0] )} );
1411 0 my $val = $block->SHIFT;
1412 0 ▶ for( my $i=1; $i<@$blocks; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$blocks) 0 0 0
1413 0 my $now_block = tied( @{$store->fetch( $blocks->[$i] )} );
1414 0 my $prev_block = tied( @{$store->fetch( $blocks->[$i-1] )} );
1415 0 $prev_block->PUSH( $now_block->SHIFT );
1416 0 ▶ if( $#$blocks == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks->$#* == -1) 0 0 0
1417 0 pop @$blocks;
1418 0 last;
1419 }
1420 }
1421 0 $self->[ITEM_COUNT]--;
1422 0 $self->_dirty;
1423 0 return $val;
1424 }
1425 0 return;
1426 }
1427  
1428 sub UNSHIFT {
1429 0 ▶ shift->_unshift(0,@_);
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
1430 }
1431  
1432 sub _unshift {
1433 0 ▶ my( $self, $offset, @vals ) = @_;
Subroutine _unshift: not called CC=8 SCAR=42.8
1434  
1435 0 ▶ return unless @vals;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals 0 0 0
1436  
1437 0 my $newcount = $self->[ITEM_COUNT] + @vals;
1438 0 $self->_ensure_capacity( $newcount );
1439  
1440 0 my( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( $offset );
1441  
1442 0 ▶ if( $self->[LEVEL] == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[7] == 1) { } 0 0 0
1443 0 ▶ while( @vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 0 0 0
1444 0 $tied_block->SPLICE( $offset , 0, @vals );
1445 0 $offset = 0;
1446  
1447 0 ▶ if( $#$block >= $self->[BLOCK_SIZE] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->$#* >= $self->[5]) { } 0 0 0
1448 0 (@vals) = @$block[$self->[BLOCK_SIZE]..$#$block];
1449 0 $#$block = $self->[BLOCK_SIZE] - 1;
1450  
1451 0 ( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( (1+$block_idx)*$self->[BLOCK_SIZE] );
1452  
1453 } else {
1454 0 @vals = ();
1455 }
1456 }
1457 } else {
1458 0 ▶ while( @vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 0 0 0
1459 0 my $overflow = (@vals + $tied_block->[ITEM_COUNT]) - $self->[BLOCK_SIZE];
1460 0 ▶ if( $overflow > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overflow > 0) { } 0 0 0
1461 0 my $cut_index = $self->[BLOCK_SIZE] - @vals;
1462 0 my( @backlog ) = @$block[$cut_index..$#$block];
1463  
1464 0 $tied_block->STORESIZE( $cut_index );
1465  
1466 0 my @additions = splice @vals, 0, (1+$cut_index);
1467  
1468 0 $tied_block->_unshift( $offset, @additions ); #offset is only on the first block
1469 0 $offset = 0;
1470 0 push @vals, @backlog;
1471  
1472 0 ▶ if( @vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 0 0 0
1473 0 ( $block, $tied_block, $block_idx, $block_start_idx ) = $self->_block( (1+$block_idx)*$self->[BLOCK_SIZE] );
1474 }
1475 } else {
1476 0 $tied_block->SPLICE( $offset, 0, @vals );
1477 0 @vals = ();
1478 }
1479 }
1480 }
1481  
1482 0 $self->[ITEM_COUNT] = $newcount;
1483 0 $self->_dirty;
1484  
1485 } #UNSHIFT
1486  
1487 sub SPLICE {
1488 0 ▶ my( $self, $offset, $remove_length, @vals ) = @_;
Subroutine SPLICE: not called CC=8 SCAR=42.8
1489  
1490 0 ▶ return unless @vals || $remove_length;
Branch: 1 branch
BranchTrueFalseTotal
unless @vals or $remove_length 0 0 0
Condition0%0/3
@vals or $remove_length
l!l&&r!l&&!r
000
MC/DC0%0/2
@vals or $remove_length
@vals $remove_length
Truth table0%0/3
A or B
A= @valsB= $remove_length
ABresult
1X1
011
000
1491  
1492 0 my @removed;
1493  
1494 0 my $delta = @vals - $remove_length;
1495 0 my $new_size = $self->[ITEM_COUNT] + $delta;
1496  
1497  
1498 0 ▶ if( $delta > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 0) 0 0 0
1499 0 $self->_ensure_capacity( $new_size );
1500 }
1501  
1502 #
1503 # add things
1504 #
1505  
1506 0 ▶ if( @vals ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) 0 0 0
1507 # this adjusts the item count
1508 0 $self->_unshift( $offset, @vals );
1509 }
1510  
1511 0 ▶ if( $remove_length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove_length > 0) 0 0 0
1512 #
1513 # remove things by shifting things down over the removed items
1514 #
1515 0 my $remove_start_idx = $offset + @vals;
1516 0 my $remove_end_idx = $remove_start_idx + $remove_length;
1517 0 my $new_last_idx = $new_size - 1;
1518  
1519 0 ▶ while ( $remove_start_idx <= $new_last_idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove_start_idx <= $new_last_idx) 0 0 0
1520
1521 0 my( $remstart ) = $self->_block( $remove_start_idx );
1522 0 my( $remend ) = $self->_block( $remove_end_idx + 1 );
1523  
1524 0 my $removed = $self->FETCH( $remove_start_idx );
1525 0 ▶ if( @removed < $remove_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@removed < $remove_length) 0 0 0
1526 0 push @removed, $removed;
1527 }
1528 0 $self->STORE( $remove_start_idx, $self->FETCH( $remove_end_idx ) );
1529 0 $remove_start_idx++;
1530 0 $remove_end_idx++;
1531 }
1532 }
1533 #
1534 # Trim this array to its new size
1535 #
1536 0 $self->STORESIZE( $new_size );
1537  
1538 0 return @removed;
1539  
1540 } #SPLICE
1541  
1542 ▶sub EXTEND {
Subroutine EXTEND: not called CC=1 SCAR=0.0
1543 }
1544  
1545 sub DESTROY {
1546 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
1547 0 delete $self->[2]->{_WEAK_REFS}{$self->[0]};
1548 }
1549  
1550 # ---------------------------------------------------------------------------------------
1551  
1552 package Yote::Hash;
1553  
1554 ######################################################################################
1555 # This module is used transparently by Yote to link hashes into its graph structure. #
1556 # This is not meant to be called explicitly or modified. #
1557 ######################################################################################
1558  
1559 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1560 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1561  
1562 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1563  
1564 1 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
1565  
1566 sub TIEHASH {
1567 0 ▶ my( $class, $obj_store, $id, %hash ) = @_;
Subroutine TIEHASH: not called CC=2 SCAR=17.9
1568  
1569 0 my $storage = {};
1570 # after $obj_store is a list reference of
1571 # id, data, store
1572 0 my $obj = bless [ $id, $storage,$obj_store ], $class;
1573 0 for my $key (keys %hash) {
1574 0 $storage->{$key} = $hash{$key};
1575 }
1576 0 return $obj;
1577 }
1578  
1579 sub STORE {
1580 0 ▶ my( $self, $key, $val ) = @_;
Subroutine STORE: not called CC=1 SCAR=6.9
1581 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1582 0 $self->[1]{$key} = $self->[2]->_xform_in( $val );
1583 }
1584  
1585 sub FIRSTKEY {
1586 0 ▶ my $self = shift;
Subroutine FIRSTKEY: not called CC=2 SCAR=17.9
1587 0 my $a = scalar keys %{$self->[1]};
1588 0 my( $k, $val ) = each %{$self->[1]};
1589 0 ▶ return wantarray ? ( $k => $val ) : $k;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1590 }
1591 sub NEXTKEY {
1592 0 ▶ my $self = shift;
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
1593 0 my( $k, $val ) = each %{$self->[1]};
1594 0 ▶ return wantarray ? ( $k => $val ) : $k;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1595 }
1596  
1597 sub FETCH {
1598 0 ▶ my( $self, $key ) = @_;
Subroutine FETCH: not called CC=1 SCAR=6.9
1599 0 return $self->[2]->_xform_out( $self->[1]{$key} );
1600 }
1601  
1602 sub EXISTS {
1603 0 ▶ my( $self, $key ) = @_;
Subroutine EXISTS: not called CC=1 SCAR=6.9
1604 0 return defined( $self->[1]{$key} );
1605 }
1606 sub DELETE {
1607 0 ▶ my( $self, $key ) = @_;
Subroutine DELETE: not called CC=1 SCAR=6.9
1608 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1609 0 return delete $self->[1]{$key};
1610 }
1611 sub CLEAR {
1612 0 ▶ my $self = shift;
Subroutine CLEAR: not called CC=1 SCAR=6.9
1613 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1614 0 %{$self->[1]} = ();
1615 }
1616  
1617 sub DESTROY {
1618 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
1619 0 delete $self->[2]->{_WEAK_REFS}{$self->[0]};
1620 }
1621  
1622  
1623 # ---------------------------------------------------------------------------------------
1624  
1625 package Yote::BigHash;
1626  
1627 ######################################################################################
1628 # This module is used transparently by Yote to link hashes into its graph structure. #
1629 # This is not meant to be called explicitly or modified. #
1630 ######################################################################################
1631  
1632 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1633 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1634  
1635 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1636 1 ▶no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
1637  
1638 1 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
1639  
1640 1 $Yote::BigHash::SIZE = 977;
1641  
1642 use constant {
1643 1 ID => 0,
1644 DATA => 1,
1645 DSTORE => 2,
1646 NEXT => 3,
1647 KEYS => 4,
1648 DEEP => 5,
1649 SIZE => 6,
1650 THRESH => 7,
1651 TYPE => 8,
1652 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
1653  
1654 sub _bucket {
1655 0 ▶ my( $self, $key, $return_undef ) = @_;
Subroutine _bucket: not called CC=4 SCAR=30.0
1656 0 my $hval = 0;
1657 0 foreach (split //, $key) {
1658 0 $hval = $hval*33 - ord($_);
1659 }
1660  
1661 0 $hval = $hval % $self->[SIZE];
1662 0 my $obj_id = $self->[DATA][$hval];
1663 0 my $store = $self->[DSTORE];
1664 0 ▶ unless( $obj_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj_id) 0 0 0
1665 0 ▶ return ($hval, undef) if $return_undef;
Branch: 1 branch
BranchTrueFalseTotal
if $return_undef 0 0 0
1666 0 my $bucket = [];
1667 0 my $id = $store->_get_id( $bucket );
1668 0 $self->[DATA][$hval] = $id;
1669 0 return $hval, $bucket;
1670 }
1671 0 $hval, $store->_xform_out( $obj_id );
1672 }
1673  
1674 sub TIEHASH {
1675 0 ▶ my( $class, $obj_store, $id, $type, $size, @fetch_buckets ) = @_;
Subroutine TIEHASH: not called CC=7 SCAR=37.2
1676  
1677 0 ▶ $type ||= 'S'; #small
Condition0%0/2
$type ||= "S"
l!l
00
MC/DC0%0/1
$type ||= "S"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
1678 0 ▶ $size ||= $Yote::BigHash::SIZE;
Condition0%0/3
$size ||= $Yote::BigHash::SIZE
l!l&&r!l&&!r
000
MC/DC0%0/2
$size ||= $Yote::BigHash::SIZE
$size $Yote::BigHash::SIZE
Truth table0%0/3
A ||= B
A= $sizeB= $Yote::BigHash::SIZE
ABresult
1X1
011
000
1679  
1680 my $deep_buckets = $type eq 'B' ?
1681 0 ▶ [ map { $_ > 0 && ref( $obj_store->_xform_out($_) ) eq 'HASH' ? 1 : 0 } @fetch_buckets ] : [];
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'B' ? : 0 0 0
$_ > 0 && ref $obj_store->_xform_out($_) eq 'HASH' ? : 0 0 0
Condition0%0/3
$_ > 0 and ref $obj_store->_xform_out($_) eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$_ > 0 and ref $obj_store->_xform_out($_) eq "HASH"
$_ > 0 ref $obj_store->_xform_out($_) eq "HASH"
Truth table0%0/3
A and B
A= $_ > 0B= ref $obj_store->_xform_out($_) eq "HASH"
ABresult
0X0
100
111
1682  
1683 1 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
1684 #
1685 # after $obj_store is a list reference of
1686 # id, data, store
1687 0 my $obj;
1688 0 ▶ if( $type eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'S') { } 0 0 0
1689 0 $obj = bless [ $id, {@fetch_buckets}, $obj_store, [], 0, $deep_buckets, $size, $size * 2, $type ], $class;
1690 }
1691 else {
1692 0 $obj = bless [ $id, [@fetch_buckets], $obj_store, [], 0, $deep_buckets, $size, $size * 2, $type ], $class;
1693 }
1694  
1695 0 return $obj;
1696 }
1697  
1698 sub STORE {
1699 0 ▶ my( $self, $key, $val ) = @_;
Subroutine STORE: not called CC=12 SCAR=50.5
1700  
1701 0 my $store = $self->[DSTORE];
1702 0 $store->_dirty( $store->{_WEAK_REFS}{$self->[ID]}, $self->[ID] );
1703  
1704 0 ▶ if( $self->[TYPE] eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[8] eq "S") 0 0 0
1705 0 my $data = $self->[DATA];
1706 0 ▶ $self->[KEYS]++ unless exists $data->{$key}; #obj count
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$key} 0 0 0
1707 0 $data->{$key} = $store->_xform_in($val);
1708  
1709 0 ▶ if( $self->[KEYS] > $self->[THRESH] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[4] > $self->[7]) 0 0 0
1710 0 $self->[TYPE] = 'B'; #big
1711 #convert to buckets
1712 0 $self->[DATA] = [];
1713 0 for my $key (keys %$data) {
1714 0 $self->STORE( $key, $store->_xform_out($data->{$key}) );
1715 }
1716 }
1717 0 return;
1718 }
1719  
1720 0 my( $bid, $bucket ) = $self->_bucket( $key );
1721  
1722 0 ▶ if( $self->[DEEP][$bid] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5][$bid]) { } 0 0 0
1723 0 ▶ $self->[KEYS]++ unless exists $bucket->{$key}; #obj count
Branch: 1 branch
BranchTrueFalseTotal
unless exists $bucket->{$key} 0 0 0
1724 0 $bucket->{$key} = $val;
1725 } else {
1726 0 ▶ if( @$bucket > $self->[THRESH] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$bucket > $self->[7]) 0 0 0
1727 0 my $newbuck = {};
1728 0 my $id = $store->_get_id( $newbuck );
1729  
1730 0 my $tied = tied %$newbuck;
1731 0 $tied->[SIZE] = $self->[SIZE] * 2;
1732 0 $tied->[THRESH] = $tied->[SIZE] * 2;
1733 0 ▶ for( my $i=0; $i<$#$bucket; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bucket->$#*) 0 0 0
1734 0 $newbuck->{$bucket->[$i]} = $bucket->[$i+1];
1735 }
1736 0 ▶ $self->[KEYS]++ unless exists $newbuck->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $newbuck->{$key} 0 0 0
1737 0 $newbuck->{$key} = $val;
1738 0 $self->[DEEP][$bid] = 1;
1739 0 $self->[DATA][$bid] = $id;
1740 0 return;
1741 }
1742 0 ▶ for( my $i=0; $i<$#$bucket; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bucket->$#*) 0 0 0
1743 0 ▶ if( $bucket->[$i] eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket->[$i] eq $key) 0 0 0
1744 0 $bucket->[$i+1] = $val;
1745 0 return;
1746 }
1747 }
1748 0 $self->[KEYS]++; #obj count
1749 0 push @$bucket, $key, $val;
1750 }
1751 } #STORE
1752  
1753 sub FIRSTKEY {
1754 0 ▶ my $self = shift;
Subroutine FIRSTKEY: not called CC=3 SCAR=24.8
1755  
1756 0 ▶ if( $self->[TYPE] eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[8] eq "S") 0 0 0
1757 0 my $a = scalar keys %{$self->[DATA]}; #reset things
1758 0 my( $k, $val ) = each %{$self->[DATA]};
1759 0 ▶ return wantarray ? ( $k => $val ) : $k;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1760 }
1761 0 @{ $self->[NEXT] } = ( 0, undef, undef );
1762 0 return $self->NEXTKEY;
1763 }
1764  
1765 sub NEXTKEY {
1766 0 ▶ my $self = shift;
Subroutine NEXTKEY: not called CC=13 SCAR=52.0
1767  
1768 0 ▶ if( $self->[TYPE] eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[8] eq "S") 0 0 0
1769 0 my( $k, $val ) = each %{$self->[DATA]};
1770 0 ▶ return wantarray ? ( $k, $val ) : $k;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1771 }
1772  
1773 0 my $buckets = $self->[DATA];
1774 0 my $store = $self->[DSTORE];
1775 0 my $current = $self->[NEXT];
1776  
1777 0 my( $bucket_idx, $idx_in_bucket ) = @$current;
1778  
1779 0 ▶ for( my $bid = $bucket_idx; $bid < @$buckets; $bid++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bid < @$buckets) 0 0 0
1780 0 ▶ my $bucket = defined( $bid ) ? $store->_xform_out($buckets->[$bid]) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $bid ? : 0 0 0
1781 0 ▶ if( $bucket ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket) 0 0 0
1782 0 ▶ if( $self->[DEEP][$bid] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5][$bid]) { } 0 0 0
1783 0 my $tied = tied %$bucket;
1784 0 ▶ my $key = defined( $idx_in_bucket) ? $tied->NEXTKEY : $tied->FIRSTKEY;
Branch: 1 branch
BranchTrueFalseTotal
defined $idx_in_bucket ? : 0 0 0
1785 0 ▶ if( defined($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
1786 # the bucket must be there to keep a weak reference
1787 # to itself. If it was not here, it would load from
1788 # the filesystem each call to NEXTKEY
1789 0 @$current = ( $bid, 0, $bucket );
1790 0 ▶ return wantarray ? ( $key => $bucket->{$key} ): $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1791 }
1792 } else {
1793 0 ▶ if( $idx_in_bucket < $#$bucket ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx_in_bucket < $bucket->$#*) 0 0 0
1794 0 @$current = ( $bid, $idx_in_bucket + 2, undef );
1795 0 ▶ my $key = $bucket->[$idx_in_bucket||0];
Condition0%0/2
$idx_in_bucket || 0
l!l
00
MC/DC0%0/1
$idx_in_bucket || 0
$idx_in_bucket
Truth table0%0/2
A
A= $idx_in_bucket
Aresult
11
00
1796 0 ▶ return wantarray ? ( $key => $bucket->[$idx_in_bucket+1] ) : $key;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1797 }
1798 }
1799 0 undef $bucket;
1800 }
1801 0 undef $idx_in_bucket;
1802 }
1803 0 @$current = ( 0, undef, undef );
1804 0 return undef;
1805  
1806 } #NEXTKEY
1807  
1808 sub FETCH {
1809 0 ▶ my( $self, $key ) = @_;
Subroutine FETCH: not called CC=5 SCAR=34.0
1810 0 ▶ if( $self->[TYPE] eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[8] eq "S") 0 0 0
1811 0 my $x = $self->[DSTORE]->_xform_out($self->[DATA]{$key});
1812 0 return $self->[DSTORE]->_xform_out( $self->[DATA]{$key} );
1813 }
1814 0 my( $bid, $bucket ) = $self->_bucket( $key );
1815 0 ▶ if( $self->[DEEP][$bid] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5][$bid]) { } 0 0 0
1816 0 return $bucket->{$key};
1817 } else {
1818 0 ▶ for( my $i=0; $i<$#$bucket; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bucket->$#*) 0 0 0
1819 0 ▶ if( $bucket->[$i] eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket->[$i] eq $key) 0 0 0
1820 0 return $bucket->[$i+1];
1821 }
1822 }
1823 }
1824 } #FETCH
1825  
1826 sub EXISTS {
1827 0 ▶ my( $self, $key ) = @_;
Subroutine EXISTS: not called CC=5 SCAR=34.0
1828 0 ▶ if( $self->[TYPE] eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[8] eq "S") 0 0 0
1829 0 return exists $self->[DATA]{$key};
1830 }
1831  
1832 0 my( $bid, $bucket ) = $self->_bucket( $key );
1833 0 ▶ if( $self->[DEEP][$bid] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5][$bid]) { } 0 0 0
1834 0 return exists $bucket->{$key};
1835 } else {
1836 0 ▶ for( my $i=0; $i<$#$bucket; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bucket->$#*) 0 0 0
1837 0 ▶ if( $bucket->[$i] eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket->[$i] eq $key) 0 0 0
1838 0 return 1;
1839 }
1840 }
1841 }
1842 0 return 0;
1843 } #EXISTS
1844  
1845 sub DELETE {
1846 0 ▶ my( $self, $key ) = @_;
Subroutine DELETE: not called CC=8 SCAR=42.8
1847  
1848 0 my $store = $self->[DSTORE];
1849  
1850 0 ▶ if( $self->[TYPE] eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[8] eq "S") 0 0 0
1851 0 ▶ if( exists $self->[DATA]{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[1]{$key}) 0 0 0
1852 0 $store->_dirty( $store->{_WEAK_REFS}{$self->[ID]}, $self->[ID] );
1853 0 $self->[KEYS]--;
1854 0 delete $self->[DATA]{$key};
1855 }
1856 0 return;
1857 }
1858  
1859 0 my( $bid, $bucket ) = $self->_bucket( $key, 'return_undef' );
1860 0 ▶ return 0 unless $bucket;
Branch: 1 branch
BranchTrueFalseTotal
unless $bucket 0 0 0
1861  
1862 # TODO - see if the buckets revert back to arrays
1863 0 ▶ if( $self->[DEEP][$bid] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5][$bid]) { } 0 0 0
1864 0 $store->_dirty( $store->{_WEAK_REFS}{$self->[ID]}, $self->[ID] );
1865 0 ▶ $self->[KEYS]-- if exists $bucket->{$key}; #obj count
Branch: 1 branch
BranchTrueFalseTotal
if exists $bucket->{$key} 0 0 0
1866 0 return delete $bucket->{$key};
1867 } else {
1868 0 ▶ for( my $i=0; $i<$#$bucket; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $bucket->$#*) 0 0 0
1869 0 ▶ if( $bucket->[$i] eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bucket->[$i] eq $key) 0 0 0
1870 0 splice @$bucket, $i, 2;
1871 0 $store->_dirty( $store->{_WEAK_REFS}{$self->[ID]}, $self->[ID] );
1872 0 $self->[KEYS]--; #obj count
1873 0 return 1;
1874 }
1875 }
1876 }
1877 0 return 0;
1878 } #DELETE
1879  
1880 sub CLEAR {
1881 0 ▶ my $self = shift;
Subroutine CLEAR: not called CC=4 SCAR=30.0
1882 0 my $store = $self->[DSTORE];
1883 0 $self->[KEYS] = 0;
1884 0 $store->_dirty( $store->{_WEAK_REFS}{$self->[ID]}, $self->[ID] );
1885  
1886 0 ▶ if( $self->[TYPE] eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[8] eq "S") 0 0 0
1887 0 $self->[DATA] = {};
1888 }
1889  
1890 0 my $buckets = $self->[DATA];
1891 0 ▶ for( my $bid=0; $bid<@$buckets; $bid++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bid < @$buckets) 0 0 0
1892 0 ▶ if( $self->[DEEP][$bid] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5][$bid]) { } 0 0 0
1893 0 my $buck = tied %{$buckets->[$bid]};
1894 0 $buck->CLEAR;
1895 } else {
1896 0 my $buck = $buckets->[$bid];
1897 0 splice @$buck, 0, scalar( @$buck );
1898 }
1899 }
1900 0 splice @$buckets, 0, scalar(@$buckets);
1901 }
1902  
1903 sub DESTROY {
1904 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
1905  
1906 #remove all WEAK_REFS to the buckets
1907 0 undef $self->[DATA];
1908  
1909 0 delete $self->[DSTORE]->{_WEAK_REFS}{$self->[ID]};
1910 }
1911  
1912 # ---------------------------------------------------------------------------------------
1913  
1914 package Yote::YoteDB;
1915  
1916 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1917 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1918  
1919 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1920  
1921 1 ▶use Data::RecordStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
1922  
1923 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
1924  
1925 use constant {
1926 1 CLS => 1,
1927 DATA => 2,
1928 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
1929  
1930 #
1931 # This the main index and stores in which table and position
1932 # in that table that this object lives.
1933 #
1934 sub open {
1935 1 ▶ my( $pkg, $obj_store, $args ) = @_;
Subroutine open: called CC=4 SCAR=16.5
1936 1 ▶ my $class = ref( $pkg ) || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
1937  
1938 1 my $DATA_STORE;
1939 1 eval {
1940 1 $DATA_STORE = Data::RecordStore->open( $args->{ store } );
1941 };
1942 1 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
1943 1 ▶ if( $@ =~ /old format/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /old format/) 0 1 1
1944 0 die "This yote store is of an older format. It can be converted using the yote_explorer";
1945 }
1946 1 die $@;
1947 }
1948 0 my $self = bless {
1949 args => $args,
1950 OBJ_STORE => $obj_store,
1951 DATA_STORE => $DATA_STORE,
1952 }, $class;
1953 0 $self->{DATA_STORE}->ensure_entry_count( 1 );
1954 0 $self;
1955 } #open
1956  
1957 #
1958 # Return a list reference containing [ id, class, data ] that
1959 # corresponds to the $id argument. This is used by Yote::ObjStore
1960 # to build the yote object.
1961 #
1962 sub _fetch {
1963 0 ▶ my( $self, $id ) = @_;
Subroutine _fetch: not called CC=13 SCAR=52.0
1964 0 my $data = $self->{DATA_STORE}->fetch( $id );
1965  
1966 0 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
1967  
1968 0 my $pos = index( $data, ' ' ); #there is a always a space after the class.
1969 0 ▶ $pos = ( length( $data ) ) if $pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $pos == -1 0 0 0
1970 0 ▶ die "Malformed record '$data'" if $pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $pos == -1 0 0 0
1971 0 my $class = substr $data, 0, $pos;
1972 0 my $val = substr $data, $pos + 1;
1973 0 my $ret = [$id,$class,$val];
1974  
1975 # so foo` or foo\\` but not foo\\\`
1976 # also this will never start with a `
1977 0 my $parts = [ split /\`/, $val, -1 ];
1978  
1979 # check to see if any of the parts were split on escapes
1980 # like mypart`foo`oo (should be translated to mypart\`foo\`oo
1981 0 ▶ if( 0 < grep { /\\$/ } @$parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < grep({/\\$/;} @$parts)) 0 0 0
1982 0 my $newparts = [];
1983  
1984 0 my $is_hanging = 0;
1985 0 my $working_part = '';
1986  
1987 0 for my $part (@$parts) {
1988  
1989 # if the part ends in a hanging escape
1990 0 ▶ if( $part =~ /(^|[^\\])((\\\\)+)?[\\]$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($part =~ /(^|[^\\])((\\\\)+)?[\\]$/) { } 0 0 0
elsif ($is_hanging) { } 0 0 0
1991 0 ▶ if( $is_hanging ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hanging) { } 0 0 0
1992 0 $working_part .= "`$part";
1993 } else {
1994 0 $working_part = $part;
1995 }
1996 0 $is_hanging = 1;
1997 } elsif( $is_hanging ) {
1998 0 my $newpart = "$working_part`$part";
1999 0 $newpart =~ s/\\`/`/gs;
2000 0 $newpart =~ s/\\\\/\\/gs;
2001 0 push @$newparts, $newpart;
2002 0 $is_hanging = 0;
2003 } else {
2004 # normal part
2005 0 push @$newparts, $part;
2006 }
2007 }
2008 0 ▶ if( $is_hanging ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hanging) 0 0 0
2009 0 die "Error in parsing parts\n";
2010 }
2011 0 $parts = $newparts;
2012 }
2013  
2014 0 ▶ if( $class eq 'ARRAY' || $class eq 'Yote::BigHash' || $class eq 'Yote::ArrayGatekeeper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'ARRAY' or $class eq 'Yote::BigHash' or $class eq 'Yote::ArrayGatekeeper') { } 0 0 0
Condition0%0/6
$class eq "ARRAY" or $class eq "Yote::BigHash" or $class eq "Yote::ArrayGatekeeper"
l!l&&r!l&&!r
000
$class eq "ARRAY" or $class eq "Yote::BigHash"
l!l&&r!l&&!r
000
MC/DC0%0/3
$class eq "ARRAY" or $class eq "Yote::BigHash" or $class eq "Yote::ArrayGatekeeper"
$class eq "ARRAY" $class eq "Yote::BigHash" $class eq "Yote::ArrayGatekeeper"
Truth table0%0/4
A or B or C
A= $class eq "ARRAY"B= $class eq "Yote::BigHash"C= $class eq "Yote::ArrayGatekeeper"
ABCresult
1XX1
01X1
0011
0000
2015 0 $ret->[DATA] = $parts;
2016 } else {
2017 0 $ret->[DATA] = { @$parts };
2018 }
2019  
2020 0 $ret;
2021 } #_fetch
2022  
2023 #
2024 # The first object in a yote data store can trace a reference to
2025 # all active objects.
2026 #
2027 sub _first_id {
2028 0 ▶ return 1;
Subroutine _first_id: not called CC=1 SCAR=6.9
2029 } #_first_id
2030  
2031 #
2032 # Create a new object id and return it.
2033 #
2034 sub _get_id {
2035 0 ▶ my $self = shift;
Subroutine _get_id: not called CC=1 SCAR=6.9
2036 0 $self->{DATA_STORE}->next_id;
2037 } #_get_id
2038  
2039  
2040 # used for debugging and testing
2041 sub _max_id {
2042 0 ▶ shift->{DATA_STORE}->entry_count;
Subroutine _max_id: not called CC=1 SCAR=6.9
2043 }
2044  
2045 sub _generate_keep_db {
2046 0 ▶ my $self = shift;
Subroutine _generate_keep_db: not called CC=18 SCAR=58.3
2047 0 my $mark_to_keep_store = Data::RecordStore::FixedStore->open( "I", $self->{args}{store} . '/PURGE_KEEP' );
2048  
2049 0 $mark_to_keep_store->empty();
2050 0 $mark_to_keep_store->ensure_entry_count( $self->{DATA_STORE}->entry_count );
2051  
2052 0 my $check_store = Data::RecordStore::FixedStore->open( "L", $self->{args}{store} . '/CHECK' );
2053 0 $check_store->empty();
2054  
2055 0 $mark_to_keep_store->put_record( 1, [ 1 ] );
2056  
2057 0 my( %seen );
2058 0 my( @checks ) = ( 1 );
2059  
2060 0 for my $referenced_id ( grep { $_ != 1 } grep { defined($self->{OBJ_STORE}{_WEAK_REFS}{$_}) } keys %{ $self->{OBJ_STORE}{_WEAK_REFS} } ) {
2061 0 push @checks, $referenced_id;
2062 }
2063  
2064 #
2065 # While there are items to check, check them.
2066 #
2067 0 ▶ while( @checks || $check_store->entry_count > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@checks or $check_store->entry_count > 0) 0 0 0
Condition0%0/3
@checks or $check_store->entry_count > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
@checks or $check_store->entry_count > 0
@checks $check_store->entry_count > 0
Truth table0%0/3
A or B
A= @checksB= $check_store->entry_count > 0
ABresult
1X1
011
000
2068 0 ▶ my $check_id = shift( @checks ) || $check_store->pop->[0];
Condition0%0/3
shift @checks || $check_store->pop->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @checks || $check_store->pop->[0]
shift @checks $check_store->pop->[0]
Truth table0%0/3
A || B
A= shift @checksB= $check_store->pop->[0]
ABresult
1X1
011
000
2069 0 $mark_to_keep_store->put_record( $check_id, [ 1 ] );
2070  
2071 0 my $obj_arry = $self->_fetch( $check_id );
2072 0 $seen{$check_id} = 1;
2073 0 my( @additions );
2074  
2075 0 ▶ if( $obj_arry->[CLS] eq 'Yote::BigHash' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($obj_arry->[1] eq 'Yote::BigHash') { } 0 0 0
elsif ($obj_arry->[1] eq 'Yote::ArrayGatekeeper') { } 0 0 0
elsif (ref $obj_arry->[2] eq 'ARRAY') { } 0 0 0
2076 0 my $type = shift @{$obj_arry->[DATA]};
2077 0 shift @{$obj_arry->[DATA]}; #remove the size
2078 0 ▶ if( $type eq 'S' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'S') { } 0 0 0
2079 0 my $d = {@{$obj_arry->[DATA]}};
2080 0 ▶ ( @additions ) = grep { /^[^v]/ && ! $seen{$_}++ } values %$d;
Branch: 1 branch
BranchTrueFalseTotal
/^[^v]/ and not $seen{$_}++ 0 0 0
2081 } else { #BIG type
2082 0 ▶ ( @additions ) = grep { /^[^v]/ && ! $seen{$_}++ } @{$obj_arry->[DATA]};
Branch: 1 branch
BranchTrueFalseTotal
/^[^v]/ and not $seen{$_}++ 0 0 0
2083 }
2084 }
2085 elsif ( $obj_arry->[CLS] eq 'Yote::ArrayGatekeeper' ) {
2086 0 shift @{$obj_arry->[DATA]}; #item_count
2087 0 shift @{$obj_arry->[DATA]}; #block_count
2088 0 shift @{$obj_arry->[DATA]}; #block_size
2089 0 shift @{$obj_arry->[DATA]}; #level
2090 0 ▶ ( @additions ) = grep { /^[^v]/ && ! $seen{$_}++ } @{$obj_arry->[DATA]};
Branch: 1 branch
BranchTrueFalseTotal
/^[^v]/ and not $seen{$_}++ 0 0 0
2091 }
2092 elsif ( ref( $obj_arry->[DATA] ) eq 'ARRAY' ) {
2093 0 ▶ ( @additions ) = grep { /^[^v]/ && ! $seen{$_}++ } @{$obj_arry->[DATA]};
Branch: 1 branch
BranchTrueFalseTotal
/^[^v]/ and not $seen{$_}++ 0 0 0
2094 }
2095 else { # Yote::Obj
2096 0 ▶ ( @additions ) = grep { /^[^v]/ && ! $seen{$_}++ } values %{$obj_arry->[DATA]};
Branch: 1 branch
BranchTrueFalseTotal
/^[^v]/ and not $seen{$_}++ 0 0 0
2097 }
2098 0 ▶ if( @checks > 1_000_000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@checks > 1000000) 0 0 0
2099 0 for my $cid (@checks) {
2100 0 my( $has_keep ) = $mark_to_keep_store->get_record( $cid )->[0];
2101 0 ▶ unless( $has_keep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_keep) 0 0 0
2102 0 $check_store->push( [ $cid ] );
2103 }
2104 }
2105 0 splice @checks;
2106 }
2107 0 ▶ if( scalar( keys(%seen) ) > 1_000_000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %seen > 1000000) 0 0 0
2108 0 %seen = ();
2109 }
2110 0 push @checks, @additions;
2111 }
2112 0 $check_store->unlink_store;
2113  
2114 0 $mark_to_keep_store;
2115  
2116 } #_generate_keep_db
2117  
2118 #
2119 # Checks to see if the last entries of the stores can be popped off, making the purging quicker
2120 #
2121 sub _truncate_dbs {
2122 0 ▶ my( $self, $mark_to_keep_store, $keep_tally ) = @_;
Subroutine _truncate_dbs: not called CC=7 SCAR=40.3
2123 #loop through each database
2124 0 my $stores = $self->{DATA_STORE}->all_stores;
2125 0 my( @purged );
2126 0 for my $store (@$stores) {
2127 0 my $fn = $store->{FILENAME}; $fn =~ s!/[^/]+$!!;
2128 0 my $keep;
2129 0 ▶ while( ! $keep && $store->entry_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $keep and $store->entry_count) 0 0 0
Condition0%0/3
not $keep and $store->entry_count
!ll&&!rl&&r
000
MC/DC0%0/2
not $keep and $store->entry_count
not $keep $store->entry_count
Truth table0%0/3
not(A) and B
A= not $keepB= $store->entry_count
ABresult
0X0
100
111
2130 0 my( $check_id ) = @{ $store->get_record($store->entry_count) };
2131 0 ( $keep ) = $mark_to_keep_store->get_record( $check_id )->[0];
2132 0 ▶ if( ! $keep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keep) 0 0 0
2133 0 ▶ if( $self->{DATA_STORE}->delete( $check_id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DATA_STORE"}->delete($check_id)) 0 0 0
2134 0 ▶ if( $keep_tally ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_tally) 0 0 0
2135 0 push @purged, $check_id;
2136 }
2137 0 $mark_to_keep_store->put_record( $check_id, [ 2 ] ); #mark as already removed by truncate
2138 }
2139 }
2140 }
2141 }
2142 0 \@purged;
2143 }
2144  
2145  
2146 sub _update_recycle_ids {
2147 0 ▶ my( $self, $mark_to_keep_store ) = @_;
Subroutine _update_recycle_ids: not called CC=6 SCAR=37.4
2148  
2149 0 ▶ return unless $mark_to_keep_store->entry_count > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $mark_to_keep_store->entry_count > 0 0 0 0
2150  
2151 0 my $store = $self->{DATA_STORE};
2152  
2153  
2154 # find the higest still existing ID and cap the index to this
2155 0 my $highest_keep_id;
2156 0 for my $cand (reverse ( 1..$mark_to_keep_store->entry_count )) {
2157 0 my( $keep ) = $mark_to_keep_store->get_record( $cand )->[0];
2158 0 ▶ if( $keep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep) 0 0 0
2159 0 $store->set_entry_count( $cand );
2160 0 $highest_keep_id = $cand;
2161 0 last;
2162 }
2163 }
2164  
2165 0 $store->empty_recycler;
2166  
2167 # iterate each id in the entire object store and add those
2168 # not marked for keeping into the recycling
2169 0 for my $cand (reverse( 1.. $highest_keep_id) ) {
2170 0 my( $keep ) = $mark_to_keep_store->get_record( $cand )->[0];
2171 0 ▶ unless( $keep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keep) 0 0 0
2172 0 $store->recycle( $cand );
2173 }
2174 }
2175 } #_update_recycle_ids
2176  
2177  
2178 sub _purge_objects {
2179 0 ▶ my( $self, $mark_to_keep_store, $keep_tally ) = @_;
Subroutine _purge_objects: not called CC=7 SCAR=40.3
2180  
2181 0 my $purged = $self->_truncate_dbs( $mark_to_keep_store );
2182  
2183 0 for my $cand ( 1..$mark_to_keep_store->entry_count) { #iterate each id in the entire object store
2184 0 my( $keep ) = $mark_to_keep_store->get_record( $cand )->[0];
2185  
2186 0 ▶ die "Tried to purge root entry" if $cand == 1 && ! $keep;
Branch: 1 branch
BranchTrueFalseTotal
if $cand == 1 and not $keep 0 0 0
Condition0%0/3
$cand == 1 and not $keep
!ll&&!rl&&r
000
MC/DC0%0/2
$cand == 1 and not $keep
$cand == 1 not $keep
Truth table0%0/3
A and not(B)
A= $cand == 1B= not $keep
ABresult
0X0
100
111
2187 0 ▶ if ( ! $keep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keep) 0 0 0
2188 0 ▶ if( $self->{DATA_STORE}->delete( $cand ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DATA_STORE"}->delete($cand)) 0 0 0
2189 0 $mark_to_keep_store->put_record( $cand, [ 3 ] ); #mark as already removed by purge
2190 0 ▶ if( $keep_tally ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_tally) 0 0 0
2191 0 push @$purged, $cand;
2192 }
2193 }
2194 }
2195 }
2196  
2197 0 $purged;
2198  
2199 } #_purge_objects
2200  
2201  
2202 #
2203 # Saves the object data for object $id to the data store.
2204 #
2205 sub _stow { #Yote::YoteDB::_stow
2206 0 ▶ my( $self, $id, $class, $data ) = @_;
Subroutine _stow: not called CC=1 SCAR=6.9
2207 0 my $save_data = "$class $data";
2208 0 $self->{DATA_STORE}->stow( $save_data, $id );
2209 } #_stow
2210  
2211 #
2212 # Takes a list of object data references and stows them all in the datastore.
2213 # returns how many are stowed.
2214 #
2215 sub _stow_all {
2216 0 ▶ my( $self, $objs ) = @_;
Subroutine _stow_all: not called CC=2 SCAR=17.9
2217 0 my $count = 0;
2218 0 for my $o ( @$objs ) {
2219 0 $count += $self->_stow( @$o );
2220 }
2221 0 return $count;
2222 } #_stow_all
2223  
2224 1 1;
2225  
2226 __END__
↑ Summary Build »