blib/lib/File/Meta/Cache.pm

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