Sync/SDBM_File.pm

filter: Statementstmt 94.7% 90 / 95 Branchbran 67.3% 31 / 46 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 84.3% 140 / 166 CC 27 SCAR 18.7
CC
27
Cov
85%
STORE
9.5
FETCH
8.3
DELETE
7.5
CRAP 6.5
« Sync.pm ↑ Summary
1  
2 package MLDBM::Sync::SDBM_File;
3 7 $VERSION = .17;
4  
5 7 ▶use SDBM_File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use vars qw(@ISA $MaxSegments $MaxSegmentLength %KEYS $Zlib $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 @ISA = qw(SDBM_File);
10 7 $MaxSegments = 8192; # to a 1M limit
11 # leave room for key index pad
12 7 $MaxSegmentLength = 128;
13 7 ▶eval "use Compress::Zlib";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶$Zlib = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 7 7
15  
16 23 ▶sub FETCH {
Subroutine FETCH: called CC=8 SCAR=21.1
17 23 my($self, $key) = @_;
18 23 my $segment_length = $MaxSegmentLength;
19  
20 23 my $total_rv;
21 23 ▶ for(my $index = 0; $index < $MaxSegments; $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $MLDBM::Sync::SDBM_File::MaxSegments) 29 0 29
22 29 my $rv = $self->SUPER::FETCH(_index_key($key, $index));
23 29 ▶ if(defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) { } 28 1 29
24 28 ▶ $total_rv ||= '';
Condition100%2/2
$total_rv ||= ""
l!l
622
MC/DC100%1/1
$total_rv ||= ""
$total_rv
Truth table100%2/2
A
A= $total_rv
Aresult
11
00
25 28 $total_rv .= $rv;
26 28 ▶ last if length($rv) < $segment_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $rv < $segment_length 22 6 28
27 } else {
28 1 last;
29 }
30 }
31  
32 23 ▶ if(defined $total_rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $total_rv) 22 1 23
33 22 $total_rv =~ s/^(..)//s;
34 22 my $type = $1;
35 22 ▶ if($type eq 'G}') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'G}') { } 22 0 22
elsif ($type eq 'N}') { } 0 0 0
36 22 $total_rv = uncompress($total_rv);
37 } elsif ($type eq 'N}') {
38 # nothing
39 } else {
40 # old SDBM_File ?
41 0 $total_rv = $type . $total_rv;
42 }
43 }
44  
45 23 $total_rv;
46 }
47  
48 13 ▶sub STORE {
Subroutine STORE: called CC=9 SCAR=22.5
49 13 my($self, $key, $value) = @_;
50 13 my $segment_length = $MaxSegmentLength;
51  
52 # DELETE KEYS FIRST
53 13 ▶ for(my $index = 0; $index < $MaxSegments; $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $MLDBM::Sync::SDBM_File::MaxSegments) 19 0 19
54 19 my $index_key = _index_key($key, $index);
55 19 my $rv = $self->SUPER::FETCH($index_key);
56 19 ▶ if(defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) { } 7 12 19
57 7 $self->SUPER::DELETE($index_key);
58 } else {
59 12 last;
60 }
61 7 ▶ last if length($rv) < $segment_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $rv < $segment_length 1 6 7
62 }
63  
64 # G - Gzip compression
65 # N - No compression
66 #
67 13 my $old_value = $value;
68 13 ▶ $value = ($Zlib && (length($value) >= $segment_length/2)) ? "G}".compress($value) : "N}".$value;
Branch: 1 branch
BranchTrueFalseTotal
$MLDBM::Sync::SDBM_File::Zlib && length $value >= $segment_length / 2 ? : 13 0 13
Condition33%1/3
$MLDBM::Sync::SDBM_File::Zlib and length $value >= $segment_length / 2
!ll&&!rl&&r
0013
MC/DC0%0/2
$MLDBM::Sync::SDBM_File::Zlib and length $value >= $segment_length / 2
$MLDBM::Sync::SDBM_File::Zlib length $value >= $segment_length / 2
Truth table33%1/3
A and B
A= $MLDBM::Sync::SDBM_File::ZlibB= length $value >= $segment_length / 2
ABresult
0X0
100
111
69  
70 13 my($total_rv, $last_index);
71 13 ▶ for(my $index = 0; $index < $MaxSegments; $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $MLDBM::Sync::SDBM_File::MaxSegments) 38 0 38
72 38 ▶ if($index == $MaxSegments) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $MLDBM::Sync::SDBM_File::MaxSegments) 0 38 38
73 0 die("can't store more than $MaxSegments segments of $MaxSegmentLength bytes per key in ".__PACKAGE__);
74 }
75 38 $value =~ s/^(.{0,$segment_length})//so;
76 38 my $segment = $1;
77
78 38 ▶ last if length($segment) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $segment == 0 13 25 38
79 # print "STORING "._index_key($key, $index)." $segment\n";
80 25 my $rv = $self->SUPER::STORE(_index_key($key, $index), $segment);
81 25 $total_rv .= $segment;
82 25 $last_index = $index;
83 }
84  
85 # use Time::HiRes;
86 # print "[".&Time::HiRes::time()."] STORED ".($last_index+1)." segments for length ".
87 # length($total_rv)." bytes for value ".length($old_value)."\n";
88  
89 13 $old_value;
90 }
91  
92 12 ▶sub DELETE {
Subroutine DELETE: called CC=7 SCAR=20.1
93 12 my($self, $key) = @_;
94 12 my $segment_length = $MaxSegmentLength;
95  
96 12 my $total_rv;
97 12 ▶ for(my $index = 0; $index < $MaxSegments; $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $MLDBM::Sync::SDBM_File::MaxSegments) 18 0 18
98 18 my $index_key = _index_key($key, $index);
99 18 ▶ my $rv = $self->SUPER::FETCH($index_key) || '';
Condition50%1/2
$self->SUPER::FETCH($index_key) || ''
l!l
180
MC/DC0%0/1
$self->SUPER::FETCH($index_key) || ''
$self->SUPER::FETCH($index_key)
Truth table50%1/2
A
A= $self->SUPER::FETCH($index_key)
Aresult
11
00
100 18 $self->SUPER::DELETE($index_key);
101 18 ▶ $total_rv ||= '';
Condition100%2/2
$total_rv ||= ""
l!l
612
MC/DC100%1/1
$total_rv ||= ""
$total_rv
Truth table100%2/2
A
A= $total_rv
Aresult
11
00
102 18 $total_rv .= $rv;
103 18 ▶ last if length($rv) < $segment_length;
Branch: 1 branch
BranchTrueFalseTotal
if length $rv < $segment_length 12 6 18
104 }
105  
106 12 $total_rv =~ s/^(..)//s;
107 12 my $type = $1;
108 12 ▶ if($type eq 'G}') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'G}') { } 12 0 12
elsif ($type eq 'N}') { } 0 0 0
109 12 $total_rv = uncompress($total_rv);
110 } elsif ($type eq 'N}') {
111 # normal
112 } else {
113 # old SDBM_File
114 0 $total_rv = $type.$total_rv;
115 }
116  
117 12 $total_rv;
118 }
119  
120 5 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=5 SCAR=16.2
121 5 my $self = shift;
122  
123 5 my $key = $self->SUPER::FIRSTKEY();
124 5 my @keys = ();
125 5 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 3 2 5
126 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$key = $self->SUPER::NEXTKEY($key) and do { do { unless ($key =~ /\*\*\d+$/s) { if (my $new_key = _decode_key($key)) { push @keys, $new_key; } ; } } } 27 3 30
127 30 ▶ if($key !~ /\*\*\d+$/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /\*\*\d+$/s) 30 0 30
128 30 ▶ if(my $new_key = _decode_key($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $new_key = _decode_key($key)) 30 0 30
129 30 push(@keys, $new_key);
130 }
131 }
132 } while($key = $self->SUPER::NEXTKEY($key));
133 }
134 5 $KEYS{$self} = \@keys;
135  
136 5 $self->NEXTKEY;
137 }
138  
139 35 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
140 35 my $self = shift;
141 35 shift(@{$KEYS{$self}});
142 }
143  
144 91 ▶sub _index_key {
Subroutine _index_key: called CC=2 SCAR=7.0
145 91 my($key, $index) = @_;
146 0 $key =~ s/([\%\*])/uc sprintf("%%%02x",ord($1))/esg;
147 91 ▶ $index ? $key.'**'.$index : $key;
Branch: 1 branch
BranchTrueFalseTotal
$index ? : 30 61 91
148 }
149  
150 30 ▶sub _decode_key {
Subroutine _decode_key: called CC=1 SCAR=0.2
151 30 my $key = shift;
152 0 $key =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
153 30 $key;
154 }
155  
156 7 1;
157  
« Sync.pm ↑ Summary