TagFile.pm

filter: Statementstmt 73.7% 87 / 118 Branchbran 55.5% 20 / 36 Conditioncond 15.7% 3 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 72.7% 16 / 22 Podpod 0.0% 0 / 7 totaltotal 59.1% 126 / 213 CC 29 SCAR 21.3
CC
29
Cov
64%
STORE
30.0
sync
20.0
EXISTS
12.0
CRAP 8.4
↑ Summary TagRecord.pm »
1 package AsciiDB::TagFile;
2  
3 # Copyright (c) 1997-2001 Jose A. Rodriguez. All rights reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6  
7 5 require Tie::Hash;
8 5 @ISA = (Tie::Hash);
9  
10 5 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use vars qw($VERSION $catPathFileName);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
15 5 ▶ eval "use File::Spec";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 $catPathFileName = ($@) ?
17 0 ▶ sub { join('/', @_) } : # Unix way
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 5 ▶ sub { File::Spec->catfile(@_) }; # Portable way
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 5 5
19 }
20  
21 5 $VERSION = '1.06';
22  
23 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use AsciiDB::TagRecord;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 5 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=8 SCAR=23.0
27 5 my $class = shift;
28 5 my %params = @_;
29  
30 5 my $self = {};
31 5 ▶ $self->{_DIRECTORY} = $params{DIRECTORY} || cwd;
Condition33%1/3
$params{'DIRECTORY'} || cwd()
l!l&&r!l&&!r
500
MC/DC0%0/2
$params{'DIRECTORY'} || cwd()
$params{'DIRECTORY'} cwd()
Truth table33%1/3
A || B
A= $params{'DIRECTORY'}B= cwd()
ABresult
1X1
011
000
32 5 ▶ $self->{_SUFIX} = $params{SUFIX} || '';
Condition50%1/2
$params{'SUFIX'} || ''
l!l
50
MC/DC0%0/1
$params{'SUFIX'} || ''
$params{'SUFIX'}
Truth table50%1/2
A
A= $params{'SUFIX'}
Aresult
11
00
33 5 $self->{_SCHEMA} = $params{SCHEMA};
34 5 $self->{_READONLY} = $params{READONLY};
35 5 $self->{_FILEMODE} = $params{FILEMODE};
36 5 ▶ $self->{_LOCK} = $params{LOCK} || 0;
Condition50%1/2
$params{'LOCK'} || 0
l!l
05
MC/DC0%0/1
$params{'LOCK'} || 0
$params{'LOCK'}
Truth table50%1/2
A
A= $params{'LOCK'}
Aresult
11
00
37 5 $self->{_CACHESIZE} = $params{CACHESIZE};
38  
39 5 ▶ unless (-d $self->{_DIRECTORY}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"_DIRECTORY"}) 0 5 5
40 0 croak "Directory '$params{DIRECTORY}' does not exist";
41 }
42  
43 5 ▶ if (defined $self->{_CACHESIZE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_CACHESIZE"}) 1 4 5
44 1 $self->{_CACHESIZE} = int($self->{_CACHESIZE});
45  
46 1 ▶ if ($self->{_CACHESIZE} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_CACHESIZE'} == 0) { } 0 1 1
elsif ($self->{'_CACHESIZE'} < 1) { } 0 1 1
47 0 undef $self->{_CACHESIZE};
48 } elsif ($self->{_CACHESIZE} < 1) {
49 0 croak "Cache size should be >= 0 (0 means no cache)";
50 0 $self->{_CACHESIZE} = 1;
51 }
52 }
53  
54 # Number of internal keys (ie. '_key')
55 5 $self->{_INTKEYCOUNT} = 1 + keys %$self;
56  
57 5 bless $self, $class;
58 }
59  
60 154 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=6.9
61 154 my ($self, $key) = @_;
62  
63 154 ▶ return $self->{$key} if exists ($self->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 51 103 154
64  
65 103 return $self->newRecord($key);
66 }
67  
68 0 ▶sub STORE {
Subroutine STORE: not called CC=5 SCAR=34.0
69 0 my ($self, $key, $value) = @_;
70  
71 # Return if the user is assigning an object to itself ($a{A} = $a{A})
72 0 ▶ return if exists $self->{$key} && $self->{$key} == $value;
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} and $self->{$key} == $value 0 0 0
Condition0%0/3
exists $self->{$key} and $self->{$key} == $value
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{$key} and $self->{$key} == $value
exists $self->{$key} $self->{$key} == $value
Truth table0%0/3
A and B
A= exists $self->{$key}B= $self->{$key} == $value
ABresult
0X0
100
111
73  
74 0 ▶ $self->newRecord($key) unless (exists $self->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 0 0 0
75  
76 0 my $field;
77 0 foreach $field (keys %{$self->{$key}}) {
78 0 $self->{$key}{$field} = $value->{$field};
79 }
80 }
81  
82 1 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.1
83 1 my $self = shift;
84  
85 # Current keys are the union of saved keys and new created but
86 # not saved keys
87 1 my %currentKeys;
88  
89 1 my $sufix = $self->{_SUFIX};
90  
91 0 map { $currentKeys{$_} = 1 }
92 0 map { $self->decodeKey($_) }
93 1 grep { $_ =~ /(.+)\Q$sufix\E$/; $_ = $1 }
94 $self->getDirFiles();
95 0 map { $currentKeys{$_} = 1 } grep(!/^_/, keys %$self);
96  
97 1 my @currentKeys = keys %currentKeys;
98 1 $self->{_ITERATOR} = \@currentKeys;
99  
100 1 shift @{$self->{_ITERATOR}};
101 }
102  
103 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
104 0 my $self = shift;
105
106 0 shift @{$self->{_ITERATOR}};
107 }
108  
109 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=3 SCAR=24.8
110 0 my ($self, $key) = @_;
111  
112 0 ▶ $self->{$key} || -f $self->fileName($key) || 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{$key} or -f $self->fileName($key) or 0 0 0 0
Condition0%0/3
$self->{$key} or -f $self->fileName($key)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$key} or -f $self->fileName($key)
$self->{$key} -f $self->fileName($key)
Truth table0%0/3
A or B
A= $self->{$key}B= -f $self->fileName($key)
ABresult
1X1
011
000
113 }
114  
115 54 ▶sub DELETE {
Subroutine DELETE: called CC=4 SCAR=14.0
116 54 my ($self, $key) = @_;
117  
118 54 ▶ return if $self->{_READONLY};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_READONLY"} 0 54 54
119  
120 54 unlink $self->fileName($key);
121  
122 11 tied(%{$self->{$key}})->deleteRecord()
123 54 ▶ if tied(%{$self->{$key}});
Branch: 1 branch
BranchTrueFalseTotal
if tied %{$$self{$key};} 11 43 54
124  
125 54 ▶ delete $self->{$key} if exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$key} 54 0 54
126 }
127  
128 0 ▶sub sync {
Subroutine sync: not called CC=4 SCAR=30.0
Pod undocumented
129 0 my $self = shift;
130  
131 my @recordsToSync = grep {
132 0 ▶ ref $_ && ref($_) eq 'HASH' && tied(%$_)
Condition0%0/6
ref $_ && ref $_ eq 'HASH' && tied %$_
!ll&&!rl&&r
000
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $_ && ref $_ eq 'HASH' && tied %$_
ref $_ ref $_ eq "HASH" tied %$_
Truth table0%0/4
A and B && C
A= ref $_B= ref $_ eq "HASH"C= tied %$_
ABCresult
0XX0
10X0
1100
1111
133 0 } values %{$self};
134  
135 0 my $record;
136 0 foreach $record (@recordsToSync) {
137 0 tied(%$record)->sync();
138 }
139 }
140  
141 101 ▶sub purge {
Subroutine purge: called CC=4 SCAR=13.9
Pod undocumented
142 101 my $self = shift;
143 101 my ($cacheSize) = @_;
144  
145 101 ▶ if (defined($cacheSize)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cacheSize) 100 1 101
146 100 my $dataRecords = scalar(keys %$self) - $self->{_INTKEYCOUNT};
147 100 ▶ return if $dataRecords < $cacheSize;
Branch: 1 branch
BranchTrueFalseTotal
if $dataRecords < $cacheSize 92 8 100
148 }
149  
150 # This works in 5.004 no 5.003
151 #delete @$self{grep !/^_/, keys %{$self}};
152 # instead we use this...
153 9 foreach (grep !/^_/, keys %{$self}) {
154 90 delete $self->{$_};
155 }
156 }
157  
158 103 ▶sub newRecord {
Subroutine newRecord: called CC=2 SCAR=6.9
Pod undocumented
159 103 my $self = shift;
160 103 my ($key) = @_;
161  
162 103 ▶ $self->purge($self->{_CACHESIZE}) if defined($self->{_CACHESIZE});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_CACHESIZE"} 100 3 103
163  
164 103 my %record;
165 tie %record, 'AsciiDB::TagRecord',
166 FILENAME => $self->fileName($key),
167 SCHEMA => $self->{_SCHEMA},
168 READONLY => $self->{_READONLY},
169 103 FILEMODE => $self->{_FILEMODE};
170
171 103 $self->{$key} = \%record;
172 }
173  
174 161 ▶sub encodeKey {
Subroutine encodeKey: called CC=2 SCAR=7.0
Pod undocumented
175 161 my $self = shift;
176 161 my ($key) = @_;
177  
178 161 my $encodeSub = $self->{_SCHEMA}{KEY}{ENCODE};
179 161 ▶ ($encodeSub) ? &$encodeSub($key) : $key;
Branch: 1 branch
BranchTrueFalseTotal
$encodeSub ? : 161 0 161
180 }
181  
182 0 ▶sub decodeKey {
Subroutine decodeKey: not called CC=2 SCAR=17.9
Pod undocumented
183 0 my $self = shift;
184 0 my ($key) = @_;
185  
186 0 my $decodeSub = $self->{_SCHEMA}{KEY}{DECODE};
187 0 ▶ ($decodeSub) ? &$decodeSub($key) : $key;
Branch: 1 branch
BranchTrueFalseTotal
$decodeSub ? : 0 0 0
188 }
189  
190 161 ▶sub fileName {
Subroutine fileName: called CC=1 SCAR=0.0
Pod undocumented
191 161 my $self = shift;
192 161 my ($key) = $self->encodeKey(@_);
193  
194 161 &$catPathFileName($$self{_DIRECTORY}, "$key$$self{_SUFIX}")
195 }
196  
197 1 ▶sub getDirFiles {
Subroutine getDirFiles: called CC=2 SCAR=7.0
Pod undocumented
198 1 my $self = shift;
199  
200 1 local *DIR;
201 opendir(DIR, $$self{_DIRECTORY})
202 1 ▶ || die "Can't opendir $$self{_DIRECTORY}: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $self->{"_DIRECTORY"} or die qq[Can't opendir $self->{"_DIRECTORY"}: $!] 1 0 1
203 1 my @files = grep { -f &$catPathFileName($$self{_DIRECTORY}, $_) }
204 readdir(DIR);
205 1 closedir DIR;
206  
207 1 @files;
208 }
209  
210 5 1;
211 __END__
↑ Summary TagRecord.pm »