lib/File/SmartTail/DB.pm

filter: Statementstmt 68.6% 46 / 67 Branchbran 28.1% 9 / 32 Conditioncond 31.8% 7 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 80.0% 8 / 10 Podpod 0.0% 0 / 4 totaltotal 47.3% 70 / 148 CC 23 SCAR 30.8
CC
23
Cov
51%
DumpStatus
42.0
new
31.0
DESTROY
6.0
CRAP 21.7
« lib/File/SmartTail.pm ↑ Summary lib/File/SmartTail/ConfigData.pm »
1 #
2 # $Id: DB.pm,v 1.9 2008/07/09 20:40:20 mprewitt Exp $
3 # $Source: /usr/local/src/perllib/Tail/0.1/lib/File/SmartTail/RCS/DB.pm,v $
4 #
5 # DMJA, Inc <smarttail@dmja.com>
6 #
7 # Copyright (C) 2003-2008 DMJA, Inc, File::SmartTail comes with
8 # ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to
9 # redistribute it and/or modify it under the same terms as Perl itself.
10 # See the "The Artistic License" L<LICENSE> for more details.
11 package File::SmartTail::DB;
12  
13 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use constant MAX_RETRIES => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶if ( ! -d "/var/tmp/filestatus" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d "/var/tmp/filestatus") 1 4 5
21 1 ▶ mkdir( "/var/tmp/filestatus", 01777 ) ||
Branch: 1 branch
BranchTrueFalseTotal
mkdir "/var/tmp/filestatus", 1023 or die "Unable to make status directory /var/tmp/filestatus [$!].\n" 1 0 1
22 die "Unable to make status directory /var/tmp/filestatus [$!].\n";
23  
24 1 chmod( 01777, "/var/tmp/filestatus" );
25 }
26  
27 {
28 5 my %cache;
29 1 ▶ sub new {
Subroutine new: called CC=13 SCAR=34.3
Pod undocumented
30 1 my $type = shift;
31 1 my %h = @_;
32  
33 my $statuskey = $h{statuskey} or
34 1 ▶ LOG()->logdie( "required param: statuskey" );
Branch: 1 branch
BranchTrueFalseTotal
my $statuskey = $h{"statuskey"} or LOG()->logdie("required param: statuskey") 1 0 1
35 1 ▶ my $tietype = $h{tietype} || 'DB_File';
Condition50%1/2
$h{'tietype'} || 'DB_File'
l!l
10
MC/DC0%0/1
$h{'tietype'} || 'DB_File'
$h{'tietype'}
Truth table50%1/2
A
A= $h{'tietype'}
Aresult
11
00
36 1 my $cachekey = join "\0", $statuskey, $tietype;
37 1 ▶ $cache{$cachekey} and return $cache{$cachekey};
Branch: 1 branch
BranchTrueFalseTotal
$cache{$cachekey} and return $cache{$cachekey} 0 1 1
38  
39 1 my $filename = "/var/tmp/filestatus/$statuskey";
40 1 my $saverr = "";
41 1 ▶ my $fullname = $filename . (($tietype eq 'NDBM_File') ? ".pag" : "");
Branch: 1 branch
BranchTrueFalseTotal
$tietype eq 'NDBM_File' ? : 0 1 1
42 1 ▶ if (-f $fullname && open(FH, ">> $fullname ")) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $fullname and open FH, ">> $fullname ") 0 1 1
Condition33%1/3
-f $fullname and open FH, ">> $fullname "
!ll&&!rl&&r
100
MC/DC0%0/2
-f $fullname and open FH, ">> $fullname "
-f $fullname open FH, ">> $fullname "
Truth table33%1/3
A and B
A= -f $fullnameB= open FH, ">> $fullname "
ABresult
0X0
100
111
43 0 my $count = 0;
44 0 ▶ while (++$count < MAX_RETRIES) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$count < 6) 0 0 0
45 0 ▶ last if flock(FH, LOCK_EX | LOCK_NB);
Branch: 1 branch
BranchTrueFalseTotal
if flock FH, 6 0 0 0
46 0 $saverr = $!;
47 0 sleep (2 ** $count);
48 }
49 0 ▶ LOG()->logdie( "Could not lock $filename in @{[MAX_RETRIES()]} attemps [$saverr].\n" )
Branch: 1 branch
BranchTrueFalseTotal
if $count >= 6 0 0 0
50 if ($count >= MAX_RETRIES);
51 0 flock(FH, LOCK_UN | LOCK_NB);
52 0 close FH;
53 }
54  
55 1 my %STATUS;
56 1 ▶ eval "use $tietype";
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶ die "Unable to use $tietype [$@]" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
58 1 ▶ my $STATFILE = tie( %STATUS, $tietype, $filename,
Condition33%1/3
tie(%STATUS, $tietype, $filename, 66, 384) || LOG()->logdie("Tie of status for $statuskey failed [$!].\n")
l!l&&r!l&&!r
100
MC/DC0%0/2
tie(%STATUS, $tietype, $filename, 66, 384) || LOG()->logdie("Tie of status for $statuskey failed [$!].\n")
tie(%STATUS, $tietype, $filename, 66, 384) LOG()->logdie("Tie of status for $statuskey failed [$!].\n")
Truth table33%1/3
A || B
A= tie(%STATUS, $tietype, $filename, 66, 384)B= LOG()->logdie("Tie of status for $statuskey failed [$!].\n")
ABresult
1X1
011
000
59 O_RDWR | O_CREAT, 0600 ) ||
60 LOG()->logdie( "Tie of status for $statuskey failed [$!].\n" );
61  
62 1 ▶ my $self = bless {
Condition33%1/3
ref $type || $type
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table33%1/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
63 STATUS => \%STATUS,
64 STATFILE => $STATFILE,
65 STATUSKEY => $statuskey,
66 TIETYPE => $tietype,
67 }, ref $type || $type;
68  
69 #
70 # keep our own reference, so the logging object is
71 # destroyed AFTER $self.
72 #
73 1 $self->{LOG} = LOG();
74  
75 1 $self->sync;
76  
77 1 return $cache{$cachekey} = $self;
78 }
79 }
80  
81 5 ▶sub sync {
Subroutine sync: called CC=3 SCAR=11.8
Pod undocumented
82 1 my $self = shift;
83  
84 1 ▶ if ($self->{STATFILE} && $self->can('sync')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"STATFILE"} and $self->can("sync")) 1 0 1
Condition33%1/3
$self->{"STATFILE"} and $self->can("sync")
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"STATFILE"} and $self->can("sync")
$self->{"STATFILE"} $self->can("sync")
Truth table33%1/3
A and B
A= $self->{"STATFILE"}B= $self->can("sync")
ABresult
0X0
100
111
85 1 eval {
86 $self->{STATFILE}->sync
87 1 };
88 }
89 }
90  
91 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
92 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
93  
94 0 $self->sync;
95 # LOG()->debug( sub {
96 # my $tt = $self->{TIETYPE} || '';
97 # my $sf = $self->{STATFILE} || '';
98 # my $ds = $self->DumpStatus || '';
99 # "sub DESTROY: TIETYPE: $tt; STATFILE: $sf; $ds";
100 # } );
101 # delete $self->{STATFILE}; # undef necessary?
102 # untie %{ $self->{STATUS} }; # will this do the right thing?
103 }
104  
105 0 ▶sub DumpStatus {
Subroutine DumpStatus: not called CC=6 SCAR=37.4
Pod undocumented
106 0 ▶ my $self = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
107 0 my %h = @_;
108  
109 0 my $indent = $h{indent};
110 0 ▶ defined $indent or $indent = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $indent or $indent = 1 0 0 0
111  
112 0 my $tab = "\t" x $indent;
113  
114 0 ▶ my $sk = $self->{STATUSKEY} || '';
Condition0%0/2
$self->{'STATUSKEY'} || ''
l!l
00
MC/DC0%0/1
$self->{'STATUSKEY'} || ''
$self->{'STATUSKEY'}
Truth table0%0/2
A
A= $self->{'STATUSKEY'}
Aresult
11
00
115 0 ▶ my @k = $self->{STATUS} ? sort keys %{ $self->{STATUS} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'STATUS'} ? : 0 0 0
116 0 ▶ my @m = @k ? map "$_ =>\t$self->{STATUS}->{$_}", @k : ();
Branch: 1 branch
BranchTrueFalseTotal
@k ? : 0 0 0
117 0 return join "\n$tab", "STATUSKEY file: $sk:", @m;
118 }
119  
120 {
121 5 my $v;
122 1 ▶ sub LOG {
Subroutine LOG: called CC=3 SCAR=15.4
Pod undocumented
123 1 ▶ $v ||= require File::SmartTail::Logger && File::SmartTail::Logger::LOG();
Condition33%2/6
$v ||= require File::SmartTail::Logger && File::SmartTail::Logger::LOG()
l!l&&r!l&&!r
010
require File::SmartTail::Logger && File::SmartTail::Logger::LOG()
!ll&&!rl&&r
001
MC/DC0%0/3
$v ||= require File::SmartTail::Logger && File::SmartTail::Logger::LOG()
$v require File::SmartTail::Logger File::SmartTail::Logger::LOG()
Truth table0%0/4
A ||= B && C
A= $vB= require File::SmartTail::LoggerC= File::SmartTail::Logger::LOG()
ABCresult
1XX1
0111
00X0
0100
124 }
125 }
126  
127 5 1;
« lib/File/SmartTail.pm ↑ Summary lib/File/SmartTail/ConfigData.pm »