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

filter: Statementstmt 98.2% 164 / 167 Branchbran 85.7% 60 / 70 Conditioncond 83.3% 10 / 12 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 15 totaltotal 88.9% 258 / 290 CC 45 SCAR 17.0
CC
45
Cov
94%
get_data_location_for
12.0
write_md5
7.1
find_md5
6.0
CRAP 5.5
« blib/lib/DBM/Deep/Array.pm ↑ Summary _build/lib/Module/Build/Custom.pm »
1 package DBM::Deep::Sector::File::BucketList;
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 );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 53 my $STALE_SIZE = 2;
11  
12 # Please refer to the pack() documentation for further information
13 53 my %StP = (
14 1 => 'C', # Unsigned char value (no order needed as it's just one byte)
15 2 => 'n', # Unsigned short in "network" (big-endian) order
16 4 => 'N', # Unsigned long in "network" (big-endian) order
17 8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent)
18 );
19  
20 sub _init {
21 6418 ▶ my $self = shift;
Subroutine _init: called CC=3 SCAR=11.0
22  
23 6418 my $engine = $self->engine;
24  
25 6418 ▶ unless ( $self->offset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->offset) 613 5805 6418
26 613 my $leftover = $self->size - $self->base_size;
27  
28 613 $self->{offset} = $engine->_request_blist_sector( $self->size );
29 613 $engine->storage->print_at( $self->offset, $engine->SIG_BLIST ); # Sector type
30 # Skip staleness counter
31 613 $engine->storage->print_at( $self->offset + $self->base_size,
32 chr(0) x $leftover, # Zero-fill the data
33 );
34 }
35  
36 6418 ▶ if ( $self->{key_md5} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"key_md5"}) 510 5908 6418
37 510 $self->find_md5;
38 }
39  
40 6418 return $self;
41 }
42  
43 sub wipe {
44 10 ▶ my $self = shift;
Subroutine wipe: called CC=1 SCAR=0.0
Pod undocumented
45 10 $self->engine->storage->print_at( $self->offset + $self->base_size,
46 chr(0) x ($self->size - $self->base_size), # Zero-fill the data
47 );
48 }
49  
50 sub size {
51 1325 ▶ my $self = shift;
Subroutine size: called CC=2 SCAR=6.9
Pod undocumented
52 1325 ▶ unless ( $self->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"size"}) 656 669 1325
53 656 my $e = $self->engine;
54 # Base + numbuckets * bucketsize
55 656 $self->{size} = $self->base_size + $e->max_buckets * $self->bucket_size;
56 }
57 1325 return $self->{size};
58 }
59  
60 41 ▶sub free_meth { '_add_free_blist_sector' }
Subroutine free_meth: called CC=1 SCAR=0.0
Pod undocumented
61  
62 sub free {
63 41 ▶ my $self = shift;
Subroutine free: called CC=6 SCAR=17.9
Pod undocumented
64  
65 41 my $e = $self->engine;
66 41 foreach my $bucket ( $self->chopped_up ) {
67 75 my $rest = $bucket->[-1];
68  
69 # Delete the keysector
70 75 my $l = unpack( $StP{$e->byte_size}, substr( $rest, $e->hash_size, $e->byte_size ) );
71 75 ▶ my $s = $e->load_sector( $l ); $s->free if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 75 0 75
72  
73 # Delete the HEAD sector
74 75 $l = unpack( $StP{$e->byte_size},
75 substr( $rest,
76 $e->hash_size + $e->byte_size,
77 $e->byte_size,
78 ),
79 );
80 75 ▶ $s = $e->load_sector( $l ); $s->free if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 73 2 75
81  
82 75 foreach my $txn ( 0 .. $e->num_txns - 2 ) {
83 4 my $l = unpack( $StP{$e->byte_size},
84 substr( $rest,
85 $e->hash_size + 2 * $e->byte_size + $txn * ($e->byte_size + $STALE_SIZE),
86 $e->byte_size,
87 ),
88 );
89 4 ▶ my $s = $e->load_sector( $l ); $s->free if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 2 2 4
90 }
91 }
92  
93 41 $self->SUPER::free();
94 }
95  
96 sub bucket_size {
97 30961 ▶ my $self = shift;
Subroutine bucket_size: called CC=2 SCAR=6.9
Pod undocumented
98 30961 ▶ unless ( $self->{bucket_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bucket_size"}) 6418 24543 30961
99 6418 my $e = $self->engine;
100 # Key + head (location) + transactions (location + staleness-counter)
101 6418 my $location_size = $e->byte_size + $e->byte_size + ($e->num_txns - 1) * ($e->byte_size + $STALE_SIZE);
102 6418 $self->{bucket_size} = $e->hash_size + $location_size;
103 }
104 30961 return $self->{bucket_size};
105 }
106  
107 # XXX This is such a poor hack. I need to rethink this code.
108 sub chopped_up {
109 53 ▶ my $self = shift;
Subroutine chopped_up: called CC=3 SCAR=11.0
Pod undocumented
110  
111 53 my $e = $self->engine;
112  
113 53 my @buckets;
114 53 foreach my $idx ( 0 .. $e->max_buckets - 1 ) {
115 281 my $spot = $self->offset + $self->base_size + $idx * $self->bucket_size;
116 281 my $md5 = $e->storage->read_at( $spot, $e->hash_size );
117  
118 #XXX If we're chopping, why would we ever have the blank_md5?
119 281 ▶ last if $md5 eq $e->blank_md5;
Branch: 1 branch
BranchTrueFalseTotal
if $md5 eq $e->blank_md5 43 238 281
120  
121 238 my $rest = $e->storage->read_at( undef, $self->bucket_size - $e->hash_size );
122 238 push @buckets, [ $spot, $md5 . $rest ];
123 }
124  
125 53 return @buckets;
126 }
127  
128 sub write_at_next_open {
129 160 ▶ my $self = shift;
Subroutine write_at_next_open: called CC=2 SCAR=6.9
Pod undocumented
130 160 my ($entry) = @_;
131  
132 #XXX This is such a hack!
133 160 ▶ $self->{_next_open} = 0 unless exists $self->{_next_open};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_next_open"} 97 63 160
134  
135 160 my $spot = $self->offset + $self->base_size + $self->{_next_open}++ * $self->bucket_size;
136 160 $self->engine->storage->print_at( $spot, $entry );
137  
138 160 return $spot;
139 }
140  
141 sub has_md5 {
142 7276 ▶ my $self = shift;
Subroutine has_md5: called CC=2 SCAR=7.6
Pod undocumented
143 7276 ▶ unless ( exists $self->{found} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"found"}) 0 7276 7276
144 0 $self->find_md5;
145 }
146 7276 return $self->{found};
147 }
148  
149 sub find_md5 {
150 5962 ▶ my $self = shift;
Subroutine find_md5: called CC=6 SCAR=17.9
Pod undocumented
151  
152 5962 $self->{found} = undef;
153 5962 $self->{idx} = -1;
154  
155 5962 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5452 510 5962
156 5452 $self->{key_md5} = shift;
157 }
158  
159 # If we don't have an MD5, then what are we supposed to do?
160 5962 ▶ unless ( exists $self->{key_md5} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"key_md5"}) 0 5962 5962
161 0 DBM::Deep->_throw_error( "Cannot find_md5 without a key_md5 set" );
162 }
163  
164 5962 my $e = $self->engine;
165 5962 foreach my $idx ( 0 .. $e->max_buckets - 1 ) {
166 15553 my $potential = $e->storage->read_at(
167 $self->offset + $self->base_size + $idx * $self->bucket_size, $e->hash_size,
168 );
169  
170 15553 ▶ if ( $potential eq $e->blank_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($potential eq $e->blank_md5) 2489 13064 15553
171 2489 $self->{idx} = $idx;
172 2489 return;
173 }
174  
175 13064 ▶ if ( $potential eq $self->{key_md5} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($potential eq $self->{"key_md5"}) 3465 9599 13064
176 3465 $self->{found} = 1;
177 3465 $self->{idx} = $idx;
178 3465 return;
179 }
180 }
181  
182 8 return;
183 }
184  
185 sub write_md5 {
186 1849 ▶ my $self = shift;
Subroutine write_md5: called CC=7 SCAR=19.5
Pod undocumented
187 1849 my ($args) = @_;
188  
189 1849 ▶ DBM::Deep->_throw_error( "write_md5: no key" ) unless exists $args->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"key"} 0 1849 1849
190 1849 ▶ DBM::Deep->_throw_error( "write_md5: no key_md5" ) unless exists $args->{key_md5};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"key_md5"} 0 1849 1849
191 1849 ▶ DBM::Deep->_throw_error( "write_md5: no value" ) unless exists $args->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"value"} 0 1849 1849
192  
193 1849 my $engine = $self->engine;
194  
195 1849 ▶ $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"trans_id"} 1845 4 1849
196  
197 1849 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
198 1849 $engine->add_entry( $args->{trans_id}, $spot );
199  
200 1849 ▶ unless ($self->{found}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"found"}) 1649 200 1849
201 my $key_sector = DBM::Deep::Sector::File::Scalar->new({
202 engine => $engine,
203 data => $args->{key},
204 1649 });
205  
206 $engine->storage->print_at( $spot,
207 $args->{key_md5},
208 1649 pack( $StP{$engine->byte_size}, $key_sector->offset ),
209 );
210 }
211  
212 1849 my $loc = $spot
213 + $engine->hash_size
214 + $engine->byte_size;
215  
216 1849 ▶ if ( $args->{trans_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'trans_id'}) { } 73 1776 1849
217 73 $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE );
218  
219 $engine->storage->print_at( $loc,
220 pack( $StP{$engine->byte_size}, $args->{value}->offset ),
221 73 pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ),
222 );
223 }
224 else {
225 $engine->storage->print_at( $loc,
226 1776 pack( $StP{$engine->byte_size}, $args->{value}->offset ),
227 );
228 }
229 }
230  
231 sub mark_deleted {
232 54 ▶ my $self = shift;
Subroutine mark_deleted: called CC=4 SCAR=13.9
Pod undocumented
233 54 my ($args) = @_;
234 54 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
540
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
235  
236 54 my $engine = $self->engine;
237  
238 54 ▶ $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"trans_id"} 12 42 54
239  
240 54 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
241 54 $engine->add_entry( $args->{trans_id}, $spot );
242  
243 54 my $loc = $spot
244 + $engine->hash_size
245 + $engine->byte_size;
246  
247 54 ▶ if ( $args->{trans_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'trans_id'}) { } 13 41 54
248 13 $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE );
249  
250 $engine->storage->print_at( $loc,
251 pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted
252 13 pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ),
253 );
254 }
255 else {
256 $engine->storage->print_at( $loc,
257 41 pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted
258 );
259 }
260 }
261  
262 sub delete_md5 {
263 52 ▶ my $self = shift;
Subroutine delete_md5: called CC=2 SCAR=6.9
Pod undocumented
264 52 my ($args) = @_;
265  
266 52 my $engine = $self->engine;
267 52 ▶ return undef unless $self->{found};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"found"} 2 50 52
268  
269 # Save the location so that we can free the data
270 50 my $location = $self->get_data_location_for({
271 allow_head => 0,
272 });
273 50 my $key_sector = $self->get_key_for;
274  
275 50 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
276 $engine->storage->print_at( $spot,
277 $engine->storage->read_at(
278 $spot + $self->bucket_size,
279 50 $self->bucket_size * ( $engine->max_buckets - $self->{idx} - 1 ),
280 ),
281 chr(0) x $self->bucket_size,
282 );
283  
284 50 $key_sector->free;
285  
286 50 my $data_sector = $self->engine->load_sector( $location );
287 50 my $data = $data_sector->data({ export => 1 });
288 50 $data_sector->free;
289  
290 50 return $data;
291 }
292  
293 sub get_data_location_for {
294 11536 ▶ my $self = shift;
Subroutine get_data_location_for: called CC=12 SCAR=24.9
Pod undocumented
295 11536 my ($args) = @_;
296 11536 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
115360
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
297  
298 11536 ▶ $args->{allow_head} = 0 unless exists $args->{allow_head};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"allow_head"} 0 11536 11536
299 11536 ▶ $args->{trans_id} = $self->engine->trans_id unless exists $args->{trans_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"trans_id"} 10621 915 11536
300 11536 ▶ $args->{idx} = $self->{idx} unless exists $args->{idx};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"idx"} 5346 6190 11536
301  
302 11536 my $e = $self->engine;
303  
304 my $spot = $self->offset + $self->base_size
305 11536 + $args->{idx} * $self->bucket_size
306 + $e->hash_size
307 + $e->byte_size;
308  
309 11536 ▶ if ( $args->{trans_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trans_id"}) 1103 10433 11536
310 1103 $spot += $e->byte_size + ($args->{trans_id} - 1) * ( $e->byte_size + $STALE_SIZE );
311 }
312  
313 11536 my $buffer = $e->storage->read_at(
314 $spot,
315 $e->byte_size + $STALE_SIZE,
316 );
317 11536 my ($loc, $staleness) = unpack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, $buffer );
318  
319 # XXX Merge the two if-clauses below
320 11536 ▶ if ( $args->{trans_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trans_id"}) 1103 10433 11536
321 # We have found an entry that is old, so get rid of it
322 1103 ▶ if ( $staleness != (my $s = $e->get_txn_staleness_counter( $args->{trans_id} ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($staleness != (my $s = $e->get_txn_staleness_counter($args->{"trans_id"}))) 283 820 1103
323 $e->storage->print_at(
324 $spot,
325 283 pack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ),
326 );
327 283 $loc = 0;
328 }
329 }
330  
331 # If we're in a transaction and we never wrote to this location, try the
332 # HEAD instead.
333 11536 ▶ if ( $args->{trans_id} && !$loc && $args->{allow_head} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trans_id"} and not $loc and $args->{"allow_head"}) 908 10628 11536
Condition100%6/6
$args->{"trans_id"} and not $loc and $args->{"allow_head"}
!ll&&!rl&&r
1055177908
$args->{"trans_id"} and not $loc
!ll&&!rl&&r
10433118985
MC/DC100%3/3
$args->{"trans_id"} and not $loc and $args->{"allow_head"}
$args->{"trans_id"} not $loc $args->{"allow_head"}
Truth table100%4/4
A and not(B) and C
A= $args->{"trans_id"}B= not $locC= $args->{"allow_head"}
ABCresult
0XX0
10X0
1100
1111
334 return $self->get_data_location_for({
335 trans_id => 0,
336 allow_head => 1,
337 idx => $args->{idx},
338 908 });
339 }
340  
341 10628 ▶ return $loc <= 1 ? 0 : $loc;
Branch: 1 branch
BranchTrueFalseTotal
$loc <= 1 ? : 6632 3996 10628
342 }
343  
344 sub get_data_for {
345 180 ▶ my $self = shift;
Subroutine get_data_for: called CC=3 SCAR=11.0
Pod undocumented
346 180 my ($args) = @_;
347 180 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
1764
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
348  
349 180 ▶ return unless $self->{found};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"found"} 0 180 180
350 my $location = $self->get_data_location_for({
351 allow_head => $args->{allow_head},
352 180 });
353 180 return $self->engine->load_sector( $location );
354 }
355  
356 sub get_key_for {
357 434 ▶ my $self = shift;
Subroutine get_key_for: called CC=4 SCAR=14.2
Pod undocumented
358 434 my ($idx) = @_;
359 434 ▶ $idx = $self->{idx} unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 50 384 434
360  
361 434 ▶ if ( $idx >= $self->engine->max_buckets ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= $self->engine->max_buckets) 0 434 434
362 0 DBM::Deep->_throw_error( "get_key_for(): Attempting to retrieve $idx" );
363 }
364  
365 434 my $location = $self->engine->storage->read_at(
366 $self->offset + $self->base_size + $idx * $self->bucket_size + $self->engine->hash_size,
367 $self->engine->byte_size,
368 );
369 434 $location = unpack( $StP{$self->engine->byte_size}, $location );
370 434 ▶ DBM::Deep->_throw_error( "get_key_for: No location?" ) unless $location;
Branch: 1 branch
BranchTrueFalseTotal
unless $location 0 434 434
371  
372 434 return $self->engine->load_sector( $location );
373 }
374  
375 53 1;
376 __END__
« blib/lib/DBM/Deep/Array.pm ↑ Summary _build/lib/Module/Build/Custom.pm »