line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
1
|
|
|
1
|
|
124374
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
2
|
1
|
|
|
1
|
|
19
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
3
|
|
|
|
|
|
|
package File::Meta::Cache; |
4
|
|
|
|
|
|
|
our $VERSION="v0.1.1"; |
5
|
|
|
|
|
|
|
# Default Opening Mode |
6
|
1
|
|
|
1
|
|
6
|
use Fcntl qw(O_RDONLY); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
71
|
|
7
|
1
|
|
|
1
|
|
558
|
use enum qw; |
|
1
|
|
|
|
|
1170
|
|
|
1
|
|
|
|
|
6
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
1135
|
use Object::Pad; |
|
1
|
|
|
|
|
11279
|
|
|
1
|
|
|
|
|
7
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
class File::Meta::Cache; |
12
|
1
|
|
|
1
|
|
368
|
use feature qw; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
1807
|
use Log::ger; # Logger |
|
1
|
|
|
|
|
50
|
|
|
1
|
|
|
|
|
5
|
|
15
|
1
|
|
|
1
|
|
683
|
use Log::OK; # Logger enabler |
|
1
|
|
|
|
|
3782
|
|
|
1
|
|
|
|
|
5
|
|
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
1
|
|
326
|
use POSIX(); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2647
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
field $_sweep_size; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
field $_no_fh :param =undef; |
24
|
|
|
|
|
|
|
field $_enabled; |
25
|
|
|
|
|
|
|
field $_sweeper; |
26
|
|
|
|
|
|
|
field %_cache; |
27
|
|
|
|
|
|
|
field $_opener; |
28
|
|
|
|
|
|
|
field $_closer; |
29
|
|
|
|
|
|
|
field $_updater; |
30
|
|
|
|
|
|
|
field $_http_headers; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
BUILD{ |
33
|
|
|
|
|
|
|
$_sweep_size//=100; |
34
|
|
|
|
|
|
|
$_enabled=1; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
1
|
1
|
2
|
method sweeper { |
38
|
|
|
|
|
|
|
$_sweeper//= sub { |
39
|
1
|
|
|
1
|
|
15
|
my $i=0; |
40
|
1
|
|
|
|
|
2
|
my $entry; |
41
|
1
|
|
|
|
|
3
|
my $closer=$self->closer; |
42
|
1
|
|
|
|
|
12
|
for(keys %_cache){ |
43
|
1
|
|
|
|
|
3
|
$entry=$_cache{$_}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# If the cached_ field reaches 1, this is the last code to use it. so close it |
46
|
|
|
|
|
|
|
# |
47
|
1
|
50
|
|
|
|
6
|
$closer->($entry) if($entry->[valid_]==1); |
48
|
1
|
50
|
|
|
|
10
|
last if ++$i >= $_sweep_size; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
1
|
|
50
|
|
|
13
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# returns a sub to execute. Object::Pad method lookup is slow. so bypass it |
54
|
|
|
|
|
|
|
# when we don't need it |
55
|
|
|
|
|
|
|
# |
56
|
5
|
|
|
5
|
1
|
17
|
method opener{ |
57
|
|
|
|
|
|
|
$_opener//= |
58
|
|
|
|
|
|
|
sub { |
59
|
5
|
|
|
5
|
|
16
|
my ( $key_path, $mode, $force)=@_; |
60
|
5
|
|
|
|
|
7
|
my $in_fd; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# Entry is identified by the path, however, the actual data can come from another file |
63
|
|
|
|
|
|
|
# |
64
|
5
|
|
|
|
|
9
|
my $existing_entry=$_cache{$key_path}; |
65
|
5
|
|
100
|
|
|
16
|
$mode//=O_RDONLY; |
66
|
5
|
100
|
100
|
|
|
24
|
if(!$existing_entry or $force){ |
67
|
4
|
|
|
|
|
7
|
Log::OK::TRACE and log_trace __PACKAGE__.": Searching for: $key_path"; |
68
|
|
|
|
|
|
|
|
69
|
4
|
|
|
|
|
70
|
my @stat=stat $key_path; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# If the stat fail or is not a file return undef. |
72
|
|
|
|
|
|
|
# If this is a reopen(force), the force close the file to invalidate the cache |
73
|
|
|
|
|
|
|
# |
74
|
4
|
50
|
33
|
|
|
30
|
unless(@stat and -f _){ |
75
|
0
|
0
|
|
|
|
0
|
$_closer->($existing_entry, 1) if $existing_entry; |
76
|
0
|
|
|
|
|
0
|
return undef; |
77
|
|
|
|
|
|
|
}; |
78
|
|
|
|
|
|
|
|
79
|
4
|
|
|
|
|
12
|
my @entry; |
80
|
4
|
|
|
|
|
85
|
$in_fd=POSIX::open($key_path, $mode); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
4
|
50
|
|
|
|
16
|
if(defined $in_fd){ |
85
|
|
|
|
|
|
|
|
86
|
4
|
100
|
|
|
|
9
|
if($existing_entry){ |
87
|
|
|
|
|
|
|
# Duplicate and Close unused fd |
88
|
2
|
|
|
|
|
22
|
POSIX::dup2 $in_fd, $existing_entry->[fd_]; |
89
|
2
|
|
|
|
|
15
|
POSIX::close $in_fd; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Copy stat into existing array |
92
|
2
|
|
|
|
|
13
|
$existing_entry->[stat_]->@*=@stat; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
else { |
95
|
|
|
|
|
|
|
# Only create a file handle if its enabled |
96
|
2
|
50
|
|
|
|
71
|
open($entry[fh_], "+<&=$in_fd") unless($_no_fh); |
97
|
|
|
|
|
|
|
|
98
|
2
|
|
|
|
|
8
|
$entry[stat_]=\@stat; |
99
|
2
|
|
|
|
|
5
|
$entry[key_]=$key_path; |
100
|
2
|
|
|
|
|
4
|
$entry[fd_]=$in_fd; |
101
|
2
|
|
|
|
|
4
|
$entry[valid_]=1;#$count; |
102
|
|
|
|
|
|
|
|
103
|
2
|
|
|
|
|
3
|
$existing_entry =\@entry; |
104
|
2
|
50
|
|
|
|
11
|
$_cache{$key_path}=$existing_entry if($_enabled); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
else { |
108
|
0
|
|
|
|
|
0
|
Log::OK::ERROR and log_error __PACKAGE__." Error opening file $key_path: $!"; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# Increment the counter |
113
|
|
|
|
|
|
|
# |
114
|
5
|
50
|
|
|
|
12
|
$existing_entry->[valid_]++ if $existing_entry; |
115
|
5
|
|
|
|
|
21
|
$existing_entry; |
116
|
|
|
|
|
|
|
} |
117
|
5
|
|
100
|
|
|
50
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Mark the cache as disabled. Dumps all values and closes |
121
|
|
|
|
|
|
|
# all fds |
122
|
|
|
|
|
|
|
# |
123
|
0
|
|
|
0
|
1
|
0
|
method disable{ |
124
|
0
|
|
|
|
|
0
|
$_enabled=undef; |
125
|
0
|
|
|
|
|
0
|
for(values %_cache){ |
126
|
0
|
|
|
|
|
0
|
POSIX::close($_cache{$_}[0]); |
127
|
|
|
|
|
|
|
} |
128
|
0
|
|
|
|
|
0
|
%_cache=(); |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Generates a sub to close a cached fd |
132
|
|
|
|
|
|
|
# removes meta data from the cache also |
133
|
|
|
|
|
|
|
# |
134
|
3
|
|
|
3
|
1
|
11
|
method closer { |
135
|
|
|
|
|
|
|
$_closer//=sub { |
136
|
3
|
|
|
3
|
|
11
|
my $entry=$_[0]; |
137
|
3
|
100
|
66
|
|
|
20
|
if(--$entry->[valid_] <=0 or $_[1]){ |
138
|
1
|
|
|
|
|
4
|
my $actual=delete $_cache{$entry->[key_]}; |
139
|
1
|
50
|
|
|
|
4
|
if($actual){ |
140
|
|
|
|
|
|
|
# Attempt to close only if the entry exists |
141
|
1
|
|
|
|
|
2
|
$actual->[valid_]=0; #Mark as invalid |
142
|
1
|
|
|
|
|
16
|
POSIX::close($actual->[fd_]); |
143
|
1
|
|
|
|
|
33
|
$actual->[fh_]=undef; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
else { |
146
|
0
|
|
|
|
|
0
|
die "Entry does not exist"; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
3
|
|
100
|
|
|
29
|
} |
151
|
|
|
|
|
|
|
|
152
|
1
|
|
|
1
|
1
|
4
|
method updater{ |
153
|
|
|
|
|
|
|
$_updater//=sub { |
154
|
|
|
|
|
|
|
# To a stat on the entry |
155
|
1
|
|
|
1
|
|
18
|
$_[0][stat_]->@*=stat $_[0][key_]; |
156
|
1
|
50
|
33
|
|
|
9
|
unless($_[0][stat_]->@* and -f _){ |
157
|
|
|
|
|
|
|
# This is an error force close the file |
158
|
0
|
|
|
|
|
0
|
$_closer->($_[0], 1 ); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
} |
161
|
1
|
|
50
|
|
|
20
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
# OO Interface |
164
|
|
|
|
|
|
|
# |
165
|
|
|
|
|
|
|
|
166
|
5
|
|
|
5
|
1
|
2488
|
method open { |
167
|
5
|
|
|
|
|
22
|
$self->opener->&*; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
2
|
|
|
2
|
1
|
612
|
method close { |
171
|
2
|
|
|
|
|
7
|
$self->closer->&*; |
172
|
|
|
|
|
|
|
} |
173
|
1
|
|
|
1
|
1
|
702
|
method update{ |
174
|
1
|
|
|
|
|
10
|
$self->updater->&*; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
1
|
|
|
1
|
1
|
362
|
method sweep { |
178
|
1
|
|
|
|
|
8
|
$self->sweeper->&*; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
0
|
1
|
|
method enable{ $_enabled=1; } |
|
0
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
1; |
184
|
|
|
|
|
|
|
|