blib/lib/Yote.pm

filter: Statementstmt 19.2% 141 / 732 Branchbran 0.8% 2 / 246 Conditioncond 1.4% 1 / 68 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 35.1% 45 / 128 Podpod 100% 1 / 1 totaltotal 15.5% 190 / 1220 CC 171 SCAR 42.9
CC
171
Cov
14%
_stow
420.0
__get_id
210.0
_raw_data
156.0
CRAP 73.2
↑ 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.01';
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=10 SCAR=47.0
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: 2 branches
BranchTrueFalseTotal
if ($class eq 'ARRAY') { } 0 0 0
elsif ($class eq 'HASH') { } 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 'HASH' ) {
316 0 my( %hash );
317 0 tie %hash, 'Yote::Hash', $self, $id, map { $_ => $data->{$_} } keys %$data;
318 0 $self->_store_weak( $id, \%hash );
319 0 return \%hash;
320 }
321 else {
322 0 my $obj;
323 0 eval {
324 0 my $path = $class;
325 0 ▶ unless( $INC{ $class } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$class}) 0 0 0
326 0 eval("use $class");
327 }
328 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
329 };
330 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
331 0 $obj->{DATA} = $data;
332 0 $obj->{ID} = $id;
333 0 $self->_store_weak( $id, $obj );
334 0 $obj->_load();
335 0 return $obj;
336 }
337 }
338 0 return undef;
339 } #fetch
340  
341 =head2 run_purger
342  
343 =cut
344 sub run_purger {
345 0 ▶ my( $self, $make_tally, $copy_only ) = @_;
Subroutine run_purger: not called CC=5 SCAR=34.0
346 0 $self->stow_all();
347  
348 0 my $keep_db = $self->{_DATASTORE}->_generate_keep_db();
349  
350 # analyze to see what percentage would be kept
351 0 my $total = $keep_db->entry_count;
352 0 my $keep = 0;
353 0 for my $tid (1..$total) {
354 0 my( $has_keep ) = $keep_db->get_record( $tid )->[0];
355 0 ▶ $keep++ if $has_keep;
Branch: 1 branch
BranchTrueFalseTotal
if $has_keep 0 0 0
356 }
357  
358 #
359 # If there are more things to keep than not, do a db purge,
360 # otherwise, rebuild the db.
361 #
362 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
363 0 my $purged;
364 0 ▶ if( $do_purge ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_purge) { } 0 0 0
365 0 $purged = $self->{_DATASTORE}->_purge_objects( $keep_db, $make_tally );
366 } else {
367 0 $purged = $self->_copy_active_ids( $keep_db, $make_tally );
368 }
369  
370 0 $self->{_DATASTORE}->_update_recycle_ids( $keep_db );
371  
372 # commenting out for a test
373 0 $keep_db->unlink_store;
374  
375 0 $purged;
376 } #run_purger
377  
378 sub _copy_active_ids {
379 0 ▶ my( $self, $copy_db ) = @_;
Subroutine _copy_active_ids: not called CC=10 SCAR=47.0
380 0 $self->stow_all();
381  
382 0 my $original_dir = $self->{args}{store};
383 0 my $backdir = $original_dir . '_COMPRESS_BACK_RECENT';
384 0 my $newdir = $original_dir . '_NEW_RECYC';
385  
386 0 ▶ if( -e $backdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $backdir) 0 0 0
387 0 my $oldback = $original_dir . '_COMPRESS_BACK_OLD';
388 0 ▶ if( -d $oldback ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $oldback) 0 0 0
389 0 warn "Removing old compression backup directory";
390 0 remove_tree( $oldback );
391 }
392 0 ▶ move( $backdir, $oldback ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
move($backdir, $oldback) or die $! 0 0 0
393 }
394  
395 0 ▶ if( -x $newdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x $newdir) 0 0 0
396 0 die "Unable to run compress store, temp directory '$newdir' already exists.";
397 }
398 0 my $newstore = Yote::ObjStore->_new( { store => $newdir } );
399  
400 0 my( @purges );
401 0 for my $keep_id ( 1..$copy_db->entry_count ) {
402  
403 0 my( $has_keep ) = $copy_db->get_record( $keep_id )->[0];
404 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
405 0 my $obj = $self->fetch( $keep_id );
406  
407 0 $newstore->{_DATASTORE}{DATA_STORE}->ensure_entry_count( $keep_id - 1 );
408 0 $newstore->_dirty( $obj, $keep_id );
409 0 $newstore->_stow( $obj, $keep_id );
410 } elsif( $self->{_DATASTORE}{DATA_STORE}->has_id( $keep_id ) ) {
411 0 push @purges, $keep_id;
412 }
413 } #each entry id
414  
415 0 ▶ move( $original_dir, $backdir ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
move($original_dir, $backdir) or die $! 0 0 0
416 0 ▶ move( $newdir, $original_dir ) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
move($newdir, $original_dir) or die $! 0 0 0
417  
418 0 \@purges;
419  
420 } #_copy_active_ids
421  
422 =head2 has_id
423  
424 Returns true if there is a valid reference linked to the id
425  
426 =cut
427 sub has_id {
428 0 ▶ my( $self, $id ) = @_;
Subroutine has_id: not called CC=1 SCAR=6.9
429 0 return $self->{_DATASTORE}{DATA_STORE}->has_id( $id );
430 }
431  
432 =head2 stow_all
433  
434 Saves all newly created or dirty objects.
435  
436 =cut
437 sub stow_all {
438 0 ▶ my $self = shift;
Subroutine stow_all: not called CC=6 SCAR=37.4
439 0 my @odata;
440 0 for my $obj (values %{$self->{_DIRTY}} ) {
441 0 my $cls;
442 0 my $ref = ref( $obj );
443 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' or $ref eq 'Yote::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
444 0 $cls = 'ARRAY';
445 ▶ } elsif( $ref eq 'HASH' || $ref eq 'Yote::Hash' ) {
Condition0%0/3
$ref eq "HASH" or $ref eq "Yote::Hash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref eq "HASH" or $ref eq "Yote::Hash"
$ref eq "HASH" $ref eq "Yote::Hash"
Truth table0%0/3
A or B
A= $ref eq "HASH"B= $ref eq "Yote::Hash"
ABresult
1X1
011
000
446 0 $cls = 'HASH';
447 } else {
448 0 $cls = $ref;
449 }
450 0 my( $text_rep ) = $self->_raw_data( $obj );
451 0 push( @odata, [ $self->_get_id( $obj ), $cls, $text_rep ] );
452 }
453 0 $self->{_DATASTORE}->_stow_all( \@odata );
454 0 $self->{_DIRTY} = {};
455 } #stow_all
456  
457  
458 =head2 stow( $obj )
459  
460 Saves that object to the database
461  
462 =cut
463 sub stow {
464 0 ▶ my( $self, $obj ) = @_;
Subroutine stow: not called CC=5 SCAR=34.0
465 0 my $cls;
466 0 my $ref = ref( $obj );
467 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' or $ref eq 'Yote::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
468 0 $cls = 'ARRAY';
469 ▶ } elsif( $ref eq 'HASH' || $ref eq 'Yote::Hash' ) {
Condition0%0/3
$ref eq "HASH" or $ref eq "Yote::Hash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref eq "HASH" or $ref eq "Yote::Hash"
$ref eq "HASH" $ref eq "Yote::Hash"
Truth table0%0/3
A or B
A= $ref eq "HASH"B= $ref eq "Yote::Hash"
ABresult
1X1
011
000
470 0 $cls = 'HASH';
471 } else {
472 0 $cls = $ref;
473 }
474 0 my $id = $self->_get_id( $obj );
475 0 my( $text_rep ) = $self->_raw_data( $obj );
476 0 $self->{_DATASTORE}->_stow( $id, $cls, $text_rep );
477 0 delete $self->{_DIRTY}{$id};
478 } #stow
479  
480  
481  
482 # -------------------------------
483 # * PRIVATE METHODS *
484 # -------------------------------
485 sub _new { #Yote::ObjStore
486 1 ▶ my( $pkg, $args ) = @_;
Subroutine _new: called CC=1 SCAR=0.2
487 1 my $self = bless {
488 _DIRTY => {},
489 _WEAK_REFS => {},
490 args => $args,
491 }, $pkg;
492 1 $self->{_DATASTORE} = Yote::YoteDB->open( $self, $args );
493 0 $self;
494 } #_new
495  
496 sub _init {
497 0 ▶ my $self = shift;
Subroutine _init: not called CC=3 SCAR=24.8
498 0 for my $pkg ( qw( Yote::Obj Yote::Array Yote::Hash ) ) {
499 0 ▶ $INC{ $pkg } or eval("use $pkg");
Branch: 1 branch
BranchTrueFalseTotal
$INC{$pkg} or eval "use $pkg" 0 0 0
500 }
501 0 $self->fetch_root;
502 0 $self->stow_all;
503 0 $self;
504 }
505  
506  
507 sub dirty_count {
508 0 ▶ my $self = shift;
Subroutine dirty_count: not called CC=1 SCAR=6.9
509 0 return scalar( keys %{$self->{_DIRTY}} );
510 }
511  
512 #
513 # Markes given object as dirty.
514 #
515 sub _dirty {
516 # ( $self, $ref, $id
517 0 ▶ $_[0]->{_DIRTY}->{$_[2]} = $_[1];
Subroutine _dirty: not called CC=1 SCAR=6.9
518 } #_dirty
519  
520 #
521 # Returns the first ID that is associated with the root Root object
522 #
523 sub _first_id {
524 0 ▶ shift->{_DATASTORE}->_first_id();
Subroutine _first_id: not called CC=1 SCAR=6.9
525 } #_first_id
526  
527 sub _get_id {
528 # for debugging I think?
529 0 ▶ shift->__get_id( shift );
Subroutine _get_id: not called CC=1 SCAR=6.9
530 }
531  
532 sub __get_id {
533 0 ▶ my( $self, $ref ) = @_;
Subroutine __get_id: not called CC=14 SCAR=53.5
534  
535 0 my $class = ref( $ref );
536 0 ▶ die "__get_id requires reference. got '$ref'" unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
537  
538 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') { } 0 0 0
elsif ($class eq 'HASH') { } 0 0 0
539 0 return $ref->[0];
540 }
541 elsif( $class eq 'ARRAY' ) {
542 0 my $tied = tied @$ref;
543 0 ▶ if( $tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied) 0 0 0
544 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
545 0 return $tied->[0];
546 }
547 0 my( @data ) = @$ref;
548 0 my $id = $self->{_DATASTORE}->_get_id( $class );
549 0 tie @$ref, 'Yote::Array', $self, $id;
550 0 push( @$ref, @data );
551 0 $self->_dirty( $ref, $id );
552 0 $self->_store_weak( $id, $ref );
553 0 return $id;
554 }
555 elsif( $class eq 'Yote::Hash' ) {
556 0 my $wref = $ref;
557 0 return $ref->[0];
558 }
559 elsif( $class eq 'HASH' ) {
560 0 my $tied = tied %$ref;
561 0 ▶ if( $tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied) 0 0 0
562 0 ▶ $tied->[0] ||= $self->{_DATASTORE}->_get_id( "HASH" );
Condition0%0/3
$tied->[0] ||= $self->{"_DATASTORE"}->_get_id("HASH")
l!l&&r!l&&!r
000
MC/DC0%0/2
$tied->[0] ||= $self->{"_DATASTORE"}->_get_id("HASH")
$tied->[0] $self->{"_DATASTORE"}->_get_id("HASH")
Truth table0%0/3
A ||= B
A= $tied->[0]B= $self->{"_DATASTORE"}->_get_id("HASH")
ABresult
1X1
011
000
563 0 return $tied->[0];
564 }
565 0 my $id = $self->{_DATASTORE}->_get_id( $class );
566  
567 0 my( %vals ) = %$ref;
568  
569 0 tie %$ref, 'Yote::Hash', $self, $id;
570 0 for my $key (keys %vals) {
571 0 $ref->{$key} = $vals{$key};
572 }
573 0 $self->_dirty( $ref, $id );
574 0 $self->_store_weak( $id, $ref );
575 0 return $id;
576 }
577 else {
578 0 ▶ return $ref->{ID} if $ref->{ID};
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"ID"} 0 0 0
579 0 ▶ if( $class eq 'Yote::Root' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'Yote::Root') { } 0 0 0
580 0 $ref->{ID} = $self->{_DATASTORE}->_first_id( $class );
581 } else {
582 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
583 }
584  
585 0 return $ref->{ID};
586 }
587  
588 } #_get_id
589  
590 sub _stow {
591 0 ▶ my( $self, $obj, $id ) = @_;
Subroutine _stow: not called CC=20 SCAR=60.4
592  
593 0 my $class = ref( $obj );
594 0 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
595 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
596 0 ▶ die unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
597  
598 0 my( $text_rep, $data ) = $self->_raw_data( $obj );
599  
600 0 ▶ if( $class eq 'ARRAY' ) {
Branch: 4 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::Hash') { } 0 0 0
601 0 $self->{_DATASTORE}->_stow( $id,'ARRAY', $text_rep );
602 0 $self->_clean( $id );
603 }
604 elsif( $class eq 'HASH' ) {
605 0 $self->{_DATASTORE}->_stow( $id,'HASH',$text_rep );
606 0 $self->_clean( $id );
607 }
608 elsif( $class eq 'Yote::Array' ) {
609 0 ▶ if( $self->_is_dirty( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_dirty($id)) 0 0 0
610 0 $self->{_DATASTORE}->_stow( $id,'ARRAY',$text_rep );
611 0 $self->_clean( $id );
612 }
613 0 for my $child (@$data) {
614 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
615 0 $self->_stow( $child, $self->{_DIRTY}->{$child} );
616 }
617 }
618 }
619 elsif( $class eq 'Yote::Hash' ) {
620 0 ▶ if( $self->_is_dirty( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_dirty($id)) 0 0 0
621 0 $self->{_DATASTORE}->_stow( $id, 'HASH', $text_rep );
622 }
623 0 $self->_clean( $id );
624 0 for my $child (values %$data) {
625 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
626 0 $self->_stow( $child, $self->{_DIRTY}->{$child} );
627 }
628 }
629 }
630 else {
631 0 ▶ if( $self->_is_dirty( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_dirty($id)) 0 0 0
632 0 $self->{_DATASTORE}->_stow( $id, $class, $text_rep );
633 0 $self->_clean( $id );
634 }
635 0 for my $val (values %$data) {
636 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
637 0 $self->_stow( $val, $self->{_DIRTY}->{$val} );
638 }
639 }
640 }
641 0 $id;
642 } #_stow
643  
644 sub _xform_in {
645 0 ▶ my( $self, $val ) = @_;
Subroutine _xform_in: not called CC=3 SCAR=24.8
646 0 ▶ if( ref( $val ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 0 0 0
647 0 return $self->_get_id( $val );
648 }
649 0 ▶ return defined $val ? "v$val" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 0 0 0
650 }
651  
652 sub _xform_out {
653 0 ▶ my( $self, $val ) = @_;
Subroutine _xform_out: not called CC=3 SCAR=24.8
654 0 ▶ return undef unless defined( $val );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
655 0 ▶ if( index($val,'v') == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($val, "v") == 0) 0 0 0
656 0 return substr( $val, 1 );
657 }
658 0 return $self->fetch( $val );
659 }
660  
661 sub _clean {
662 0 ▶ my( $self, $id ) = @_;
Subroutine _clean: not called CC=1 SCAR=6.9
663 0 delete $self->{_DIRTY}{$id};
664 } #_clean
665  
666 sub _is_dirty {
667 0 ▶ my( $self, $obj ) = @_;
Subroutine _is_dirty: not called CC=2 SCAR=17.9
668 0 ▶ my $id = ref($obj) ? _get_id($obj) : $obj;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj ? : 0 0 0
669 0 my $ans = $self->{_DIRTY}{$id};
670 0 $ans;
671 } #_is_dirty
672  
673 #
674 # Returns data structure representing object. References are integers. Values start with 'v'.
675 #
676 sub _raw_data {
677 0 ▶ my( $self, $obj ) = @_;
Subroutine _raw_data: not called CC=12 SCAR=50.5
678 0 my $class = ref( $obj );
679 0 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
680 0 my $id = $self->_get_id( $obj );
681 0 ▶ die unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
682 0 my( $r, $is_array );
683 0 ▶ if( $class eq 'ARRAY' ) {
Branch: 4 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::Hash') { } 0 0 0
684 0 my $tied = tied @$obj;
685 0 ▶ if( $tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied) { } 0 0 0
686 0 $r = $tied->[1];
687 0 $is_array = 1;
688 } else {
689 0 die;
690 }
691 }
692 elsif( $class eq 'HASH' ) {
693 0 my $tied = tied %$obj;
694 0 ▶ if( $tied ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tied) { } 0 0 0
695 0 $r = $tied->[1];
696 } else {
697 0 die;
698 }
699 }
700 elsif( $class eq 'Yote::Array' ) {
701 0 $r = $obj->[1];
702 0 $is_array = 1;
703 }
704 elsif( $class eq 'Yote::Hash' ) {
705 0 $r = $obj->[1];
706 }
707 else {
708 0 $r = $obj->{DATA};
709 }
710  
711 0 ▶ if( $is_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_array) 0 0 0
712 0 ▶ return join( "`", map { if( defined($_) ) { s/[\\]/\\\\/gs; s/`/\\`/gs; } $_ } @$r ), $r;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
713 }
714 0 ▶ return join( "`", map { if( defined($_) ) { s/[\\]/\\\\/gs; s/`/\\`/gs; } $_ } %$r ), $r;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
715  
716 } #_raw_data
717  
718  
719 sub _store_weak {
720 0 ▶ my( $self, $id, $ref ) = @_;
Subroutine _store_weak: not called CC=2 SCAR=17.9
721 0 ▶ die unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 0 0
722 0 $self->{_WEAK_REFS}{$id} = $ref;
723  
724 0 weaken( $self->{_WEAK_REFS}{$id} );
725 } #_store_weak
726  
727 # ---------------------------------------------------------------------------------------------------------------------
728  
729 =head1 NAME
730  
731 Yote::Obj - Generic container object for graph.
732  
733 =head1 DESCRIPTION
734  
735 A Yote::Obj is a container class that as a specific idiom for getters
736 and setters. This idiom is set up to avoid confusion and collision
737 with any method names.
738  
739 # sets the 'foo' field to the given value.
740 $obj->set_foo( { value => $store->newobj } );
741  
742 # returns the value for bar, and if none, sets it to 'default'
743 my $bar = $obj->get_bar( "default" );
744  
745 $obj->add_to_somelist( "Freddish" );
746 my $list = $obj->get_somelist;
747 $list->[ 0 ] == "Freddish";
748  
749  
750 $obj->remove_from_somelist( "Freddish" );
751  
752 =cut
753 package Yote::Obj;
754  
755 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
756 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
757 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
758  
759 #
760 # The string version of the yote object is simply its id. This allows
761 # objet ids to easily be stored as hash keys.
762 #
763 use overload
764 0 ▶ '""' => sub { shift->{ID} }, # for hash keys
Subroutine __ANON__: not called CC=1 SCAR=6.9
765 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
766 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
767 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
768 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
769 1 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
770  
771 =head2 absorb( hashref )
772  
773 pulls the hash data into this object.
774  
775 =cut
776 sub absorb {
777 0 ▶ my( $self, $data ) = @_;
Subroutine absorb: not called CC=2 SCAR=17.9
778 0 my $obj_store = $self->{STORE};
779 0 for my $key ( sort keys %$data ) {
780 0 my $item = $data->{ $key };
781 0 $self->{DATA}{$key} = $obj_store->_xform_in( $item );
782 }
783 0 $obj_store->_dirty( $self, $self->{ID} );
784  
785 } #absorb
786  
787 sub id {
788 0 ▶ shift->{ID};
Subroutine id: not called CC=1 SCAR=6.9
789 }
790  
791 =head2 set( $field, $value )
792  
793 Assigns the given value to the field in this object and returns the
794 assigned value.
795  
796 =cut
797 sub set {
798 0 ▶ my( $self, $fld, $val ) = @_;
Subroutine set: not called CC=3 SCAR=24.8
799  
800 0 my $inval = $self->{STORE}->_xform_in( $val );
801 0 ▶ if( $self->{DATA}{$fld} ne $inval ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DATA"}{$fld} ne $inval) 0 0 0
802 0 $self->{STORE}->_dirty( $self, $self->{ID} );
803 }
804  
805 0 ▶ unless( defined $inval ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inval) 0 0 0
806 0 delete $self->{DATA}{$fld};
807 0 return;
808 }
809 0 $self->{DATA}{$fld} = $inval;
810 0 return $self->{STORE}->_xform_out( $self->{DATA}{$fld} );
811 } #set
812  
813  
814 =head2 get( $field, $default-value )
815  
816 Returns the value assigned to the field, assinging the default
817 value to it if the value is currently not defined.
818  
819 =cut
820 sub get {
821 0 ▶ my( $self, $fld, $default ) = @_;
Subroutine get: not called CC=4 SCAR=30.0
822 0 my $cur = $self->{DATA}{$fld};
823 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
824 0 ▶ if( ref( $default ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default) 0 0 0
825 # 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
826 0 $self->{STORE}->_dirty( $default->{STORE}->_get_id( $default ) );
827 }
828 0 $self->{STORE}->_dirty( $self, $self->{ID} );
829 0 $self->{DATA}{$fld} = $self->{STORE}->_xform_in( $default );
830 }
831 0 return $self->{STORE}->_xform_out( $self->{DATA}{$fld} );
832 } #get
833  
834  
835 # -----------------------
836 #
837 # Public Methods
838 # -----------------------
839 #
840 # Defines get_foo, set_foo, add_to_list, remove_from_list
841 #
842 sub AUTOLOAD {
843 0 ▶ my( $s, $arg ) = @_;
Subroutine AUTOLOAD: not called CC=7 SCAR=33.3
844 0 my $func = our $AUTOLOAD;
845  
846 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
847 0 my( $fld ) = $1;
848 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
849 *$AUTOLOAD = sub {
850 0 ▶ my( $self, @vals ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
851 0 my $get = "get_$fld";
852 0 my $arry = $self->$get([]); # init array if need be
853 0 push( @$arry, @vals );
854 0 };
855 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
856 0 goto &$AUTOLOAD;
857 } #add_to
858 elsif( $func =~/:add_once_to_(.*)/ ) {
859 0 my( $fld ) = $1;
860 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
861 *$AUTOLOAD = sub {
862 0 ▶ my( $self, @vals ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
863 0 my $get = "get_$fld";
864 0 my $arry = $self->$get([]); # init array if need be
865 0 for my $val ( @vals ) {
866 0 ▶ unless( grep { $val eq $_ } @$arry ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$val eq $_;} @$arry) 0 0 0
867 0 push @$arry, $val;
868 }
869 }
870 0 };
871 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
872 0 goto &$AUTOLOAD;
873 } #add_once_to
874 elsif( $func =~ /:remove_from_(.*)/ ) { #removes the first instance of the target thing from the list
875 0 my $fld = $1;
876 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
877 *$AUTOLOAD = sub {
878 0 ▶ my( $self, @vals ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
879 0 my $get = "get_$fld";
880 0 my $arry = $self->$get([]); # init array if need be
881 0 for my $val (@vals ) {
882 0 for my $i (0..$#$arry) {
883 0 ▶ if( $arry->[$i] eq $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arry->[$i] eq $val) 0 0 0
884 0 splice @$arry, $i, 1;
885 0 last;
886 }
887 }
888 }
889 0 };
890 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
891 0 goto &$AUTOLOAD;
892 }
893 elsif( $func =~ /:remove_all_from_(.*)/ ) { #removes the first instance of the target thing from the list
894 0 my $fld = $1;
895 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
896 *$AUTOLOAD = sub {
897 0 ▶ my( $self, @vals ) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
898 0 my $get = "get_$fld";
899 0 my $arry = $self->$get([]); # init array if need be
900 0 for my $val (@vals) {
901 0 my $count = grep { $_ eq $val } @$arry;
902 0 ▶ while( $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
903 0 for my $i (0..$#$arry) {
904 0 ▶ if( $arry->[$i] eq $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arry->[$i] eq $val) 0 0 0
905 0 --$count;
906 0 splice @$arry, $i, 1;
907 0 ▶ last unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 0 0
908 }
909 }
910 }
911 }
912 0 };
913 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
914 0 goto &$AUTOLOAD;
915 }
916 elsif ( $func =~ /:set_(.*)/ ) {
917 0 my $fld = $1;
918 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
919 *$AUTOLOAD = sub {
920 0 ▶ my( $self, $val ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
921 0 my $inval = $self->{STORE}->_xform_in( $val );
922 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
923 0 ▶ unless( defined $inval ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $inval) 0 0 0
924 0 delete $self->{DATA}{$fld};
925 0 return;
926 }
927 0 $self->{DATA}{$fld} = $inval;
928 0 return $self->{STORE}->_xform_out( $self->{DATA}{$fld} );
929 0 };
930 0 goto &$AUTOLOAD;
931 }
932 elsif( $func =~ /:get_(.*)/ ) {
933 0 my $fld = $1;
934 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
935 *$AUTOLOAD = sub {
936 0 ▶ my( $self, $init_val ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
937 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
938 0 ▶ if( ref( $init_val ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $init_val) 0 0 0
939 # 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
940 0 $self->{STORE}->_dirty( $init_val, $self->{STORE}->_get_id( $init_val ) );
941 }
942 0 $self->{STORE}->_dirty( $self, $self->{ID} );
943 0 $self->{DATA}{$fld} = $self->{STORE}->_xform_in( $init_val );
944 }
945 0 return $self->{STORE}->_xform_out( $self->{DATA}{$fld} );
946 0 };
947 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
948 0 goto &$AUTOLOAD;
949 }
950 else {
951 0 die "Unknown Yote::Obj function '$func'";
952 }
953  
954 } #AUTOLOAD
955  
956 # -----------------------
957 #
958 # Overridable Methods
959 # -----------------------
960  
961 =head2 _init
962  
963 This is called the first time an object is created. It is not
964 called when the object is loaded from storage. This can be used
965 to set up defaults. This is meant to be overridden.
966  
967 =cut
968 ▶sub _init {}
Subroutine _init: not called CC=1 SCAR=0.0
969  
970 =head2 _init
971  
972 This is called each time the object is loaded from the data store.
973 This is meant to be overridden.
974  
975 =cut
976 ▶sub _load {}
Subroutine _load: not called CC=1 SCAR=0.0
977  
978  
979  
980 # -----------------------
981 #
982 # Private Methods
983 #
984 # -----------------------
985  
986  
987 sub _new { #new Yote::Obj
988 0 ▶ my( $pkg, $obj_store, $data, $_id ) = @_;
Subroutine _new: not called CC=5 SCAR=34.0
989  
990 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
991 0 my $obj = bless {
992 DATA => {},
993 STORE => $obj_store,
994 }, $class;
995 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
996 0 $obj_store->_dirty( $obj, $obj->{ID} );
997 0 $obj->_init(); #called the first time the object is created.
998  
999 0 ▶ if( ref( $data ) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 0 0 0
elsif ($data) { } 0 0 0
1000 0 $obj->absorb( $data );
1001 } elsif( $data ) {
1002 0 die "Yote::Obj::new must be called with hash or undef. Was called with '". ref( $data ) . "'";
1003 }
1004 0 return $obj;
1005 } #_new
1006  
1007 sub _store {
1008 0 ▶ return shift->{STORE};
Subroutine _store: not called CC=1 SCAR=6.9
1009 }
1010  
1011 #
1012 # Called by the object provider; returns a Yote::Obj the object
1013 # provider will stuff data into. Takes the class and id as arguments.
1014 #
1015 sub _instantiate {
1016 0 ▶ bless { ID => $_[1], DATA => {}, STORE => $_[2] }, $_[0];
Subroutine _instantiate: not called CC=1 SCAR=6.9
1017 } #_instantiate
1018  
1019 sub DESTROY {
1020 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
1021 0 delete $self->{STORE}{_WEAK_REFS}{$self->{ID}};
1022 }
1023  
1024  
1025 # ---------------------------------------------------------------------------------------------------------------------
1026  
1027 package Yote::Array;
1028  
1029 ############################################################################################################
1030 # This module is used transparently by Yote to link arrays into its graph structure. This is not meant to #
1031 # be called explicitly or modified. #
1032 ############################################################################################################
1033  
1034 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1035 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1036  
1037 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1038 1 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
1039  
1040 sub TIEARRAY {
1041 0 ▶ my( $class, $obj_store, $id, @list ) = @_;
Subroutine TIEARRAY: not called CC=2 SCAR=17.9
1042 0 my $storage = [];
1043  
1044 # once the array is tied, an additional data field will be added
1045 # so obj will be [ $id, $storage, $obj_store ]
1046 0 my $obj = bless [$id,$storage,$obj_store], $class;
1047 0 for my $item (@list) {
1048 0 push( @$storage, $item );
1049 }
1050 0 return $obj;
1051 }
1052  
1053 sub FETCH {
1054 0 ▶ my( $self, $idx ) = @_;
Subroutine FETCH: not called CC=1 SCAR=6.9
1055 0 return $self->[2]->_xform_out ( $self->[1][$idx] );
1056 }
1057  
1058 sub FETCHSIZE {
1059 0 ▶ my $self = shift;
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
1060 0 return scalar(@{$self->[1]});
1061 }
1062  
1063 sub STORE {
1064 0 ▶ my( $self, $idx, $val ) = @_;
Subroutine STORE: not called CC=1 SCAR=6.9
1065 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1066 0 $self->[1][$idx] = $self->[2]->_xform_in( $val );
1067 }
1068 ▶sub STORESIZE {} #stub for array
Subroutine STORESIZE: not called CC=1 SCAR=0.0
1069  
1070 sub EXISTS {
1071 0 ▶ my( $self, $idx ) = @_;
Subroutine EXISTS: not called CC=1 SCAR=6.9
1072 0 return defined( $self->[1][$idx] );
1073 }
1074 sub DELETE {
1075 0 ▶ my( $self, $idx ) = @_;
Subroutine DELETE: not called CC=1 SCAR=6.9
1076 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1077 0 delete $self->[1][$idx];
1078 }
1079  
1080 sub CLEAR {
1081 0 ▶ my $self = shift;
Subroutine CLEAR: not called CC=1 SCAR=6.9
1082 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1083 0 @{$self->[1]} = ();
1084 }
1085 sub PUSH {
1086 0 ▶ my( $self, @vals ) = @_;
Subroutine PUSH: not called CC=1 SCAR=6.9
1087 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1088 0 push( @{$self->[1]}, map { $self->[2]->_xform_in($_) } @vals );
1089 }
1090 sub POP {
1091 0 ▶ my $self = shift;
Subroutine POP: not called CC=1 SCAR=6.9
1092 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1093 0 return $self->[2]->_xform_out( pop @{$self->[1]} );
1094 }
1095 sub SHIFT {
1096 0 ▶ my( $self ) = @_;
Subroutine SHIFT: not called CC=1 SCAR=6.9
1097 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1098 0 my $val = splice @{$self->[1]}, 0, 1;
1099 0 return $self->[2]->_xform_out( $val );
1100 }
1101 sub UNSHIFT {
1102 0 ▶ my( $self, @vals ) = @_;
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
1103 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1104 0 unshift @{$self->[1]}, map {$self->[2]->_xform_in($_)} @vals;
1105 }
1106 sub SPLICE {
1107 0 ▶ my( $self, $offset, $length, @vals ) = @_;
Subroutine SPLICE: not called CC=1 SCAR=6.9
1108 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1109 0 return map { $self->[2]->_xform_out($_) } splice @{$self->[1]}, $offset, $length, map {$self->[2]->_xform_in($_)} @vals;
1110 }
1111 ▶sub EXTEND {}
Subroutine EXTEND: not called CC=1 SCAR=0.0
1112  
1113 sub DESTROY {
1114 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
1115 0 delete $self->[2]->{_WEAK_REFS}{$self->[0]};
1116 }
1117  
1118 # ---------------------------------------------------------------------------------------
1119  
1120 package Yote::Hash;
1121  
1122 ######################################################################################
1123 # This module is used transparently by Yote to link hashes into its graph structure. #
1124 # This is not meant to be called explicitly or modified. #
1125 ######################################################################################
1126  
1127 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1128 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1129  
1130 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1131  
1132 1 ▶use Tie::Hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
1133  
1134 sub TIEHASH {
1135 0 ▶ my( $class, $obj_store, $id, %hash ) = @_;
Subroutine TIEHASH: not called CC=2 SCAR=17.9
1136 0 my $storage = {};
1137 # after $obj_store is a list reference of
1138 # id, data, store
1139 0 my $obj = bless [ $id, $storage,$obj_store ], $class;
1140 0 for my $key (keys %hash) {
1141 0 $storage->{$key} = $hash{$key};
1142 }
1143 0 return $obj;
1144 }
1145  
1146 sub STORE {
1147 0 ▶ my( $self, $key, $val ) = @_;
Subroutine STORE: not called CC=1 SCAR=6.9
1148 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1149 0 $self->[1]{$key} = $self->[2]->_xform_in( $val );
1150 }
1151  
1152 sub FIRSTKEY {
1153 0 ▶ my $self = shift;
Subroutine FIRSTKEY: not called CC=2 SCAR=17.9
1154 0 my $a = scalar keys %{$self->[1]};
1155 0 my( $k, $val ) = each %{$self->[1]};
1156 0 ▶ return wantarray ? ( $k => $val ) : $k;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1157 }
1158 sub NEXTKEY {
1159 0 ▶ my $self = shift;
Subroutine NEXTKEY: not called CC=2 SCAR=17.9
1160 0 my( $k, $val ) = each %{$self->[1]};
1161 0 ▶ return wantarray ? ( $k => $val ) : $k;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1162 }
1163  
1164 sub FETCH {
1165 0 ▶ my( $self, $key ) = @_;
Subroutine FETCH: not called CC=1 SCAR=6.9
1166 0 return $self->[2]->_xform_out( $self->[1]{$key} );
1167 }
1168  
1169 sub EXISTS {
1170 0 ▶ my( $self, $key ) = @_;
Subroutine EXISTS: not called CC=1 SCAR=6.9
1171 0 return defined( $self->[1]{$key} );
1172 }
1173 sub DELETE {
1174 0 ▶ my( $self, $key ) = @_;
Subroutine DELETE: not called CC=1 SCAR=6.9
1175 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1176 0 return delete $self->[1]{$key};
1177 }
1178 sub CLEAR {
1179 0 ▶ my $self = shift;
Subroutine CLEAR: not called CC=1 SCAR=6.9
1180 0 $self->[2]->_dirty( $self->[2]{_WEAK_REFS}{$self->[0]}, $self->[0] );
1181 0 %{$self->[1]} = ();
1182 }
1183  
1184 sub DESTROY {
1185 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
1186 0 delete $self->[2]->{_WEAK_REFS}{$self->[0]};
1187 }
1188  
1189 # ---------------------------------------------------------------------------------------
1190  
1191 package Yote::YoteDB;
1192  
1193 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1194 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1195  
1196 1 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1197  
1198 1 ▶use Data::RecordStore;
Subroutine BEGIN: called CC=1 SCAR=0.0
1199  
1200 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
1201  
1202 use constant {
1203 1 DATA => 2,
1204 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
1205  
1206 #
1207 # This the main index and stores in which table and position
1208 # in that table that this object lives.
1209 #
1210 sub open {
1211 1 ▶ my( $pkg, $obj_store, $args ) = @_;
Subroutine open: called CC=4 SCAR=16.5
1212 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
1213  
1214 1 my $DATA_STORE;
1215 1 eval {
1216 1 $DATA_STORE = Data::RecordStore->open( $args->{ store } );
1217
1218 };
1219 1 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
1220 1 ▶ if( $@ =~ /old format/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /old format/) 0 1 1
1221 0 die "This yote store is of an older format. It can be converted using the yote_explorer";
1222 }
1223 1 die $@;
1224 }
1225 0 my $self = bless {
1226 args => $args,
1227 OBJ_STORE => $obj_store,
1228 DATA_STORE => $DATA_STORE,
1229 }, $class;
1230 0 $self->{DATA_STORE}->ensure_entry_count( 1 );
1231 0 $self;
1232 } #open
1233  
1234 #
1235 # Return a list reference containing [ id, class, data ] that
1236 # corresponds to the $id argument. This is used by Yote::ObjStore
1237 # to build the yote object.
1238 #
1239 sub _fetch {
1240 0 ▶ my( $self, $id ) = @_;
Subroutine _fetch: not called CC=11 SCAR=48.8
1241 0 my $data = $self->{DATA_STORE}->fetch( $id );
1242  
1243 0 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
1244  
1245 0 my $pos = index( $data, ' ' ); #there is a always a space after the class.
1246 0 ▶ $pos = ( length( $data ) ) if $pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $pos == -1 0 0 0
1247 0 ▶ die "Malformed record '$data'" if $pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $pos == -1 0 0 0
1248 0 my $class = substr $data, 0, $pos;
1249 0 my $val = substr $data, $pos + 1;
1250 0 my $ret = [$id,$class,$val];
1251  
1252 # so foo` or foo\\` but not foo\\\`
1253 # also this will never start with a `
1254 0 my $parts = [ split /\`/, $val, -1 ];
1255  
1256 # check to see if any of the parts were split on escapes
1257 # like mypart`foo`oo (should be translated to mypart\`foo\`oo
1258 0 ▶ if( 0 < grep { /\\$/ } @$parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < grep({/\\$/;} @$parts)) 0 0 0
1259 0 my $newparts = [];
1260  
1261 0 my $is_hanging = 0;
1262 0 my $working_part = '';
1263
1264 0 for my $part (@$parts) {
1265  
1266 # if the part ends in a hanging escape
1267 0 ▶ if( $part =~ /(^|[^\\])((\\\\)+)?[\\]$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($part =~ /(^|[^\\])((\\\\)+)?[\\]$/) { } 0 0 0
elsif ($is_hanging) { } 0 0 0
1268 0 ▶ if( $is_hanging ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hanging) { } 0 0 0
1269 0 $working_part .= "`$part";
1270 } else {
1271 0 $working_part = $part;
1272 }
1273 0 $is_hanging = 1;
1274 } elsif( $is_hanging ) {
1275 0 my $newpart = "$working_part`$part";
1276 0 $newpart =~ s/\\`/`/gs;
1277 0 $newpart =~ s/\\\\/\\/gs;
1278 0 push @$newparts, $newpart;
1279 0 $is_hanging = 0;
1280 } else {
1281 # normal part
1282 0 push @$newparts, $part;
1283 }
1284 }
1285 0 ▶ if( $is_hanging ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hanging) 0 0 0
1286 0 die "Error in parsing parts\n";
1287 }
1288 0 $parts = $newparts;
1289 }
1290  
1291 0 ▶ if( $class eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq 'ARRAY') { } 0 0 0
1292 0 $ret->[DATA] = $parts;
1293 } else {
1294 0 $ret->[DATA] = { @$parts };
1295 }
1296  
1297 0 $ret;
1298 } #_fetch
1299  
1300 #
1301 # The first object in a yote data store can trace a reference to
1302 # all active objects.
1303 #
1304 sub _first_id {
1305 0 ▶ return 1;
Subroutine _first_id: not called CC=1 SCAR=6.9
1306 } #_first_id
1307  
1308 #
1309 # Create a new object id and return it.
1310 #
1311 sub _get_id {
1312 0 ▶ my $self = shift;
Subroutine _get_id: not called CC=1 SCAR=6.9
1313 0 $self->{DATA_STORE}->next_id;
1314 } #_get_id
1315  
1316  
1317 # used for debugging and testing
1318 sub _max_id {
1319 0 ▶ shift->{DATA_STORE}->entry_count;
Subroutine _max_id: not called CC=1 SCAR=6.9
1320 }
1321  
1322 sub _generate_keep_db {
1323 0 ▶ my $self = shift;
Subroutine _generate_keep_db: not called CC=12 SCAR=50.5
1324 0 my $mark_to_keep_store = Data::RecordStore::FixedStore->open( "I", $self->{args}{store} . '/PURGE_KEEP' );
1325  
1326 0 $mark_to_keep_store->empty();
1327 0 $mark_to_keep_store->ensure_entry_count( $self->{DATA_STORE}->entry_count );
1328  
1329 0 my $check_store = Data::RecordStore::FixedStore->open( "L", $self->{args}{store} . '/CHECK' );
1330 0 $check_store->empty();
1331  
1332 0 $mark_to_keep_store->put_record( 1, [ 1 ] );
1333  
1334 0 my( %seen );
1335 0 my( @checks ) = ( 1 );
1336  
1337 0 for my $referenced_id ( grep { defined($self->{OBJ_STORE}{_WEAK_REFS}{$_}) } keys %{ $self->{OBJ_STORE}{_WEAK_REFS} } ) {
1338 0 push @checks, $referenced_id;
1339 }
1340  
1341  
1342 #
1343 # While there are items to check, check them.
1344 #
1345 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
1346 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
1347 0 $mark_to_keep_store->put_record( $check_id, [ 1 ] );
1348  
1349 0 my $item = $self->_fetch( $check_id );
1350 0 $seen{$check_id} = 1;
1351 0 my( @additions );
1352 0 ▶ if ( ref( $item->[DATA] ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item->[2] eq 'ARRAY') { } 0 0 0
1353 0 ▶ ( @additions ) = grep { /^[^v]/ && ! $seen{$_}++ } @{$item->[DATA]};
Branch: 1 branch
BranchTrueFalseTotal
/^[^v]/ and not $seen{$_}++ 0 0 0
1354 } else {
1355 0 ▶ ( @additions ) = grep { /^[^v]/ && ! $seen{$_}++ } values %{$item->[DATA]};
Branch: 1 branch
BranchTrueFalseTotal
/^[^v]/ and not $seen{$_}++ 0 0 0
1356 }
1357 0 ▶ if( @checks > 1_000_000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@checks > 1000000) 0 0 0
1358 0 for my $cid (@checks) {
1359 0 my( $has_keep ) = $mark_to_keep_store->get_record( $cid )->[0];
1360 0 ▶ unless( $has_keep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_keep) 0 0 0
1361 0 $check_store->push( [ $cid ] );
1362 }
1363 }
1364 0 splice @checks;
1365 }
1366 0 ▶ if( scalar( keys(%seen) ) > 1_000_000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %seen > 1000000) 0 0 0
1367 0 %seen = ();
1368 }
1369 0 push @checks, @additions;
1370 }
1371 0 $check_store->unlink_store;
1372  
1373 0 $mark_to_keep_store;
1374  
1375 } #_generate_keep_db
1376  
1377 #
1378 # Checks to see if the last entries of the stores can be popped off, making the purging quicker
1379 #
1380 sub _truncate_dbs {
1381 0 ▶ my( $self, $mark_to_keep_store, $keep_tally ) = @_;
Subroutine _truncate_dbs: not called CC=7 SCAR=40.3
1382 #loop through each database
1383 0 my $stores = $self->{DATA_STORE}->all_stores;
1384 0 my( @purged );
1385 0 for my $store (@$stores) {
1386 0 my $fn = $store->{FILENAME}; $fn =~ s!/[^/]+$!!;
1387 0 my $keep;
1388 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
1389 0 my( $check_id ) = @{ $store->get_record($store->entry_count) };
1390 0 ( $keep ) = $mark_to_keep_store->get_record( $check_id )->[0];
1391 0 ▶ if( ! $keep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keep) 0 0 0
1392 0 ▶ if( $self->{DATA_STORE}->delete( $check_id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DATA_STORE"}->delete($check_id)) 0 0 0
1393 0 ▶ if( $keep_tally ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_tally) 0 0 0
1394 0 push @purged, $check_id;
1395 }
1396 0 $mark_to_keep_store->put_record( $check_id, [ 2 ] ); #mark as already removed by truncate
1397 }
1398 }
1399 }
1400 }
1401 0 \@purged;
1402 }
1403  
1404  
1405 sub _update_recycle_ids {
1406 0 ▶ my( $self, $mark_to_keep_store ) = @_;
Subroutine _update_recycle_ids: not called CC=6 SCAR=37.4
1407  
1408 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
1409  
1410 0 my $store = $self->{DATA_STORE};
1411  
1412  
1413 # find the higest still existing ID and cap the index to this
1414 0 my $highest_keep_id;
1415 0 for my $cand (reverse ( 1..$mark_to_keep_store->entry_count )) {
1416 0 my( $keep ) = $mark_to_keep_store->get_record( $cand )->[0];
1417 0 ▶ if( $keep ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep) 0 0 0
1418 0 $store->set_entry_count( $cand );
1419 0 $highest_keep_id = $cand;
1420 0 last;
1421 }
1422 }
1423  
1424 0 $store->empty_recycler;
1425  
1426 # iterate each id in the entire object store and add those
1427 # not marked for keeping into the recycling
1428 0 for my $cand (reverse( 1.. $highest_keep_id) ) {
1429 0 my( $keep ) = $mark_to_keep_store->get_record( $cand )->[0];
1430 0 ▶ unless( $keep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keep) 0 0 0
1431 0 $store->recycle( $cand );
1432 }
1433 }
1434 } #_update_recycle_ids
1435  
1436  
1437 sub _purge_objects {
1438 0 ▶ my( $self, $mark_to_keep_store, $keep_tally ) = @_;
Subroutine _purge_objects: not called CC=7 SCAR=40.3
1439  
1440 0 my $purged = $self->_truncate_dbs( $mark_to_keep_store );
1441  
1442 0 for my $cand ( 1..$mark_to_keep_store->entry_count) { #iterate each id in the entire object store
1443 0 my( $keep ) = $mark_to_keep_store->get_record( $cand )->[0];
1444  
1445 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
1446 0 ▶ if ( ! $keep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keep) 0 0 0
1447 0 ▶ if( $self->{DATA_STORE}->delete( $cand ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DATA_STORE"}->delete($cand)) 0 0 0
1448 0 $mark_to_keep_store->put_record( $cand, [ 3 ] ); #mark as already removed by purge
1449 0 ▶ if( $keep_tally ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_tally) 0 0 0
1450 0 push @$purged, $cand;
1451 }
1452 }
1453 }
1454 }
1455  
1456 0 $purged;
1457  
1458 } #_purge_objects
1459  
1460  
1461 #
1462 # Saves the object data for object $id to the data store.
1463 #
1464 sub _stow { #Yote::YoteDB::_stow
1465 0 ▶ my( $self, $id, $class, $data ) = @_;
Subroutine _stow: not called CC=1 SCAR=6.9
1466 0 my $save_data = "$class $data";
1467 0 $self->{DATA_STORE}->stow( $save_data, $id );
1468 } #_stow
1469  
1470 #
1471 # Takes a list of object data references and stows them all in the datastore.
1472 # returns how many are stowed.
1473 #
1474 sub _stow_all {
1475 0 ▶ my( $self, $objs ) = @_;
Subroutine _stow_all: not called CC=2 SCAR=17.9
1476 0 my $count = 0;
1477 0 for my $o ( @$objs ) {
1478 0 $count += $self->_stow( @$o );
1479 }
1480 0 return $count;
1481 } #_stow_all
1482  
1483 1 1;
1484  
1485 __END__
↑ Summary Build »