CriticDB/DB/Stor.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 73.3% 22 / 30 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 3 totaltotal 87.7% 100 / 114 CC 17 SCAR 16.7
CC
17
Cov
90%
write
9.2
read
6.1
__ANON__
4.0
CRAP 5.3
« CriticDB/DB.pm ↑ Summary CriticDB/DB/Index/Set.pm »
1 package App::CriticDB::DB::Stor;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use parent 'App::CriticDB::DB';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Fcntl qw/:flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Temp qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Storable qw/nstore_fd fd_retrieve/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION='0.0.6';
13  
14 6 ▶sub storfileUnchanged {
Subroutine storfileUnchanged: called CC=1 SCAR=0.0
Pod undocumented
15 6 my ($self)=@_;
16 6 return !$self->_fileNewer($$self{file},$$self{mtime});
17 }
18  
19 6 ▶sub read {
Subroutine read: called CC=6 SCAR=18.0
Pod undocumented
20 6 my ($self)=@_;
21 6 ▶ if($$self{mtime}&&!$self->_fileNewer($$self{file},$$self{mtime})) { return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mtime"} and not $self->_fileNewer($self->{"file"}, $self->{"mtime"})) 1 5 6
Condition66%2/3
$self->{"mtime"} and not $self->_fileNewer($self->{"file"}, $self->{"mtime"})
!ll&&!rl&&r
501
MC/DC50%1/2
$self->{"mtime"} and not $self->_fileNewer($self->{"file"}, $self->{"mtime"})
$self->{"mtime"} not $self->_fileNewer($self->{"file"}, $self->{"mtime"})
Truth table66%2/3
A and not(B)
A= $self->{"mtime"}B= not $self->_fileNewer($self->{"file"}, $self->{"mtime"})
ABresult
0X0
100
111
22 5 ▶ if(!-e $$self{file}) { return $self->_init() }
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->{"file"}) 3 2 5
23 2 $$self{mtime}=time();
24 2 ▶ open(my $fh,'<',$$self{file}) or confess("Read failed on $$self{file}: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"file"} or confess(qq[Read failed on $self->{"file"}: $!]) 2 0 2
25 2 ▶ flock($fh, LOCK_SH) or confess("Cannot lock $$self{file} for reading: $!");
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 1 or confess(qq[Cannot lock $self->{"file"} for reading: $!]) 2 0 2
26 2 $$self{store}=fd_retrieve($fh);
27 2 flock($fh, LOCK_UN);
28 2 close($fh);
29 2 return $self;
30 }
31  
32 7 ▶sub write {
Subroutine write: called CC=9 SCAR=22.2
Pod undocumented
33 7 my ($self,$fn)=@_;
34 0 ▶ if(!$$self{file}) { confess("Filename has not been specified") }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"file"}) 0 7 7
35 7 ▶ if(!-e $$self{file}) { delete($$self{mtime}) }
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->{"file"}) 4 3 7
36 7 my ($fh,$tmpname,$lockfh);
37 2 ▶ my $failed=sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
38 2 my ($message,$raw)=@_;
39 2 ▶ if($tmpname) { unlink($tmpname) }
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpname) 2 0 2
40 2 ▶ if($lockfh) { flock($lockfh,LOCK_UN); close($lockfh) }
Branch: 1 branch
BranchTrueFalseTotal
if ($lockfh) 1 1 2
41 2 ▶ if($message) { confess("Failed to $message") }
Branch: 1 branch
BranchTrueFalseTotal
if ($message) 1 1 2
42 1 confess($raw);
43 7 };
44 7 $fh=File::Temp->new(TEMPLATE=>"$$self{file}-XXXXXXXX",UNLINK=>0,SUFFIX=>'.tmp',PERMS=>(0666&(~umask())));
45 7 $tmpname=$fh->filename();
46 7 ▶ nstore_fd($$self{store},$fh) or &$failed("write to $tmpname");
Branch: 1 branch
BranchTrueFalseTotal
nstore_fd($self->{"store"}, $fh) or &$failed("write to $tmpname") 7 0 7
47 7 ▶ close($fh) or &$failed("fully write to $tmpname");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or &$failed("fully write to $tmpname") 7 0 7
48 7 ▶ open($lockfh,'>>',$$self{file}) or &$failed("open $$self{file}: $!",undef($lockfh));
Branch: 1 branch
BranchTrueFalseTotal
open $lockfh, ">>", $self->{"file"} or &$failed(qq[open $self->{"file"}: $!], undef $lockfh) 6 1 7
49 6 ▶ flock($lockfh,LOCK_EX) or &$failed("lock $$self{file} for writing: $!");
Branch: 1 branch
BranchTrueFalseTotal
flock $lockfh, 2 or &$failed(qq[lock $self->{"file"} for writing: $!]) 6 0 6
50 6 ▶ $self->storfileUnchanged() or &$failed(0,"File changed underneath us, not yet handled");
Branch: 1 branch
BranchTrueFalseTotal
$self->storfileUnchanged or &$failed(0, "File changed underneath us, not yet handled") 5 1 6
51 5 ▶ rename($tmpname,$$self{file}) or &$failed("rename $tmpname to $$self{file}: $!");
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpname, $self->{"file"} or &$failed(qq[rename $tmpname to $self->{"file"}: $!]) 5 0 5
52 5 flock($lockfh, LOCK_UN);
53 5 close($lockfh);
54 5 $$self{mtime}=time();
55 5 return $self;
56 }
57  
58 1 1;
59  
60 __END__
« CriticDB/DB.pm ↑ Summary CriticDB/DB/Index/Set.pm »