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

filter: Statementstmt 97.3% 74 / 76 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 4 totaltotal 92.5% 99 / 107 CC 9 SCAR 11.0
CC
9
Cov
96%
_init
6.0
data
3.0
free
2.1
CRAP 3.0
« blib/lib/DBM/Deep/Sector/DBI/Scalar.pm ↑ Summary blib/lib/DBM/Deep.pm »
1 package DBM::Deep::Sector::File::Scalar;
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::Sector::File::Data );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 53 my $STALE_SIZE = 2;
12  
13 # Please refer to the pack() documentation for further information
14 53 my %StP = (
15 1 => 'C', # Unsigned char value (no order needed as it's just one byte)
16 2 => 'n', # Unsigned short in "network" (big-endian) order
17 4 => 'N', # Unsigned long in "network" (big-endian) order
18 8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent)
19 );
20  
21 sub free {
22 409 ▶ my $self = shift;
Subroutine free: called CC=2 SCAR=7.2
Pod undocumented
23  
24 409 my $chain_loc = $self->chain_loc;
25  
26 409 $self->SUPER::free();
27  
28 409 ▶ if ( $chain_loc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chain_loc) 0 409 409
29 0 $self->engine->load_sector( $chain_loc )->free;
30 }
31  
32 409 return;
33 }
34  
35 sub _init {
36 6421 ▶ my $self = shift;
Subroutine _init: called CC=6 SCAR=17.9
37  
38 6421 my $engine = $self->engine;
39  
40 6421 ▶ unless ( $self->offset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->offset) 2221 4200 6421
41 2221 my $data_section = $self->size - $self->base_size - $engine->byte_size - 1;
42  
43 2221 $self->{offset} = $engine->_request_data_sector( $self->size );
44  
45 2221 my $data = delete $self->{data};
46 53 ▶ my $utf8 = do { no warnings 'utf8'; $data !~ /^[\0-\xff]*\z/ };
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2221 ▶ if($utf8){
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) { } 7 2214 2221
48 7 ▶ if($engine->{v} < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($engine->{"v"} < 4) 0 7 7
49 0 DBM::Deep->_throw_error(
50 "This database format version is too old for Unicode"
51 );
52 }
53 7 utf8::encode $data;
54 7 $self->{type} = $engine->SIG_UNIDATA;
55 }
56 2214 else { $self->{type} = $engine->SIG_DATA; }
57  
58 2221 my $dlen = length $data;
59 2221 my $continue = 1;
60 2221 my $curr_offset = $self->offset;
61 2221 ▶ while ( $continue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($continue) 4737 2221 6958
62  
63 4737 my $next_offset = 0;
64  
65 4737 my ($leftover, $this_len, $chunk);
66 4737 ▶ if ( $dlen > $data_section ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dlen > $data_section) { } 2516 2221 4737
67 2516 $leftover = 0;
68 2516 $this_len = $data_section;
69 2516 $chunk = substr( $data, 0, $this_len );
70  
71 2516 $dlen -= $data_section;
72 2516 $next_offset = $engine->_request_data_sector( $self->size );
73 2516 $data = substr( $data, $this_len );
74 }
75 else {
76 2221 $leftover = $data_section - $dlen;
77 2221 $this_len = $dlen;
78 2221 $chunk = $data;
79  
80 2221 $continue = 0;
81 }
82  
83 4737 $engine->storage->print_at( $curr_offset, $self->type ); # Sector type
84 # Skip staleness
85 $engine->storage->print_at( $curr_offset + $self->base_size,
86 pack( $StP{$engine->byte_size}, $next_offset ), # Chain loc
87 4737 pack( $StP{1}, $this_len ), # Data length
88 $chunk, # Data to be stored in this sector
89 chr(0) x $leftover, # Zero-fill the rest
90 );
91  
92 4737 $curr_offset = $next_offset;
93 }
94  
95 2221 return;
96 }
97 }
98  
99 sub data_length {
100 3739 ▶ my $self = shift;
Subroutine data_length: called CC=1 SCAR=0.0
Pod undocumented
101  
102 3739 my $buffer = $self->engine->storage->read_at(
103 $self->offset + $self->base_size + $self->engine->byte_size, 1
104 );
105  
106 3739 return unpack( $StP{1}, $buffer );
107 }
108  
109 sub chain_loc {
110 4148 ▶ my $self = shift;
Subroutine chain_loc: called CC=1 SCAR=0.0
Pod undocumented
111 return unpack(
112 4148 $StP{$self->engine->byte_size},
113 $self->engine->storage->read_at(
114 $self->offset + $self->base_size,
115 $self->engine->byte_size,
116 ),
117 );
118 }
119  
120 sub data {
121 1206 ▶ my $self = shift;
Subroutine data: called CC=3 SCAR=11.0
Pod undocumented
122 1206 my $engine = $self->engine;
123  
124 1206 my $data;
125 1206 while ( 1 ) {
126 3739 my $chain_loc = $self->chain_loc;
127  
128 3739 $data .= $engine->storage->read_at(
129 $self->offset + $self->base_size + $engine->byte_size + 1, $self->data_length,
130 );
131  
132 3739 ▶ last unless $chain_loc;
Branch: 1 branch
BranchTrueFalseTotal
unless $chain_loc 1206 2533 3739
133  
134 2533 $self = $engine->load_sector( $chain_loc );
135 }
136  
137 1206 ▶ utf8::decode $data if $self->type eq $engine->SIG_UNIDATA;
Branch: 1 branch
BranchTrueFalseTotal
if $self->type eq $engine->SIG_UNIDATA 8 1198 1206
138  
139 1206 return $data;
140 }
141  
142 53 1;
143 __END__
« blib/lib/DBM/Deep/Sector/DBI/Scalar.pm ↑ Summary blib/lib/DBM/Deep.pm »