blib/lib/File/Meta/Cache.pm

filter: Statementstmt 93.3% 112 / 120 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.3% 180 / 224 CC 26 SCAR 18.5
CC
26
Cov
82%
__ANON__
16.7
__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.1";
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 {
68 $_sweeper//= sub {
69 1 ▶ my $cb=shift;
Subroutine __ANON__: called CC=5 SCAR=16.4
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 ▶}
Subroutine sweeper: called CC=2 SCAR=8.1
Pod documented
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 method opener{
91 $_opener//=
92 sub {
93 7 ▶ my ( $KEYpath, $mode, $force, $enable_redirect)=@_;
Subroutine __ANON__: called CC=13 SCAR=28.1
94 7 my $in_fd;
95  
96 # Entry is identified by the path, however, the actual data can come from another file
97 #
98 7 my $existing_entry=$_cache{$KEYpath};
99 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
100 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
101 #Log::OK::TRACE and log_trace __PACKAGE__.": Searching for: $KEYpath";
102  
103 6 my @entry;
104 my $path;
105 6 ▶ if($enable_redirect){
Branch: 1 branch
BranchTrueFalseTotal
if ($enable_redirect) { } 0 6 6
106 # Attempt to redirect file if appropriate
107 0 local $@;
108 0 eval {$path=follow_redirect $KEYpath};
109  
110 0 ▶ return undef if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
111 0 $entry[PATH]=$path;
112  
113 }
114 else {
115 6 $path=$KEYpath;
116  
117 }
118  
119 6 $entry[PATH]=$path;
120 6 my @stat=stat $path; #$KEYpath;
121
122 # If the stat fail or is not a file return undef.
123 # If this is a reopen(force), the force close the file to invalidate the cache
124 #
125 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
126 0 ▶ $_closer->($existing_entry, 1) if $existing_entry;
Branch: 1 branch
BranchTrueFalseTotal
if $existing_entry 0 0 0
127 0 return undef;
128 };
129  
130 6 $in_fd=$_open->($path, $mode);
131
132  
133  
134 6 ▶ if(defined $in_fd){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $in_fd) { } 6 0 6
135
136 6 ▶ if($existing_entry){
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_entry) { } 2 4 6
137 # Duplicate and Close unused fd
138 #POSIX::dup2 $in_fd, $existing_entry->[FD];
139 2 $_dup2->($in_fd, $existing_entry->[FD]);
140 #POSIX::close $in_fd;
141 2 $_close->($in_fd);
142  
143 # Copy stat into existing array
144 2 $existing_entry->[STAT]->@*=@stat;
145 }
146 else {
147 # Only create a file handle if its enabled
148 4 ▶ open($entry[FH], "+<&=$in_fd") unless($_no_fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $_no_fh 4 0 4
149  
150 4 $entry[STAT]=\@stat;
151 4 $entry[KEY]=$KEYpath;
152 4 $entry[FD]=$in_fd;
153 4 $entry[VALID]=1;#$count;
154  
155 4 $existing_entry =\@entry;
156 4 ▶ if($_enabled){
Branch: 1 branch
BranchTrueFalseTotal
if ($_enabled) 2 2 4
157 2 $_cache{$KEYpath}=$existing_entry;
158 2 $existing_entry->[VALID]++;
159 }
160 }
161 }
162 else {
163 #Log::OK::ERROR and log_error __PACKAGE__." Error opening file $KEYpath: $!";
164 }
165 }
166 else {
167 # Increment the counter of existing
168 #
169 1 $existing_entry->[VALID]++;
170 }
171  
172 7 $existing_entry;
173 }
174 7 ▶}
Subroutine opener: called CC=2 SCAR=7.1
Pod documented
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); 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); 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); 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); 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
175  
176  
177 # Mark the cache as disabled. Dumps all values and closes
178 # all fds
179 #
180 method disable{
181 1 ▶ $_enabled=undef;
Subroutine disable: called CC=2 SCAR=6.9
Pod documented
182 1 for(values %_cache){
183 #POSIX::close($_cache{$_}[0]);
184 1 $_close->($_->[FD]);
185 }
186 1 %_cache=();
187 1 $self;
188 1 }
189  
190 # Generates a sub to close a cached fd
191 # removes meta data from the cache also
192 #
193 method closer {
194 $_closer//=sub {
195 #Log::OK::TRACE and log_trace ("FMC closer called");
196 3 ▶ my $entry=$_[0];
Subroutine __ANON__: called CC=3 SCAR=11.0
197 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
198 #Log::OK::TRACE and log_trace ("FMC closer valid 0 or lesss, or maybe force flag");
199 # Delete from cache
200 1 delete $_cache{$entry->[KEY]};
201 # Attempt to close only if the entry exists
202 1 $entry->[VALID]=0; #Mark as invalid
203 1 $entry->[FH]=undef;
204 1 $_close->($entry->[FD]);
205 }
206 }
207 3 ▶}
Subroutine closer: called CC=2 SCAR=7.1
Pod documented
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
208  
209 method updater{
210 $_updater//=sub {
211 # Do a stat on the entry
212 1 ▶ $_[0][STAT]->@*=stat $_[0][KEY];
Subroutine __ANON__: called CC=3 SCAR=14.2
213 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
214 # This is an error force close the file
215 0 $_closer->($_[0], 1 );
216 }
217 }
218 1 ▶}
Subroutine updater: called CC=2 SCAR=8.1
Pod documented
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
219  
220 # OO Interface
221 #
222  
223 method open {
224 7 ▶ $self->opener->&*;
Subroutine open: called CC=1 SCAR=0.0
Pod documented
225 1 }
226  
227 # First argument is entry, second is force flag
228 method close {
229 2 ▶ $self->closer->&*;
Subroutine close: called CC=1 SCAR=0.0
Pod documented
230 1 }
231  
232 # First argument is entry
233 method update{
234 1 ▶ $self->updater->&*;
Subroutine update: called CC=1 SCAR=0.0
Pod documented
235 1 }
236  
237 # First argument is callback to call if close is called
238 method sweep {
239 1 ▶ $self->sweeper->&*;
Subroutine sweep: called CC=1 SCAR=0.0
Pod documented
240 1 }
241  
242 1 ▶method enable{ $_enabled=1; $self }
Subroutine enable: called CC=1 SCAR=0.0
Pod documented
243  
244 method info {
245 2 ▶ $_cache{$_[0]}
Subroutine info: called CC=1 SCAR=0.0
Pod documented
246 1 }
247  
248 1 1;
249  
↑ Summary