FusqlFS/Cache/File.pm

filter: Statementstmt 97.7% 87 / 89 Branchbran 57.6% 15 / 26 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 25 / 25 Podpod 50.0% 1 / 2 totaltotal 87.1% 135 / 155 CC 17 SCAR 9.3
CC
17
Cov
87%
FETCH
5.6
CLEAR
4.2
STORE
4.1
CRAP 2.5
« FusqlFS/Backend/MySQL.pm ↑ Summary FusqlFS/Backend/SQLite/Table/Data.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package FusqlFS::Cache::File;
5 2 ▶use FusqlFS::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our $VERSION = $FusqlFS::Version::VERSION;
7 2 ▶use parent 'FusqlFS::Cache::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 FusqlFS::Cache::File - FusqlFS file backed cache strategy implementation
12  
13 =head1 SYNOPSIS
14  
15 use FusqlFS::Cache::File;
16  
17 our %cache;
18 tie %cache, 'FusqlFS::Cache::File', 2048;
19  
20 =head1 DESCRIPTION
21  
22 This is file backed cache strategy implementation. This class is not to be used
23 directly.
24  
25 This cache strategy accepts single `threshold' parameter which must be an
26 integer greater than zero and determines minimum file system entry size to be
27 stored in file.
28  
29 The idea behind this cache strategy is simple: it stores file records with size
30 greater than given threshold in real files on disk. This cache method processes
31 simple plain files and pseudopipes only, directories and symlinks are always
32 stored in memory, which is not that bad as they are usually quite small in
33 comparison to plain files and psuedopipes output cache.
34  
35 Disk cache is situated in F</tmp/fusqlfs-$PID.cache>, where C<$PID> is
36 substituted with current fusqlfs daemon's pid. This directory should be removed
37 after fusqlfs is unmounted, if it doesn't, you can remove it by yourself and
38 report me a bug (unless daemon was finished in some unusual way like power
39 failure, system crash or C<kill -9>, as it had not any way to cleanup its
40 garbage in such a case).
41  
42 This cache strategy is good if you are going to mount database with a lot
43 number of records with large blob or text fields in it, as it focuses on per
44 records cache optimization. If you need to keep memory usage low and have a
45 database with a really lot number of quite small records you can consider using
46 L<FusqlFS::Cache::Limited> strategy instead.
47  
48 =head1 SEE ALSO
49  
50 =over
51  
52 =item *
53  
54 L<FusqlFS::Cache> and L<FusqlFS::Cache::Base> about FusqlFS cache strategies.
55  
56 =item *
57  
58 L<FusqlFS::Entry> for file system entry structure description.
59  
60 =item *
61  
62 L<perltie> about object tie()ing in perl.
63  
64 =back
65  
66 =cut
67  
68 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 =begin testing
71  
72 #!noinst
73  
74 ok FusqlFS::Cache::File->is_needed(10), 'File cache is needed';
75 ok !FusqlFS::Cache::File->is_needed(0), 'File cache isn\'t needed';
76  
77 =end testing
78 =cut
79  
80 sub is_needed
81 2 ▶{
Subroutine is_needed: called CC=1 SCAR=0.0
Pod documented
82 2 return $_[1] > 0;
83 }
84  
85 =begin testing
86  
87 # Tie tests
88 my %cache;
89 isa_ok tie(%cache, 'FusqlFS::Cache::File', 10), 'FusqlFS::Cache::File', 'File cache tied';
90  
91 ok !scalar(%cache), 'Cache is empty';
92  
93 # Store & fetch tests
94 $cache{'shorttest'} = [ 'pkg', 'names', 'entry' ];
95 $cache{'longtest'} = [ 'pkg', 'names', 'long entry' ];
96  
97 # Exists tests
98 is_deeply $cache{'shorttest'}, [ 'pkg', 'names', 'entry' ], 'Fetch short entry';
99 is_deeply $cache{'longtest'} , [ 'pkg', 'names', 'long entry' ], 'Fetch long entry';
100 is $cache{'unknown'}, undef, 'Unknown entry is undef';
101  
102 ok scalar(%cache), 'Cache is not empty';
103  
104 # Rewrite store tests
105 $cache{'shorttest'} = [ 'pkg', 'names', 'entri' ];
106 $cache{'longtest'} = [ 'pkg', 'names', 'very long entry' ];
107 is_deeply $cache{'shorttest'}, [ 'pkg', 'names', 'entri' ], 'Fetch short entry after rewrite';
108 is_deeply $cache{'longtest'} , [ 'pkg', 'names', 'very long entry' ], 'Fetch long entry after rewrite';
109  
110 # Iterate tests
111 while (my ($key, $val) = each %cache)
112 {
113 if ($key eq 'shorttest')
114 {
115 is_deeply $val, [ 'pkg', 'names', 'entri' ], 'Fetch short entry (iterating)';
116 }
117 elsif ($key eq 'longtest')
118 {
119 is_deeply $val, [ 'pkg', 'names', 'very long entry' ], 'Fetch long entry (iterating)';
120 }
121 else
122 {
123 fail "Key-value pair not stored before: $key => $val";
124 }
125 }
126  
127 # Delete & clear tests
128 delete $cache{'shorttest'};
129 ok !exists($cache{'shorttest'}), 'Short entry deleted';
130 is $cache{'shorttest'}, undef, 'Short entry undefined';
131  
132 delete $cache{'longtest'};
133 ok !exists($cache{'longtest'}), 'Long entry deleted';
134 is $cache{'longtest'}, undef, 'Long entry undefined';
135  
136 ok !scalar(%cache), 'Cache is empty after delete';
137  
138 $cache{'othertest'} = [ 'pkg', 'names', '' ];
139 %cache = ();
140 ok !scalar(%cache), 'Cache is empty after cleanup';
141  
142 =end testing
143 =cut
144  
145 sub TIEHASH
146 1 ▶{
Subroutine TIEHASH: called CC=2 SCAR=7.0
147 1 my $class = shift;
148 1 my $threshold = shift;
149 # real storage, cache dir, size threshold
150  
151 1 my $cachedir = "/tmp/fusqlfs-$$.cache";
152 1 ▶ mkdir $cachedir or croak "Unable to create cache dir $cachedir: $@";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $cachedir or croak("Unable to create cache dir ${cachedir}: $@") 1 0 1
153  
154 1 my $self = [ {}, $cachedir, 0+$threshold ];
155 1 bless $self, $class;
156 }
157  
158 sub FETCH
159 9 ▶{
Subroutine FETCH: called CC=2 SCAR=6.9
160 9 my ($self, $key) = @_;
161 9 ▶ return $self->[0]->{$key}||undef;
Condition100%2/2
$self->[0]{$key} || undef
l!l
63
MC/DC100%1/1
$self->[0]{$key} || undef
$self->[0]{$key}
Truth table100%2/2
A
A= $self->[0]{$key}
Aresult
11
00
162 }
163  
164 sub STORE
165 5 ▶{
Subroutine STORE: called CC=4 SCAR=14.0
166 5 my ($self, $key, $value) = @_;
167 5 $self->[0]->{$key} = $value;
168 5 ▶ return if tied $value->[2];
Branch: 1 branch
BranchTrueFalseTotal
if tied $value->[2] 0 5 5
169  
170 5 ▶ if (!ref($value->[2]) && length($value->[2]) > $self->[2])
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value->[2] and length $value->[2] > $self->[2]) 1 4 5
Condition66%2/3
not ref $value->[2] and length $value->[2] > $self->[2]
!ll&&!rl&&r
041
MC/DC50%1/2
not ref $value->[2] and length $value->[2] > $self->[2]
not ref $value->[2] length $value->[2] > $self->[2]
Truth table66%2/3
not(A) and B
A= not ref $value->[2]B= length $value->[2] > $self->[2]
ABresult
0X0
100
111
171 {
172 1 tie $value->[2], 'FusqlFS::Cache::File::Record', $self->cachefile($key), $value->[2];
173 }
174 }
175  
176 sub CLEAR
177 2 ▶{
Subroutine CLEAR: called CC=4 SCAR=14.2
178 2 my ($self) = @_;
179 2 $self->[0] = {};
180 2 ▶ opendir my $dh, $self->[1] or croak "Unable to open cache dir $self->[1]: $@";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->[1] or croak("Unable to open cache dir $self->[1]: $@") 2 0 2
181 2 ▶ while (my $file = readdir($dh))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 4 2 6
182 {
183 4 my $key = "$self->[1]/$file";
184 4 ▶ next unless -f $key;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $key 4 0 4
185 0 unlink $key ;#or carp "Unable to remove cache file $key: $@";
186 }
187 2 closedir $dh;
188 }
189  
190 sub DELETE
191 2 ▶{
Subroutine DELETE: called CC=2 SCAR=7.1
192 2 my ($self, $key) = @_;
193 2 ▶ return unless exists $self->[0]->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[0]{$key} 0 2 2
194 2 delete $self->[0]->{$key};
195 }
196  
197 sub EXISTS
198 2 ▶{
Subroutine EXISTS: called CC=1 SCAR=0.0
199 2 my ($self, $key) = @_;
200 2 return exists $self->[0]->{$key};
201 }
202  
203 sub FIRSTKEY
204 1 ▶{
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
205 1 my ($self) = @_;
206 1 my @keys = keys %{$self->[0]};
207 1 each %{$self->[0]};
208 }
209  
210 sub NEXTKEY
211 2 ▶{
Subroutine NEXTKEY: called CC=1 SCAR=0.0
212 2 my ($self, $lastkey) = @_;
213 2 each %{$self->[0]};
214 }
215  
216 sub SCALAR
217 4 ▶{
Subroutine SCALAR: called CC=1 SCAR=0.0
218 4 my ($self) = @_;
219 4 return scalar(%{$self->[0]});
220 }
221  
222 sub UNTIE
223 1 ▶{
Subroutine UNTIE: called CC=1 SCAR=0.0
224 1 my ($self) = @_;
225 1 $self->CLEAR();
226 1 rmdir $self->[1] ; #or carp "Unable to remove cache dir $self->[1]: $@";
227 }
228  
229 sub DESTROY
230 1 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
231 1 $_[0]->UNTIE();
232 }
233  
234 sub cachefile
235 1 ▶{
Subroutine cachefile: called CC=1 SCAR=0.2
Pod undocumented
236 1 my ($self, $key) = @_;
237 0 $key =~ s/([^a-zA-Z0-9])/sprintf('_%02x'.ord($1))/ge;
238 1 return "$self->[1]/$key";
239 }
240  
241 2 1;
242  
243 package FusqlFS::Cache::File::Record;
244 2 ▶use FusqlFS::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
245 2 our $VERSION = $FusqlFS::Version::VERSION;
246  
247 =head1 NAME
248  
249 FusqlFS::Cache::File::Record - file cache strategy backend class
250  
251 =head1 SYNOPSIS
252  
253 use FusqlFS::Cache::File::Record;
254  
255 my $value;
256 tie $value, 'FusqlFS::Cache::File::Record', '/tmp/value-storage.tmp';
257  
258 =head1 DESCRIPTION
259  
260 This class is scalar tie()-able class, which stores tied scalar in a file on
261 disk. This is a backend class for L<FusqlFS::Cache::File> used to store single
262 entry value in a file. This class is not to be used by itself.
263  
264 Please see L<FusqlFS::Cache::File> for full description about `file' cache
265 strategy and everything related to it.
266  
267 =cut
268  
269 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
270  
271 =begin testing
272  
273 #!req FusqlFS::Cache::File
274 #!noinst
275  
276 my $string = '';
277  
278 use File::Temp qw(:mktemp);
279 my $tempfile = mktemp('fusqlfs_test_XXXXXXX');
280  
281 isa_ok tie($string, 'FusqlFS::Cache::File::Record', $tempfile, 'stored value'), 'FusqlFS::Cache::File::Record', 'File cache record tied';
282 is $string, 'stored value', 'File cache record is sane';
283 $string = 'new value';
284 is $string, 'new value', 'File cache record is sane after rewrite';
285  
286 =end testing
287 =cut
288  
289 sub TIESCALAR
290 2 ▶{
Subroutine TIESCALAR: called CC=2 SCAR=7.0
291 2 my $value = $_[1];
292 2 my $self = bless \$value, $_[0];
293 2 ▶ $self->STORE($_[2]) if defined $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[2] 2 0 2
294 2 return $self;
295 }
296  
297 sub FETCH
298 4 ▶{
Subroutine FETCH: called CC=5 SCAR=17.3
299 4 my $self = shift;
300 4 my $size = -s $$self;
301 4 ▶ return '' if !$size || $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $size or $size == 0 0 4 4
Condition33%1/3
not $size or $size == 0
l!l&&r!l&&!r
004
MC/DC0%0/2
not $size or $size == 0
not $size $size == 0
Truth table33%1/3
not(A) or B
A= not $sizeB= $size == 0
ABresult
1X1
011
000
302  
303 4 ▶ open my $fh, '<', $$self or croak "Unable to open cache file $$self: $@";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $$self or croak("Unable to open cache file $${self}: $@") 4 0 4
304  
305 4 my $buffer;
306 4 ▶ read $fh, $buffer, $size or croak "Unable to read cache file $$self: $@";
Branch: 1 branch
BranchTrueFalseTotal
read $fh, $buffer, $size or croak("Unable to read cache file $${self}: $@") 4 0 4
307 4 close $fh;
308  
309 4 return $buffer;
310 }
311  
312 sub STORE
313 3 ▶{
Subroutine STORE: called CC=3 SCAR=11.4
314 3 my $self = shift;
315  
316 3 ▶ open my $fh, '>', $$self or croak "Unable to open cache file $$self: $@";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $$self or croak("Unable to open cache file $${self}: $@") 3 0 3
317  
318 3 ▶ print $fh $_[0] or croak "Unable to write cache file $$self: $@";
Branch: 1 branch
BranchTrueFalseTotal
print $fh $_[0] or croak("Unable to write cache file $${self}: $@") 3 0 3
319 3 close $fh;
320 }
321  
322 sub UNTIE
323 2 ▶{
Subroutine UNTIE: called CC=1 SCAR=0.0
324 2 unlink ${$_[0]} ;#or carp "Unable to remove cache file ${$_[0]}: $@";
325 }
326  
327 sub DESTROY
328 2 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
329 2 $_[0]->UNTIE();
330 }
331  
332 2 1;
333  
« FusqlFS/Backend/MySQL.pm ↑ Summary FusqlFS/Backend/SQLite/Table/Data.pm »