blib/lib/PDL/DiskCache.pm

filter: Statementstmt 54.7% 69 / 126 Branchbran 31.0% 23 / 74 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 75.0% 9 / 12 Podpod 66.6% 2 / 3 totaltotal 46.7% 107 / 229 CC 45 SCAR 36.2
CC
45
Cov
46%
purge
132.0
STORESIZE
42.0
FETCH
20.0
CRAP 37.5
« blib/lib/PDL/Primitive.pm ↑ Summary blib/lib/PDL/PP/Dump.pm »
1 =head1 NAME
2  
3 PDL::DiskCache -- Non-memory-resident array object
4  
5 =head1 SYNOPSIS
6  
7 NON-OO:
8  
9 use PDL::DiskCache;
10 tie @a,'PDL::DiskCache', \@files, \%options;
11 imag $a[3];
12  
13 OO:
14  
15 use PDL::DiskCache;
16 $a = diskcache(\@files,\%options);
17 imag $a->[3];
18  
19 or
20  
21 use PDL::DiskCache;
22 $a = new PDL::DiskCache(\@files,\%options);
23 imag $a->[4];
24  
25 =over 3
26  
27 =item \@files
28  
29 an array ref containing a list of file names
30  
31 =item \%options
32  
33 a hash ref containing options for the PDL::DiskCache object (see "TIEARRAY"
34 below for details)
35  
36 =back
37  
38 =head1 DESCRIPTION
39  
40 A PDL::DiskCache object is a perl L<"tied array"|perltie> that is useful
41 for operations where you have to look at a large collection of PDLs one
42 or a few at a time (such as tracking features through an image sequence).
43 You can write prototype code that uses a perl list of a few PDLs, then
44 scale up to to millions of PDLs simply by handing the prototype code
45 a DiskCache tied array instead of a native perl array. The individual
46 PDLs are stored on disk and a few of them are swapped into memory on a
47 FIFO basis. You can set whether the data are read-only or writeable.
48  
49 By default, PDL::DiskCache uses FITS files to represent the PDLs, but
50 you can use any sort of file at all -- the read/write routines are the
51 only place where it examines the underlying data, and you can specify
52 the routines to use at construction time (or, of course, subclass
53 PDL::DiskCache).
54  
55 Items are swapped out on a FIFO basis, so if you have 10 slots
56 and an expression with 10 items in it then you're OK (but you probably
57 want more slots than that); but if you use more items in an expression than
58 there are slots, thrashing will occur!
59  
60 The hash ref interface is kept for historical reasons; you can access
61 the sync() and purge() method calls directly from the returned array ref.
62  
63 =head1 Shortcomings & caveats
64  
65 There's no file locking, so you could really hose yourself by having two of
66 these things going at once on the same files.
67  
68 Since this is a tied array, things like Dumper traverse it transparently.
69 That is sort-of good but also sort-of dangerous. You wouldn't want to
70 PDL::Dumper::sdump() a large PDL::DiskCache, for example -- that would defeat
71 the purpose of using a PDL::DiskCache in the first place.
72  
73  
74  
75 =head1 Author, license, no warranty
76  
77 Copyright 2001, Craig DeForest
78  
79 This code may be distributed under the same terms as Perl itself
80 (license available at L<http://www.perl.org>). Copying, reverse engineering,
81 distribution, and modification are explicitly allowed so long as this notice
82 is preserved intact and modified versions are clearly marked as such.
83  
84 If you modify the code and it's useful, please send a copy of the modified
85 version to cdeforest@solar.stanford.edu.
86  
87 This package comes with NO WARRANTY.
88  
89 =head1 FUNCTIONS
90  
91 =cut
92  
93 ######################################################################
94 # Package initialization
95 1 $PDL::DiskCache::VERSION = 1.1;
96
97 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 =head2 diskcache
101  
102 Object constructor.
103  
104 =for usage
105  
106 $a = diskcache(\@f,\%options);
107  
108 Options
109  
110 =over 3
111  
112 =item
113  
114 See the TIEARRAY options,below.
115  
116 =back
117  
118 =cut
119  
120 sub diskcache {
121 2 ▶ my($f,$opt) = @_;
Subroutine diskcache: called CC=1 SCAR=0.0
122 2 return PDL::DiskCache::new('PDL::DiskCache',$f,$opt);
123 }
124  
125 sub PDL::DiskCache::new {
126 2 ▶ my($class,$f,$opt) = @_;
Subroutine new: called CC=3 SCAR=11.8
Pod undocumented
127 2 my($a)=[];
128  
129 2 my($b) = tie @{$a},$class,$f,$opt;
130 2 ▶ if($opt->{bless}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"bless"}) 0 2 2
131 0 $a = bless($a,$class);
132 }
133  
134 2 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 0 2
135 2 return ($a,bless($b,$class));
136 } else {
137 0 return $a;
138 }
139 }
140  
141 1 *PDL::DiskCache::diskcache = *diskcache;
142  
143 =head2 TIEARRAY
144  
145 =for ref
146  
147 Tied-array constructor; invoked by perl during object construction.
148  
149 =for usage
150  
151 TIEARRAY(class,\@f,\%options)
152  
153 Options
154  
155 =over 3
156  
157 =item ro (default 0)
158  
159 If set, treat the files as read-only (modifications
160 to the tied array will only persist until the changed elements are
161 swapped out)
162  
163 =item rw (default 1)
164  
165 If set, allow reading and writing to the files.
166 Because there's currently no way to determine reliably whether a PDL
167 has been modified, rw files are always written to disk when they're
168 swapped out -- this causes a slight performance hit.
169  
170 =item mem (default 20)
171  
172 Number of files to be cached in memory at once.
173  
174 =item read (default \&rfits)
175  
176 A function ref pointing to code that will read
177 list objects from disk. The function must have the same syntax as
178 rfits: $object = rfits(filename).
179  
180 =item write (default \&wfits)
181  
182 A function ref pointing to code that will
183 write list objects to disk. The function must have the same syntax as
184 wfits: func(object,filename).
185  
186 =item bless (default 0)
187  
188 If set to a nonzero value, then the array ref gets
189 blessed into the DiskCache class for for easier access to the "purge"
190 and "sync" methods. This means that you can say C<< $a->sync >> instead
191 of the more complex C<< (%{tied @$a})->sync >>, but C<ref $a> will return
192 "PDL::DiskCache" instead of "ARRAY", which could break some code.
193  
194 =item verbose (default 0)
195  
196 Get chatty.
197  
198 =back
199  
200 =cut
201  
202 sub PDL::DiskCache::TIEARRAY {
203 2 ▶ my($class,$f,$opt) = @_;
Subroutine TIEARRAY: called CC=6 SCAR=18.8
204  
205 2 ▶ croak "PDL::DiskCache needs array ref as 2nd arg (did you pass an array instead?)\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $f ne "ARRAY" 0 2 2
206 if(ref $f ne 'ARRAY');
207 my($new) = {files => $f # File list
208 2 ▶ , n => scalar(@{$f}) # no. of el.
Condition50%3/6
$opt->{'write'} || \&main::wfits
l!l
02
$opt->{'read'} || \&main::rfits
l!l
02
$opt->{'mem'} || 20
l!l
02
MC/DC0%0/1
$opt->{'write'} || \&main::wfits
$opt->{'write'}
MC/DC0%0/1
$opt->{'read'} || \&main::rfits
$opt->{'read'}
MC/DC0%0/1
$opt->{'mem'} || 20
$opt->{'mem'}
Truth table50%1/2
A
A= $opt->{'write'}
Aresult
11
00
Truth table50%1/2
A
A= $opt->{'read'}
Aresult
11
00
Truth table50%1/2
A
A= $opt->{'mem'}
Aresult
11
00
209 , write => $opt->{write} || \&main::wfits # Write routine
210 , read => $opt->{read} || \&main::rfits # Read routine
211 , mem => $opt->{mem} || 20 # No. of mem slots
212 2 , rw => (!($opt->{ro})) # rw or ro
213 , fdex => [] # Current file stored in each slot, by slot
214 , slot => [] # Current slot in which each file is stored
215 , cache => [] # Actual cached stuff gets held here
216 , opt => {} # Options stashed here for later reference
217 , cache_next => 0 # Next cache slot to be used
218 };
219 2 foreach $_(keys %{$opt}) {
220 2 $new->{opt}->{$_} = $opt->{$_};
221 }
222  
223 2 return bless($new,$class);
224 }
225  
226 =head2 purge
227  
228 Remove an item from the oldest slot in the cache, writing to disk as necessary.
229 You also send in how many slots to purge (default 1; sending in -1 purges
230 everything.)
231  
232 For most uses, a nice MODIFIED flag in the data structure could save
233 some hassle here. But PDLs can get modified out from under us
234 with slicing and .= -- so for now we always assume everything is tainted
235 and must be written to disk.
236  
237 =cut
238  
239 sub PDL::DiskCache::purge {
240 0 ▶ my($me,$n) = @_,1;
Subroutine purge: not called CC=11 SCAR=48.8
Pod documented
241 0 ▶ $me = (tied @{$me}) if("$me" =~ m/^PDL\:\:DiskCache\=ARRAY/);
Branch: 1 branch
BranchTrueFalseTotal
if "$me" =~ /^PDL\:\:DiskCache\=ARRAY/ 0 0 0
242  
243 0 ▶ $n = $me->{mem} if($n<0);
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 0 0 0
244
245 0 ▶ print "purging $n items..." if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
246  
247
248 0 my($dex) = $me->{cache_next};
249  
250 0 local($_);
251 0 for(1..$n) {
252 0 ▶ if($me->{rw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"rw"}) 0 0 0
253 print "writing $me->{files}->[$me->{fdex}->[$dex]]: "
254 0 ▶ if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
255  
256 0 eval {&{$me->{write}}($me->{cache}->[$dex],
257 0 $me->{files}->[$me->{fdex}->[$dex]]);
258 };
259 0 ▶ print "WARNING: PDL::DiskCache::purge: problems with write of ".$me->{files}->[$me->{fdex}->[$dex]].", item $me->{fdex}->[$dex] from slot $dex: `$@' (".$me->{opt}->{varname}.") \n" if($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
260 0 $@ = 0;
261  
262 0 ▶ print "ok.\n" if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
263 }
264
265  
266 0 ▶ print "Purging item $dex (file $me->{fdex}->[$dex])...\n" if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
267 0 undef $me->{slot}->[$me->{fdex}->[$dex]]; # Purge from slot location list
268 0 undef $me->{fdex}->[$dex]; # Purge from slot fdex list
269 0 undef $me->{cache}->[$dex]; # Purge from memory
270  
271 0 $dex++;
272 0 $dex %= $me->{mem};
273 }
274 0 ▶ print "...done with purge.\n" if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
275 }
276  
277 sub PDL::DiskCache::FETCH {
278 2 ▶ my($me,$i) = @_;
Subroutine FETCH: called CC=10 SCAR=29.9
279  
280 2 ▶ if($i < 0 || $i >= $me->{n}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0 or $i >= $me->{"n"}) 0 2 2
Condition33%1/3
$i < 0 or $i >= $me->{"n"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$i < 0 or $i >= $me->{"n"}
$i < 0 $i >= $me->{"n"}
Truth table33%1/3
A or B
A= $i < 0B= $i >= $me->{"n"}
ABresult
1X1
011
000
281 0 carp("PDL::DiskCache: Element $i is outside range of 0-",$me->{n}-1,"\n");
282 0 return undef;
283 }
284  
285 2 ▶ if(defined $me->{slot}->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"slot"}[$i]) 0 2 2
286 0 ▶ print "Item $i is in the cache...\n" if ($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
287 0 return ($me->{cache}->[$me->{slot}->[$i]]);
288 }
289
290 ### Got here -- we have to get the item from disk.
291  
292 print "Item $i ($me->{files}->[$i]) not present. Retrieving..."
293 2 ▶ if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 2 2
294
295 2 ▶ if(defined($me->{fdex}->[$me->{cache_next}])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"fdex"}[$me->{"cache_next"}]) 0 2 2
296 0 ▶ print "cache full..." if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
297  
298 0 $me->purge(1);
299 }
300
301 2 my($a) = $me->{cache_next};
302 2 $me->{cache}->[$a] = eval {
303 2 &{$me->{read}}($me->{files}->[$i])
304 } ;
305 2 undef $@; # Keep this from hanging anything else.
306 print "result is ",(defined $me->{cache}->[$a] ? "" : "un")."defined.\n"
307 2 ▶ if($me->{opt}->{verbose});
Branch: 2 branches
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 2 2
defined $me->{'cache'}[$a] ? : 0 0 0
308  
309 2 $me->{slot}->[$i] = $me->{cache_next};
310 2 $me->{fdex}->[$me->{cache_next}] = $i;
311 2 $me->{cache_next}++;
312 2 $me->{cache_next} %= $me->{mem};
313 2 $me->{cache}->[$a];
314 }
315  
316 sub PDL::DiskCache::STORE {
317 3 ▶ my($me, $i, $val) = @_;
Subroutine STORE: called CC=6 SCAR=23.0
318  
319 3 ▶ if( $me->{slot}->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{'slot'}[$i]) { } 0 3 3
320 0 ▶ print "Storing index $i, in cache\n" if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
321 0 $me->sync($i);
322 0 return $me->{cache}->[$me->{slot}->[$i]] = $val;
323 } else {
324 3 ▶ print "Storing index $i, not in cache\n" if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 3 0 3
325 3 ▶ if(defined ($me->{fdex}->[$me->{cache_next}])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"fdex"}[$me->{"cache_next"}]) 0 3 3
326 0 ▶ print "cache full..." if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
327 0 $me->purge(1);
328 }
329
330 3 my($a) = $me->{cache_next};
331 3 $me->{slot}->[$i] = $a;
332 3 $me->{fdex}->[$a] = $i;
333 3 $me->{cache_next}++;
334 3 $me->{cache_next} %= $me->{mem};
335 3 $me->sync($i);
336 3 return $me->{cache}->[$a] = $val;
337 }
338  
339 0 croak("This never happens");
340  
341 }
342
343 sub PDL::DiskCache::FETCHSIZE {
344 0 ▶ my($me) = shift;
Subroutine FETCHSIZE: not called CC=1 SCAR=6.9
345  
346 0 $me->{n};
347 }
348  
349 sub PDL::DiskCache::STORESIZE {
350 0 ▶ my($me,$newsize) = @_;
Subroutine STORESIZE: not called CC=6 SCAR=37.4
351  
352 0 ▶ if($newsize > $me->{n}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newsize > $me->{"n"}) 0 0 0
353 0 croak("PDL::DiskCache: Can't augment array size (yet)!\n");
354 }
355
356 0 ▶ for( my($i) = $newsize-1; $i<$me->{n}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $me->{"n"}) 0 0 0
357 0 ▶ if(defined $me->{slot}->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"slot"}[$i]) 0 0 0
358 0 ▶ if($me->{rw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"rw"}) 0 0 0
359 print "Writing $me->{files}->[$me->{fdex}->[$i]]\n"
360 0 ▶ if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 0 0 0
361 0 eval {&{$me->{write}}($me->{cache}->[$me->{slot}->[$i]],
362 0 $me->{files}->[$i]);
363 };
364 0 $@ = 0; # Keep from hanging anything else
365 }
366 0 undef $me->{fdex}->[$me->{slot}->[$i]];
367 }
368 }
369 0 $#{$me->{slot}} = $newsize-1;
370 0 $#{$me->{files}} = $newsize-1;
371 0 $me->{n} = $newsize;
372 }
373  
374 =head2 sync
375  
376 In a rw cache, flush items out to disk but retain them in the cache.
377  
378 Accepts a single scalar argument, which is the index number of a
379 single item that should be written to disk. Passing (-1), or no
380 argument, writes all items to disk, similar to purge(-1).
381  
382 For ro caches, this is a not-too-slow (but safe) no-op.
383  
384 =cut
385  
386 sub PDL::DiskCache::sync {
387 5 ▶ my($me) = shift;
Subroutine sync: called CC=9 SCAR=22.1
Pod documented
388 5 ▶ $me = (tied @{$me}) if("$me" =~ m/^PDL\:\:DiskCache\=ARRAY/);
Branch: 1 branch
BranchTrueFalseTotal
if "$me" =~ /^PDL\:\:DiskCache\=ARRAY/ 0 5 5
389 5 my($syncn) = shift;
390 5 ▶ $syncn = -1 unless defined $syncn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $syncn 2 3 5
391 5 ▶ print "PDL::DiskCache::sync\n" if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 4 1 5
392
393 5 ▶ my @list = $syncn==-1 ? (0..$me->{mem}-1) : ($syncn);
Branch: 1 branch
BranchTrueFalseTotal
$syncn == -1 ? : 2 3 5
394  
395 5 ▶ if($me->{rw}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"rw"}) 4 1 5
396 4 for(@list) {
397 23 ▶ if(defined $me->{fdex}->[$_]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"fdex"}[$_]) 6 17 23
398  
399 print " writing $me->{files}->[$me->{fdex}->[$_]]...\n"
400 6 ▶ if($me->{opt}->{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"opt"}{"verbose"} 6 0 6
401  
402 6 eval {&{$me->{write}}($me->{cache}->[$_],
403 6 $me->{files}->[$me->{fdex}->[$_]]);
404 };
405 6 $@ = 0; # keep from hanging anything else
406 }
407 }
408 }
409 }
410  
411 =head2 DESTROY
412  
413 This is the perl hook for object destruction. It just makes a call to
414 "sync", to flush the cache out to disk. Destructor calls from perl don't
415 happen at a guaranteed time, so be sure to call "sync" if you need to
416 ensure that the files get flushed out, e.g. to use 'em somewhere else.
417  
418 =cut
419  
420 sub PDL::DiskCache::DESTROY {
421 2 ▶ my($me) = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
422  
423 2 $me->sync;
424 }
425  
426 # return true
427 1 1;
« blib/lib/PDL/Primitive.pm ↑ Summary blib/lib/PDL/PP/Dump.pm »