Sync.pm

filter: Statementstmt 80.7% 143 / 177 Branchbran 47.5% 39 / 82 Conditioncond 55.0% 11 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 95.8% 23 / 24 Podpod 0.0% 0 / 8 totaltotal 68.4% 221 / 323 CC 51 SCAR 26.2
CC
51
Cov
69%
SyncCacheSize
56.0
SyncSize
23.5
AUTOLOAD
17.6
CRAP 13.7
↑ Summary Sync/SDBM_File.pm »
1  
2 package MLDBM::Sync;
3 7 $VERSION = '0.30';
4  
5 7 ▶use MLDBM;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use MLDBM::Sync::SDBM_File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use vars qw($AUTOLOAD @EXT $CACHE_ERR $LOCK_SH $LOCK_EX $LOCK_UN);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶eval "use Tie::Cache;";
Subroutine BEGIN: called CC=1 SCAR=1.2
16 7 ▶if (($@)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 7 0 7
17 7 $CACHE_ERR = $@;
18 }
19  
20 7 $LOCK_SH = LOCK_SH;
21 7 $LOCK_UN = LOCK_UN;
22 7 $LOCK_EX = LOCK_EX;
23  
24 7 @EXT = ('.pag', '.dir', '');
25  
26 6 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
27 6 my($class, $file, @args) = @_;
28  
29 6 $file =~ /^(.*)$/s;
30 6 $file = $1;
31 6 my $fh = $file.".lock";
32  
33 6 my $self = bless {
34 'file' => $file,
35 'args' => [ $file, @args ],
36 'lock_fh' => $fh,
37 'lock_file' => $fh,
38 'lock_num' => 0,
39 'md5_keys' => 0,
40 'pid' => $$,
41 'keys' => [],
42 'db_type' => $MLDBM::UseDB,
43 'serializer' => $MLDBM::Serializer,
44 'remove_taint' => $MLDBM::RemoveTaint,
45 };
46  
47 6 $self;
48 }
49  
50 6 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=9.2
51 6 my $self = shift;
52 6 ▶ if($self->{lock_num}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lock_num"}) 0 6 6
53 0 $self->{lock_num} = 1;
54 0 $self->UnLock;
55 }
56 }
57  
58 845 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=13 SCAR=28.7
59 845 my($self, $key, $value) = @_;
60 845 $AUTOLOAD =~ /::([^:]+)$/;
61 845 my $func = $1;
62 845 ▶ grep($func eq $_, ('FETCH', 'STORE', 'EXISTS', 'DELETE'))
Branch: 1 branch
BranchTrueFalseTotal
grep(($func eq $_), ("FETCH", "STORE", "EXISTS", "DELETE")) or die "$func not handled by object $self" 845 0 845
63 || die("$func not handled by object $self");
64  
65 ## CHECKSUM KEYS
66 845 ▶ if(defined $key && $self->{md5_keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and $self->{"md5_keys"}) 200 645 845
Condition66%2/3
defined $key and $self->{"md5_keys"}
!ll&&!rl&&r
0645200
MC/DC50%1/2
defined $key and $self->{"md5_keys"}
defined $key $self->{"md5_keys"}
Truth table66%2/3
A and B
A= defined $keyB= $self->{"md5_keys"}
ABresult
0X0
100
111
67 200 $key = $self->SyncChecksum($key);
68 }
69  
70 # CACHE, short circuit if found in cache on FETCH/EXISTS
71 # after checksum, since that's what we store
72 845 ▶ my $cache = (defined $key) ? $self->{cache} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 845 0 845
73 845 ▶ if($cache && ($func eq 'FETCH' or $func eq 'EXISTS')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache and $func eq "FETCH" || $func eq "EXISTS") 0 845 845
Condition16%1/6
$cache and $func eq "FETCH" || $func eq "EXISTS"
!ll&&!rl&&r
84500
$func eq "FETCH" or $func eq "EXISTS"
l!l&&r!l&&!r
000
MC/DC0%0/3
$cache and $func eq "FETCH" || $func eq "EXISTS"
$cache $func eq "FETCH" $func eq "EXISTS"
Truth table25%1/4
A and B or C
A= $cacheB= $func eq "FETCH"C= $func eq "EXISTS"
ABCresult
0XX0
1000
11X1
1011
74 0 my $rv = $cache->$func($key);
75 0 ▶ defined($rv) && return($rv);
Branch: 1 branch
BranchTrueFalseTotal
defined $rv and return $rv 0 0 0
76 }
77  
78 845 my $rv;
79 845 ▶ if ($func eq 'FETCH' or $func eq 'EXISTS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq 'FETCH' or $func eq 'EXISTS') { } 425 420 845
Condition66%2/3
$func eq "FETCH" or $func eq "EXISTS"
l!l&&r!l&&!r
4250420
MC/DC50%1/2
$func eq "FETCH" or $func eq "EXISTS"
$func eq "FETCH" $func eq "EXISTS"
Truth table66%2/3
A or B
A= $func eq "FETCH"B= $func eq "EXISTS"
ABresult
1X1
011
000
80 425 $self->read_lock;
81 } else {
82 420 $self->lock;
83 }
84  
85 {
86 844 local $MLDBM::RemoveTaint = $self->{remove_taint};
87 844 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 415 429 844
88 415 $rv = $self->{dbm}->$func($key, $value);
89 } else {
90 429 $rv = $self->{dbm}->$func($key);
91 }
92 }
93  
94 844 $self->unlock;
95  
96 # do after lock critical section, no point taking
97 # any extra time there
98 844 ▶ $cache && $cache->$func($key, $value);
Branch: 1 branch
BranchTrueFalseTotal
$cache and $cache->$func($key, $value) 0 844 844
99  
100 844 $rv;
101 }
102  
103 11 ▶sub CLEAR {
Subroutine CLEAR: called CC=9 SCAR=23.6
104 11 my $self = shift;
105
106 11 $self->lock;
107 11 $self->{dbm}->CLEAR;
108 11 $self->{dbm} = undef;
109 # delete the files to free disk space
110 11 my $unlinked = 0;
111 11 for (@EXT) {
112 33 my $file = $self->{file}.$_;
113 33 ▶ next if(! -e $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 11 22 33
114 22 ▶ if(-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) { } 0 22 22
115 0 ▶ rmdir($file) || warn("can't unlink dir $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
rmdir $file or warn "can't unlink dir ${file}: $!" 0 0 0
116 } else {
117 22 ▶ unlink($file) || die("can't unlink file $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "can't unlink file ${file}: $!" 22 0 22
118 }
119  
120 22 $unlinked++;
121 }
122 11 ▶ if($self->{lock_num} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lock_num"} > 1) 0 11 11
123 0 $self->SyncTie; # recreate, not done with it yet
124 }
125  
126 11 $self->unlock;
127 11 ▶ if($self->{lock_num} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lock_num"} == 0) 11 0 11
128 # only unlink if we are clear of all the locks
129 11 unlink($self->{lock_file});
130 }
131
132 11 ▶ $self->{cache} && $self->{cache}->CLEAR;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"cache"} and $self->{"cache"}->CLEAR 0 11 11
133  
134 11 1;
135 };
136  
137 # don't bother with cache for first/next key since it'll kill
138 # the cache anyway likely
139 12 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=3 SCAR=11.0
140 12 my $self = shift;
141  
142 12 ▶ if($self->{md5_keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"md5_keys"}) 1 11 12
143 1 confess("can't get keys() or each() on MLDBM::Sync database ".
144 "with SyncKeysChecksum(1) set");
145 }
146
147 11 $self->read_lock;
148 11 my $key = $self->{dbm}->FIRSTKEY();
149 11 my @keys;
150 11 while(1) {
151 531 ▶ last if ! defined($key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 11 520 531
152 520 push(@keys, $key);
153 520 $key = $self->{dbm}->NEXTKEY($key);
154 }
155 11 $self->unlock;
156 11 $self->{'keys'} = \@keys;
157  
158 11 $self->NEXTKEY;
159 }
160  
161 531 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=2 SCAR=7.4
162 531 my $self = shift;
163  
164 531 ▶ if($self->{md5_keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"md5_keys"}) 0 531 531
165 0 confess("can't get keys() or each() on MLDBM::Sync database ".
166 "with SyncKeysChecksum(1) set");
167 }
168
169 531 my $rv = shift(@{$self->{'keys'}});
170 }
171  
172 200 ▶sub SyncChecksum {
Subroutine SyncChecksum: called CC=2 SCAR=7.6
Pod undocumented
173 200 my($self, $key) = @_;
174 200 ▶ if(ref $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key) { } 0 200 200
175 0 join('g', md5_hex($$key), sprintf("%07d",length($$key)));
176 } else {
177 200 join('g', md5_hex($key), sprintf("%07d", length($key)));
178 }
179 }
180  
181 0 ▶sub SyncCacheSize {
Subroutine SyncCacheSize: not called CC=7 SCAR=40.3
Pod undocumented
182 0 my($self, $size) = @_;
183 0 ▶ $CACHE_ERR && die("need Tie::Cache installed to use this feature: $@");
Branch: 1 branch
BranchTrueFalseTotal
$MLDBM::Sync::CACHE_ERR and die "need Tie::Cache installed to use this feature: $@" 0 0 0
184  
185 0 ▶ if ($size =~ /^(\d+)(M|K)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size =~ /^(\d+)(M|K)$/) { } 0 0 0
186 0 my($num, $type) = ($1, $2);
187 0 ▶ if (($type eq 'M')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'M') { } 0 0 0
elsif ($type eq 'K') { } 0 0 0
188 0 $size = $num * 1024 * 1024;
189 } elsif (($type eq 'K')) {
190 0 $size = $num * 1024;
191 } else {
192 0 die "$type symbol not understood for $size";
193 }
194 } else {
195 0 ▶ ($size =~ /^\d+$/) or die("$size must be bytes size for cache");
Branch: 1 branch
BranchTrueFalseTotal
$size =~ /^\d+$/ or die "$size must be bytes size for cache" 0 0 0
196 }
197
198 0 ▶ if ($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache"}) 0 0 0
199 0 $self->{cache}->CLEAR(); # purge old cache, to free up RAM maybe for mem leaks
200 }
201
202 0 my %cache;
203 0 my $cache = tie %cache, 'Tie::Cache', { MaxBytes => $size };
204 0 $self->{cache} = $cache; # use non tied interface, faster
205 }
206  
207 471 ▶sub SyncTie {
Subroutine SyncTie: called CC=2 SCAR=7.0
Pod undocumented
208 471 my $self = shift;
209 471 my %temp_hash;
210 471 my $args = $self->{args};
211 471 local $MLDBM::UseDB = $self->{db_type};
212 471 local $MLDBM::Serializer = $self->{serializer};
213 471 local $MLDBM::RemoveTaint = $self->{remove_taint};
214 471 ▶ $self->{dbm} = tie(%temp_hash, 'MLDBM', @$args) || die("can't tie to MLDBM with args: ".join(',', @$args)."; error: $!");
Condition50%1/2
tie(%temp_hash, 'MLDBM', @$args) || die(q[can't tie to MLDBM with args: ] . join(',', @$args) . "; error: $!")
l!l
4710
MC/DC0%0/1
tie(%temp_hash, 'MLDBM', @$args) || die(q[can't tie to MLDBM with args: ] . join(',', @$args) . "; error: $!")
tie(%temp_hash, 'MLDBM', @$args)
Truth table50%1/2
A
A= tie(%temp_hash, 'MLDBM', @$args)
Aresult
11
00
215  
216 471 $self->{dbm};
217 }
218  
219 #### DOCUMENTED API ################################################################
220  
221 1 ▶sub SyncKeysChecksum {
Subroutine SyncKeysChecksum: called CC=2 SCAR=7.6
Pod undocumented
222 1 my($self, $setting) = @_;
223 1 ▶ if(defined $setting) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $setting) { } 1 0 1
224 1 $self->{md5_keys} = $setting;
225 } else {
226 0 $self->{md5_keys};
227 }
228 }
229  
230 7 *read_lock = *ReadLock;
231 439 ▶sub ReadLock { shift->Lock(1); }
Subroutine ReadLock: called CC=1 SCAR=0.0
Pod undocumented
232  
233 7 *lock = *SyncLock = *Lock;
234 873 ▶sub Lock {
Subroutine Lock: called CC=8 SCAR=21.1
Pod undocumented
235 873 my($self, $read_lock) = @_;
236  
237 873 ▶ if($self->{lock_num}++ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'lock_num'}++ == 0) { } 471 402 873
238 471 my $file = $self->{lock_file};
239 471 ▶ open($self->{lock_fh}, "+>$file") || die("can't open file $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open $self->{"lock_fh"}, "+>$file" or die "can't open file ${file}: $!" 471 0 471
240 471 ▶ flock($self->{lock_fh}, ($read_lock ? $LOCK_SH : $LOCK_EX))
Branch: 3 branches
BranchTrueFalseTotal
flock $self->{"lock_fh"}, $read_lock ? $MLDBM::Sync::LOCK_SH : $MLDBM::Sync::LOCK_EX or die q[can't ] . ($read_lock ? "read" : "write") . " lock ${file}: $!" 471 0 471
$read_lock ? : 238 233 471
$read_lock ? : 0 0 0
241 || die("can't ". ($read_lock ? "read" : "write") ." lock $file: $!");
242 471 $self->{read_lock} = $read_lock;
243 471 $self->SyncTie;
244 } else {
245 402 ▶ if ($self->{read_lock} and ! $read_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"read_lock"} and not $read_lock) 1 401 402
Condition100%3/3
$self->{"read_lock"} and not $read_lock
!ll&&!rl&&r
2012001
MC/DC100%2/2
$self->{"read_lock"} and not $read_lock
$self->{"read_lock"} not $read_lock
Truth table100%3/3
A and not(B)
A= $self->{"read_lock"}B= not $read_lock
ABresult
0X0
100
111
246 1 $self->{lock_num}--; # roll back lock count
247 # confess here to help developer track this down
248 1 confess("Can't upgrade lock type from LOCK_SH to LOCK_EX! ".
249 "This could happen if you tried to write to the MLDBM ".
250 "in a critical section locked by ReadLock(). ".
251 "Also the read expression my \$v = \$db{'key1'}{'key2'} will trigger a write ".
252 "if \$db{'key1'} does not already exist, so this will error in a ReadLock() section"
253 );
254 }
255 401 1;
256 }
257 }
258  
259 7 *unlock = *SyncUnLock = *UnLock;
260 872 ▶sub UnLock {
Subroutine UnLock: called CC=5 SCAR=16.3
Pod undocumented
261 872 my $self = shift;
262  
263 872 ▶ if($self->{lock_num} && $self->{lock_num}-- == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'lock_num'} and $self->{'lock_num'}-- == 1) { } 471 401 872
Condition66%2/3
$self->{"lock_num"} and $self->{"lock_num"}-- == 1
!ll&&!rl&&r
0401471
MC/DC50%1/2
$self->{"lock_num"} and $self->{"lock_num"}-- == 1
$self->{"lock_num"} $self->{"lock_num"}-- == 1
Truth table66%2/3
A and B
A= $self->{"lock_num"}B= $self->{"lock_num"}-- == 1
ABresult
0X0
100
111
264 471 $self->{lock_num} = 0;
265 471 undef $self->{dbm};
266 471 ▶ flock($self->{'lock_fh'}, $LOCK_UN) || die("can't unlock $self->{'lock_file'}: $!");
Branch: 1 branch
BranchTrueFalseTotal
flock $self->{"lock_fh"}, $MLDBM::Sync::LOCK_UN or die qq[can't unlock $self->{"lock_file"}: $!] 471 0 471
267 471 ▶ close($self->{'lock_fh'}) || die("can't close $self->{'lock_file'}");
Branch: 1 branch
BranchTrueFalseTotal
close $self->{"lock_fh"} or die qq[can't close $self->{"lock_file"}] 471 0 471
268 471 $self->{read_lock} = undef;
269 471 1;
270 } else {
271 401 1;
272 }
273 }
274  
275 1 ▶sub SyncSize {
Subroutine SyncSize: called CC=7 SCAR=31.6
Pod undocumented
276 1 my $self = shift;
277 1 my $size = 0;
278 1 for (@EXT) {
279 3 my $file = $self->{file}.$_;
280 3 ▶ next unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 3 0 3
281 0 $size += (stat($file))[7];
282  
283 0 ▶ if(-d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $file) 0 0 0
284 0 $size += (stat($file))[7];
285 0 ▶ opendir(DIR, $file) || next;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $file or next 0 0 0
286 0 my @files = readdir(DIR);
287 0 for my $dir_file (@files) {
288 0 ▶ next if $dir_file =~ /^\.\.?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $dir_file =~ /^\.\.?$/ 0 0 0
289 0 $size += (stat("$file/$dir_file"))[7];
290 }
291 0 closedir(DIR);
292 }
293 }
294  
295 1 $size;
296 }
297  
298 7 1;
299  
300 __END__
↑ Summary Sync/SDBM_File.pm »