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

filter: Statementstmt 98.2% 164 / 167 Branchbran 84.2% 59 / 70 Conditioncond 83.3% 10 / 12 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 15 totaltotal 88.6% 257 / 290 CC 45 SCAR 17.0
CC
45
Cov
94%
get_data_location_for
12.0
write_md5
7.1
free
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 47 ▶use 5.006_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 47 ▶use base qw( DBM::Deep::Sector::File );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 47 my $STALE_SIZE = 2;
11  
12 # Please refer to the pack() documentation for further information
13 47 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 6282 ▶ my $self = shift;
Subroutine _init: called CC=3 SCAR=11.0
22  
23 6282 my $engine = $self->engine;
24  
25 6282 ▶ unless ( $self->offset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->offset) 612 5670 6282
26 612 my $leftover = $self->size - $self->base_size;
27  
28 612 $self->{offset} = $engine->_request_blist_sector( $self->size );
29 612 $engine->storage->print_at( $self->offset, $engine->SIG_BLIST ); # Sector type
30 # Skip staleness counter
31 612 $engine->storage->print_at( $self->offset + $self->base_size,
32 chr(0) x $leftover, # Zero-fill the data
33 );
34 }
35  
36 6282 ▶ if ( $self->{key_md5} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"key_md5"}) 498 5784 6282
37 498 $self->find_md5;
38 }
39  
40 6282 return $self;
41 }
42  
43 sub wipe {
44 11 ▶ my $self = shift;
Subroutine wipe: called CC=1 SCAR=0.0
Pod undocumented
45 11 $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 1319 ▶ my $self = shift;
Subroutine size: called CC=2 SCAR=6.9
Pod undocumented
52 1319 ▶ unless ( $self->{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"size"}) 651 668 1319
53 651 my $e = $self->engine;
54 # Base + numbuckets * bucketsize
55 651 $self->{size} = $self->base_size + $e->max_buckets * $self->bucket_size;
56 }
57 1319 return $self->{size};
58 }
59  
60 40 ▶sub free_meth { return '_add_free_blist_sector' }
Subroutine free_meth: called CC=1 SCAR=0.0
Pod undocumented
61  
62 sub free {
63 40 ▶ my $self = shift;
Subroutine free: called CC=6 SCAR=18.0
Pod undocumented
64  
65 40 my $e = $self->engine;
66 40 foreach my $bucket ( $self->chopped_up ) {
67 67 my $rest = $bucket->[-1];
68  
69 # Delete the keysector
70 67 my $l = unpack( $StP{$e->byte_size}, substr( $rest, $e->hash_size, $e->byte_size ) );
71 67 ▶ my $s = $e->load_sector( $l ); $s->free if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 67 0 67
72  
73 # Delete the HEAD sector
74 67 $l = unpack( $StP{$e->byte_size},
75 substr( $rest,
76 $e->hash_size + $e->byte_size,
77 $e->byte_size,
78 ),
79 );
80 67 ▶ $s = $e->load_sector( $l ); $s->free if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 65 2 67
81  
82 67 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 0 4 4
90 }
91 }
92  
93 40 $self->SUPER::free();
94 }
95  
96 sub bucket_size {
97 30420 ▶ my $self = shift;
Subroutine bucket_size: called CC=2 SCAR=6.9
Pod undocumented
98 30420 ▶ unless ( $self->{bucket_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"bucket_size"}) 6282 24138 30420
99 6282 my $e = $self->engine;
100 # Key + head (location) + transactions (location + staleness-counter)
101 6282 my $location_size = $e->byte_size + $e->byte_size + ($e->num_txns - 1) * ($e->byte_size + $STALE_SIZE);
102 6282 $self->{bucket_size} = $e->hash_size + $location_size;
103 }
104 30420 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 52 ▶ my $self = shift;
Subroutine chopped_up: called CC=3 SCAR=11.0
Pod undocumented
110  
111 52 my $e = $self->engine;
112  
113 52 my @buckets;
114 52 foreach my $idx ( 0 .. $e->max_buckets - 1 ) {
115 285 my $spot = $self->offset + $self->base_size + $idx * $self->bucket_size;
116 285 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 285 ▶ last if $md5 eq $e->blank_md5;
Branch: 1 branch
BranchTrueFalseTotal
if $md5 eq $e->blank_md5 41 244 285
120  
121 244 my $rest = $e->storage->read_at( undef, $self->bucket_size - $e->hash_size );
122 244 push @buckets, [ $spot, $md5 . $rest ];
123 }
124  
125 52 return @buckets;
126 }
127  
128 sub write_at_next_open {
129 176 ▶ my $self = shift;
Subroutine write_at_next_open: called CC=2 SCAR=6.9
Pod undocumented
130 176 my ($entry) = @_;
131  
132 #XXX This is such a hack!
133 176 ▶ $self->{_next_open} = 0 unless exists $self->{_next_open};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_next_open"} 108 68 176
134  
135 176 my $spot = $self->offset + $self->base_size + $self->{_next_open}++ * $self->bucket_size;
136 176 $self->engine->storage->print_at( $spot, $entry );
137  
138 176 return $spot;
139 }
140  
141 sub has_md5 {
142 7089 ▶ my $self = shift;
Subroutine has_md5: called CC=2 SCAR=7.6
Pod undocumented
143 7089 ▶ unless ( exists $self->{found} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"found"}) 0 7089 7089
144 0 $self->find_md5;
145 }
146 7089 return $self->{found};
147 }
148  
149 sub find_md5 {
150 5811 ▶ my $self = shift;
Subroutine find_md5: called CC=6 SCAR=17.9
Pod undocumented
151  
152 5811 $self->{found} = undef;
153 5811 $self->{idx} = -1;
154  
155 5811 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5313 498 5811
156 5313 $self->{key_md5} = shift;
157 }
158  
159 # If we don't have an MD5, then what are we supposed to do?
160 5811 ▶ unless ( exists $self->{key_md5} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"key_md5"}) 0 5811 5811
161 0 DBM::Deep->_throw_error( "Cannot find_md5 without a key_md5 set" );
162 }
163  
164 5811 my $e = $self->engine;
165 5811 foreach my $idx ( 0 .. $e->max_buckets - 1 ) {
166 15099 my $potential = $e->storage->read_at(
167 $self->offset + $self->base_size + $idx * $self->bucket_size, $e->hash_size,
168 );
169  
170 15099 ▶ if ( $potential eq $e->blank_md5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($potential eq $e->blank_md5) 2412 12687 15099
171 2412 $self->{idx} = $idx;
172 2412 return;
173 }
174  
175 12687 ▶ if ( $potential eq $self->{key_md5} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($potential eq $self->{"key_md5"}) 3391 9296 12687
176 3391 $self->{found} = 1;
177 3391 $self->{idx} = $idx;
178 3391 return;
179 }
180 }
181  
182 8 return;
183 }
184  
185 sub write_md5 {
186 1801 ▶ my $self = shift;
Subroutine write_md5: called CC=7 SCAR=19.5
Pod undocumented
187 1801 my ($args) = @_;
188  
189 1801 ▶ DBM::Deep->_throw_error( "write_md5: no key" ) unless exists $args->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"key"} 0 1801 1801
190 1801 ▶ DBM::Deep->_throw_error( "write_md5: no key_md5" ) unless exists $args->{key_md5};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"key_md5"} 0 1801 1801
191 1801 ▶ DBM::Deep->_throw_error( "write_md5: no value" ) unless exists $args->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"value"} 0 1801 1801
192  
193 1801 my $engine = $self->engine;
194  
195 1801 ▶ $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"trans_id"} 1797 4 1801
196  
197 1801 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
198 1801 $engine->add_entry( $args->{trans_id}, $spot );
199  
200 1801 ▶ unless ($self->{found}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"found"}) 1589 212 1801
201 my $key_sector = DBM::Deep::Sector::File::Scalar->new({
202 engine => $engine,
203 data => $args->{key},
204 1589 });
205  
206 $engine->storage->print_at( $spot,
207 $args->{key_md5},
208 1589 pack( $StP{$engine->byte_size}, $key_sector->offset ),
209 );
210 }
211  
212 1801 my $loc = $spot
213 + $engine->hash_size
214 + $engine->byte_size;
215  
216 1801 ▶ if ( $args->{trans_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'trans_id'}) { } 74 1727 1801
217 74 $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 74 pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ),
222 );
223 }
224 else {
225 $engine->storage->print_at( $loc,
226 1727 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 42 ▶ my $self = shift;
Subroutine delete_md5: called CC=2 SCAR=6.9
Pod undocumented
264 42 my ($args) = @_;
265  
266 42 my $engine = $self->engine;
267 42 ▶ return undef unless $self->{found};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"found"} 2 40 42
268  
269 # Save the location so that we can free the data
270 40 my $location = $self->get_data_location_for({
271 allow_head => 0,
272 });
273 40 my $key_sector = $self->get_key_for;
274  
275 40 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 40 $self->bucket_size * ( $engine->max_buckets - $self->{idx} - 1 ),
280 ),
281 chr(0) x $self->bucket_size,
282 );
283  
284 40 $key_sector->free;
285  
286 40 my $data_sector = $self->engine->load_sector( $location );
287 40 my $data = $data_sector->data({ export => 1 });
288 40 $data_sector->free;
289  
290 40 return $data;
291 }
292  
293 sub get_data_location_for {
294 11531 ▶ my $self = shift;
Subroutine get_data_location_for: called CC=12 SCAR=24.9
Pod undocumented
295 11531 my ($args) = @_;
296 11531 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
115310
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
297  
298 11531 ▶ $args->{allow_head} = 0 unless exists $args->{allow_head};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"allow_head"} 0 11531 11531
299 11531 ▶ $args->{trans_id} = $self->engine->trans_id unless exists $args->{trans_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"trans_id"} 10616 915 11531
300 11531 ▶ $args->{idx} = $self->{idx} unless exists $args->{idx};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"idx"} 5213 6318 11531
301  
302 11531 my $e = $self->engine;
303  
304 my $spot = $self->offset + $self->base_size
305 11531 + $args->{idx} * $self->bucket_size
306 + $e->hash_size
307 + $e->byte_size;
308  
309 11531 ▶ if ( $args->{trans_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trans_id"}) 1104 10427 11531
310 1104 $spot += $e->byte_size + ($args->{trans_id} - 1) * ( $e->byte_size + $STALE_SIZE );
311 }
312  
313 11531 my $buffer = $e->storage->read_at(
314 $spot,
315 $e->byte_size + $STALE_SIZE,
316 );
317 11531 my ($loc, $staleness) = unpack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, $buffer );
318  
319 # XXX Merge the two if-clauses below
320 11531 ▶ if ( $args->{trans_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trans_id"}) 1104 10427 11531
321 # We have found an entry that is old, so get rid of it
322 1104 ▶ 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 821 1104
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 11531 ▶ if ( $args->{trans_id} && !$loc && $args->{allow_head} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"trans_id"} and not $loc and $args->{"allow_head"}) 908 10623 11531
Condition100%6/6
$args->{"trans_id"} and not $loc and $args->{"allow_head"}
!ll&&!rl&&r
1054677908
$args->{"trans_id"} and not $loc
!ll&&!rl&&r
10427119985
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 10623 ▶ return $loc <= 1 ? 0 : $loc;
Branch: 1 branch
BranchTrueFalseTotal
$loc <= 1 ? : 6706 3917 10623
342 }
343  
344 sub get_data_for {
345 191 ▶ my $self = shift;
Subroutine get_data_for: called CC=3 SCAR=11.0
Pod undocumented
346 191 my ($args) = @_;
347 191 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
1874
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
348  
349 191 ▶ return unless $self->{found};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"found"} 0 191 191
350 my $location = $self->get_data_location_for({
351 allow_head => $args->{allow_head},
352 191 });
353 191 return $self->engine->load_sector( $location );
354 }
355  
356 sub get_key_for {
357 419 ▶ my $self = shift;
Subroutine get_key_for: called CC=4 SCAR=14.2
Pod undocumented
358 419 my ($idx) = @_;
359 419 ▶ $idx = $self->{idx} unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 40 379 419
360  
361 419 ▶ if ( $idx >= $self->engine->max_buckets ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= $self->engine->max_buckets) 0 419 419
362 0 DBM::Deep->_throw_error( "get_key_for(): Attempting to retrieve $idx" );
363 }
364  
365 419 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 419 $location = unpack( $StP{$self->engine->byte_size}, $location );
370 419 ▶ DBM::Deep->_throw_error( "get_key_for: No location?" ) unless $location;
Branch: 1 branch
BranchTrueFalseTotal
unless $location 0 419 419
371  
372 419 return $self->engine->load_sector( $location );
373 }
374  
375 47 1;
376 __END__
« blib/lib/DBM/Deep/Array.pm ↑ Summary _build/lib/Module/Build/Custom.pm »