blib/lib/DBM/Deep/Sector/File/Reference.pm

filter: Statementstmt 100% 220 / 220 Branchbran 91.0% 91 / 100 Conditioncond 83.3% 25 / 30 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 16 totaltotal 90.8% 369 / 406 CC 66 SCAR 23.1
CC
66
Cov
96%
get_bucket_list
21.1
write_data
12.0
delete_key
9.0
CRAP 10.1
« blib/lib/DBM/Deep/ConfigData.pm ↑ Summary blib/lib/DBM/Deep/Sector/DBI.pm »
1 package DBM::Deep::Sector::File::Reference;
2  
3 53 ▶use 5.008_004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 53 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 53 ▶use base qw( DBM::Deep::Sector::File::Data );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 53 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 53 my $STALE_SIZE = 2;
13  
14 # Please refer to the pack() documentation for further information
15 53 my %StP = (
16 1 => 'C', # Unsigned char value (no order needed as it's just one byte)
17 2 => 'n', # Unsigned short in "network" (big-endian) order
18 4 => 'N', # Unsigned long in "network" (big-endian) order
19 8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent)
20 );
21  
22 sub _init {
23 9647 ▶ my $self = shift;
Subroutine _init: called CC=3 SCAR=11.0
24  
25 9647 my $e = $self->engine;
26  
27 9647 ▶ unless ( $self->offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->offset) { } 9338 309 9647
28 309 my $classname = Scalar::Util::blessed( delete $self->{data} );
29 309 my $leftover = $self->size - $self->base_size - 3 * $e->byte_size;
30  
31 309 my $class_offset = 0;
32 309 ▶ if ( defined $classname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $classname) 22 287 309
33 22 my $class_sector = DBM::Deep::Sector::File::Scalar->new({
34 engine => $e,
35 data => $classname,
36 });
37 22 $class_offset = $class_sector->offset;
38 }
39  
40 309 $self->{offset} = $e->_request_data_sector( $self->size );
41 309 $e->storage->print_at( $self->offset, $self->type ); # Sector type
42 # Skip staleness counter
43 $e->storage->print_at( $self->offset + $self->base_size,
44 pack( $StP{$e->byte_size}, 0 ), # Index/BList loc
45 pack( $StP{$e->byte_size}, $class_offset ), # Classname loc
46 309 pack( $StP{$e->byte_size}, 1 ), # Initial refcount
47 chr(0) x $leftover, # Zero-fill the rest
48 );
49 }
50 else {
51 9338 $self->{type} = $e->storage->read_at( $self->offset, 1 );
52 }
53  
54 $self->{staleness} = unpack(
55 9647 $StP{$STALE_SIZE},
56 $e->storage->read_at( $self->offset + $e->SIG_SIZE, $STALE_SIZE ),
57 );
58  
59 9647 return;
60 }
61  
62 sub get_data_location_for {
63 4310 ▶ my $self = shift;
Subroutine get_data_location_for: called CC=6 SCAR=18.0
Pod undocumented
64 4310 my ($args) = @_;
65  
66 # Assume that the head is not allowed unless otherwise specified.
67 4310 ▶ $args->{allow_head} = 0 unless exists $args->{allow_head};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"allow_head"} 0 4310 4310
68  
69 # Assume we don't create a new blist location unless otherwise specified.
70 4310 ▶ $args->{create} = 0 unless exists $args->{create};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"create"} 4310 0 4310
71  
72 my $blist = $self->get_bucket_list({
73 key_md5 => $args->{key_md5},
74 key => $args->{key},
75 create => $args->{create},
76 4310 });
77 4310 ▶ return unless $blist && $blist->{found};
Branch: 1 branch
BranchTrueFalseTotal
unless $blist and $blist->{"found"} 1103 3207 4310
Condition100%3/3
$blist and $blist->{"found"}
!ll&&!rl&&r
2648393207
MC/DC100%2/2
$blist and $blist->{"found"}
$blist $blist->{"found"}
Truth table100%3/3
A and B
A= $blistB= $blist->{"found"}
ABresult
0X0
100
111
78  
79 # At this point, $blist knows where the md5 is. What it -doesn't- know yet
80 # is whether or not this transaction has this key. That's part of the next
81 # function call.
82 my $location = $blist->get_data_location_for({
83 allow_head => $args->{allow_head},
84 3207 ▶ }) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $location = $blist->get_data_location_for({"allow_head", $args->{"allow_head"}}) or return 53 3154 3207
85  
86 3154 return $location;
87 }
88  
89 sub get_data_for {
90 3297 ▶ my $self = shift;
Subroutine get_data_for: called CC=2 SCAR=6.9
Pod undocumented
91 3297 my ($args) = @_;
92  
93 3297 ▶ my $location = $self->get_data_location_for( $args )
Branch: 1 branch
BranchTrueFalseTotal
my $location = $self->get_data_location_for($args) or return 147 3150 3297
94 or return;
95  
96 3150 return $self->engine->load_sector( $location );
97 }
98  
99 sub write_data {
100 1838 ▶ my $self = shift;
Subroutine write_data: called CC=12 SCAR=24.9
Pod undocumented
101 1838 my ($args) = @_;
102  
103 my $blist = $self->get_bucket_list({
104 key_md5 => $args->{key_md5},
105 key => $args->{key},
106 1838 ▶ create => 1,
Branch: 1 branch
BranchTrueFalseTotal
my $blist = $self->get_bucket_list({"key_md5", $args->{"key_md5"}, "key", $args->{"key"}, "create", 1}) or "DBM::Deep"->_throw_error("How did write_data fail (no blist)?!") 0 1838 1838
107 }) or DBM::Deep->_throw_error( "How did write_data fail (no blist)?!" );
108  
109 # Handle any transactional bookkeeping.
110 1838 ▶ if ( $self->engine->trans_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->engine->trans_id) { } 67 1771 1838
111 67 ▶ if ( ! $blist->has_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blist->has_md5) 40 27 67
112 40 $blist->mark_deleted({
113 trans_id => 0,
114 });
115 }
116 }
117 else {
118 1771 my @trans_ids = $self->engine->get_running_txn_ids;
119 1771 ▶ if ( $blist->has_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blist->has_md5) { } 169 1602 1771
120 169 ▶ if ( @trans_ids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@trans_ids) 4 165 169
121 4 my $old_value = $blist->get_data_for;
122 4 foreach my $other_trans_id ( @trans_ids ) {
123 4 ▶ next if $blist->get_data_location_for({
Branch: 1 branch
BranchTrueFalseTotal
if $blist->get_data_location_for({"trans_id", $other_trans_id, "allow_head", 0}) 1 3 4
124 trans_id => $other_trans_id,
125 allow_head => 0,
126 });
127 $blist->write_md5({
128 trans_id => $other_trans_id,
129 key => $args->{key},
130 key_md5 => $args->{key_md5},
131 3 value => $old_value->clone,
132 });
133 }
134 }
135 }
136 else {
137 1602 ▶ if ( @trans_ids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@trans_ids) 2 1600 1602
138 2 foreach my $other_trans_id ( @trans_ids ) {
139 #XXX This doesn't seem to possible to ever happen . . .
140 2 ▶ next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 });
Branch: 1 branch
BranchTrueFalseTotal
if $blist->get_data_location_for({"trans_id", $other_trans_id, "allow_head", 0}) 0 2 2
141 2 $blist->mark_deleted({
142 trans_id => $other_trans_id,
143 });
144 }
145 }
146 }
147 }
148  
149 #XXX Is this safe to do transactionally?
150 # Free the place we're about to write to.
151 1838 ▶ if ( $blist->get_data_location_for({ allow_head => 0 }) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blist->get_data_location_for({"allow_head", 0})) 176 1662 1838
152 176 $blist->get_data_for({ allow_head => 0 })->free;
153 }
154  
155 $blist->write_md5({
156 key => $args->{key},
157 key_md5 => $args->{key_md5},
158 value => $args->{value},
159 1838 });
160 }
161  
162 sub delete_key {
163 65 ▶ my $self = shift;
Subroutine delete_key: called CC=9 SCAR=22.0
Pod undocumented
164 65 my ($args) = @_;
165  
166 # This can return nothing if we are deleting an entry in a hashref that was
167 # auto-vivified as part of the delete process. For example:
168 # my $x = {};
169 # delete $x->{foo}{bar};
170 my $blist = $self->get_bucket_list({
171 key_md5 => $args->{key_md5},
172 65 ▶ }) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $blist = $self->get_bucket_list({"key_md5", $args->{"key_md5"}}) or return 1 64 65
173  
174 # Save the location so that we can free the data
175 64 my $location = $blist->get_data_location_for({
176 allow_head => 0,
177 });
178 64 ▶ my $old_value = $location && $self->engine->load_sector( $location );
Condition66%2/3
$location && $self->engine->load_sector($location)
!ll&&!rl&&r
13051
MC/DC50%1/2
$location && $self->engine->load_sector($location)
$location $self->engine->load_sector($location)
Truth table66%2/3
A && B
A= $locationB= $self->engine->load_sector($location)
ABresult
0X0
100
111
179  
180 64 my @trans_ids = $self->engine->get_running_txn_ids;
181  
182 # If we're the HEAD and there are running txns, then we need to clone this
183 # value to the other transactions to preserve Isolation.
184 64 ▶ if ( $self->engine->trans_id == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->engine->trans_id == 0) 53 11 64
185 53 ▶ if ( @trans_ids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@trans_ids) 1 52 53
186 1 foreach my $other_trans_id ( @trans_ids ) {
187 1 ▶ next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 });
Branch: 1 branch
BranchTrueFalseTotal
if $blist->get_data_location_for({"trans_id", $other_trans_id, "allow_head", 0}) 0 1 1
188 $blist->write_md5({
189 trans_id => $other_trans_id,
190 key => $args->{key},
191 key_md5 => $args->{key_md5},
192 1 value => $old_value->clone,
193 });
194 }
195 }
196 }
197  
198 64 my $data;
199 64 ▶ if ( @trans_ids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@trans_ids) { } 12 52 64
200 12 $blist->mark_deleted( $args );
201  
202 12 ▶ if ( $old_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_value) 1 11 12
203 #XXX Is this export => 1 actually doing anything?
204 1 $data = $old_value->data({ export => 1 });
205 1 $old_value->free;
206 }
207 }
208 else {
209 52 $data = $blist->delete_md5( $args );
210 }
211  
212 64 return $data;
213 }
214  
215 sub write_blist_loc {
216 256 ▶ my $self = shift;
Subroutine write_blist_loc: called CC=1 SCAR=0.0
Pod undocumented
217 256 my ($loc) = @_;
218  
219 256 my $engine = $self->engine;
220 $engine->storage->print_at( $self->offset + $self->base_size,
221 256 pack( $StP{$engine->byte_size}, $loc ),
222 );
223 }
224  
225 sub get_blist_loc {
226 6635 ▶ my $self = shift;
Subroutine get_blist_loc: called CC=1 SCAR=0.0
Pod undocumented
227  
228 6635 my $e = $self->engine;
229 6635 my $blist_loc = $e->storage->read_at( $self->offset + $self->base_size, $e->byte_size );
230 6635 return unpack( $StP{$e->byte_size}, $blist_loc );
231 }
232  
233 sub get_bucket_list {
234 6213 ▶ my $self = shift;
Subroutine get_bucket_list: called CC=21 SCAR=30.5
Pod undocumented
235 6213 my ($args) = @_;
236 6213 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
62130
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
237  
238 # XXX Add in check here for recycling?
239  
240 6213 my $engine = $self->engine;
241  
242 6213 my $blist_loc = $self->get_blist_loc;
243  
244 # There's no index or blist yet
245 6213 ▶ unless ( $blist_loc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blist_loc) 282 5931 6213
246 282 ▶ return unless $args->{create};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"create"} 26 256 282
247  
248 my $blist = DBM::Deep::Sector::File::BucketList->new({
249 engine => $engine,
250 key_md5 => $args->{key_md5},
251 256 });
252  
253 256 $self->write_blist_loc( $blist->offset );
254 # $engine->storage->print_at( $self->offset + $self->base_size,
255 # pack( $StP{$engine->byte_size}, $blist->offset ),
256 # );
257  
258 256 return $blist;
259 }
260  
261 5931 ▶ my $sector = $engine->load_sector( $blist_loc )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $engine->load_sector($blist_loc) or "DBM::Deep"->_throw_error("Cannot read sector at $blist_loc in get_bucket_list()") 0 5931 5931
262 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
263 5931 my $i = 0;
264 5931 my $last_sector = undef;
265 5931 ▶ while ( $sector->isa( 'DBM::Deep::Sector::File::Index' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sector->isa("DBM::Deep::Sector::File::Index")) 4074 5438 9512
266 4074 $blist_loc = $sector->get_entry( ord( substr( $args->{key_md5}, $i++, 1 ) ) );
267 4074 $last_sector = $sector;
268 4074 ▶ if ( $blist_loc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blist_loc) { } 3581 493 4074
269 3581 ▶ $sector = $engine->load_sector( $blist_loc )
Branch: 1 branch
BranchTrueFalseTotal
$sector = $engine->load_sector($blist_loc) or "DBM::Deep"->_throw_error("Cannot read sector at $blist_loc in get_bucket_list()") 0 3581 3581
270 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
271 }
272 else {
273 493 $sector = undef;
274 493 last;
275 }
276 }
277  
278 # This means we went through the Index sector(s) and found an empty slot
279 5931 ▶ unless ( $sector ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sector) 493 5438 5931
280 493 ▶ return unless $args->{create};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"create"} 239 254 493
281  
282 254 ▶ DBM::Deep->_throw_error( "No last_sector when attempting to build a new entry" )
Branch: 1 branch
BranchTrueFalseTotal
unless $last_sector 0 254 254
283 unless $last_sector;
284  
285 my $blist = DBM::Deep::Sector::File::BucketList->new({
286 engine => $engine,
287 key_md5 => $args->{key_md5},
288 254 });
289  
290 254 $last_sector->set_entry( ord( substr( $args->{key_md5}, $i - 1, 1 ) ) => $blist->offset );
291  
292 254 return $blist;
293 }
294  
295 5438 $sector->find_md5( $args->{key_md5} );
296  
297 # See whether or not we need to reindex the bucketlist
298 # Yes, the double-braces are there for a reason. if() doesn't create a
299 # redo-able block, so we have to create a bare block within the if() for
300 # redo-purposes.
301 # Patch and idea submitted by sprout@cpan.org. -RobK, 2008-01-09
302 5438 ▶ if ( !$sector->has_md5 && $args->{create} && $sector->{idx} == -1 ) {{
Branch: 1 branch
BranchTrueFalseTotal
if (not $sector->has_md5 and $args->{"create"} and $sector->{"idx"} == -1) 7 5431 5438
Condition100%6/6
not $sector->has_md5 and $args->{"create"} and $sector->{"idx"} == -1
!ll&&!rl&&r
429911327
not $sector->has_md5 and $args->{"create"}
!ll&&!rl&&r
34588411139
MC/DC100%3/3
not $sector->has_md5 and $args->{"create"} and $sector->{"idx"} == -1
not $sector->has_md5 $args->{"create"} $sector->{"idx"} == -1
Truth table100%4/4
not(A) and B and C
A= not $sector->has_md5B= $args->{"create"}C= $sector->{"idx"} == -1
ABCresult
0XX0
10X0
1100
1111
303 7 my $redo;
304  
305 10 my $new_index = DBM::Deep::Sector::File::Index->new({
306 engine => $engine,
307 });
308  
309 10 my %blist_cache;
310 #XXX q.v. the comments for this function.
311 10 foreach my $entry ( $sector->chopped_up ) {
312 160 my ($spot, $md5) = @{$entry};
313 160 my $idx = ord( substr( $md5, $i, 1 ) );
314  
315 # XXX This is inefficient
316 160 ▶ my $blist = $blist_cache{$idx}
Condition66%2/3
$blist_cache{$idx} ||= "DBM::Deep::Sector::File::BucketList"->new({"engine", $engine})
l!l&&r!l&&!r
63970
MC/DC0%0/2
$blist_cache{$idx} ||= "DBM::Deep::Sector::File::BucketList"->new({"engine", $engine})
$blist_cache{$idx} "DBM::Deep::Sector::File::BucketList"->new({"engine", $engine})
Truth table66%2/3
A ||= B
A= $blist_cache{$idx}B= "DBM::Deep::Sector::File::BucketList"->new({"engine", $engine})
ABresult
1X1
011
000
317 ||= DBM::Deep::Sector::File::BucketList->new({
318 engine => $engine,
319 });
320  
321 160 $new_index->set_entry( $idx => $blist->offset );
322  
323 160 my $new_spot = $blist->write_at_next_open( $md5 );
324 160 $engine->reindex_entry( $spot => $new_spot );
325 }
326  
327 # Handle the new item separately.
328 {
329 10 my $idx = ord( substr( $args->{key_md5}, $i, 1 ) );
330  
331 # If all the previous blist's items have been thrown into one
332 # blist and the new item belongs in there too, we need
333 # another index.
334 10 ▶ if ( keys %blist_cache == 1 and each %blist_cache == $idx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %blist_cache == 1 and each %blist_cache == $idx) { } 3 7 10
Condition66%2/3
keys %blist_cache == 1 and each %blist_cache == $idx
!ll&&!rl&&r
703
MC/DC50%1/2
keys %blist_cache == 1 and each %blist_cache == $idx
keys %blist_cache == 1 each %blist_cache == $idx
Truth table66%2/3
A and B
A= keys %blist_cache == 1B= each %blist_cache == $idx
ABresult
0X0
100
111
335 3 ++$i, ++$redo;
336 } else {
337 7 ▶ my $blist = $blist_cache{$idx}
Condition66%2/3
$blist_cache{$idx} ||= "DBM::Deep::Sector::File::BucketList"->new({"engine", $engine})
l!l&&r!l&&!r
160
MC/DC0%0/2
$blist_cache{$idx} ||= "DBM::Deep::Sector::File::BucketList"->new({"engine", $engine})
$blist_cache{$idx} "DBM::Deep::Sector::File::BucketList"->new({"engine", $engine})
Truth table66%2/3
A ||= B
A= $blist_cache{$idx}B= "DBM::Deep::Sector::File::BucketList"->new({"engine", $engine})
ABresult
1X1
011
000
338 ||= DBM::Deep::Sector::File::BucketList->new({
339 engine => $engine,
340 });
341
342 7 $new_index->set_entry( $idx => $blist->offset );
343
344 #XXX THIS IS HACKY!
345 7 $blist->find_md5( $args->{key_md5} );
346 $blist->write_md5({
347 key => $args->{key},
348 key_md5 => $args->{key_md5},
349 7 value => DBM::Deep::Sector::File::Null->new({
350 engine => $engine,
351 data => undef,
352 }),
353 });
354 }
355 }
356  
357 10 ▶ if ( $last_sector ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_sector) { } 3 7 10
358 $last_sector->set_entry(
359 3 ord( substr( $args->{key_md5}, $i - 1, 1 ) ),
360 $new_index->offset,
361 );
362 } else {
363 $engine->storage->print_at( $self->offset + $self->base_size,
364 7 pack( $StP{$engine->byte_size}, $new_index->offset ),
365 );
366 }
367  
368 10 $sector->wipe;
369 10 $sector->free;
370  
371 10 ▶ if ( $redo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redo) 3 7 10
372 3 (undef, $sector) = %blist_cache;
373 3 $last_sector = $new_index;
374 3 redo;
375 }
376  
377 7 $sector = $blist_cache{ ord( substr( $args->{key_md5}, $i, 1 ) ) };
378 7 $sector->find_md5( $args->{key_md5} );
379 }}
380  
381 5438 return $sector;
382 }
383  
384 sub get_class_offset {
385 2380 ▶ my $self = shift;
Subroutine get_class_offset: called CC=1 SCAR=0.0
Pod undocumented
386  
387 2380 my $e = $self->engine;
388 return unpack(
389 2380 $StP{$e->byte_size},
390 $e->storage->read_at(
391 $self->offset + $self->base_size + 1 * $e->byte_size, $e->byte_size,
392 ),
393 );
394 }
395  
396 sub get_classname {
397 2340 ▶ my $self = shift;
Subroutine get_classname: called CC=2 SCAR=6.9
Pod undocumented
398  
399 2340 my $class_offset = $self->get_class_offset;
400  
401 2340 ▶ return unless $class_offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $class_offset 2297 43 2340
402  
403 43 return $self->engine->load_sector( $class_offset )->data;
404 }
405  
406 # Look to hoist this method into a ::Reference trait
407 sub data {
408 2357 ▶ my $self = shift;
Subroutine data: called CC=8 SCAR=20.8
Pod undocumented
409 2357 my ($args) = @_;
410 2357 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
162341
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
411  
412 2357 my $engine = $self->engine;
413 2357 ▶ my $cache_entry = $engine->cache->{ $self->offset } ||= {};
Condition100%2/2
$engine->cache->{$self->offset} ||= {}
l!l
2234123
MC/DC100%1/1
$engine->cache->{$self->offset} ||= {}
$engine->cache->{$self->offset}
Truth table100%2/2
A
A= $engine->cache->{$self->offset}
Aresult
11
00
414 2357 my $trans_id = $engine->trans_id;
415 2357 my $obj;
416 2357 ▶ if ( !defined $$cache_entry{ $trans_id } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cache_entry->{$trans_id}) { } 2336 21 2357
417 2336 $obj = DBM::Deep->new({
418 type => $self->type,
419 base_offset => $self->offset,
420 staleness => $self->staleness,
421 storage => $engine->storage,
422 engine => $engine,
423 });
424  
425 2336 $$cache_entry{ $trans_id } = $obj;
426 2336 Scalar::Util::weaken($$cache_entry{ $trans_id });
427 }
428 else {
429 21 $obj = $$cache_entry{ $trans_id };
430 }
431  
432 # We're not exporting, so just return.
433 2357 ▶ unless ( $args->{export} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"export"}) 2341 16 2357
434 2341 ▶ if ( $engine->storage->{autobless} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine->storage->{"autobless"}) 2328 13 2341
435 2328 my $classname = $self->get_classname;
436 2328 ▶ if ( defined $classname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $classname) 39 2289 2328
437 39 bless $obj, $classname;
438 }
439 }
440  
441 2341 return $obj;
442 }
443  
444 # We shouldn't export if this is still referred to.
445 16 ▶ if ( $self->get_refcount > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_refcount > 1) 10 6 16
446 10 return $obj;
447 }
448  
449 6 return $obj->export;
450 }
451  
452 sub free {
453 74 ▶ my $self = shift;
Subroutine free: called CC=8 SCAR=20.8
Pod undocumented
454  
455 # We're not ready to be removed yet.
456 74 ▶ return if $self->decrement_refcount > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->decrement_refcount > 0 34 40 74
457  
458 40 my $e = $self->engine;
459  
460 # Rebless the object into DBM::Deep::Null.
461 # In external_refs mode, this will already have been removed from
462 # the cache, so we can skip this.
463 40 ▶ if(!$e->{external_refs}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($e->{"external_refs"}) 33 7 40
464 # eval { %{ $e->cache->{ $self->offset }{ $e->trans_id } } = (); };
465 # eval { @{ $e->cache->{ $self->offset }{ $e->trans_id } } = (); };
466 33 my $cache = $e->cache;
467 33 my $off = $self->offset;
468 33 ▶ if( exists $cache->{ $off }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache->{$off} and exists $cache->{$off}{my $trans_id = $e->trans_id}) 21 12 33
Condition100%3/3
exists $cache->{$off} and exists $cache->{$off}{my $trans_id = $e->trans_id}
!ll&&!rl&&r
11121
MC/DC100%2/2
exists $cache->{$off} and exists $cache->{$off}{my $trans_id = $e->trans_id}
exists $cache->{$off} exists $cache->{$off}{my $trans_id = $e->trans_id}
Truth table100%3/3
A and B
A= exists $cache->{$off}B= exists $cache->{$off}{my $trans_id = $e->trans_id}
ABresult
0X0
100
111
469 and exists $cache->{ $off }{ my $trans_id = $e->trans_id } ) {
470 bless $cache->{ $off }{ $trans_id }, 'DBM::Deep::Null'
471 21 ▶ if defined $cache->{ $off }{ $trans_id };
Branch: 1 branch
BranchTrueFalseTotal
if defined $cache->{$off}{$trans_id} 1 20 21
472 21 delete $cache->{ $off }{ $trans_id };
473 }
474 }
475  
476 40 my $blist_loc = $self->get_blist_loc;
477 40 ▶ $e->load_sector( $blist_loc )->free if $blist_loc;
Branch: 1 branch
BranchTrueFalseTotal
if $blist_loc 23 17 40
478  
479 40 my $class_loc = $self->get_class_offset;
480 40 ▶ $e->load_sector( $class_loc )->free if $class_loc;
Branch: 1 branch
BranchTrueFalseTotal
if $class_loc 10 30 40
481  
482 40 $self->SUPER::free();
483 }
484  
485 sub increment_refcount {
486 1040 ▶ my $self = shift;
Subroutine increment_refcount: called CC=1 SCAR=0.0
Pod undocumented
487  
488 1040 my $refcount = $self->get_refcount;
489  
490 1040 $refcount++;
491  
492 1040 $self->write_refcount( $refcount );
493  
494 1040 return $refcount;
495 }
496  
497 sub decrement_refcount {
498 74 ▶ my $self = shift;
Subroutine decrement_refcount: called CC=1 SCAR=0.0
Pod undocumented
499  
500 74 my $refcount = $self->get_refcount;
501  
502 74 $refcount--;
503  
504 74 $self->write_refcount( $refcount );
505  
506 74 return $refcount;
507 }
508  
509 sub get_refcount {
510 1133 ▶ my $self = shift;
Subroutine get_refcount: called CC=1 SCAR=0.0
Pod undocumented
511  
512 1133 my $e = $self->engine;
513 return unpack(
514 1133 $StP{$e->byte_size},
515 $e->storage->read_at(
516 $self->offset + $self->base_size + 2 * $e->byte_size, $e->byte_size,
517 ),
518 );
519 }
520  
521 sub write_refcount {
522 1114 ▶ my $self = shift;
Subroutine write_refcount: called CC=1 SCAR=0.0
Pod undocumented
523 1114 my ($num) = @_;
524  
525 1114 my $e = $self->engine;
526 $e->storage->print_at(
527 $self->offset + $self->base_size + 2 * $e->byte_size,
528 1114 pack( $StP{$e->byte_size}, $num ),
529 );
530 }
531  
532 sub clear {
533 225 ▶ my $self = shift;
Subroutine clear: called CC=4 SCAR=13.9
Pod undocumented
534  
535 225 ▶ my $blist_loc = $self->get_blist_loc or return;
Branch: 1 branch
BranchTrueFalseTotal
my $blist_loc = $self->get_blist_loc or return 215 10 225
536  
537 10 my $engine = $self->engine;
538  
539 # This won't work with autoblessed items.
540 10 ▶ if ($engine->get_running_txn_ids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine->get_running_txn_ids) 2 8 10
541 # ~~~ Temporary; the code below this block needs to be modified to
542 # take transactions into account.
543 2 $self->data->_get_self->_clear;
544 2 return;
545 }
546  
547 8 ▶ my $sector = $engine->load_sector( $blist_loc )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $engine->load_sector($blist_loc) or "DBM::Deep"->_throw_error("Cannot read sector at $blist_loc in clear()") 0 8 8
548 or DBM::Deep->_throw_error(
549 "Cannot read sector at $blist_loc in clear()"
550 );
551  
552 # Set blist offset to 0
553 $engine->storage->print_at( $self->offset + $self->base_size,
554 8 pack( $StP{$engine->byte_size}, 0 ),
555 );
556  
557 # Free the blist
558 8 $sector->free;
559  
560 8 return;
561 }
562  
563 53 1;
564 __END__
« blib/lib/DBM/Deep/ConfigData.pm ↑ Summary blib/lib/DBM/Deep/Sector/DBI.pm »