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

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