| 1 | package KiokuDB::Collapser::Buffer; | ||||||||||||||||||||||||||||
| 2 | 20 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use Hash::Util::FieldHash::Compat qw(idhash); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use Set::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 20 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 20 | has live_objects => ( | |||||||||||||||||||||||||||
| 10 | isa => "KiokuDB::LiveObjects", | ||||||||||||||||||||||||||||
| 11 | is => "ro", | ||||||||||||||||||||||||||||
| 12 | required => 1, | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | has _objects => ( | ||||||||||||||||||||||||||||
| 16 | isa => "HashRef", | ||||||||||||||||||||||||||||
| 17 | is => "ro", | ||||||||||||||||||||||||||||
| 18 | 1356 | ▶ | default => sub { idhash my %hash }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 20 | ); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub object_to_id { | ||||||||||||||||||||||||||||
| 22 | 4507 | ▶ | my ( $self, $object ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
object_to_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 4507 | $self->_objects->{$object}; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub merged_objects_to_ids { | ||||||||||||||||||||||||||||
| 27 | 1338 | ▶ | my ( $self, @objects ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
merged_objects_to_ids: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1338 | my $l = $self->live_objects; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1338 | ▶ | map { $self->object_to_id($_) || $l->object_to_id($_) } @objects; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has _ids => ( | ||||||||||||||||||||||||||||
| 35 | isa => "HashRef", | ||||||||||||||||||||||||||||
| 36 | is => "ro", | ||||||||||||||||||||||||||||
| 37 | 1356 | ▶ | default => sub { return {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 20 | ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | has _entry_args => ( | ||||||||||||||||||||||||||||
| 41 | isa => "HashRef", | ||||||||||||||||||||||||||||
| 42 | is => "ro", | ||||||||||||||||||||||||||||
| 43 | 1356 | ▶ | default => sub { return {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 20 | ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub id_to_object { | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my ( $self, $id ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
id_to_object: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ( defined ( my $obj = $self->_ids->{$id} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | return $obj; | |||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||
| 52 | 0 | return $self->live_objects->id_to_object($id); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | has entries => ( | ||||||||||||||||||||||||||||
| 57 | traits => ["Hash"], | ||||||||||||||||||||||||||||
| 58 | isa => "HashRef", | ||||||||||||||||||||||||||||
| 59 | reader => "_entries", | ||||||||||||||||||||||||||||
| 60 | 1356 | ▶ | default => sub { return {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 20 | handles => { | |||||||||||||||||||||||||||
| 62 | entries => "values", | ||||||||||||||||||||||||||||
| 63 | ids => "keys", | ||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub id_to_entry { | ||||||||||||||||||||||||||||
| 68 | 58 | ▶ | my ( $self, $id ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
id_to_entry: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 58 | $self->_entries->{$id}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | has intrinsic => ( | ||||||||||||||||||||||||||||
| 73 | isa => "HashRef", | ||||||||||||||||||||||||||||
| 74 | is => "ro", | ||||||||||||||||||||||||||||
| 75 | 1356 | ▶ | default => sub { idhash my %hash }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 20 | ); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub intrinsic_entry { | ||||||||||||||||||||||||||||
| 79 | 2254 | ▶ | my ( $self, $obj ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
intrinsic_entry: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 2254 | $self->intrinsic->{$obj}; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub insert_intrinsic { | ||||||||||||||||||||||||||||
| 84 | 104 | ▶ | my ( $self, $object, $entry ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
insert_intrinsic: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 104 | $self->intrinsic->{$object} = $entry; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # a list of the IDs of all simple entries | ||||||||||||||||||||||||||||
| 89 | has simple_entries => ( | ||||||||||||||||||||||||||||
| 90 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||
| 91 | is => "ro", | ||||||||||||||||||||||||||||
| 92 | 1356 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 20 | ); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # first_class keeps track of the simple references which are first class | ||||||||||||||||||||||||||||
| 96 | # (either weak or shared, and must have an entry) | ||||||||||||||||||||||||||||
| 97 | has first_class => ( | ||||||||||||||||||||||||||||
| 98 | isa => 'Set::Object', | ||||||||||||||||||||||||||||
| 99 | is => "ro", | ||||||||||||||||||||||||||||
| 100 | 1356 | ▶ | default => sub { Set::Object->new }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | 20 | ); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | has options => ( | ||||||||||||||||||||||||||||
| 104 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 105 | is => "ro", | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 107 | 20 | ); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | sub insert { | ||||||||||||||||||||||||||||
| 110 | 17304 | ▶ | my ( $self, $id, $object, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 17304 | $self->_objects->{$object} = $id; | |||||||||||||||||||||||||||
| 113 | 17304 | $self->_ids->{$id} = $object; | |||||||||||||||||||||||||||
| 114 | 17304 | ▶ | $self->_entry_args->{$id} = \@args if @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub insert_entry { | ||||||||||||||||||||||||||||
| 118 | 9150 | ▶ | my ( $self, $id, $entry, $object, @args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
insert_entry: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 9150 | $self->_entries->{$id} = $entry; | |||||||||||||||||||||||||||
| 121 | 9150 | $self->insert($id, $object, @args); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | sub compact_entries { | ||||||||||||||||||||||||||||
| 125 | 1337 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
compact_entries: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1337 | my ( $entries, $fc, $simple, $options ) = ( $self->_entries, $self->first_class, $self->simple_entries, $self->options ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # unify non shared simple references | ||||||||||||||||||||||||||||
| 130 | 1337 | ▶ | if ( my @flatten = grep { not $fc->includes($_) } @$simple ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 644 | my %flatten; | |||||||||||||||||||||||||||
| 132 | 644 | @flatten{@flatten} = delete @{$entries}{@flatten}; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 644 | $self->compact_entry($_, \%flatten) for values %$entries; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub compact_entry { | ||||||||||||||||||||||||||||
| 139 | 8425 | ▶ | my ( $self, $entry, $flatten ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
compact_entry: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 8425 | my $data = $entry->data; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 8425 | ▶ | if ( $self->compact_data($data, $flatten) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 4 | $entry->_data($data); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub compact_data { | ||||||||||||||||||||||||||||
| 149 | 17140 | ▶ | my ( $self, $data, $flatten ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
compact_data: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 17140 | ▶ | if ( ref $data eq 'KiokuDB::Reference' ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 152 | 8554 | my $id = $data->id; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 8554 | ▶ | if ( my $entry = $flatten->{$id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | # replace reference with data from entry, so that the | ||||||||||||||||||||||||||||
| 156 | # simple data is inlined, and mark that entry for removal | ||||||||||||||||||||||||||||
| 157 | 5973 | $self->compact_entry($entry, $flatten); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 5973 | ▶ | if ( $entry->tied or $entry->class ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$entry->tied or $entry->class
MC/DC50%1/2
$entry->tied or $entry->class
$entry->tied
$entry->class
Truth table66%2/3
A or B
A= $entry->tiedB= $entry->class
| |||||||||||||||||||||||||||||
| 160 | 4 | $entry->clear_id; | |||||||||||||||||||||||||||
| 161 | 4 | $_[1] = $entry; | |||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||
| 163 | 5969 | $_[1] = $entry->data; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 5973 | return 1; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } elsif ( ref($data) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 168 | 5950 | ▶ | ref && $self->compact_data($_, $flatten) for @$data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | } elsif ( ref($data) eq 'HASH' ) { | ||||||||||||||||||||||||||||
| 170 | 2339 | ▶ | ref && $self->compact_data($_, $flatten) for values %$data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | ▶ | } elsif ( ref($data) eq 'SCALAR' || ref($data) eq 'REF' ) { | |||||||||||||||||||||||||||
|
Condition100%3/3
ref $data eq "SCALAR" or ref $data eq "REF"
MC/DC100%2/2
ref $data eq "SCALAR" or ref $data eq "REF"
ref $data eq "SCALAR"
ref $data eq "REF"
Truth table100%3/3
A or B
A= ref $data eq "SCALAR"B= ref $data eq "REF"
| |||||||||||||||||||||||||||||
| 172 | 43 | $self->compact_data($$data, $flatten); | |||||||||||||||||||||||||||
| 173 | } elsif ( ref($data) eq 'KiokuDB::Entry' ) { | ||||||||||||||||||||||||||||
| 174 | 90 | $self->compact_entry($data, $flatten); | |||||||||||||||||||||||||||
| 175 | } else { | ||||||||||||||||||||||||||||
| 176 | # passthrough | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 11167 | return; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | sub imply_root { | ||||||||||||||||||||||||||||
| 183 | 888 | ▶ | my ( $self, @ids ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
imply_root: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 888 | my $entries = $self->_entries; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 888 | foreach my $id ( @ids ) { | |||||||||||||||||||||||||||
| 188 | 1595 | ▶ | my $entry = $entries->{$id} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 1462 | ▶ | next if $entry->has_root; # set by typemap | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 1462 | $entry->root(1); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub commit { | ||||||||||||||||||||||||||||
| 195 | 1292 | ▶ | my ( $self, $backend ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
commit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 1292 | $self->insert_to_backend($backend); | |||||||||||||||||||||||||||
| 198 | 1292 | $self->update_entries( in_storage => 1 ); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | sub insert_to_backend { | ||||||||||||||||||||||||||||
| 202 | 1292 | ▶ | my ( $self, $backend ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
insert_to_backend: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1292 | $backend->insert(values %{ $self->_entries }); | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | sub update_entries { | ||||||||||||||||||||||||||||
| 208 | 1299 | ▶ | my ( $self, @shared_args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
update_entries: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1299 | my ( $e, $o ) = ( $self->_entries, $self->_ids ); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 1299 | my $l = $self->live_objects; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 1299 | my $args = $self->_entry_args; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 1299 | foreach my $id ( keys %$e ) { | |||||||||||||||||||||||||||
| 217 | 3123 | my ( $object, $entry ) = ( $o->{$id}, $e->{$id} ); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 3123 | ▶ | my @args = @{ $args->{$id} || [] }; # FIXME XXX FIXME FIXME XXX BLAH BLAH | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 3123 | $l->register_entry( $id => $entry, @shared_args ); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 3123 | ▶ | unless ( $l->object_to_id($object) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 2020 | $l->register_object( $id => $object, @args ); | |||||||||||||||||||||||||||
| 225 | } else { | ||||||||||||||||||||||||||||
| 226 | 1103 | $l->update_object_entry( $object, $entry, @args ); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 20 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 20 | __PACKAGE__ | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | __END__ | ||||||||||||||||||||||||||||