blib/lib/DBM/Deep/Engine/File.pm

filter: Statementstmt 91.8% 374 / 407 Branchbran 80.0% 120 / 150 Conditioncond 88.8% 24 / 27 MC/DCmcdc 80.0% 12 / 15 Subroutinesub 98.3% 58 / 59 Podpod 97.3% 37 / 38 totaltotal 89.8% 625 / 696 CC 94 SCAR 20.3
CC
94
Cov
89%
write_value
18.2
_dump_file
17.6
new
16.0
CRAP 7.6
« blib/lib/DBM/Deep/Sector/File/Reference.pm ↑ Summary blib/lib/DBM/Deep/Sector/DBI.pm »
1 package DBM::Deep::Engine::File;
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 47 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 47 ▶use base qw( DBM::Deep::Engine );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 47 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 47 ▶use DBM::Deep::Null ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 47 ▶use DBM::Deep::Sector::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 47 ▶use DBM::Deep::Storage::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22546 ▶sub sector_type { 'DBM::Deep::Sector::File' }
Subroutine sector_type: called CC=1 SCAR=0.0
Pod documented
18 153 ▶sub iterator_class { 'DBM::Deep::Iterator::File' }
Subroutine iterator_class: called CC=1 SCAR=0.0
Pod undocumented
19  
20 47 my $STALE_SIZE = 2;
21  
22 # Setup file and tag signatures. These should never change.
23 sub SIG_FILE () { 'DPDB' }
24 sub SIG_HEADER () { 'h' }
25 sub SIG_NULL () { 'N' }
26 sub SIG_DATA () { 'D' }
27 sub SIG_INDEX () { 'I' }
28 sub SIG_BLIST () { 'B' }
29 sub SIG_FREE () { 'F' }
30 sub SIG_SIZE () { 1 }
31 # SIG_HASH and SIG_ARRAY are defined in DBM::Deep::Engine
32  
33 # Please refer to the pack() documentation for further information
34 47 my %StP = (
35 1 => 'C', # Unsigned char value (no order needed as it's just one byte)
36 2 => 'n', # Unsigned short in "network" (big-endian) order
37 4 => 'N', # Unsigned long in "network" (big-endian) order
38 8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent)
39 );
40  
41 =head1 NAME
42  
43 DBM::Deep::Engine::File
44  
45 =head1 PURPOSE
46  
47 This is the engine for use with L<DBM::Deep::Storage::File>.
48  
49 =head1 EXTERNAL METHODS
50  
51 =head2 new()
52  
53 This takes a set of args. These args are described in the documentation for
54 L<DBM::Deep/new>.
55  
56 =cut
57  
58 sub new {
59 382 ▶ my $class = shift;
Subroutine new: called CC=16 SCAR=27.7
Pod documented
60 382 my ($args) = @_;
61  
62 $args->{storage} = DBM::Deep::Storage::File->new( $args )
63 382 ▶ unless exists $args->{storage};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"storage"} 382 0 382
64  
65 381 my $self = bless {
66 byte_size => 4,
67  
68 digest => undef,
69 hash_size => 16, # In bytes
70 hash_chars => 256, # Number of chars the algorithm uses per byte
71 max_buckets => 16,
72 num_txns => 1, # The HEAD
73 trans_id => 0, # Default to the HEAD
74  
75 data_sector_size => 64, # Size in bytes of each data sector
76  
77 entries => {}, # This is the list of entries for transactions
78 storage => undef,
79 }, $class;
80  
81 # Never allow byte_size to be set directly.
82 381 delete $args->{byte_size};
83 381 ▶ if ( defined $args->{pack_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"pack_size"}) 3 378 381
84 3 ▶ if ( lc $args->{pack_size} eq 'small' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $args->{'pack_size'} eq 'small') { } 1 2 3
elsif (lc $args->{'pack_size'} eq 'medium') { } 1 1 2
elsif (lc $args->{'pack_size'} eq 'large') { } 1 0 1
85 1 $args->{byte_size} = 2;
86 }
87 elsif ( lc $args->{pack_size} eq 'medium' ) {
88 1 $args->{byte_size} = 4;
89 }
90 elsif ( lc $args->{pack_size} eq 'large' ) {
91 1 $args->{byte_size} = 8;
92 }
93 else {
94 0 DBM::Deep->_throw_error( "Unknown pack_size value: '$args->{pack_size}'" );
95 }
96 }
97  
98 # Grab the parameters we want to use
99 381 foreach my $param ( keys %$self ) {
100 3810 ▶ next unless exists $args->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$param} 3129 681 3810
101 681 $self->{$param} = $args->{$param};
102 }
103  
104 381 my %validations = (
105 max_buckets => { floor => 16, ceil => 256 },
106 num_txns => { floor => 1, ceil => 255 },
107 data_sector_size => { floor => 32, ceil => 256 },
108 );
109  
110 381 ▶ while ( my ($attr, $c) = each %validations ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %validations) 1143 381 1524
111 1143 ▶ if ( !defined $self->{$attr}
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{$attr} && length $self->{$attr} or $self->{$attr} =~ /\D/ or $self->{$attr} < $c->{'floor'}) { } 12 1131 1143
elsif ($self->{$attr} > $c->{'ceil'}) { } 3 1128 1131
Condition100%9/9
not defined $self->{$attr} && length $self->{$attr} or $self->{$attr} =~ /\D/ or $self->{$attr} < $c->{"floor"}
l!l&&r!l&&!r
931131
not defined $self->{$attr} && length $self->{$attr} or $self->{$attr} =~ /\D/
l!l&&r!l&&!r
631134
defined $self->{$attr} && length $self->{$attr}
!ll&&!rl&&r
331137
MC/DC100%4/4
not defined $self->{$attr} && length $self->{$attr} or $self->{$attr} =~ /\D/ or $self->{$attr} < $c->{"floor"}
defined $self->{$attr} length $self->{$attr} $self->{$attr} =~ /\D/ $self->{$attr} < $c->{"floor"}
Truth table100%5/5
not(A && B) or C or D
A= defined $self->{$attr}B= length $self->{$attr}C= $self->{$attr} =~ /\D/D= $self->{$attr} < $c->{"floor"}
ABCDresult
0XXX1
10XX1
111X1
11011
11000
112 || !length $self->{$attr}
113 || $self->{$attr} =~ /\D/
114 || $self->{$attr} < $c->{floor}
115 ) {
116 12 ▶ $self->{$attr} = '(undef)' if !defined $self->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$attr} 3 9 12
117 12 warn "Floor of $attr is $c->{floor}. Setting it to $c->{floor} from '$self->{$attr}'\n";
118 12 $self->{$attr} = $c->{floor};
119 }
120 elsif ( $self->{$attr} > $c->{ceil} ) {
121 3 warn "Ceiling of $attr is $c->{ceil}. Setting it to $c->{ceil} from '$self->{$attr}'\n";
122 3 $self->{$attr} = $c->{ceil};
123 }
124 }
125  
126 381 ▶ if ( !$self->{digest} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"digest"}) 380 1 381
127 380 require Digest::MD5;
128 380 $self->{digest} = \&Digest::MD5::md5;
129 }
130  
131 381 return $self;
132 }
133  
134 sub read_value {
135 3047 ▶ my $self = shift;
Subroutine read_value: called CC=4 SCAR=13.9
Pod documented
136 3047 my ($obj, $key) = @_;
137  
138 # This will be a Reference sector
139 3047 ▶ my $sector = $self->load_sector( $obj->_base_offset )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $self->load_sector($obj->_base_offset) or return 1 3046 3047
140 or return;
141  
142 3046 ▶ if ( $sector->staleness != $obj->_staleness ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sector->staleness != $obj->_staleness) 0 3046 3046
143 0 return;
144 }
145  
146 3046 my $key_md5 = $self->_apply_digest( $key );
147  
148 3046 my $value_sector = $sector->get_data_for({
149 key_md5 => $key_md5,
150 allow_head => 1,
151 });
152  
153 3046 ▶ unless ( $value_sector ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value_sector) 54 2992 3046
154 54 $value_sector = DBM::Deep::Sector::File::Null->new({
155 engine => $self,
156 data => undef,
157 });
158  
159 54 $sector->write_data({
160 key_md5 => $key_md5,
161 key => $key,
162 value => $value_sector,
163 });
164 }
165  
166 3046 return $value_sector->data;
167 }
168  
169 sub get_classname {
170 10 ▶ my $self = shift;
Subroutine get_classname: called CC=3 SCAR=11.8
Pod documented
171 10 my ($obj) = @_;
172  
173 # This will be a Reference sector
174 10 ▶ my $sector = $self->load_sector( $obj->_base_offset )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $self->load_sector($obj->_base_offset) or "DBM::Deep"->_throw_error("How did get_classname fail (no sector for '${obj}')?!") 0 10 10
175 or DBM::Deep->_throw_error( "How did get_classname fail (no sector for '$obj')?!" );
176  
177 10 ▶ if ( $sector->staleness != $obj->_staleness ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sector->staleness != $obj->_staleness) 0 10 10
178 0 return;
179 }
180  
181 10 return $sector->get_classname;
182 }
183  
184 sub make_reference {
185 22 ▶ my $self = shift;
Subroutine make_reference: called CC=5 SCAR=18.7
Pod documented
186 22 my ($obj, $old_key, $new_key) = @_;
187  
188 # This will be a Reference sector
189 22 ▶ my $sector = $self->load_sector( $obj->_base_offset )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $self->load_sector($obj->_base_offset) or "DBM::Deep"->_throw_error("How did make_reference fail (no sector for '${obj}')?!") 0 22 22
190 or DBM::Deep->_throw_error( "How did make_reference fail (no sector for '$obj')?!" );
191  
192 22 ▶ if ( $sector->staleness != $obj->_staleness ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sector->staleness != $obj->_staleness) 0 22 22
193 0 return;
194 }
195  
196 22 my $old_md5 = $self->_apply_digest( $old_key );
197  
198 22 my $value_sector = $sector->get_data_for({
199 key_md5 => $old_md5,
200 allow_head => 1,
201 });
202  
203 22 ▶ unless ( $value_sector ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value_sector) 0 22 22
204 0 $value_sector = DBM::Deep::Sector::File::Null->new({
205 engine => $self,
206 data => undef,
207 });
208  
209 0 $sector->write_data({
210 key_md5 => $old_md5,
211 key => $old_key,
212 value => $value_sector,
213 });
214 }
215  
216 22 ▶ if ( $value_sector->isa( 'DBM::Deep::Sector::File::Reference' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_sector->isa('DBM::Deep::Sector::File::Reference')) { } 0 22 22
217 0 $sector->write_data({
218 key => $new_key,
219 key_md5 => $self->_apply_digest( $new_key ),
220 value => $value_sector,
221 });
222 0 $value_sector->increment_refcount;
223 }
224 else {
225 22 $sector->write_data({
226 key => $new_key,
227 key_md5 => $self->_apply_digest( $new_key ),
228 value => $value_sector->clone,
229 });
230 }
231  
232 22 return;
233 }
234  
235 # exists returns '', not undefined.
236 sub key_exists {
237 128 ▶ my $self = shift;
Subroutine key_exists: called CC=4 SCAR=14.0
Pod documented
238 128 my ($obj, $key) = @_;
239  
240 # This will be a Reference sector
241 128 ▶ my $sector = $self->load_sector( $obj->_base_offset )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $self->load_sector($obj->_base_offset) or return "" 1 127 128
242 or return '';
243  
244 127 ▶ if ( $sector->staleness != $obj->_staleness ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sector->staleness != $obj->_staleness) 0 127 127
245 0 return '';
246 }
247  
248 127 my $data = $sector->get_data_for({
249 key_md5 => $self->_apply_digest( $key ),
250 allow_head => 1,
251 });
252  
253 # exists() returns 1 or '' for true/false.
254 127 ▶ return $data ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 61 66 127
255 }
256  
257 sub delete_key {
258 56 ▶ my $self = shift;
Subroutine delete_key: called CC=3 SCAR=11.2
Pod documented
259 56 my ($obj, $key) = @_;
260  
261 56 ▶ my $sector = $self->load_sector( $obj->_base_offset )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $self->load_sector($obj->_base_offset) or return 1 55 56
262 or return;
263  
264 55 ▶ if ( $sector->staleness != $obj->_staleness ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sector->staleness != $obj->_staleness) 0 55 55
265 0 return;
266 }
267  
268 55 return $sector->delete_key({
269 key_md5 => $self->_apply_digest( $key ),
270 allow_head => 0,
271 });
272 }
273  
274 sub write_value {
275 1725 ▶ my $self = shift;
Subroutine write_value: called CC=18 SCAR=29.0
Pod documented
276 1725 my ($obj, $key, $value) = @_;
277  
278 1725 ▶ my $r = Scalar::Util::reftype( $value ) || '';
Condition100%2/2
builtin::reftype($value) || ''
l!l
1211514
MC/DC100%1/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table100%2/2
A
A= builtin::reftype($value)
Aresult
11
00
279 {
280 1725 ▶ last if $r eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "" 514 1211 1725
281 1211 ▶ last if $r eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "HASH" 163 1048 1211
282 1048 ▶ last if $r eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "ARRAY" 1043 5 1048
283  
284 5 DBM::Deep->_throw_error(
285 "Storage of references of type '$r' is not supported."
286 );
287 }
288  
289 # This will be a Reference sector
290 1720 ▶ my $sector = $self->load_sector( $obj->_base_offset )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $self->load_sector($obj->_base_offset) or "DBM::Deep"->_throw_error("Cannot write to a deleted spot in DBM::Deep.") 1 1719 1720
291 or DBM::Deep->_throw_error( "Cannot write to a deleted spot in DBM::Deep." );
292  
293 1719 ▶ if ( $sector->staleness != $obj->_staleness ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sector->staleness != $obj->_staleness) 0 1719 1719
294 0 DBM::Deep->_throw_error( "Cannot write to a deleted spot in DBM::Deep." );
295 }
296  
297 1719 my ($class, $type);
298 1719 ▶ if ( !defined $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 56 1663 1719
elsif ($r eq 'ARRAY' or $r eq 'HASH') { } 1206 457 1663
299 56 $class = 'DBM::Deep::Sector::File::Null';
300 }
301 ▶ elsif ( $r eq 'ARRAY' || $r eq 'HASH' ) {
Condition100%3/3
$r eq "ARRAY" or $r eq "HASH"
l!l&&r!l&&!r
1043163457
MC/DC100%2/2
$r eq "ARRAY" or $r eq "HASH"
$r eq "ARRAY" $r eq "HASH"
Truth table100%3/3
A or B
A= $r eq "ARRAY"B= $r eq "HASH"
ABresult
1X1
011
000
302 1206 my $tmpvar;
303 1206 ▶ if ( $r eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 1043 163 1206
elsif ($r eq 'HASH') { } 163 0 163
304 1043 $tmpvar = tied @$value;
305 } elsif ( $r eq 'HASH' ) {
306 163 $tmpvar = tied %$value;
307 }
308  
309 1206 ▶ if ( $tmpvar ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpvar) 1014 192 1206
310 1014 my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $tmpvar->isa( 'DBM::Deep' ); };
311  
312 1014 ▶ unless ( $is_dbm_deep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_dbm_deep) 4 1010 1014
313 4 DBM::Deep->_throw_error( "Cannot store something that is tied." );
314 }
315  
316 1010 ▶ unless ( $tmpvar->_engine->storage == $self->storage ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmpvar->_engine->storage == $self->storage) 0 1010 1010
317 0 DBM::Deep->_throw_error( "Cannot store values across DBM::Deep files. Please use export() instead." );
318 }
319  
320 # First, verify if we're storing the same thing to this spot. If we
321 # are, then this should be a no-op. -EJS, 2008-05-19
322 1010 my $loc = $sector->get_data_location_for({
323 key_md5 => $self->_apply_digest( $key ),
324 allow_head => 1,
325 });
326  
327 1010 ▶ if ( defined($loc) && $loc == $tmpvar->_base_offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loc and $loc == $tmpvar->_base_offset) 1 1009 1010
Condition100%3/3
defined $loc and $loc == $tmpvar->_base_offset
!ll&&!rl&&r
100721
MC/DC100%2/2
defined $loc and $loc == $tmpvar->_base_offset
defined $loc $loc == $tmpvar->_base_offset
Truth table100%3/3
A and B
A= defined $locB= $loc == $tmpvar->_base_offset
ABresult
0X0
100
111
328 1 return 1;
329 }
330  
331 #XXX Can this use $loc?
332 1009 my $value_sector = $self->load_sector( $tmpvar->_base_offset );
333 1009 $sector->write_data({
334 key => $key,
335 key_md5 => $self->_apply_digest( $key ),
336 value => $value_sector,
337 });
338 1009 $value_sector->increment_refcount;
339  
340 1009 return 1;
341 }
342  
343 192 $class = 'DBM::Deep::Sector::File::Reference';
344 192 $type = substr( $r, 0, 1 );
345 }
346 else {
347 457 ▶ if ( tied($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied $value) 0 457 457
348 0 DBM::Deep->_throw_error( "Cannot store something that is tied." );
349 }
350 457 $class = 'DBM::Deep::Sector::File::Scalar';
351 }
352  
353 # Create this after loading the reference sector in case something bad
354 # happens. This way, we won't allocate value sector(s) needlessly.
355 705 my $value_sector = $class->new({
356 engine => $self,
357 data => $value,
358 type => $type,
359 });
360  
361 705 $sector->write_data({
362 key => $key,
363 key_md5 => $self->_apply_digest( $key ),
364 value => $value_sector,
365 });
366  
367 705 $self->_descend( $value, $value_sector );
368  
369 705 return 1;
370 }
371  
372 sub setup {
373 2911 ▶ my $self = shift;
Subroutine setup: called CC=5 SCAR=16.1
Pod documented
374 2911 my ($obj) = @_;
375  
376 # We're opening the file.
377 2911 ▶ unless ( $obj->_base_offset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->_base_offset) 381 2530 2911
378 381 my $bytes_read = $self->_read_file_header;
379  
380 # Creating a new file
381 377 ▶ unless ( $bytes_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_read) { } 289 88 377
382 88 $self->_write_file_header;
383  
384 # 1) Create Array/Hash entry
385 88 my $initial_reference = DBM::Deep::Sector::File::Reference->new({
386 engine => $self,
387 type => $obj->_type,
388 });
389 88 $obj->{base_offset} = $initial_reference->offset;
390 88 $obj->{staleness} = $initial_reference->staleness;
391  
392 88 $self->storage->flush;
393 }
394 # Reading from an existing file
395 else {
396 289 $obj->{base_offset} = $bytes_read;
397 289 my $initial_reference = DBM::Deep::Sector::File::Reference->new({
398 engine => $self,
399 offset => $obj->_base_offset,
400 });
401 289 ▶ unless ( $initial_reference ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($initial_reference) 0 289 289
402 0 DBM::Deep->_throw_error("Corrupted file, no master index record");
403 }
404  
405 289 ▶ unless ($obj->_type eq $initial_reference->type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->_type eq $initial_reference->type) 6 283 289
406 6 DBM::Deep->_throw_error("File type mismatch");
407 }
408  
409 283 $obj->{staleness} = $initial_reference->staleness;
410 }
411 }
412  
413 2901 $self->storage->set_inode;
414  
415 2901 return 1;
416 }
417  
418 sub begin_work {
419 276 ▶ my $self = shift;
Subroutine begin_work: called CC=6 SCAR=18.1
Pod documented
420 276 my ($obj) = @_;
421  
422 276 ▶ if ( $self->trans_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trans_id) 1 275 276
423 1 DBM::Deep->_throw_error( "Cannot begin_work within an active transaction" );
424 }
425  
426 275 my @slots = $self->read_txn_slots;
427 275 my $found;
428 275 for my $i ( 0 .. $#slots ) {
429 32661 ▶ next if $slots[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $slots[$i] 32386 275 32661
430  
431 275 $slots[$i] = 1;
432 275 $self->set_trans_id( $i + 1 );
433 275 $found = 1;
434 275 last;
435 }
436 275 ▶ unless ( $found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 275 275
437 0 DBM::Deep->_throw_error( "Cannot allocate transaction ID" );
438 }
439 275 $self->write_txn_slots( @slots );
440  
441 275 ▶ if ( !$self->trans_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->trans_id) 0 275 275
442 0 DBM::Deep->_throw_error( "Cannot begin_work - no available transactions" );
443 }
444  
445 275 return;
446 }
447  
448 sub rollback {
449 12 ▶ my $self = shift;
Subroutine rollback: called CC=4 SCAR=13.9
Pod documented
450 12 my ($obj) = @_;
451  
452 12 ▶ if ( !$self->trans_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->trans_id) 1 11 12
453 1 DBM::Deep->_throw_error( "Cannot rollback without an active transaction" );
454 }
455  
456 # Each entry is the file location for a bucket that has a modification for
457 # this transaction. The entries need to be expunged.
458 11 foreach my $entry (@{ $self->get_entries } ) {
459 # Remove the entry here
460 39 my $read_loc = $entry
461 + $self->hash_size
462 + $self->byte_size
463 + $self->byte_size
464 + ($self->trans_id - 1) * ( $self->byte_size + $STALE_SIZE );
465  
466 39 my $data_loc = $self->storage->read_at( $read_loc, $self->byte_size );
467 39 $data_loc = unpack( $StP{$self->byte_size}, $data_loc );
468 39 $self->storage->print_at( $read_loc, pack( $StP{$self->byte_size}, 0 ) );
469  
470 39 ▶ if ( $data_loc > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_loc > 1) 30 9 39
471 30 $self->load_sector( $data_loc )->free;
472 }
473 }
474  
475 11 $self->clear_entries;
476  
477 11 my @slots = $self->read_txn_slots;
478 11 $slots[$self->trans_id-1] = 0;
479 11 $self->write_txn_slots( @slots );
480 11 $self->inc_txn_staleness_counter( $self->trans_id );
481 11 $self->set_trans_id( 0 );
482  
483 11 return 1;
484 }
485  
486 sub commit {
487 10 ▶ my $self = shift;
Subroutine commit: called CC=4 SCAR=13.9
Pod documented
488 10 my ($obj) = @_;
489  
490 10 ▶ if ( !$self->trans_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->trans_id) 1 9 10
491 1 DBM::Deep->_throw_error( "Cannot commit without an active transaction" );
492 }
493  
494 9 foreach my $entry (@{ $self->get_entries } ) {
495 # Overwrite the entry in head with the entry in trans_id
496 35 my $base = $entry
497 + $self->hash_size
498 + $self->byte_size;
499  
500 35 my $head_loc = $self->storage->read_at( $base, $self->byte_size );
501 35 $head_loc = unpack( $StP{$self->byte_size}, $head_loc );
502  
503 35 my $spot = $base + $self->byte_size + ($self->trans_id - 1) * ( $self->byte_size + $STALE_SIZE );
504 35 my $trans_loc = $self->storage->read_at(
505 $spot, $self->byte_size,
506 );
507  
508 35 $self->storage->print_at( $base, $trans_loc );
509 $self->storage->print_at(
510 $spot,
511 35 pack( $StP{$self->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ),
512 );
513  
514 35 ▶ if ( $head_loc > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head_loc > 1) 11 24 35
515 11 $self->load_sector( $head_loc )->free;
516 }
517 }
518  
519 9 $self->clear_entries;
520  
521 9 my @slots = $self->read_txn_slots;
522 9 $slots[$self->trans_id-1] = 0;
523 9 $self->write_txn_slots( @slots );
524 9 $self->inc_txn_staleness_counter( $self->trans_id );
525 9 $self->set_trans_id( 0 );
526  
527 9 return 1;
528 }
529  
530 =head1 INTERNAL METHODS
531  
532 The following methods are internal-use-only to DBM::Deep::Engine::File.
533  
534 =cut
535  
536 =head2 read_txn_slots()
537  
538 This takes no arguments.
539  
540 This will return an array with a 1 or 0 in each slot. Each spot represents one
541 available transaction. If the slot is 1, that transaction is taken. If it is 0,
542 the transaction is available.
543  
544 =cut
545  
546 sub read_txn_slots {
547 2080 ▶ my $self = shift;
Subroutine read_txn_slots: called CC=1 SCAR=0.0
Pod documented
548 2080 my $bl = $self->txn_bitfield_len;
549 2080 my $num_bits = $bl * 8;
550 2080 return split '', unpack( 'b'.$num_bits,
551 $self->storage->read_at(
552 $self->trans_loc, $bl,
553 )
554 );
555 }
556  
557 =head2 write_txn_slots( @slots )
558  
559 This takes an array of 1's and 0's. This array represents the transaction slots
560 returned by L</read_txn_slots()>. In other words, the following is true:
561  
562 @x = read_txn_slots( write_txn_slots( @x ) );
563  
564 (With the obviously missing object referents added back in.)
565  
566 =cut
567  
568 sub write_txn_slots {
569 295 ▶ my $self = shift;
Subroutine write_txn_slots: called CC=1 SCAR=0.0
Pod documented
570 295 my $num_bits = $self->txn_bitfield_len * 8;
571 295 $self->storage->print_at( $self->trans_loc,
572 pack( 'b'.$num_bits, join('', @_) ),
573 );
574 }
575  
576 =head2 get_running_txn_ids()
577  
578 This takes no arguments.
579  
580 This will return an array of taken transaction IDs. This wraps L</read_txn_slots()>.
581  
582 =cut
583  
584 sub get_running_txn_ids {
585 1785 ▶ my $self = shift;
Subroutine get_running_txn_ids: called CC=1 SCAR=0.0
Pod documented
586 1785 my @transactions = $self->read_txn_slots;
587 1785 my @trans_ids = map { $_+1 } grep { $transactions[$_] } 0 .. $#transactions;
588 }
589  
590 =head2 get_txn_staleness_counter( $trans_id )
591  
592 This will return the staleness counter for the given transaction ID. Please see
593 L</TRANSACTION STALENESS> for more information.
594  
595 =cut
596  
597 sub get_txn_staleness_counter {
598 1211 ▶ my $self = shift;
Subroutine get_txn_staleness_counter: called CC=2 SCAR=7.0
Pod documented
599 1211 my ($trans_id) = @_;
600  
601 # Hardcode staleness of 0 for the HEAD
602 1211 ▶ return 0 unless $trans_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $trans_id 0 1211 1211
603  
604 1211 return unpack( $StP{$STALE_SIZE},
605 $self->storage->read_at(
606 $self->trans_loc + $self->txn_bitfield_len + $STALE_SIZE * ($trans_id - 1),
607 $STALE_SIZE,
608 )
609 );
610 }
611  
612 =head2 inc_txn_staleness_counter( $trans_id )
613  
614 This will increment the staleness counter for the given transaction ID. Please see
615 L</TRANSACTION STALENESS> for more information.
616  
617 =cut
618  
619 sub inc_txn_staleness_counter {
620 20 ▶ my $self = shift;
Subroutine inc_txn_staleness_counter: called CC=2 SCAR=7.0
Pod documented
621 20 my ($trans_id) = @_;
622  
623 # Hardcode staleness of 0 for the HEAD
624 20 ▶ return 0 unless $trans_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $trans_id 0 20 20
625  
626 $self->storage->print_at(
627 $self->trans_loc + $self->txn_bitfield_len + $STALE_SIZE * ($trans_id - 1),
628 20 pack( $StP{$STALE_SIZE}, $self->get_txn_staleness_counter( $trans_id ) + 1 ),
629 );
630 }
631  
632 =head2 get_entries()
633  
634 This takes no arguments.
635  
636 This returns a list of all the sectors that have been modified by this transaction.
637  
638 =cut
639  
640 sub get_entries {
641 20 ▶ my $self = shift;
Subroutine get_entries: called CC=2 SCAR=6.9
Pod documented
642 20 ▶ return [ keys %{ $self->{entries}{$self->trans_id} ||= {} } ];
Condition100%2/2
$self->{"entries"}{$self->trans_id} ||= {}
l!l
173
MC/DC100%1/1
$self->{"entries"}{$self->trans_id} ||= {}
$self->{"entries"}{$self->trans_id}
Truth table100%2/2
A
A= $self->{"entries"}{$self->trans_id}
Aresult
11
00
643 }
644  
645 =head2 add_entry( $trans_id, $location )
646  
647 This takes a transaction ID and a file location and marks the sector at that
648 location as having been modified by the transaction identified by $trans_id.
649  
650 This returns nothing.
651  
652 B<NOTE>: Unlike all the other _entries() methods, there are several cases where
653 C<< $trans_id != $self->trans_id >> for this method.
654  
655 =cut
656  
657 sub add_entry {
658 1855 ▶ my $self = shift;
Subroutine add_entry: called CC=2 SCAR=6.9
Pod documented
659 1855 my ($trans_id, $loc) = @_;
660  
661 1855 ▶ $self->{entries}{$trans_id} ||= {};
Condition100%2/2
$self->{"entries"}{$trans_id} ||= {}
l!l
177283
MC/DC100%1/1
$self->{"entries"}{$trans_id} ||= {}
$self->{"entries"}{$trans_id}
Truth table100%2/2
A
A= $self->{"entries"}{$trans_id}
Aresult
11
00
662 1855 $self->{entries}{$trans_id}{$loc} = undef;
663 }
664  
665 =head2 reindex_entry( $old_loc, $new_loc )
666  
667 This takes two locations (old and new, respectively). If a location that has
668 been modified by this transaction is subsequently reindexed due to a bucketlist
669 overflowing, then the entries hash needs to be made aware of this change.
670  
671 This returns nothing.
672  
673 =cut
674  
675 sub reindex_entry {
676 176 ▶ my $self = shift;
Subroutine reindex_entry: called CC=3 SCAR=11.0
Pod documented
677 176 my ($old_loc, $new_loc) = @_;
678  
679 TRANS:
680 176 ▶ while ( my ($trans_id, $locs) = each %{ $self->{entries} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"entries"};}) 208 176 384
681 208 ▶ if ( exists $locs->{$old_loc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $locs->{$old_loc}) 206 2 208
682 206 delete $locs->{$old_loc};
683 206 $locs->{$new_loc} = undef;
684 206 next TRANS;
685 }
686 }
687 }
688  
689 =head2 clear_entries()
690  
691 This takes no arguments. It will clear the entries list for the running
692 transaction.
693  
694 This returns nothing.
695  
696 =cut
697  
698 sub clear_entries {
699 20 ▶ my $self = shift;
Subroutine clear_entries: called CC=1 SCAR=0.0
Pod documented
700 20 delete $self->{entries}{$self->trans_id};
701 }
702  
703 =head2 _write_file_header()
704  
705 This writes the file header for a new file. This will write the various settings
706 that set how the file is interpreted.
707  
708 =head2 _read_file_header()
709  
710 This reads the file header from an existing file. This will read the various
711 settings that set how the file is interpreted.
712  
713 =cut
714  
715 {
716 47 my $header_fixed = length( __PACKAGE__->SIG_FILE ) + 1 + 4 + 4;
717 47 my $this_file_version = 3;
718  
719 sub _write_file_header {
720 88 ▶ my $self = shift;
Subroutine _write_file_header: called CC=1 SCAR=0.0
721  
722 88 my $nt = $self->num_txns;
723 88 my $bl = $self->txn_bitfield_len;
724  
725 88 my $header_var = 1 + 1 + 1 + 1 + $bl + $STALE_SIZE * ($nt - 1) + 3 * $self->byte_size;
726  
727 88 my $loc = $self->storage->request_space( $header_fixed + $header_var );
728  
729 $self->storage->print_at( $loc,
730 $self->SIG_FILE,
731 $self->SIG_HEADER,
732 pack('N', $this_file_version), # At this point, we're at 9 bytes
733 pack('N', $header_var), # header size
734 # --- Above is $header_fixed. Below is $header_var
735 pack('C', $self->byte_size),
736  
737 # These shenanigans are to allow a 256 within a C
738 pack('C', $self->max_buckets - 1),
739 pack('C', $self->data_sector_size - 1),
740  
741 pack('C', $nt),
742 pack('C' . $bl, 0 ), # Transaction activeness bitfield
743 pack($StP{$STALE_SIZE}.($nt-1), 0 x ($nt-1) ), # Transaction staleness counters
744 pack($StP{$self->byte_size}, 0), # Start of free chain (blist size)
745 pack($StP{$self->byte_size}, 0), # Start of free chain (data size)
746 88 pack($StP{$self->byte_size}, 0), # Start of free chain (index size)
747 );
748  
749 #XXX Set these less fragilely
750 88 $self->set_trans_loc( $header_fixed + 4 );
751 88 $self->set_chains_loc( $header_fixed + 4 + $bl + $STALE_SIZE * ($nt-1) );
752  
753 88 return;
754 }
755  
756 sub _read_file_header {
757 383 ▶ my $self = shift;
Subroutine _read_file_header: called CC=8 SCAR=21.2
758  
759 383 my $buffer = $self->storage->read_at( 0, $header_fixed );
760 383 ▶ return unless length($buffer);
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer 88 295 383
761  
762 295 my ($file_signature, $sig_header, $file_version, $size) = unpack(
763 'A4 A N N', $buffer
764 );
765  
766 295 ▶ unless ( $file_signature eq $self->SIG_FILE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_signature eq $self->SIG_FILE) 1 294 295
767 1 $self->storage->close;
768 1 DBM::Deep->_throw_error( "Signature not found -- file is not a Deep DB" );
769 }
770  
771 294 ▶ unless ( $sig_header eq $self->SIG_HEADER ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sig_header eq $self->SIG_HEADER) 2 292 294
772 2 $self->storage->close;
773 2 DBM::Deep->_throw_error( "Pre-1.00 file version found" );
774 }
775  
776 292 ▶ unless ( $file_version == $this_file_version ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_version == $this_file_version) 1 291 292
777 1 $self->storage->close;
778 1 DBM::Deep->_throw_error(
779 "Wrong file version found - " . $file_version .
780 " - expected " . $this_file_version
781 );
782 }
783  
784 291 my $buffer2 = $self->storage->read_at( undef, $size );
785 291 my @values = unpack( 'C C C C', $buffer2 );
786  
787 291 ▶ if ( @values != 4 || grep { !defined } @values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values != 4 or grep {not defined $_;} @values) 0 291 291
Condition33%1/3
@values != 4 or grep {not defined $_;} @values
l!l&&r!l&&!r
00291
MC/DC0%0/2
@values != 4 or grep {not defined $_;} @values
@values != 4 grep {not defined $_;} @values
Truth table33%1/3
A or B
A= @values != 4B= grep {not defined $_;} @values
ABresult
1X1
011
000
788 0 $self->storage->close;
789 0 DBM::Deep->_throw_error("Corrupted file - bad header");
790 }
791  
792 #XXX Add warnings if values weren't set right
793 291 @{$self}{qw(byte_size max_buckets data_sector_size num_txns)} = @values;
794  
795 # These shenangians are to allow a 256 within a C
796 291 $self->{max_buckets} += 1;
797 291 $self->{data_sector_size} += 1;
798  
799 291 my $bl = $self->txn_bitfield_len;
800  
801 291 my $header_var = scalar(@values) + $bl + $STALE_SIZE * ($self->num_txns - 1) + 3 * $self->byte_size;
802 291 ▶ unless ( $size == $header_var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size == $header_var) 0 291 291
803 0 $self->storage->close;
804 0 DBM::Deep->_throw_error( "Unexpected size found ($size <-> $header_var)." );
805 }
806  
807 291 $self->set_trans_loc( $header_fixed + scalar(@values) );
808 291 $self->set_chains_loc( $header_fixed + scalar(@values) + $bl + $STALE_SIZE * ($self->num_txns - 1) );
809  
810 291 return length($buffer) + length($buffer2);
811 }
812 }
813  
814 =head2 _apply_digest( @stuff )
815  
816 This will apply the digest methd (default to Digest::MD5::md5) to the arguments
817 passed in and return the result.
818  
819 =cut
820  
821 47 sub _apply_digest {
822 5996 ▶ my $self = shift;
Subroutine _apply_digest: called CC=1 SCAR=0.0
823 5996 return $self->{digest}->(@_);
824 }
825  
826 =head2 _add_free_blist_sector( $offset, $size )
827  
828 =head2 _add_free_data_sector( $offset, $size )
829  
830 =head2 _add_free_index_sector( $offset, $size )
831  
832 These methods are all wrappers around _add_free_sector(), providing the proper
833 chain offset ($multiple) for the sector type.
834  
835 =cut
836  
837 40 ▶sub _add_free_blist_sector { shift->_add_free_sector( 0, @_ ) }
Subroutine _add_free_blist_sector: called CC=1 SCAR=0.0
838 448 ▶sub _add_free_data_sector { shift->_add_free_sector( 1, @_ ) }
Subroutine _add_free_data_sector: called CC=1 SCAR=0.0
839 0 ▶sub _add_free_index_sector { shift->_add_free_sector( 2, @_ ) }
Subroutine _add_free_index_sector: not called CC=1 SCAR=6.9
840  
841 =head2 _add_free_sector( $multiple, $offset, $size )
842  
843 _add_free_sector() takes the offset into the chains location, the offset of the
844 sector, and the size of that sector. It will mark the sector as a free sector
845 and put it into the list of sectors that are free of this type for use later.
846  
847 This returns nothing.
848  
849 B<NOTE>: $size is unused?
850  
851 =cut
852  
853 sub _add_free_sector {
854 488 ▶ my $self = shift;
Subroutine _add_free_sector: called CC=1 SCAR=0.0
855 488 my ($multiple, $offset, $size) = @_;
856  
857 488 my $chains_offset = $multiple * $self->byte_size;
858  
859 488 my $storage = $self->storage;
860  
861 # Increment staleness.
862 # XXX Can this increment+modulo be done by "&= 0x1" ?
863 488 my $staleness = unpack( $StP{$STALE_SIZE}, $storage->read_at( $offset + $self->SIG_SIZE, $STALE_SIZE ) );
864 488 $staleness = ($staleness + 1 ) % ( 2 ** ( 8 * $STALE_SIZE ) );
865 488 $storage->print_at( $offset + $self->SIG_SIZE, pack( $StP{$STALE_SIZE}, $staleness ) );
866  
867 488 my $old_head = $storage->read_at( $self->chains_loc + $chains_offset, $self->byte_size );
868  
869 $storage->print_at( $self->chains_loc + $chains_offset,
870 488 pack( $StP{$self->byte_size}, $offset ),
871 );
872  
873 # Record the old head in the new sector after the signature and staleness counter
874 488 $storage->print_at( $offset + $self->SIG_SIZE + $STALE_SIZE, $old_head );
875 }
876  
877 =head2 _request_blist_sector( $size )
878  
879 =head2 _request_data_sector( $size )
880  
881 =head2 _request_index_sector( $size )
882  
883 These methods are all wrappers around _request_sector(), providing the proper
884 chain offset ($multiple) for the sector type.
885  
886 =cut
887  
888 612 ▶sub _request_blist_sector { shift->_request_sector( 0, @_ ) }
Subroutine _request_blist_sector: called CC=1 SCAR=0.0
889 4915 ▶sub _request_data_sector { shift->_request_sector( 1, @_ ) }
Subroutine _request_data_sector: called CC=1 SCAR=0.0
890 11 ▶sub _request_index_sector { shift->_request_sector( 2, @_ ) }
Subroutine _request_index_sector: called CC=1 SCAR=0.0
891  
892 =head2 _request_sector( $multiple $size )
893  
894 This takes the offset into the chains location and the size of that sector.
895  
896 This returns the object with the sector. If there is an available free sector of
897 that type, then it will be reused. If there isn't one, then a new one will be
898 allocated.
899  
900 =cut
901  
902 sub _request_sector {
903 5538 ▶ my $self = shift;
Subroutine _request_sector: called CC=2 SCAR=6.9
904 5538 my ($multiple, $size) = @_;
905  
906 5538 my $chains_offset = $multiple * $self->byte_size;
907  
908 5538 my $old_head = $self->storage->read_at( $self->chains_loc + $chains_offset, $self->byte_size );
909 5538 my $loc = unpack( $StP{$self->byte_size}, $old_head );
910  
911 # We don't have any free sectors of the right size, so allocate a new one.
912 5538 ▶ unless ( $loc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loc) 5213 325 5538
913 5213 my $offset = $self->storage->request_space( $size );
914  
915 # Zero out the new sector. This also guarantees correct increases
916 # in the filesize.
917 5213 $self->storage->print_at( $offset, chr(0) x $size );
918  
919 5213 return $offset;
920 }
921  
922 # Read the new head after the signature and the staleness counter
923 325 my $new_head = $self->storage->read_at( $loc + $self->SIG_SIZE + $STALE_SIZE, $self->byte_size );
924 325 $self->storage->print_at( $self->chains_loc + $chains_offset, $new_head );
925 $self->storage->print_at(
926 $loc + $self->SIG_SIZE + $STALE_SIZE,
927 325 pack( $StP{$self->byte_size}, 0 ),
928 );
929  
930 325 return $loc;
931 }
932  
933 =head2 ACCESSORS
934  
935 The following are readonly attributes.
936  
937 =over 4
938  
939 =item * byte_size
940  
941 =item * hash_size
942  
943 =item * hash_chars
944  
945 =item * num_txns
946  
947 =item * max_buckets
948  
949 =item * blank_md5
950  
951 =item * data_sector_size
952  
953 =item * txn_bitfield_len
954  
955 =back
956  
957 =cut
958  
959 156345 ▶sub byte_size { $_[0]{byte_size} }
Subroutine byte_size: called CC=1 SCAR=0.0
Pod documented
960 51313 ▶sub hash_size { $_[0]{hash_size} }
Subroutine hash_size: called CC=1 SCAR=0.0
Pod documented
961 12975 ▶sub hash_chars { $_[0]{hash_chars} }
Subroutine hash_chars: called CC=1 SCAR=0.0
Pod documented
962 7401 ▶sub num_txns { $_[0]{num_txns} }
Subroutine num_txns: called CC=1 SCAR=0.0
Pod documented
963 18221 ▶sub max_buckets { $_[0]{max_buckets} }
Subroutine max_buckets: called CC=1 SCAR=0.0
Pod documented
964 15384 ▶sub blank_md5 { chr(0) x $_[0]->hash_size }
Subroutine blank_md5: called CC=1 SCAR=0.0
Pod documented
965 8403 ▶sub data_sector_size { $_[0]{data_sector_size} }
Subroutine data_sector_size: called CC=1 SCAR=0.0
Pod documented
966  
967 # This is a calculated value
968 sub txn_bitfield_len {
969 3985 ▶ my $self = shift;
Subroutine txn_bitfield_len: called CC=3 SCAR=11.0
Pod documented
970 3985 ▶ unless ( exists $self->{txn_bitfield_len} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"txn_bitfield_len"}) 377 3608 3985
971 377 my $temp = ($self->num_txns) / 8;
972 377 ▶ if ( $temp > int( $temp ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp > int $temp) 363 14 377
973 363 $temp = int( $temp ) + 1;
974 }
975 377 $self->{txn_bitfield_len} = $temp;
976 }
977 3985 return $self->{txn_bitfield_len};
978 }
979  
980 =pod
981  
982 The following are read/write attributes.
983  
984 =over 4
985  
986 =item * trans_id / set_trans_id( $new_id )
987  
988 =item * trans_loc / set_trans_loc( $new_loc )
989  
990 =item * chains_loc / set_chains_loc( $new_loc )
991  
992 =back
993  
994 =cut
995  
996 15252 ▶sub trans_id { $_[0]{trans_id} }
Subroutine trans_id: called CC=1 SCAR=0.0
Pod documented
997 295 ▶sub set_trans_id { $_[0]{trans_id} = $_[1] }
Subroutine set_trans_id: called CC=1 SCAR=0.0
Pod documented
998  
999 3606 ▶sub trans_loc { $_[0]{trans_loc} }
Subroutine trans_loc: called CC=1 SCAR=0.0
Pod documented
1000 379 ▶sub set_trans_loc { $_[0]{trans_loc} = $_[1] }
Subroutine set_trans_loc: called CC=1 SCAR=0.0
Pod documented
1001  
1002 6845 ▶sub chains_loc { $_[0]{chains_loc} }
Subroutine chains_loc: called CC=1 SCAR=0.0
Pod documented
1003 379 ▶sub set_chains_loc { $_[0]{chains_loc} = $_[1] }
Subroutine set_chains_loc: called CC=1 SCAR=0.0
Pod documented
1004  
1005 sub supports {
1006 9 ▶ shift;
Subroutine supports: called CC=3 SCAR=11.0
Pod documented
1007 9 my ($feature) = @_;
1008  
1009 9 ▶ return 1 if $feature eq 'transactions';
Branch: 1 branch
BranchTrueFalseTotal
if $feature eq "transactions" 8 1 9
1010 1 ▶ return if $feature eq 'singletones';
Branch: 1 branch
BranchTrueFalseTotal
if $feature eq "singletones" 0 1 1
1011 1 return;
1012 }
1013  
1014 sub clear {
1015 202 ▶ my $self = shift;
Subroutine clear: called CC=3 SCAR=11.2
Pod documented
1016 202 my $obj = shift;
1017  
1018 202 ▶ my $sector = $self->load_sector( $obj->_base_offset )
Branch: 1 branch
BranchTrueFalseTotal
my $sector = $self->load_sector($obj->_base_offset) or return 0 202 202
1019 or return;
1020  
1021 202 ▶ return unless $sector->staleness == $obj->_staleness;
Branch: 1 branch
BranchTrueFalseTotal
unless $sector->staleness == $obj->_staleness 0 202 202
1022  
1023 202 $sector->clear;
1024  
1025 202 return;
1026 }
1027  
1028 =head2 _dump_file()
1029  
1030 This method takes no arguments. It's used to print out a textual representation
1031 of the DBM::Deep DB file. It assumes the file is not-corrupted.
1032  
1033 =cut
1034  
1035 sub _dump_file {
1036 2 ▶ my $self = shift;
Subroutine _dump_file: called CC=13 SCAR=28.7
1037  
1038 # Read the header
1039 2 my $spot = $self->_read_file_header();
1040  
1041 2 my %types = (
1042 0 => 'B',
1043 1 => 'D',
1044 2 => 'I',
1045 );
1046  
1047 2 my %sizes = (
1048 'D' => $self->data_sector_size,
1049 'B' => DBM::Deep::Sector::File::BucketList->new({engine=>$self,offset=>1})->size,
1050 'I' => DBM::Deep::Sector::File::Index->new({engine=>$self,offset=>1})->size,
1051 );
1052  
1053 2 my $return = "";
1054  
1055 # Header values
1056 2 $return .= "NumTxns: " . $self->num_txns . $/;
1057  
1058 # Read the free sector chains
1059 2 my %sectors;
1060 2 foreach my $multiple ( 0 .. 2 ) {
1061 6 $return .= "Chains($types{$multiple}):";
1062 6 my $old_loc = $self->chains_loc + $multiple * $self->byte_size;
1063 6 while ( 1 ) {
1064 my $loc = unpack(
1065 6 $StP{$self->byte_size},
1066 $self->storage->read_at( $old_loc, $self->byte_size ),
1067 );
1068  
1069 # We're now out of free sectors of this kind.
1070 6 ▶ unless ( $loc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loc) 6 0 6
1071 6 last;
1072 }
1073  
1074 0 $sectors{ $types{$multiple} }{ $loc } = undef;
1075 0 $old_loc = $loc + $self->SIG_SIZE + $STALE_SIZE;
1076 0 $return .= " $loc";
1077 }
1078 6 $return .= $/;
1079 }
1080  
1081 SECTOR:
1082 2 ▶ while ( $spot < $self->storage->{end} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spot < $self->storage->{"end"}) 5 2 7
1083 # Read each sector in order.
1084 5 my $sector = $self->load_sector( $spot );
1085 5 ▶ if ( !$sector ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sector) { } 0 5 5
1086 # Find it in the free-sectors that were found already
1087 0 foreach my $type ( keys %sectors ) {
1088 0 ▶ if ( exists $sectors{$type}{$spot} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sectors{$type}{$spot}) 0 0 0
1089 0 my $size = $sizes{$type};
1090 0 $return .= sprintf "%08d: %s %04d\n", $spot, 'F' . $type, $size;
1091 0 $spot += $size;
1092 0 next SECTOR;
1093 }
1094 }
1095  
1096 0 die "********\n$return\nDidn't find free sector for $spot in chains\n********\n";
1097 }
1098 else {
1099 5 $return .= sprintf "%08d: %s %04d", $spot, $sector->type, $sector->size;
1100 5 ▶ if ( $sector->type eq 'D' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sector->type eq 'D') { } 2 3 5
elsif ($sector->type eq 'A' or $sector->type eq 'H') { } 2 1 3
elsif ($sector->type eq 'B') { } 1 0 1
1101 2 $return .= ' ' . $sector->data;
1102 }
1103 ▶ elsif ( $sector->type eq 'A' || $sector->type eq 'H' ) {
Condition66%2/3
$sector->type eq "A" or $sector->type eq "H"
l!l&&r!l&&!r
021
MC/DC50%1/2
$sector->type eq "A" or $sector->type eq "H"
$sector->type eq "A" $sector->type eq "H"
Truth table66%2/3
A or B
A= $sector->type eq "A"B= $sector->type eq "H"
ABresult
1X1
011
000
1104 2 $return .= ' REF: ' . $sector->get_refcount;
1105 }
1106 elsif ( $sector->type eq 'B' ) {
1107 1 foreach my $bucket ( $sector->chopped_up ) {
1108 1 $return .= "\n ";
1109 1 $return .= sprintf "%08d", unpack($StP{$self->byte_size},
1110 substr( $bucket->[-1], $self->hash_size, $self->byte_size),
1111 );
1112 1 my $l = unpack( $StP{$self->byte_size},
1113 substr( $bucket->[-1],
1114 $self->hash_size + $self->byte_size,
1115 $self->byte_size,
1116 ),
1117 );
1118 1 $return .= sprintf " %08d", $l;
1119 1 foreach my $txn ( 0 .. $self->num_txns - 2 ) {
1120 0 my $l = unpack( $StP{$self->byte_size},
1121 substr( $bucket->[-1],
1122 $self->hash_size + 2 * $self->byte_size + $txn * ($self->byte_size + $STALE_SIZE),
1123 $self->byte_size,
1124 ),
1125 );
1126 0 $return .= sprintf " %08d", $l;
1127 }
1128 }
1129 }
1130 5 $return .= $/;
1131  
1132 5 $spot += $sector->size;
1133 }
1134 }
1135  
1136 2 return $return;
1137 }
1138  
1139 47 1;
1140 __END__
« blib/lib/DBM/Deep/Sector/File/Reference.pm ↑ Summary blib/lib/DBM/Deep/Sector/DBI.pm »