blib/lib/File/Meta/Cache.pm

filter: Statementstmt 93.3% 113 / 121 Branchbran 57.1% 16 / 28 Conditioncond 57.6% 15 / 26 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 100% 22 / 22 Podpod 100% 11 / 11 totaltotal 80.4% 181 / 225 CC 26 SCAR 18.4
CC
26
Cov
82%
__ANON__
16.5
__ANON__
5.2
__ANON__
4.1
CRAP 6.3
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package File::Meta::Cache;
4  
5 1 our $VERSION="v0.4.2";
6  
7 # Default Opening Mode
8 #
9  
10 1 ▶use Fcntl qw(O_RDONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path::Redirect qw<follow_redirect>;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Use these keys instead
14 1 ▶use constant::more qw<KEY=0 PATH FD FH STAT VALID USER>;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 class File::Meta::Cache;
19 1 ▶use feature qw<say state>;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #use uSAC::Log; # Logger
22 #use Log::OK; # Logger enabler
23  
24  
25  
26 1 my ($_open, $_close, $_dup2);
27  
28 ##############################
29 # if(eval "require IO::FD"){ #
30 # $_open=\&IO::FD::open; #
31 # $_close=\&IO::FD::close; #
32 # $_dup2=\&IO::FD::dup2; #
33 # } #
34 # else { #
35 # require POSIX; #
36 # $_open=\&POSIX::open; #
37 # $_close=\&POSIX::close; #
38 # $_dup2=\&POSIX::dup2; #
39 # } #
40 ##############################
41 1 require POSIX;
42 1 $_open=\&POSIX::open;
43 1 $_close=\&POSIX::close;
44 1 $_dup2=\&POSIX::dup2;
45  
46  
47  
48  
49  
50  
51 1 field $_sweep_size;
52  
53 field $_no_fh :param =undef;
54 1 field $_enabled;
55 1 field $_sweeper;
56 1 field %_cache;
57 1 field $_opener;
58 1 field $_closer;
59 1 field $_updater;
60 1 field $_http_headers;
61  
62 BUILD{
63 $_sweep_size//=100;
64 $_enabled=1;
65 }
66  
67 1 ▶method sweeper {
Subroutine sweeper: called CC=2 SCAR=7.2
Pod documented
68 1 ▶ $_sweeper//= sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
69 1 my $cb=shift;
70 1 my $i=0;
71 1 my $entry;
72 1 my $closer=$self->closer;
73 1 for(keys %_cache){
74 1 $entry=$_cache{$_};
75  
76 # If the cached_ field reaches 1, this is the last code to use it. so close it
77 #
78 1 ▶ if($entry->[VALID]==1){
Branch: 1 branch
BranchTrueFalseTotal
if ($$entry[5] == 1) 1 0 1
79 1 $closer->($entry);
80 1 ▶ $cb and $cb->($entry);
Branch: 1 branch
BranchTrueFalseTotal
$cb and &$cb($entry) 0 1 1
81 }
82 1 ▶ last if ++$i >= $_sweep_size;
Branch: 1 branch
BranchTrueFalseTotal
if ++$i >= $_sweep_size 0 1 1
83 }
84 }
85 1 ▶}
Condition33%1/3
$_sweeper //= sub { my $cb = shift(); my $i = 0; my $entry; my $closer = $self->closer; foreach $_ (keys %_cache) { $entry = $_cache{$_}; if ($$entry[5] == 1) { &$closer($entry); $cb and &$cb($entry); } ; last if ++$i >= $_sweep_size; } ; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$_sweeper //= sub { my $cb = shift(); my $i = 0; my $entry; my $closer = $self->closer; foreach $_ (keys %_cache) { $entry = $_cache{$_}; if ($$entry[5] == 1) { &$closer($entry); $cb and &$cb($entry); } ; last if ++$i >= $_sweep_size; } ; }
$_sweeper sub { my $cb = shift(); my $i = 0; my $entry; my $closer = $self->closer; foreach $_ (keys %_cache) { $entry = $_cache{$_}; if ($$entry[5] == 1) { &$closer($entry); $cb and &$cb($entry); } ; last if ++$i >= $_sweep_size; } ; }
Truth table33%1/3
A //= B
A= $_sweeperB= sub { my $cb = shift(); my $i = 0; my $entry; my $closer = $self->closer; foreach $_ (keys %_cache) { $entry = $_cache{$_}; if ($$entry[5] == 1) { &$closer($entry); $cb and &$cb($entry); } ; last if ++$i >= $_sweep_size; } ; }
ABresult
1X1
011
000
86  
87 # returns a sub to execute. Object::Pad method lookup is slow. so bypass it
88 # when we don't need it
89 #
90 7 ▶method opener{
Subroutine opener: called CC=2 SCAR=7.3
Pod documented
91 $_opener//=
92 7 ▶ sub {
Subroutine __ANON__: called CC=13 SCAR=28.0
93 7 my ( $KEYpath, $mode, $force, $enable_redirect)=@_;
94 7 my $in_fd;
95  
96 # Entry is identified by the path, however, the actual data can come from another file
97 #
98  
99 7 my $existing_entry=$_cache{$KEYpath};
100 7 ▶ $mode//=O_RDONLY;
Condition100%2/2
$mode //= 0
l!l
52
MC/DC100%1/1
$mode //= 0
$mode
Truth table100%2/2
A
A= $mode
Aresult
11
00
101 7 ▶ if(!$existing_entry or $force){
Branch: 1 branch
BranchTrueFalseTotal
if (not $existing_entry or $force) { } 6 1 7
Condition100%3/3
not $existing_entry or $force
l!l&&r!l&&!r
421
MC/DC100%2/2
not $existing_entry or $force
not $existing_entry $force
Truth table100%3/3
not(A) or B
A= not $existing_entryB= $force
ABresult
1X1
011
000
102 #Log::OK::TRACE and log_trace __PACKAGE__.": Searching for: $KEYpath";
103  
104 6 my @entry;
105 my $path;
106 6 ▶ if($enable_redirect){
Branch: 1 branch
BranchTrueFalseTotal
if ($enable_redirect) { } 0 6 6
107 # Attempt to redirect file if appropriate
108 0 local $@;
109 0 eval {$path=follow_redirect $KEYpath};
110  
111 0 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
112 0 $entry[PATH]=$path;
113  
114 }
115 else {
116 6 $path=$KEYpath;
117  
118 }
119  
120 6 $entry[PATH]=$path;
121 6 my @stat=stat $path; #$KEYpath;
122
123 # If the stat fail or is not a file return undef.
124 # If this is a reopen(force), the force close the file to invalidate the cache
125 #
126 6 ▶ unless(@stat and -f _){
Branch: 1 branch
BranchTrueFalseTotal
unless (@stat and -f _) 0 6 6
Condition33%1/3
@stat and -f _
!ll&&!rl&&r
006
MC/DC0%0/2
@stat and -f _
@stat -f _
Truth table33%1/3
A and B
A= @statB= -f _
ABresult
0X0
100
111
127 0 ▶ $_closer->($existing_entry, 1) if $existing_entry;
Branch: 1 branch
BranchTrueFalseTotal
if $existing_entry 0 0 0
128 0 return undef;
129 };
130  
131 6 $in_fd=$_open->($path, $mode);
132 6 $in_fd+=0; # Convert to number? '0 but true'
133
134  
135  
136 6 ▶ if(defined $in_fd){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_fd) { } 6 0 6
137
138 6 ▶ if($existing_entry){
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_entry) { } 2 4 6
139 # Duplicate and Close unused fd
140 #POSIX::dup2 $in_fd, $existing_entry->[FD];
141 2 $_dup2->($in_fd, $existing_entry->[FD]);
142 #POSIX::close $in_fd;
143 2 $_close->($in_fd);
144  
145 # Copy stat into existing array
146 2 $existing_entry->[STAT]->@*=@stat;
147 }
148 else {
149 # Only create a file handle if its enabled
150 4 ▶ open($entry[FH], "+<&=$in_fd") unless($_no_fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $_no_fh 4 0 4
151  
152 4 $entry[STAT]=\@stat;
153 4 $entry[KEY]=$KEYpath;
154 4 $entry[FD]=$in_fd;
155 4 $entry[VALID]=1;#$count;
156  
157 4 $existing_entry =\@entry;
158 4 ▶ if($_enabled){
Branch: 1 branch
BranchTrueFalseTotal
if ($_enabled) 2 2 4
159 2 $_cache{$KEYpath}=$existing_entry;
160 2 $existing_entry->[VALID]++;
161 }
162 }
163 }
164 else {
165 #Log::OK::ERROR and log_error __PACKAGE__." Error opening file $KEYpath: $!";
166 }
167 }
168 else {
169 # Increment the counter of existing
170 #
171 1 $existing_entry->[VALID]++;
172 }
173  
174 7 $existing_entry;
175 }
176 7 ▶}
Condition66%2/3
$_opener //= sub { my($KEYpath, $mode, $force, $enable_redirect) = @_; my $in_fd; my $existing_entry = $_cache{$KEYpath}; $mode //= 0; if (not $existing_entry or $force) { my(@entry, $path); if ($enable_redirect) { local $@; eval { do { $path = follow_redirect($KEYpath) } }; return undef if $@; $entry[1] = $path; } else { $path = $KEYpath; } ; $entry[1] = $path; my(@stat) = stat $path; unless (@stat and -f _) { &$_closer($existing_entry, 1) if $existing_entry; return undef; } ; $in_fd = &$_open($path, $mode); $in_fd += 0; if (defined $in_fd) { if ($existing_entry) { &$_dup2($in_fd, $existing_entry->[2]); &$_close($in_fd); @{$$existing_entry[4]} = @stat; } else { CORE::open $entry[3], "+<&=$in_fd" unless $_no_fh; $entry[4] = \@stat; $entry[0] = $KEYpath; $entry[2] = $in_fd; $entry[5] = 1; $existing_entry = \@entry; if ($_enabled) { $_cache{$KEYpath} = $existing_entry; ++$existing_entry->[5]; } ; } ; } else { () } ; } else { ++$$existing_entry[5]; } ; $existing_entry; }
l!l&&r!l&&!r
610
MC/DC0%0/2
$_opener //= sub { my($KEYpath, $mode, $force, $enable_redirect) = @_; my $in_fd; my $existing_entry = $_cache{$KEYpath}; $mode //= 0; if (not $existing_entry or $force) { my(@entry, $path); if ($enable_redirect) { local $@; eval { do { $path = follow_redirect($KEYpath) } }; return undef if $@; $entry[1] = $path; } else { $path = $KEYpath; } ; $entry[1] = $path; my(@stat) = stat $path; unless (@stat and -f _) { &$_closer($existing_entry, 1) if $existing_entry; return undef; } ; $in_fd = &$_open($path, $mode); $in_fd += 0; if (defined $in_fd) { if ($existing_entry) { &$_dup2($in_fd, $existing_entry->[2]); &$_close($in_fd); @{$$existing_entry[4]} = @stat; } else { CORE::open $entry[3], "+<&=$in_fd" unless $_no_fh; $entry[4] = \@stat; $entry[0] = $KEYpath; $entry[2] = $in_fd; $entry[5] = 1; $existing_entry = \@entry; if ($_enabled) { $_cache{$KEYpath} = $existing_entry; ++$existing_entry->[5]; } ; } ; } else { () } ; } else { ++$$existing_entry[5]; } ; $existing_entry; }
$_opener sub { my($KEYpath, $mode, $force, $enable_redirect) = @_; my $in_fd; my $existing_entry = $_cache{$KEYpath}; $mode //= 0; if (not $existing_entry or $force) { my(@entry, $path); if ($enable_redirect) { local $@; eval { do { $path = follow_redirect($KEYpath) } }; return undef if $@; $entry[1] = $path; } else { $path = $KEYpath; } ; $entry[1] = $path; my(@stat) = stat $path; unless (@stat and -f _) { &$_closer($existing_entry, 1) if $existing_entry; return undef; } ; $in_fd = &$_open($path, $mode); $in_fd += 0; if (defined $in_fd) { if ($existing_entry) { &$_dup2($in_fd, $existing_entry->[2]); &$_close($in_fd); @{$$existing_entry[4]} = @stat; } else { CORE::open $entry[3], "+<&=$in_fd" unless $_no_fh; $entry[4] = \@stat; $entry[0] = $KEYpath; $entry[2] = $in_fd; $entry[5] = 1; $existing_entry = \@entry; if ($_enabled) { $_cache{$KEYpath} = $existing_entry; ++$existing_entry->[5]; } ; } ; } else { () } ; } else { ++$$existing_entry[5]; } ; $existing_entry; }
Truth table66%2/3
A //= B
A= $_openerB= sub { my($KEYpath, $mode, $force, $enable_redirect) = @_; my $in_fd; my $existing_entry = $_cache{$KEYpath}; $mode //= 0; if (not $existing_entry or $force) { my(@entry, $path); if ($enable_redirect) { local $@; eval { do { $path = follow_redirect($KEYpath) } }; return undef if $@; $entry[1] = $path; } else { $path = $KEYpath; } ; $entry[1] = $path; my(@stat) = stat $path; unless (@stat and -f _) { &$_closer($existing_entry, 1) if $existing_entry; return undef; } ; $in_fd = &$_open($path, $mode); $in_fd += 0; if (defined $in_fd) { if ($existing_entry) { &$_dup2($in_fd, $existing_entry->[2]); &$_close($in_fd); @{$$existing_entry[4]} = @stat; } else { CORE::open $entry[3], "+<&=$in_fd" unless $_no_fh; $entry[4] = \@stat; $entry[0] = $KEYpath; $entry[2] = $in_fd; $entry[5] = 1; $existing_entry = \@entry; if ($_enabled) { $_cache{$KEYpath} = $existing_entry; ++$existing_entry->[5]; } ; } ; } else { () } ; } else { ++$$existing_entry[5]; } ; $existing_entry; }
ABresult
1X1
011
000
177  
178  
179 # Mark the cache as disabled. Dumps all values and closes
180 # all fds
181 #
182 1 ▶method disable{
Subroutine disable: called CC=2 SCAR=6.9
Pod documented
183 1 $_enabled=undef;
184 1 for(values %_cache){
185 #POSIX::close($_cache{$_}[0]);
186 1 $_close->($_->[FD]);
187 }
188 1 %_cache=();
189 1 $self;
190 1 }
191  
192 # Generates a sub to close a cached fd
193 # removes meta data from the cache also
194 #
195 3 ▶method closer {
Subroutine closer: called CC=2 SCAR=7.0
Pod documented
196 3 ▶ $_closer//=sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
197 #Log::OK::TRACE and log_trace ("FMC closer called");
198 3 my $entry=$_[0];
199 3 ▶ if(--$entry->[VALID] <=0 or $_[1]){
Branch: 1 branch
BranchTrueFalseTotal
if (--$entry->[5] <= 0 or $_[1]) 1 2 3
Condition66%2/3
--$entry->[5] <= 0 or $_[1]
l!l&&r!l&&!r
102
MC/DC50%1/2
--$entry->[5] <= 0 or $_[1]
--$entry->[5] <= 0 $_[1]
Truth table66%2/3
A or B
A= --$entry->[5] <= 0B= $_[1]
ABresult
1X1
011
000
200 #Log::OK::TRACE and log_trace ("FMC closer valid 0 or lesss, or maybe force flag");
201 # Delete from cache
202 1 delete $_cache{$entry->[KEY]};
203 # Attempt to close only if the entry exists
204 1 $entry->[VALID]=0; #Mark as invalid
205 1 $entry->[FH]=undef;
206 1 $_close->($entry->[FD]);
207 }
208 }
209 3 ▶}
Condition66%2/3
$_closer //= sub { my $entry = $_[0]; if (--$entry->[5] <= 0 or $_[1]) { delete $_cache{$entry->[0]}; $entry->[5] = 0; $entry->[3] = undef; &$_close($entry->[2]); } ; }
l!l&&r!l&&!r
210
MC/DC0%0/2
$_closer //= sub { my $entry = $_[0]; if (--$entry->[5] <= 0 or $_[1]) { delete $_cache{$entry->[0]}; $entry->[5] = 0; $entry->[3] = undef; &$_close($entry->[2]); } ; }
$_closer sub { my $entry = $_[0]; if (--$entry->[5] <= 0 or $_[1]) { delete $_cache{$entry->[0]}; $entry->[5] = 0; $entry->[3] = undef; &$_close($entry->[2]); } ; }
Truth table66%2/3
A //= B
A= $_closerB= sub { my $entry = $_[0]; if (--$entry->[5] <= 0 or $_[1]) { delete $_cache{$entry->[0]}; $entry->[5] = 0; $entry->[3] = undef; &$_close($entry->[2]); } ; }
ABresult
1X1
011
000
210  
211 1 ▶method updater{
Subroutine updater: called CC=2 SCAR=8.7
Pod documented
212 1 ▶ $_updater//=sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
213 # Do a stat on the entry
214 1 $_[0][STAT]->@*=stat $_[0][KEY];
215 1 ▶ unless($_[0][STAT]->@* and -f _){
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$_[0][4]} and -f _) 0 1 1
Condition33%1/3
@{$_[0][4]} and -f _
!ll&&!rl&&r
001
MC/DC0%0/2
@{$_[0][4]} and -f _
@{$_[0][4]} -f _
Truth table33%1/3
A and B
A= @{$_[0][4]}B= -f _
ABresult
0X0
100
111
216 # This is an error force close the file
217 0 $_closer->($_[0], 1 );
218 }
219 }
220 1 ▶}
Condition33%1/3
$_updater //= sub { @{$_[0][4]} = stat $_[0][0]; unless (@{$_[0][4]} and -f _) { &$_closer($_[0], 1); } ; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$_updater //= sub { @{$_[0][4]} = stat $_[0][0]; unless (@{$_[0][4]} and -f _) { &$_closer($_[0], 1); } ; }
$_updater sub { @{$_[0][4]} = stat $_[0][0]; unless (@{$_[0][4]} and -f _) { &$_closer($_[0], 1); } ; }
Truth table33%1/3
A //= B
A= $_updaterB= sub { @{$_[0][4]} = stat $_[0][0]; unless (@{$_[0][4]} and -f _) { &$_closer($_[0], 1); } ; }
ABresult
1X1
011
000
221  
222 # OO Interface
223 #
224  
225 7 ▶method open {
Subroutine open: called CC=1 SCAR=0.0
Pod documented
226 7 $self->opener->&*;
227 1 }
228  
229 # First argument is entry, second is force flag
230 2 ▶method close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
231 2 $self->closer->&*;
232 1 }
233  
234 # First argument is entry
235 1 ▶method update{
Subroutine update: called CC=1 SCAR=0.0
Pod documented
236 1 $self->updater->&*;
237 1 }
238  
239 # First argument is callback to call if close is called
240 1 ▶method sweep {
Subroutine sweep: called CC=1 SCAR=0.0
Pod documented
241 1 $self->sweeper->&*;
242 1 }
243  
244 1 ▶method enable{ $_enabled=1; $self }
Subroutine enable: called CC=1 SCAR=0.0
Pod documented
245  
246 2 ▶method info {
Subroutine info: called CC=1 SCAR=0.0
Pod documented
247 2 $_cache{$_[0]}
248 1 }
249  
250 1 1;
251  
↑ Summary