lib/Tie/File.pm

filter: Statementstmt 85.1% 863 / 1013 Branchbran 76.8% 378 / 492 Conditioncond 68.2% 56 / 82 MC/DCmcdc 37.7% 20 / 53 Subroutinesub 91.0% 102 / 112 Podpod 87.5% 7 / 8 totaltotal 81.0% 1426 / 1760 CC 295 SCAR 32.8
CC
295
Cov
82%
_check_integrity
184.8
_insert
56.0
TIEARRAY
48.3
CRAP 26.5
↑ Summary
1 package Tie::File;
2  
3 38 require 5.005;
4  
5 38 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 38 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 38 ▶use Carp ':DEFAULT', 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 38 ▶use POSIX 'SEEK_SET';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 38 ▶use Fcntl 'O_CREAT', 'O_RDWR', 'LOCK_EX', 'LOCK_SH', 'O_WRONLY', 'O_RDONLY';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 sub O_ACCMODE () { O_RDONLY | O_RDWR | O_WRONLY }
12  
13  
14 38 our $VERSION = "1.07";
15 38 my $DEFAULT_MEMORY_SIZE = 1<<21; # 2 megabytes
16 38 my $DEFAULT_AUTODEFER_THRESHHOLD = 3; # 3 records
17 38 my $DEFAULT_AUTODEFER_FILELEN_THRESHHOLD = 65536; # 16 disk blocksful
18  
19 38 my %good_opt = map {$_ => 1, "-$_" => 1}
20 qw(memory dw_size mode recsep discipline
21 autodefer autochomp autodefer_threshhold concurrent);
22  
23 38 our $DIAGNOSTIC = 0;
24 38 our @OFF; # used as a temporary alias in some subroutines.
25 38 our @H; # used as a temporary alias in _annotate_ad_history
26  
27 2987 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=28 SCAR=38.8
28 2987 ▶ if (@_ % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2 != 0) 0 2987 2987
29 0 croak "usage: tie \@array, $_[0], filename, [option => value]...";
30 }
31 2987 my ($pack, $file, %opts) = @_;
32  
33 # transform '-foo' keys into 'foo' keys
34 2987 for my $key (keys %opts) {
35 57 ▶ unless ($good_opt{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($good_opt{$key}) 0 57 57
36 0 croak("$pack: Unrecognized option '$key'\n");
37 }
38 57 my $okey = $key;
39 57 ▶ if ($key =~ s/^-+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ s/^-+//) 0 57 57
40 0 $opts{$key} = delete $opts{$okey};
41 }
42 }
43  
44 2987 ▶ if ($opts{concurrent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"concurrent"}) 0 2987 2987
45 0 croak("$pack: concurrent access not supported yet\n");
46 }
47  
48 2987 ▶ unless (defined $opts{memory}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"memory"}) 2983 4 2987
49 # default is the larger of the default cache size and the
50 # deferred-write buffer size (if specified)
51 2983 $opts{memory} = $DEFAULT_MEMORY_SIZE;
52 $opts{memory} = $opts{dw_size}
53 2983 ▶ if defined $opts{dw_size} && $opts{dw_size} > $DEFAULT_MEMORY_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"dw_size"} and $opts{"dw_size"} > $DEFAULT_MEMORY_SIZE 0 2983 2983
Condition33%1/3
defined $opts{"dw_size"} and $opts{"dw_size"} > $DEFAULT_MEMORY_SIZE
!ll&&!rl&&r
298300
MC/DC0%0/2
defined $opts{"dw_size"} and $opts{"dw_size"} > $DEFAULT_MEMORY_SIZE
defined $opts{"dw_size"} $opts{"dw_size"} > $DEFAULT_MEMORY_SIZE
Truth table33%1/3
A and B
A= defined $opts{"dw_size"}B= $opts{"dw_size"} > $DEFAULT_MEMORY_SIZE
ABresult
0X0
100
111
54 # Dora Winifred Read
55 }
56 2987 ▶ $opts{dw_size} = $opts{memory} unless defined $opts{dw_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"dw_size"} 2986 1 2987
57 2987 ▶ if ($opts{dw_size} > $opts{memory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"dw_size"} > $opts{"memory"}) 0 2987 2987
58 0 croak("$pack: dw_size may not be larger than total memory allocation\n");
59 }
60 # are we in deferred-write mode?
61 2987 ▶ $opts{defer} = 0 unless defined $opts{defer};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"defer"} 2987 0 2987
62 2987 $opts{deferred} = {}; # no records are presently deferred
63 2987 $opts{deferred_s} = 0; # count of total bytes in ->{deferred}
64 2987 $opts{deferred_max} = -1; # empty
65  
66 # What's a good way to arrange that this class can be overridden?
67 2987 $opts{cache} = Tie::File::Cache->new($opts{memory});
68  
69 # autodeferment is enabled by default
70 2987 ▶ $opts{autodefer} = 1 unless defined $opts{autodefer};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"autodefer"} 2970 17 2987
71 2987 $opts{autodeferring} = 0; # but is not initially active
72 2987 $opts{ad_history} = [];
73 $opts{autodefer_threshhold} = $DEFAULT_AUTODEFER_THRESHHOLD
74 2987 ▶ unless defined $opts{autodefer_threshhold};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"autodefer_threshhold"} 2987 0 2987
75 $opts{autodefer_filelen_threshhold} = $DEFAULT_AUTODEFER_FILELEN_THRESHHOLD
76 2987 ▶ unless defined $opts{autodefer_filelen_threshhold};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"autodefer_filelen_threshhold"} 2987 0 2987
77  
78 2987 $opts{offsets} = [0];
79 2987 $opts{filename} = $file;
80 2987 ▶ unless (defined $opts{recsep}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"recsep"}) 2974 13 2987
81 2974 $opts{recsep} = _default_recsep();
82 }
83 2987 $opts{recseplen} = length($opts{recsep});
84 2987 ▶ if ($opts{recseplen} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"recseplen"} == 0) 0 2987 2987
85 0 croak "Empty record separator not supported by $pack";
86 }
87  
88 2987 ▶ $opts{autochomp} = 1 unless defined $opts{autochomp};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"autochomp"} 2968 19 2987
89  
90 2987 ▶ $opts{mode} = O_CREAT|O_RDWR unless defined $opts{mode};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"mode"} 2984 3 2987
91 2987 $opts{rdonly} = (($opts{mode} & O_ACCMODE) == O_RDONLY);
92 2987 $opts{sawlastrec} = undef;
93  
94 2987 my $fh;
95  
96 2987 ▶ if (UNIVERSAL::isa($file, 'GLOB')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($file, 'GLOB')) { } 2 2985 2987
elsif (ref $file) { } 0 2985 2985
97 # We use 1 here on the theory that some systems
98 # may not indicate failure if we use 0.
99 # MSWin32 does not indicate failure with 0, but I don't know if
100 # it will indicate failure with 1 or not.
101 2 ▶ unless (seek $file, 1, SEEK_SET) {
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $file, 1, 0) 1 1 2
102 1 croak "$pack: your filehandle does not appear to be seekable";
103 }
104 1 seek $file, 0, SEEK_SET; # put it back
105 1 $fh = $file; # setting binmode is the user's problem
106 } elsif (ref $file) {
107 0 croak "usage: tie \@array, $pack, filename, [option => value]...";
108 } else {
109 # $fh = \do { local *FH }; # XXX this is buggy
110 2985 ▶ if ($] < 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.006") 0 2985 2985
111 # perl 5.005 and earlier don't autovivify filehandles
112 0 require Symbol;
113 0 $fh = Symbol::gensym();
114 }
115 2985 ▶ sysopen $fh, $file, $opts{mode}, 0666 or return;
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fh, $file, $opts{"mode"}, 438 or return 2985 0 2985
116 2985 binmode $fh;
117 2985 ++$opts{ourfh};
118 }
119 2986 { my $ofh = select $fh; $| = 1; select $ofh } # autoflush on write
120 2986 ▶ if (defined $opts{discipline} && $] >= 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"discipline"} and $] >= "5.006") 0 2986 2986
Condition33%1/3
defined $opts{"discipline"} and $] >= "5.006"
!ll&&!rl&&r
298600
MC/DC0%0/2
defined $opts{"discipline"} and $] >= "5.006"
defined $opts{"discipline"} $] >= "5.006"
Truth table33%1/3
A and B
A= defined $opts{"discipline"}B= $] >= "5.006"
ABresult
0X0
100
111
121 # This avoids a compile-time warning under 5.005
122 0 eval 'binmode($fh, $opts{discipline})';
123 0 ▶ croak $@ if $@ =~ /unknown discipline/i;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /unknown discipline/i 0 0 0
124 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
125 }
126 2986 $opts{fh} = $fh;
127  
128 2986 bless \%opts => $pack;
129 }
130  
131 1047 ▶sub FETCH {
Subroutine FETCH: called CC=5 SCAR=16.1
132 1047 my ($self, $n) = @_;
133 1047 my $rec;
134  
135 # check the defer buffer
136 1047 ▶ $rec = $self->{deferred}{$n} if exists $self->{deferred}{$n};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"deferred"}{$n} 8 1039 1047
137 1047 ▶ $rec = $self->_fetch($n) unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 1039 8 1047
138  
139 # inlined _chomp1
140 substr($rec, - $self->{recseplen}) = ""
141 1047 ▶ if defined $rec && $self->{autochomp};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rec and $self->{"autochomp"} 747 300 1047
Condition100%3/3
defined $rec and $self->{"autochomp"}
!ll&&!rl&&r
10290747
MC/DC100%2/2
defined $rec and $self->{"autochomp"}
defined $rec $self->{"autochomp"}
Truth table100%3/3
A and B
A= defined $recB= $self->{"autochomp"}
ABresult
0X0
100
111
142 1047 $rec;
143 }
144  
145 # Chomp many records in-place; return nothing useful
146 145 ▶sub _chomp {
Subroutine _chomp: called CC=5 SCAR=16.3
147 145 my $self = shift;
148 145 ▶ return unless $self->{autochomp};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"autochomp"} 97 48 145
149 48 ▶ if ($self->{autochomp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autochomp"}) 48 0 48
150 48 for (@_) {
151 68 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 68 68
152 68 substr($_, - $self->{recseplen}) = "";
153 }
154 }
155 }
156  
157 # Chomp one record in-place; return modified record
158 246 ▶sub _chomp1 {
Subroutine _chomp1: called CC=3 SCAR=11.0
159 246 my ($self, $rec) = @_;
160 246 ▶ return $rec unless $self->{autochomp};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"autochomp"} 25 221 246
161 221 ▶ return unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 84 137 221
162 137 substr($rec, - $self->{recseplen}) = "";
163 137 $rec;
164 }
165  
166 1851 ▶sub _fetch {
Subroutine _fetch: called CC=7 SCAR=19.5
167 1851 my ($self, $n) = @_;
168  
169 # check the record cache
170 1851 { my $cached = $self->{cache}->lookup($n);
171 1851 ▶ return $cached if defined $cached;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cached 644 1207 1851
172 }
173  
174 1207 ▶ if ($#{$self->{offsets}} < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"offsets"};} < $n) 23 1184 1207
175 23 ▶ return if $self->{eof}; # request for record beyond end of file
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 12 11 23
176 11 my $o = $self->_fill_offsets_to($n);
177 # If it's still undefined, there is no such record, so return 'undef'
178 11 ▶ return unless defined $o;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o 6 5 11
179 }
180  
181 1189 my $fh = $self->{FH};
182 1189 $self->_seek($n); # we can do this now that offsets is populated
183 1189 my $rec = $self->_read_record;
184  
185 # If we happen to have just read the first record, check to see if
186 # the length of the record matches what 'tell' says. If not, Tie::File
187 # won't work, and should drop dead.
188 #
189 # if ($n == 0 && defined($rec) && tell($self->{fh}) != length($rec)) {
190 # if (defined $self->{discipline}) {
191 # croak "I/O discipline $self->{discipline} not supported";
192 # } else {
193 # croak "File encoding not supported";
194 # }
195 # }
196  
197 1189 ▶ $self->{cache}->insert($n, $rec) if defined $rec && not $self->{flushing};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rec and not $self->{"flushing"} 1157 32 1189
Condition66%2/3
defined $rec and not $self->{"flushing"}
!ll&&!rl&&r
3201157
MC/DC50%1/2
defined $rec and not $self->{"flushing"}
defined $rec not $self->{"flushing"}
Truth table66%2/3
A and not(B)
A= defined $recB= not $self->{"flushing"}
ABresult
0X0
100
111
198 1189 $rec;
199 }
200  
201 371 ▶sub STORE {
Subroutine STORE: called CC=5 SCAR=16.1
202 371 my ($self, $n, $rec) = @_;
203 371 ▶ die "STORE called from _check_integrity!" if $DIAGNOSTIC;
Branch: 1 branch
BranchTrueFalseTotal
if $Tie::File::DIAGNOSTIC 0 371 371
204  
205 371 $self->_fixrecs($rec);
206  
207 371 ▶ if ($self->{autodefer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodefer"}) 231 140 371
208 231 $self->_annotate_ad_history($n);
209 }
210  
211 371 ▶ return $self->_store_deferred($n, $rec) if $self->_is_deferring;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_deferring 71 300 371
212  
213  
214 # We need this to decide whether the new record will fit
215 # It incidentally populates the offsets table
216 # Note we have to do this before we alter the cache
217 # 20020324 Wait, but this DOES alter the cache. TODO BUG?
218 300 my $oldrec = $self->_fetch($n);
219  
220 300 ▶ if (not defined $oldrec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $oldrec) 40 260 300
221 # We're storing a record beyond the end of the file
222 40 $self->_extend_file_to($n+1);
223 40 $oldrec = $self->{recsep};
224 }
225 # return if $oldrec eq $rec; # don't bother
226 300 my $len_diff = length($rec) - length($oldrec);
227  
228 # length($oldrec) here is not consistent with text mode TODO XXX BUG
229 300 $self->_mtwrite($rec, $self->{offsets}[$n], length($oldrec));
230 300 $self->_oadjust([$n, 1, $rec]);
231 300 $self->{cache}->update($n, $rec);
232 }
233  
234 73 ▶sub _store_deferred {
Subroutine _store_deferred: called CC=6 SCAR=17.9
235 73 my ($self, $n, $rec) = @_;
236 73 $self->{cache}->remove($n);
237 73 my $old_deferred = $self->{deferred}{$n};
238  
239 73 ▶ if (defined $self->{deferred_max} && $n > $self->{deferred_max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"deferred_max"} and $n > $self->{"deferred_max"}) 68 5 73
Condition100%3/3
defined $self->{"deferred_max"} and $n > $self->{"deferred_max"}
!ll&&!rl&&r
2368
MC/DC100%2/2
defined $self->{"deferred_max"} and $n > $self->{"deferred_max"}
defined $self->{"deferred_max"} $n > $self->{"deferred_max"}
Truth table100%3/3
A and B
A= defined $self->{"deferred_max"}B= $n > $self->{"deferred_max"}
ABresult
0X0
100
111
240 68 $self->{deferred_max} = $n;
241 }
242 73 $self->{deferred}{$n} = $rec;
243  
244 73 my $len_diff = length($rec);
245 73 ▶ $len_diff -= length($old_deferred) if defined $old_deferred;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_deferred 2 71 73
246 73 $self->{deferred_s} += $len_diff;
247 73 $self->{cache}->adj_limit(-$len_diff);
248 73 ▶ if ($self->{deferred_s} > $self->{dw_size}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'deferred_s'} > $self->{'dw_size'}) { } 1 72 73
elsif ($self->_cache_too_full) { } 2 70 72
249 1 $self->_flush;
250 } elsif ($self->_cache_too_full) {
251 2 $self->_cache_flush;
252 }
253 }
254  
255 # Remove a single record from the deferred-write buffer without writing it
256 # The record need not be present
257 6 ▶sub _delete_deferred {
Subroutine _delete_deferred: called CC=4 SCAR=14.2
258 6 my ($self, $n) = @_;
259 6 my $rec = delete $self->{deferred}{$n};
260 6 ▶ return unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 2 4 6
261  
262 4 ▶ if (defined $self->{deferred_max}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"deferred_max"} and $n == $self->{"deferred_max"}) 4 0 4
Condition33%1/3
defined $self->{"deferred_max"} and $n == $self->{"deferred_max"}
!ll&&!rl&&r
004
MC/DC0%0/2
defined $self->{"deferred_max"} and $n == $self->{"deferred_max"}
defined $self->{"deferred_max"} $n == $self->{"deferred_max"}
Truth table33%1/3
A and B
A= defined $self->{"deferred_max"}B= $n == $self->{"deferred_max"}
ABresult
0X0
100
111
263 && $n == $self->{deferred_max}) {
264 4 undef $self->{deferred_max};
265 }
266  
267 4 $self->{deferred_s} -= length $rec;
268 4 $self->{cache}->adj_limit(length $rec);
269 }
270  
271 592 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=4 SCAR=13.9
272 592 my $self = shift;
273 592 ▶ my $n = $self->{eof} ? $#{$self->{offsets}} : $self->_fill_offsets;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'eof'} ? : 568 24 592
274  
275 592 my $top_deferred = $self->_defer_max;
276 592 ▶ $n = $top_deferred+1 if defined $top_deferred && $n < $top_deferred+1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $top_deferred and $n < $top_deferred + 1 3 589 592
Condition66%2/3
defined $top_deferred and $n < $top_deferred + 1
!ll&&!rl&&r
05893
MC/DC50%1/2
defined $top_deferred and $n < $top_deferred + 1
defined $top_deferred $n < $top_deferred + 1
Truth table66%2/3
A and B
A= defined $top_deferredB= $n < $top_deferred + 1
ABresult
0X0
100
111
277 592 $n;
278 }
279  
280 17 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=8 SCAR=20.8
281 17 my ($self, $len) = @_;
282  
283 17 ▶ if ($self->{autodefer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodefer"}) 16 1 17
284 16 $self->_annotate_ad_history('STORESIZE');
285 }
286  
287 17 my $olen = $self->FETCHSIZE;
288 17 ▶ return if $len == $olen; # Woo-hoo!
Branch: 1 branch
BranchTrueFalseTotal
if $len == $olen 0 17 17
289  
290 # file gets longer
291 17 ▶ if ($len > $olen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $olen) 6 11 17
292 6 ▶ if ($self->_is_deferring) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_deferring) { } 1 5 6
293 1 for ($olen .. $len-1) {
294 2 $self->_store_deferred($_, $self->{recsep});
295 }
296 } else {
297 5 $self->_extend_file_to($len);
298 }
299 6 return;
300 }
301  
302 # file gets shorter
303 11 ▶ if ($self->_is_deferring) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_deferring) 2 9 11
304 # TODO maybe replace this with map-plus-assignment?
305 2 for (grep $_ >= $len, keys %{$self->{deferred}}) {
306 2 $self->_delete_deferred($_);
307 }
308 2 $self->{deferred_max} = $len-1;
309 }
310  
311 11 $self->_seek($len);
312 11 $self->_chop_file;
313 11 $#{$self->{offsets}} = $len;
314 # $self->{offsets}[0] = 0; # in case we just chopped this
315  
316 11 $self->{cache}->remove(grep $_ >= $len, $self->{cache}->ckeys);
317 }
318  
319 ### OPTIMIZE ME
320 ### It should not be necessary to do FETCHSIZE
321 ### Just seek to the end of the file.
322 6 ▶sub PUSH {
Subroutine PUSH: called CC=1 SCAR=0.0
323 6 my $self = shift;
324 6 $self->SPLICE($self->FETCHSIZE, scalar(@_), @_);
325  
326 # No need to return:
327 # $self->FETCHSIZE; # because av.c takes care of this for me
328 }
329  
330 4 ▶sub POP {
Subroutine POP: called CC=2 SCAR=6.9
331 4 my $self = shift;
332 4 my $size = $self->FETCHSIZE;
333 4 ▶ return if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 1 3 4
334 # print STDERR "# POPPITY POP POP POP\n";
335 3 scalar $self->SPLICE($size-1, 1);
336 }
337  
338 4 ▶sub SHIFT {
Subroutine SHIFT: called CC=1 SCAR=0.0
339 4 my $self = shift;
340 4 scalar $self->SPLICE(0, 1);
341 }
342  
343 4 ▶sub UNSHIFT {
Subroutine UNSHIFT: called CC=1 SCAR=0.0
344 4 my $self = shift;
345 4 $self->SPLICE(0, 0, @_);
346 # $self->FETCHSIZE; # av.c takes care of this for me
347 }
348  
349 31 ▶sub CLEAR {
Subroutine CLEAR: called CC=2 SCAR=6.9
350 31 my $self = shift;
351  
352 31 ▶ if ($self->{autodefer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodefer"}) 21 10 31
353 21 $self->_annotate_ad_history('CLEAR');
354 }
355  
356 31 $self->_seekb(0);
357 31 $self->_chop_file;
358 31 $self->{cache}->set_limit($self->{memory});
359 31 $self->{cache}->empty;
360 31 @{$self->{offsets}} = (0);
361 31 %{$self->{deferred}}= ();
362 31 $self->{deferred_s} = 0;
363 31 $self->{deferred_max} = -1;
364 }
365  
366 28 ▶sub EXTEND {
Subroutine EXTEND: called CC=2 SCAR=6.9
367 28 my ($self, $n) = @_;
368  
369 # No need to pre-extend anything in this case
370 28 ▶ return if $self->_is_deferring;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_deferring 2 26 28
371  
372 26 $self->_fill_offsets_to($n);
373 26 $self->_extend_file_to($n);
374 }
375  
376 9 ▶sub DELETE {
Subroutine DELETE: called CC=5 SCAR=16.1
377 9 my ($self, $n) = @_;
378  
379 9 ▶ if ($self->{autodefer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodefer"}) 4 5 9
380 4 $self->_annotate_ad_history('DELETE');
381 }
382  
383 9 my $lastrec = $self->FETCHSIZE-1;
384 9 my $rec = $self->FETCH($n);
385 9 ▶ $self->_delete_deferred($n) if $self->_is_deferring;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_deferring 4 5 9
386 9 ▶ if ($n == $lastrec) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n == $lastrec) { } 4 5 9
elsif ($n < $lastrec) { } 4 1 5
387 4 $self->_seek($n);
388 4 $self->_chop_file;
389 4 $#{$self->{offsets}}--;
390 4 $self->{cache}->remove($n);
391 # perhaps in this case I should also remove trailing null records?
392 # 20020316
393 # Note that delete @a[-3..-1] deletes the records in the wrong order,
394 # so we only chop the very last one out of the file. We could repair this
395 # by tracking deleted records inside the object.
396 } elsif ($n < $lastrec) {
397 4 $self->STORE($n, "");
398 }
399 9 $rec;
400 }
401  
402 11 ▶sub EXISTS {
Subroutine EXISTS: called CC=2 SCAR=6.9
403 11 my ($self, $n) = @_;
404 11 ▶ return 1 if exists $self->{deferred}{$n};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"deferred"}{$n} 1 10 11
405 10 $n < $self->FETCHSIZE;
406 }
407  
408 393 ▶sub SPLICE {
Subroutine SPLICE: called CC=4 SCAR=13.9
409 393 my $self = shift;
410  
411 393 ▶ if ($self->{autodefer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodefer"}) 339 54 393
412 339 $self->_annotate_ad_history('SPLICE');
413 }
414  
415 393 ▶ $self->_flush if $self->_is_deferring; # move this up?
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_deferring 1 392 393
416 393 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 145 248 393
417 145 $self->_chomp(my @a = $self->_splice(@_));
418 145 @a;
419 } else {
420 248 $self->_chomp1(scalar $self->_splice(@_));
421 }
422 }
423  
424 2986 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=16.2
425 2986 my $self = shift;
426 2986 ▶ $self->flush if $self->_is_deferring;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_deferring 2 2984 2986
427 2986 ▶ $self->{cache}->delink if defined $self->{cache}; # break circular link
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"cache"} 2986 0 2986
428 2986 ▶ if ($self->{fh} and $self->{ourfh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} and $self->{"ourfh"}) 2985 1 2986
Condition66%2/3
$self->{"fh"} and $self->{"ourfh"}
!ll&&!rl&&r
012985
MC/DC50%1/2
$self->{"fh"} and $self->{"ourfh"}
$self->{"fh"} $self->{"ourfh"}
Truth table66%2/3
A and B
A= $self->{"fh"}B= $self->{"ourfh"}
ABresult
0X0
100
111
429 2985 delete $self->{ourfh};
430 2985 close delete $self->{fh};
431 }
432 }
433  
434 393 ▶sub _splice {
Subroutine _splice: called CC=18 SCAR=28.9
435 393 my ($self, $pos, $nrecs, @data) = @_;
436 393 my @result;
437  
438 393 ▶ $pos = 0 unless defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 7 386 393
439  
440 # Deal with negative and other out-of-range positions
441 # Also set default for $nrecs
442 {
443 393 my $oldsize = $self->FETCHSIZE;
444 393 ▶ $nrecs = $oldsize unless defined $nrecs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nrecs 15 378 393
445 393 my $oldpos = $pos;
446  
447 393 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 73 320 393
448 73 $pos += $oldsize;
449 73 ▶ if ($pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 2 71 73
450 2 croak "Modification of non-creatable array value attempted, " .
451 "subscript $oldpos";
452 }
453 }
454  
455 391 ▶ if ($pos > $oldsize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos > $oldsize) 14 377 391
456 14 ▶ return unless @data;
Branch: 1 branch
BranchTrueFalseTotal
unless @data 6 8 14
457 8 $pos = $oldsize; # This is what perl does for normal arrays
458 }
459  
460 # The manual is very unclear here
461 385 ▶ if ($nrecs < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nrecs < 0) 10 375 385
462 10 $nrecs = $oldsize - $pos + $nrecs;
463 10 ▶ $nrecs = 0 if $nrecs < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $nrecs < 0 4 6 10
464 }
465  
466 # nrecs is too big---it really means "until the end"
467 # 20030507
468 385 ▶ if ($nrecs + $pos > $oldsize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nrecs + $pos > $oldsize) 33 352 385
469 33 $nrecs = $oldsize - $pos;
470 }
471 }
472  
473 385 $self->_fixrecs(@data);
474 385 my $data = join '', @data;
475 385 my $datalen = length $data;
476 385 my $oldlen = 0;
477  
478 # compute length of data being removed
479 385 for ($pos .. $pos+$nrecs-1) {
480 512 ▶ last unless defined $self->_fill_offsets_to($_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_fill_offsets_to($_) 0 512 512
481 512 my $rec = $self->_fetch($_);
482 512 ▶ last unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 0 512 512
483 512 push @result, $rec;
484  
485 # Why don't we just use length($rec) here?
486 # Because that record might have come from the cache. _splice
487 # might have been called to flush out the deferred-write records,
488 # and in this case length($rec) is the length of the record to be
489 # *written*, not the length of the actual record in the file. But
490 # the offsets are still true. 20020322
491 $oldlen += $self->{offsets}[$_+1] - $self->{offsets}[$_]
492 512 ▶ if defined $self->{offsets}[$_+1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"offsets"}[$_ + 1] 512 0 512
493 }
494 385 $self->_fill_offsets_to($pos+$nrecs);
495  
496 # Modify the file
497 385 $self->_mtwrite($data, $self->{offsets}[$pos], $oldlen);
498 # Adjust the offsets table
499 385 $self->_oadjust([$pos, $nrecs, @data]);
500  
501 { # Take this read cache stuff out into a separate function
502 # You made a half-attempt to put it into _oadjust.
503 # Finish something like that up eventually.
504 # STORE also needs to do something similarish
505  
506 # update the read cache, part 1
507 # modified records
508 385 for ($pos .. $pos+$nrecs-1) {
509 512 my $new = $data[$_-$pos];
510 512 ▶ if (defined $new) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new) { } 194 318 512
511 194 $self->{cache}->update($_, $new);
512 } else {
513 318 $self->{cache}->remove($_);
514 }
515 }
516
517 # update the read cache, part 2
518 # moved records - records past the site of the change
519 # need to be renumbered
520 # Maybe merge this with the previous block?
521 {
522 385 my @oldkeys = grep $_ >= $pos + $nrecs, $self->{cache}->ckeys;
523 385 my @newkeys = map $_-$nrecs+@data, @oldkeys;
524 385 $self->{cache}->rekey(\@oldkeys, \@newkeys);
525 }
526  
527 # Now there might be too much data in the cache, if we spliced out
528 # some short records and spliced in some long ones. If so, flush
529 # the cache.
530 385 $self->_cache_flush;
531 }
532  
533 # Yes, the return value of 'splice' *is* actually this complicated
534 385 ▶ wantarray ? @result : @result ? $result[-1] : undef;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 144 241 385
@result ? : 147 94 241
535 }
536  
537  
538 # write data into the file
539 # $data is the data to be written.
540 # it should be written at position $pos, and should overwrite
541 # exactly $len of the following bytes.
542 # Note that if length($data) > $len, the subsequent bytes will have to
543 # be moved up, and if length($data) < $len, they will have to
544 # be moved down
545 179 ▶sub _twrite {
Subroutine _twrite: called CC=5 SCAR=16.1
546 179 my ($self, $data, $pos, $len) = @_;
547  
548 179 ▶ unless (defined $pos) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pos) 0 179 179
549 0 die "\$pos was undefined in _twrite";
550 }
551  
552 179 my $len_diff = length($data) - $len;
553  
554 179 ▶ if ($len_diff == 0) { # Woo-hoo!
Branch: 1 branch
BranchTrueFalseTotal
if ($len_diff == 0) 30 149 179
555 30 my $fh = $self->{fh};
556 30 $self->_seekb($pos);
557 30 $self->_write_record($data);
558 30 return; # well, that was easy.
559 }
560  
561 # the two records are of different lengths
562 # our strategy here: rewrite the tail of the file,
563 # reading ahead one buffer at a time
564 # $bufsize is required to be at least as large as the data we're overwriting
565 149 my $bufsize = _bufsize($len_diff);
566 149 my ($writepos, $readpos) = ($pos, $pos+$len);
567 149 my $next_block;
568 my $more_data;
569  
570 # Seems like there ought to be a way to avoid the repeated code
571 # and the special case here. The read(1) is also a little weird.
572 # Think about this.
573 149 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$more_data and do { do { $self->_seekb($readpos); my $br = read($self->{"fh"}, $next_block, $bufsize); $more_data = read($self->{"fh"}, my $dummy, 1); $self->_seekb($writepos); $self->_write_record($data); $readpos += $br; $writepos += length $data; $data = $next_block } } 227 149 376
574 376 $self->_seekb($readpos);
575 376 my $br = read $self->{fh}, $next_block, $bufsize;
576 376 $more_data = read $self->{fh}, my($dummy), 1;
577 376 $self->_seekb($writepos);
578 376 $self->_write_record($data);
579 376 $readpos += $br;
580 376 $writepos += length $data;
581 376 $data = $next_block;
582 } while $more_data;
583 149 $self->_seekb($writepos);
584 149 $self->_write_record($next_block);
585  
586 # There might be leftover data at the end of the file
587 149 ▶ $self->_chop_file if $len_diff < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len_diff < 0 71 78 149
588 }
589  
590 # _iwrite(D, S, E)
591 # Insert text D at position S.
592 # Let C = E-S-|D|. If C < 0; die.
593 # Data in [S,S+C) is copied to [S+D,S+D+C) = [S+D,E).
594 # Data in [S+C = E-D, E) is returned. Data in [E, oo) is untouched.
595 #
596 # In a later version, don't read the entire intervening area into
597 # memory at once; do the copying block by block.
598 101 ▶sub _iwrite {
Subroutine _iwrite: called CC=3 SCAR=11.0
599 101 my $self = shift;
600 101 my ($D, $s, $e) = @_;
601 101 my $d = length $D;
602 101 my $c = $e-$s-$d;
603 101 local *FH = $self->{fh};
604 101 ▶ confess "Not enough space to insert $d bytes between $s and $e"
Branch: 1 branch
BranchTrueFalseTotal
if $c < 0 0 101 101
605 if $c < 0;
606 101 ▶ confess "[$s,$e) is an invalid insertion range" if $e < $s;
Branch: 1 branch
BranchTrueFalseTotal
if $e < $s 0 101 101
607  
608 101 $self->_seekb($s);
609 101 read FH, my $buf, $e-$s;
610  
611 101 $D .= substr($buf, 0, $c, "");
612  
613 101 $self->_seekb($s);
614 101 $self->_write_record($D);
615  
616 101 return $buf;
617 }
618  
619 # Like _twrite, but the data-pos-len triple may be repeated; you may
620 # write several chunks. All the writing will be done in
621 # one pass. Chunks SHALL be in ascending order and SHALL NOT overlap.
622 2959 ▶sub _mtwrite {
Subroutine _mtwrite: called CC=8 SCAR=20.8
623 2959 my $self = shift;
624 2959 my $unwritten = "";
625 2959 my $delta = 0;
626  
627 2959 ▶ @_ % 3 == 0
Branch: 1 branch
BranchTrueFalseTotal
@_ % 3 == 0 or die "Arguments to _mtwrite did not come in groups of three" 2959 0 2959
628 or die "Arguments to _mtwrite did not come in groups of three";
629  
630 2959 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5143 2959 8102
631 5143 my ($data, $pos, $len) = splice @_, 0, 3;
632 5143 my $end = $pos + $len; # The OLD end of the segment to be replaced
633 5143 $data = $unwritten . $data;
634 5143 $delta -= length($unwritten);
635 5143 $unwritten = "";
636 5143 $pos += $delta; # This is where the data goes now
637 5143 my $dlen = length $data;
638 5143 $self->_seekb($pos);
639 5143 ▶ if ($len >= $dlen) { # the data will fit
Branch: 1 branch
BranchTrueFalseTotal
if ($len >= $dlen) { } 4283 860 5143
640 4283 $self->_write_record($data);
641 4283 $delta += ($dlen - $len); # everything following moves down by this much
642 4283 $data = ""; # All the data in the buffer has been written
643 } else { # won't fit
644 860 my $writable = substr($data, 0, $len - $delta, "");
645 860 $self->_write_record($writable);
646 860 $delta += ($dlen - $len); # everything following moves down by this much
647 }
648  
649 # At this point we've written some but maybe not all of the data.
650 # There might be a gap to close up, or $data might still contain a
651 # bunch of unwritten data that didn't fit.
652 5143 my $ndlen = length $data;
653 5143 ▶ if ($delta == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($delta == 0) { } 926 4217 5143
elsif ($delta < 0) { } 3370 847 4217
654 926 $self->_write_record($data);
655 } elsif ($delta < 0) {
656 # upcopy (close up gap)
657 3370 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1263 2107 3370
658 1263 $self->_upcopy($end, $end + $delta, $_[1] - $end);
659 } else {
660 2107 $self->_upcopy($end, $end + $delta);
661 }
662 } else {
663 # downcopy (insert data that didn't fit; replace this data in memory
664 # with _later_ data that doesn't fit)
665 847 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 346 501 847
666 346 $unwritten = $self->_downcopy($data, $end, $_[1] - $end);
667 } else {
668 # Make the file longer to accommodate the last segment that doesn't
669 501 $unwritten = $self->_downcopy($data, $end);
670 }
671 }
672 }
673 }
674  
675 # Copy block of data of length $len from position $spos to position $dpos
676 # $dpos must be <= $spos
677 #
678 # If $len is undefined, go all the way to the end of the file
679 # and then truncate it ($spos - $dpos bytes will be removed)
680 3424 ▶sub _upcopy {
Subroutine _upcopy: called CC=9 SCAR=22.0
681 3424 my $blocksize = 8192;
682 3424 my ($self, $spos, $dpos, $len) = @_;
683 3424 ▶ if ($dpos > $spos) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dpos > $spos) { } 0 3424 3424
elsif ($dpos == $spos) { } 16 3408 3424
684 0 die "source ($spos) was upstream of destination ($dpos) in _upcopy";
685 } elsif ($dpos == $spos) {
686 16 return;
687 }
688  
689 3408 ▶ while (! defined ($len) || $len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $len or $len > 0) 7965 1287 9252
Condition100%3/3
not defined $len or $len > 0
l!l&&r!l&&!r
610318621287
MC/DC100%2/2
not defined $len or $len > 0
not defined $len $len > 0
Truth table100%3/3
not(A) or B
A= not defined $lenB= $len > 0
ABresult
1X1
011
000
690 7965 ▶ my $readsize = ! defined($len) ? $blocksize
Branch: 2 branches
BranchTrueFalseTotal
!defined($len) ? : 6103 1862 7965
$len > $blocksize ? : 771 1091 1862
691 : $len > $blocksize ? $blocksize
692 : $len;
693
694 7965 my $fh = $self->{fh};
695 7965 $self->_seekb($spos);
696 7965 my $bytes_read = read $fh, my($data), $readsize;
697 7965 $self->_seekb($dpos);
698 7965 ▶ if ($data eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "") 2121 5844 7965
699 2121 $self->_chop_file;
700 2121 last;
701 }
702 5844 $self->_write_record($data);
703 5844 $spos += $bytes_read;
704 5844 $dpos += $bytes_read;
705 5844 ▶ $len -= $bytes_read if defined $len;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len 1862 3982 5844
706 }
707 }
708  
709 # Write $data into a block of length $len at position $pos,
710 # moving everything in the block forwards to make room.
711 # Instead of writing the last length($data) bytes from the block
712 # (because there isn't room for them any longer) return them.
713 #
714 # Undefined $len means 'until the end of the file'
715 1205 ▶sub _downcopy {
Subroutine _downcopy: called CC=10 SCAR=23.0
716 1205 my $blocksize = 8192;
717 1205 my ($self, $data, $pos, $len) = @_;
718 1205 my $fh = $self->{fh};
719  
720 1205 ▶ while (! defined $len || $len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $len or $len > 0) 2348 525 2873
Condition100%3/3
not defined $len or $len > 0
l!l&&r!l&&!r
1480868525
MC/DC100%2/2
not defined $len or $len > 0
not defined $len $len > 0
Truth table100%3/3
not(A) or B
A= not defined $lenB= $len > 0
ABresult
1X1
011
000
721 2348 ▶ my $readsize = ! defined($len) ? $blocksize
Branch: 2 branches
BranchTrueFalseTotal
!defined($len) ? : 1480 868 2348
$len > $blocksize ? : 375 493 868
722 : $len > $blocksize? $blocksize : $len;
723 2348 $self->_seekb($pos);
724 2348 read $fh, my($old), $readsize;
725 2348 my $last_read_was_short = length($old) < $readsize;
726 2348 $data .= $old;
727 2348 my $writable;
728 2348 ▶ if ($last_read_was_short) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_read_was_short) { } 680 1668 2348
729 # If last read was short, then $data now contains the entire rest
730 # of the file, so there's no need to write only one block of it
731 680 $writable = $data;
732 680 $data = "";
733 } else {
734 1668 $writable = substr($data, 0, $readsize, "");
735 }
736 2348 ▶ last if $writable eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $writable eq "" 5 2343 2348
737 2343 $self->_seekb($pos);
738 2343 $self->_write_record($writable);
739 2343 ▶ last if $last_read_was_short && $data eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $last_read_was_short and $data eq "" 675 1668 2343
Condition66%2/3
$last_read_was_short and $data eq ""
!ll&&!rl&&r
16680675
MC/DC50%1/2
$last_read_was_short and $data eq ""
$last_read_was_short $data eq ""
Truth table66%2/3
A and B
A= $last_read_was_shortB= $data eq ""
ABresult
0X0
100
111
740 1668 ▶ $len -= $readsize if defined $len;
Branch: 1 branch
BranchTrueFalseTotal
if defined $len 868 800 1668
741 1668 $pos += $readsize;
742 }
743 1205 return $data;
744 }
745  
746 # Adjust the object data structures following an '_mtwrite'
747 # Arguments are
748 # [$pos, $nrecs, @length] items
749 # indicating that $nrecs records were removed at $recpos (a record offset)
750 # and replaced with records of length @length...
751 # Arguments guarantee that $recpos is strictly increasing.
752 # No return value
753 708 ▶sub _oadjust {
Subroutine _oadjust: called CC=9 SCAR=22.0
754 708 my $self = shift;
755 708 my $delta = 0;
756 708 my $delta_recs = 0;
757 708 my $prev_end = -1;
758  
759 708 for (@_) {
760 710 my ($pos, $nrecs, @data) = @$_;
761 710 $pos += $delta_recs;
762  
763 # Adjust the offsets of the records after the previous batch up
764 # to the first new one of this batch
765 710 for my $i ($prev_end+2 .. $pos - 1) {
766 1910 $self->{offsets}[$i] += $delta;
767 }
768  
769 710 $prev_end = $pos + @data - 1; # last record moved on this pass
770  
771 # Remove the offsets for the removed records;
772 # replace with the offsets for the inserted records
773 710 my @newoff = ($self->{offsets}[$pos] + $delta);
774 710 for my $i (0 .. $#data) {
775 819 my $newlen = length $data[$i];
776 819 push @newoff, $newoff[$i] + $newlen;
777 819 $delta += $newlen;
778 }
779  
780 710 for my $i ($pos .. $pos+$nrecs-1) {
781 868 ▶ last if $i+1 > $#{$self->{offsets}};
Branch: 1 branch
BranchTrueFalseTotal
if $i + 1 > $#{$$self{"offsets"};} 10 858 868
782 858 my $oldlen = $self->{offsets}[$i+1] - $self->{offsets}[$i];
783 858 $delta -= $oldlen;
784 }
785  
786 # replace old offsets with new
787 710 splice @{$self->{offsets}}, $pos, $nrecs+1, @newoff;
788 # What if we just spliced out the end of the offsets table?
789 # shouldn't we clear $self->{eof}? Test for this XXX BUG TODO
790  
791 710 $delta_recs += @data - $nrecs; # net change in total number of records
792 }
793  
794 # The trailing records at the very end of the file
795 708 ▶ if ($delta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta) 567 141 708
796 567 for my $i ($prev_end+2 .. $#{$self->{offsets}}) {
797 1521 $self->{offsets}[$i] += $delta;
798 }
799 }
800  
801 # If we scrubbed out all known offsets, regenerate the trivial table
802 # that knows that the file does indeed start at 0.
803 708 ▶ $self->{offsets}[0] = 0 unless @{$self->{offsets}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"offsets"};} 0 708 708
804 # If the file got longer, the offsets table is no longer complete
805 # $self->{eof} = 0 if $delta_recs > 0;
806  
807 # Now there might be too much data in the cache, if we spliced out
808 # some short records and spliced in some long ones. If so, flush
809 # the cache.
810 708 $self->_cache_flush;
811 }
812  
813 # If a record does not already end with the appropriate terminator
814 # string, append one.
815 756 ▶sub _fixrecs {
Subroutine _fixrecs: called CC=4 SCAR=13.9
816 756 my $self = shift;
817 756 for (@_) {
818 834 ▶ $_ = "" unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 5 829 834
819 $_ .= $self->{recsep}
820 834 ▶ unless substr($_, - $self->{recseplen}) eq $self->{recsep};
Branch: 1 branch
BranchTrueFalseTotal
unless substr($_, -$self->{"recseplen"}) eq $self->{"recsep"} 828 6 834
821 }
822 }
823  
824  
825 ################################################################
826 #
827 # Basic read, write, and seek
828 #
829  
830 # seek to the beginning of record #$n
831 # Assumes that the offsets table is already correctly populated
832 #
833 # Note that $n=-1 has a special meaning here: It means the start of
834 # the last known record; this may or may not be the very last record
835 # in the file, depending on whether the offsets table is fully populated.
836 #
837 1378 ▶sub _seek {
Subroutine _seek: called CC=3 SCAR=11.4
838 1378 my ($self, $n) = @_;
839 1378 my $o = $self->{offsets}[$n];
840 1378 ▶ defined($o)
Branch: 1 branch
BranchTrueFalseTotal
defined $o or confess("logic error: undefined offset for record $n") 1378 0 1378
841 or confess("logic error: undefined offset for record $n");
842 1378 ▶ seek $self->{fh}, $o, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"fh"}, $o, 0 or confess("Couldn't seek filehandle: $!") 1378 0 1378
843 or confess "Couldn't seek filehandle: $!"; # "Should never happen."
844 }
845  
846 # seek to byte $b in the file
847 26928 ▶sub _seekb {
Subroutine _seekb: called CC=2 SCAR=7.2
848 26928 my ($self, $b) = @_;
849 26928 ▶ seek $self->{fh}, $b, SEEK_SET
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"fh"}, $b, 0 or die "Couldn't seek filehandle: $!" 26928 0 26928
850 or die "Couldn't seek filehandle: $!"; # "Should never happen."
851 }
852  
853 # populate the offsets table up to the beginning of record $n
854 # return the offset of record $n
855 960 ▶sub _fill_offsets_to {
Subroutine _fill_offsets_to: called CC=4 SCAR=13.9
856 960 my ($self, $n) = @_;
857  
858 960 ▶ return $self->{offsets}[$n] if $self->{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 942 18 960
859  
860 18 my $fh = $self->{fh};
861 18 local *OFF = $self->{offsets};
862 18 my $rec;
863  
864 18 ▶ until ($#OFF >= $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#Tie::File::OFF >= $n) 69 5 74
865 69 $self->_seek(-1); # tricky -- see comment at _seek
866 69 $rec = $self->_read_record;
867 69 ▶ if (defined $rec) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rec) { } 56 13 69
868 56 push @OFF, int(tell $fh); # Tels says that int() saves memory here
869 } else {
870 13 $self->{eof} = 1;
871 13 return; # It turns out there is no such record
872 }
873 }
874  
875 # we have now read all the records up to record n-1,
876 # so we can return the offset of record n
877 5 $OFF[$n];
878 }
879  
880 24 ▶sub _fill_offsets {
Subroutine _fill_offsets: called CC=2 SCAR=6.9
881 24 my ($self) = @_;
882  
883 24 my $fh = $self->{fh};
884 24 local *OFF = $self->{offsets};
885  
886 24 $self->_seek(-1); # tricky -- see comment at _seek
887  
888 # Tels says that inlining read_record() would make this loop
889 # five times faster. 20030508
890 24 ▶ while ( defined $self->_read_record()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_read_record) 214 24 238
891 # int() saves us memory here
892 214 push @OFF, int(tell $fh);
893 }
894  
895 24 $self->{eof} = 1;
896 24 $#OFF;
897 }
898  
899 # assumes that $rec is already suitably terminated
900 15140 ▶sub _write_record {
Subroutine _write_record: called CC=2 SCAR=7.0
901 15140 my ($self, $rec) = @_;
902 15140 my $fh = $self->{fh};
903 15140 local $\ = "";
904 15140 ▶ print $fh $rec
Branch: 1 branch
BranchTrueFalseTotal
print $fh $rec or die "Couldn't write record: $!" 15140 0 15140
905 or die "Couldn't write record: $!"; # "Should never happen."
906 # $self->{_written} += length($rec);
907 }
908  
909 1496 ▶sub _read_record {
Subroutine _read_record: called CC=4 SCAR=13.9
910 1496 my $self = shift;
911 1496 my $rec;
912 1496 { local $/ = $self->{recsep};
913 1496 my $fh = $self->{fh};
914 1496 $rec = <$fh>;
915 }
916 1496 ▶ return unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 69 1427 1496
917 1427 ▶ if (substr($rec, -$self->{recseplen}) ne $self->{recsep}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rec, -$self->{"recseplen"}) ne $self->{"recsep"}) 7 1420 1427
918 # improperly terminated final record --- quietly fix it.
919 # my $ac = substr($rec, -$self->{recseplen});
920 # $ac =~ s/\n/\\n/g;
921 7 $self->{sawlastrec} = 1;
922 7 ▶ unless ($self->{rdonly}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"rdonly"}) 4 3 7
923 4 local $\ = "";
924 4 my $fh = $self->{fh};
925 4 print $fh $self->{recsep};
926 }
927 7 $rec .= $self->{recsep};
928 }
929 # $self->{_read} += length($rec) if defined $rec;
930 1427 $rec;
931 }
932  
933 0 ▶sub _rw_stats {
Subroutine _rw_stats: not called CC=1 SCAR=6.9
934 0 my $self = shift;
935 0 @{$self}{'_read', '_written'};
936 }
937  
938 ################################################################
939 #
940 # Read cache management
941  
942 1095 ▶sub _cache_flush {
Subroutine _cache_flush: called CC=1 SCAR=0.0
943 1095 my ($self) = @_;
944 1095 $self->{cache}->reduce_size_to($self->{memory} - $self->{deferred_s});
945 }
946  
947 72 ▶sub _cache_too_full {
Subroutine _cache_too_full: called CC=1 SCAR=0.0
948 72 my $self = shift;
949 72 $self->{cache}->bytes + $self->{deferred_s} >= $self->{memory};
950 }
951  
952 ################################################################
953 #
954 # File custodial services
955 #
956  
957  
958 # We have read to the end of the file and have the offsets table
959 # entirely populated. Now we need to write a new record beyond
960 # the end of the file. We prepare for this by writing
961 # empty records into the file up to the position we want
962 #
963 # assumes that the offsets table already contains the offset of record $n,
964 # if it exists, and extends to the end of the file if not.
965 81 ▶sub _extend_file_to {
Subroutine _extend_file_to: called CC=2 SCAR=6.9
966 81 my ($self, $n) = @_;
967 81 $self->_seek(-1); # position after the end of the last record
968 81 my $pos = $self->{offsets}[-1];
969  
970 # the offsets table has one entry more than the total number of records
971 81 my $extras = $n - $#{$self->{offsets}};
972  
973 # Todo : just use $self->{recsep} x $extras here?
974 81 ▶ while ($extras-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extras-- > 0) 228 81 309
975 228 $self->_write_record($self->{recsep});
976 228 push @{$self->{offsets}}, int(tell $self->{fh});
977 }
978 }
979  
980 # Truncate the file at the current position
981 2238 ▶sub _chop_file {
Subroutine _chop_file: called CC=1 SCAR=0.0
982 2238 my $self = shift;
983 2238 truncate $self->{fh}, tell($self->{fh});
984 }
985  
986  
987 # compute the size of a buffer suitable for moving
988 # all the data in a file forward $n bytes
989 # ($n may be negative)
990 # The result should be at least $n.
991 149 ▶sub _bufsize {
Subroutine _bufsize: called CC=3 SCAR=11.0
992 149 my $n = shift;
993 149 ▶ return 8192 if $n <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n <= 0 71 78 149
994 78 my $b = $n & ~8191;
995 78 ▶ $b += 8192 if $n & 8191;
Branch: 1 branch
BranchTrueFalseTotal
if $n & 8191 56 22 78
996 78 $b;
997 }
998  
999 ################################################################
1000 #
1001 # Miscellaneous public methods
1002 #
1003  
1004 # Lock the file
1005 2 ▶sub flock {
Subroutine flock: called CC=5 SCAR=16.5
Pod documented
1006 2 my ($self, $op) = @_;
1007 2 ▶ unless (@_ <= 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ <= 3) 0 2 2
1008 0 my $pack = ref $self;
1009 0 croak "Usage: $pack\->flock([OPERATION])";
1010 }
1011 2 my $fh = $self->{fh};
1012 2 ▶ $op = LOCK_EX unless defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 1 1 2
1013 2 my $locked = flock $fh, $op;
1014  
1015 2 ▶ if ($locked && ($op & (LOCK_EX | LOCK_SH))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locked and $op & 3) 1 1 2
Condition66%2/3
$locked and $op & 3
!ll&&!rl&&r
011
MC/DC50%1/2
$locked and $op & 3
$locked $op & 3
Truth table66%2/3
A and B
A= $lockedB= $op & 3
ABresult
0X0
100
111
1016 # If you're locking the file, then presumably it's because
1017 # there might have been a write access by another process.
1018 # In that case, the read cache contents and the offsets table
1019 # might be invalid, so discard them. 20030508
1020 1 $self->{offsets} = [0];
1021 1 $self->{cache}->empty;
1022 }
1023  
1024 2 $locked;
1025 }
1026  
1027 # Get/set autochomp option
1028 4 ▶sub autochomp {
Subroutine autochomp: called CC=2 SCAR=6.9
Pod documented
1029 4 my $self = shift;
1030 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 2 4
1031 2 my $old = $self->{autochomp};
1032 2 $self->{autochomp} = shift;
1033 2 $old;
1034 } else {
1035 2 $self->{autochomp};
1036 }
1037 }
1038  
1039 # Get offset table entries; returns offset of nth record
1040 22 ▶sub offset {
Subroutine offset: called CC=4 SCAR=13.9
Pod documented
1041 22 my ($self, $n) = @_;
1042  
1043 22 ▶ if ($#{$self->{offsets}} < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$$self{"offsets"};} < $n) 4 18 22
1044 4 ▶ return if $self->{eof}; # request for record beyond the end of file
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"eof"} 3 1 4
1045 1 my $o = $self->_fill_offsets_to($n);
1046 # If it's still undefined, there is no such record, so return 'undef'
1047 1 ▶ return unless defined $o;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o 1 0 1
1048 }
1049  
1050 18 $self->{offsets}[$n];
1051 }
1052  
1053 0 ▶sub discard_offsets {
Subroutine discard_offsets: not called CC=1 SCAR=6.9
Pod undocumented
1054 0 my $self = shift;
1055 0 $self->{offsets} = [0];
1056 }
1057  
1058 ################################################################
1059 #
1060 # Matters related to deferred writing
1061 #
1062  
1063 # Defer writes
1064 22 ▶sub defer {
Subroutine defer: called CC=1 SCAR=0.0
Pod documented
1065 22 my $self = shift;
1066 22 $self->_stop_autodeferring;
1067 22 @{$self->{ad_history}} = ();
1068 22 $self->{defer} = 1;
1069 }
1070  
1071 # Flush deferred writes
1072 #
1073 # This could be better optimized to write the file in one pass, instead
1074 # of one pass per block of records. But that will require modifications
1075 # to _twrite, so I should have a good _twrite test suite first.
1076 16 ▶sub flush {
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
1077 16 my $self = shift;
1078  
1079 16 $self->_flush;
1080 16 $self->{defer} = 0;
1081 }
1082  
1083 0 ▶sub _old_flush {
Subroutine _old_flush: not called CC=4 SCAR=30.0
1084 0 my $self = shift;
1085 0 my @writable = sort {$a<=>$b} (keys %{$self->{deferred}});
1086  
1087 0 ▶ while (@writable) {
Branch: 1 branch
BranchTrueFalseTotal
if (@writable) 0 0 0
1088 # gather all consecutive records from the front of @writable
1089 0 my $first_rec = shift @writable;
1090 0 my $last_rec = $first_rec+1;
1091 0 ▶ ++$last_rec, shift @writable while @writable && $last_rec == $writable[0];
Branch: 1 branch
BranchTrueFalseTotal
@writable and $last_rec == $writable[0] and do { ++$last_rec, shift @writable } 0 0 0
Condition0%0/3
@writable and $last_rec == $writable[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@writable and $last_rec == $writable[0]
@writable $last_rec == $writable[0]
Truth table0%0/3
A and B
A= @writableB= $last_rec == $writable[0]
ABresult
0X0
100
111
1092 0 --$last_rec;
1093 0 $self->_fill_offsets_to($last_rec);
1094 0 $self->_extend_file_to($last_rec);
1095 $self->_splice($first_rec, $last_rec-$first_rec+1,
1096 0 @{$self->{deferred}}{$first_rec .. $last_rec});
1097 }
1098  
1099 0 $self->_discard; # clear out defered-write-cache
1100 }
1101  
1102 23 ▶sub _flush {
Subroutine _flush: called CC=5 SCAR=16.1
1103 23 my $self = shift;
1104 23 my @writable = sort {$a<=>$b} (keys %{$self->{deferred}});
1105 23 my @args;
1106 my @adjust;
1107  
1108 23 ▶ while (@writable) {
Branch: 1 branch
BranchTrueFalseTotal
if (@writable) 25 23 48
1109 # gather all consecutive records from the front of @writable
1110 25 my $first_rec = shift @writable;
1111 25 my $last_rec = $first_rec+1;
1112 25 ▶ ++$last_rec, shift @writable while @writable && $last_rec == $writable[0];
Branch: 1 branch
BranchTrueFalseTotal
@writable and $last_rec == $writable[0] and do { ++$last_rec, shift @writable } 31 25 56
Condition100%3/3
@writable and $last_rec == $writable[0]
!ll&&!rl&&r
20531
MC/DC100%2/2
@writable and $last_rec == $writable[0]
@writable $last_rec == $writable[0]
Truth table100%3/3
A and B
A= @writableB= $last_rec == $writable[0]
ABresult
0X0
100
111
1113 25 --$last_rec;
1114 25 my $end = $self->_fill_offsets_to($last_rec+1);
1115 25 ▶ if (not defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $end) 10 15 25
1116 10 $self->_extend_file_to($last_rec);
1117 10 $end = $self->{offsets}[$last_rec];
1118 }
1119 25 my ($start) = $self->{offsets}[$first_rec];
1120 push @args,
1121 25 join("", @{$self->{deferred}}{$first_rec .. $last_rec}), # data
1122 $start, # position
1123 $end-$start; # length
1124 push @adjust, [$first_rec, # starting at this position...
1125 $last_rec-$first_rec+1, # this many records...
1126 # are replaced with these...
1127 25 @{$self->{deferred}}{$first_rec .. $last_rec},
1128 ];
1129 }
1130  
1131 23 $self->_mtwrite(@args); # write multiple record groups
1132 23 $self->_discard; # clear out defered-write-cache
1133 23 $self->_oadjust(@adjust);
1134 }
1135  
1136 # Discard deferred writes and disable future deferred writes
1137 6 ▶sub discard {
Subroutine discard: called CC=1 SCAR=0.0
Pod documented
1138 6 my $self = shift;
1139 6 $self->_discard;
1140 6 $self->{defer} = 0;
1141 }
1142  
1143 # Discard deferred writes, but retain old deferred writing mode
1144 29 ▶sub _discard {
Subroutine _discard: called CC=1 SCAR=0.0
1145 29 my $self = shift;
1146 29 %{$self->{deferred}} = ();
1147 29 $self->{deferred_s} = 0;
1148 29 $self->{deferred_max} = -1;
1149 29 $self->{cache}->set_limit($self->{memory});
1150 }
1151  
1152 # Deferred writing is enabled, either explicitly ($self->{defer})
1153 # or automatically ($self->{autodeferring})
1154 4564 ▶sub _is_deferring {
Subroutine _is_deferring: called CC=2 SCAR=6.9
1155 4564 my $self = shift;
1156 4564 ▶ $self->{defer} || $self->{autodeferring};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"defer"} or $self->{"autodeferring"} 147 4417 4564
1157 }
1158  
1159 # The largest record number of any deferred record
1160 592 ▶sub _defer_max {
Subroutine _defer_max: called CC=4 SCAR=13.9
1161 592 my $self = shift;
1162 592 ▶ return $self->{deferred_max} if defined $self->{deferred_max};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"deferred_max"} 591 1 592
1163 1 my $max = -1;
1164 1 for my $key (keys %{$self->{deferred}}) {
1165 1 ▶ $max = $key if $key > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $key > $max 1 0 1
1166 }
1167 1 $self->{deferred_max} = $max;
1168 1 $max;
1169 }
1170  
1171 ################################################################
1172 #
1173 # Matters related to autodeferment
1174 #
1175  
1176 # Get/set autodefer option
1177 2 ▶sub autodefer {
Subroutine autodefer: called CC=3 SCAR=11.1
Pod documented
1178 2 my $self = shift;
1179 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 0 2
1180 2 my $old = $self->{autodefer};
1181 2 $self->{autodefer} = shift;
1182 2 ▶ if ($old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old) 1 1 2
1183 1 $self->_stop_autodeferring;
1184 1 @{$self->{ad_history}} = ();
1185 }
1186 2 $old;
1187 } else {
1188 0 $self->{autodefer};
1189 }
1190 }
1191  
1192 # The user is trying to store record #$n Record that in the history,
1193 # and then enable (or disable) autodeferment if that seems useful.
1194 # Note that it's OK for $n to be a non-number, as long as the function
1195 # is prepared to deal with that. Nobody else looks at the ad_history.
1196 #
1197 # Now, what does the ad_history mean, and what is this function doing?
1198 # Essentially, the idea is to enable autodeferring when we see that the
1199 # user has made three consecutive STORE calls to three consecutive records.
1200 # ("Three" is actually ->{autodefer_threshhold}.)
1201 # A STORE call for record #$n inserts $n into the autodefer history,
1202 # and if the history contains three consecutive records, we enable
1203 # autodeferment. An ad_history of [X, Y] means that the most recent
1204 # STOREs were for records X, X+1, ..., Y, in that order.
1205 #
1206 # Inserting a nonconsecutive number erases the history and starts over.
1207 #
1208 # Performing a special operation like SPLICE erases the history.
1209 #
1210 # There's one special case: CLEAR means that CLEAR was just called.
1211 # In this case, we prime the history with [-2, -1] so that if the next
1212 # write is for record 0, autodeferring goes on immediately. This is for
1213 # the common special case of "@a = (...)".
1214 #
1215 611 ▶sub _annotate_ad_history {
Subroutine _annotate_ad_history: called CC=9 SCAR=22.1
1216 611 my ($self, $n) = @_;
1217 611 ▶ return unless $self->{autodefer}; # feature is disabled
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"autodefer"} 0 611 611
1218 611 ▶ return if $self->{defer}; # already in explicit defer mode
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"defer"} 66 545 611
1219 545 ▶ return unless $self->{offsets}[-1] >= $self->{autodefer_filelen_threshhold};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"offsets"}[-1] >= $self->{"autodefer_filelen_threshhold"} 520 25 545
1220  
1221 25 local *H = $self->{ad_history};
1222 25 ▶ if ($n eq 'CLEAR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($n eq 'CLEAR') { } 2 23 25
elsif ($n =~ /^\d+$/) { } 23 0 23
1223 2 @H = (-2, -1); # prime the history with fake records
1224 2 $self->_stop_autodeferring;
1225 } elsif ($n =~ /^\d+$/) {
1226 23 ▶ if (@H == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Tie::File::H == 0) { } 1 22 23
1227 1 @H = ($n, $n);
1228 } else { # @H == 2
1229 22 ▶ if ($H[1] == $n-1) { # another consecutive record
Branch: 1 branch
BranchTrueFalseTotal
if ($Tie::File::H[1] == $n - 1) { } 19 3 22
1230 19 $H[1]++;
1231 19 ▶ if ($H[1] - $H[0] + 1 >= $self->{autodefer_threshhold}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Tie::File::H[1] - $Tie::File::H[0] + 1 >= $self->{"autodefer_threshhold"}) 16 3 19
1232 16 $self->{autodeferring} = 1;
1233 }
1234 } else { # nonconsecutive- erase and start over
1235 3 @H = ($n, $n);
1236 3 $self->_stop_autodeferring;
1237 }
1238 }
1239 } else { # SPLICE or STORESIZE or some such
1240 0 @H = ();
1241 0 $self->_stop_autodeferring;
1242 }
1243 }
1244  
1245 # If autodeferring was enabled, cut it out and discard the history
1246 28 ▶sub _stop_autodeferring {
Subroutine _stop_autodeferring: called CC=2 SCAR=6.9
1247 28 my $self = shift;
1248 28 ▶ if ($self->{autodeferring}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodeferring"}) 5 23 28
1249 5 $self->_flush;
1250 }
1251 28 $self->{autodeferring} = 0;
1252 }
1253  
1254 ################################################################
1255  
1256  
1257 # This is NOT a method. It is here for two reasons:
1258 # 1. To factor a fairly complicated block out of the constructor
1259 # 2. To provide access for the test suite, which need to be sure
1260 # files are being written properly.
1261 2998 ▶sub _default_recsep {
Subroutine _default_recsep: called CC=2 SCAR=7.6
1262 2998 my $recsep = $/;
1263 2998 ▶ if ($^O eq 'MSWin32') { # Dos too?
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 2998 2998
1264 # Windows users expect files to be terminated with \r\n
1265 # But $/ is set to \n instead
1266 # Note that this also transforms \n\n into \r\n\r\n.
1267 # That is a feature.
1268 0 $recsep =~ s/\n/\r\n/g;
1269 }
1270 2998 $recsep;
1271 }
1272  
1273 # Utility function for _check_integrity
1274 0 ▶sub _ci_warn {
Subroutine _ci_warn: not called CC=1 SCAR=6.9
1275 0 my $msg = shift;
1276 0 $msg =~ s/\n/\\n/g;
1277 0 $msg =~ s/\r/\\r/g;
1278 0 print "# $msg\n";
1279 }
1280  
1281 # Given a file, make sure the cache is consistent with the
1282 # file contents and the internal data structures are consistent with
1283 # each other. Returns true if everything checks out, false if not
1284 #
1285 # The $file argument is no longer used. It is retained for compatibility
1286 # with the existing test suite.
1287 380 ▶sub _check_integrity {
Subroutine _check_integrity: called CC=37 SCAR=52.2
1288 380 my ($self, $file, $warn) = @_;
1289 380 my $rsl = $self->{recseplen};
1290 380 my $rs = $self->{recsep};
1291 380 my $good = 1;
1292 380 local *_; # local $_ does not work here
1293 380 local $DIAGNOSTIC = 1;
1294  
1295 380 ▶ if (not defined $rs) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $rs) { } 0 380 380
elsif ($rs eq '') { } 0 380 380
elsif ($rsl != length $rs) { } 0 380 380
1296 0 _ci_warn("recsep is undef!");
1297 0 $good = 0;
1298 } elsif ($rs eq "") {
1299 0 _ci_warn("recsep is empty!");
1300 0 $good = 0;
1301 } elsif ($rsl != length $rs) {
1302 0 my $ln = length $rs;
1303 0 _ci_warn("recsep <$rs> has length $ln, should be $rsl");
1304 0 $good = 0;
1305 }
1306  
1307 380 ▶ if (not defined $self->{offsets}[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'offsets'}[0]) { } 0 380 380
elsif ($self->{'offsets'}[0] != 0) { } 0 380 380
1308 0 _ci_warn("offset 0 is missing!");
1309 0 $good = 0;
1310  
1311 } elsif ($self->{offsets}[0] != 0) {
1312 0 _ci_warn("rec 0: offset <$self->{offsets}[0]> s/b 0!");
1313 0 $good = 0;
1314 }
1315  
1316 380 my $cached = 0;
1317 {
1318 380 local *F = $self->{fh};
1319 380 seek F, 0, SEEK_SET;
1320 380 local $. = 0;
1321 380 local $/ = $rs;
1322  
1323 380 ▶ while (<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 2132 380 2512
1324 2132 my $n = $. - 1;
1325 2132 my $cached = $self->{cache}->_produce($n);
1326 2132 my $offset = $self->{offsets}[$.];
1327 2132 my $ao = tell F;
1328 2132 ▶ if (defined $offset && $offset != $ao) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset and $offset != $ao) 0 2132 2132
Condition66%2/3
defined $offset and $offset != $ao
!ll&&!rl&&r
1221200
MC/DC0%0/2
defined $offset and $offset != $ao
defined $offset $offset != $ao
Truth table66%2/3
A and B
A= defined $offsetB= $offset != $ao
ABresult
0X0
100
111
1329 0 _ci_warn("rec $n: offset <$offset> actual <$ao>");
1330 0 $good = 0;
1331 }
1332 2132 ▶ if (defined $cached && $_ ne $cached && ! $self->{deferred}{$n}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cached and $_ ne $cached and not $self->{"deferred"}{$n}) 0 2132 2132
Condition50%3/6
defined $cached and $_ ne $cached and not $self->{"deferred"}{$n}
!ll&&!rl&&r
213200
defined $cached and $_ ne $cached
!ll&&!rl&&r
11949380
MC/DC0%0/3
defined $cached and $_ ne $cached and not $self->{"deferred"}{$n}
defined $cached $_ ne $cached not $self->{"deferred"}{$n}
Truth table50%2/4
A and B and not(C)
A= defined $cachedB= $_ ne $cachedC= not $self->{"deferred"}{$n}
ABCresult
0XX0
10X0
1100
1111
1333 0 $good = 0;
1334 0 _ci_warn("rec $n: cached <$cached> actual <$_>");
1335 }
1336 2132 ▶ if (defined $cached && substr($cached, -$rsl) ne $rs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cached and substr($cached, -$rsl) ne $rs) 0 2132 2132
Condition66%2/3
defined $cached and substr($cached, -$rsl) ne $rs
!ll&&!rl&&r
11949380
MC/DC0%0/2
defined $cached and substr($cached, -$rsl) ne $rs
defined $cached substr($cached, -$rsl) ne $rs
Truth table66%2/3
A and B
A= defined $cachedB= substr($cached, -$rsl) ne $rs
ABresult
0X0
100
111
1337 0 $good = 0;
1338 0 _ci_warn("rec $n in the cache is missing the record separator");
1339 }
1340 2132 ▶ if (! defined $offset && $self->{eof}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $offset and $self->{"eof"}) 0 2132 2132
Condition66%2/3
not defined $offset and $self->{"eof"}
!ll&&!rl&&r
2120120
MC/DC0%0/2
not defined $offset and $self->{"eof"}
not defined $offset $self->{"eof"}
Truth table66%2/3
not(A) and B
A= not defined $offsetB= $self->{"eof"}
ABresult
0X0
100
111
1341 0 $good = 0;
1342 0 _ci_warn("The offset table was marked complete, but it is missing " .
1343 "element $.");
1344 }
1345 }
1346 380 ▶ if (@{$self->{offsets}} > $.+1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"offsets"};} > $. + 1) 0 380 380
1347 0 $good = 0;
1348 0 my $n = @{$self->{offsets}};
1349 0 _ci_warn("The offset table has $n items, but the file has only $.");
1350 }
1351  
1352 380 my $deferring = $self->_is_deferring;
1353 380 for my $n ($self->{cache}->ckeys) {
1354 938 my $r = $self->{cache}->_produce($n);
1355 938 $cached += length($r);
1356 938 ▶ next if $n+1 <= $.; # checked this already
Branch: 1 branch
BranchTrueFalseTotal
if $n + 1 <= $. 938 0 938
1357 0 _ci_warn("spurious caching of record $n");
1358 0 $good = 0;
1359 }
1360 380 my $b = $self->{cache}->bytes;
1361 380 ▶ if ($cached != $b) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached != $b) 0 380 380
1362 0 _ci_warn("cache size is $b, should be $cached");
1363 0 $good = 0;
1364 }
1365 }
1366  
1367 # That cache has its own set of tests
1368 380 ▶ $good = 0 unless $self->{cache}->_check_integrity;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"cache"}->_check_integrity 0 380 380
1369  
1370 # Now let's check the deferbuffer
1371 # Unless deferred writing is enabled, it should be empty
1372 380 ▶ if (! $self->_is_deferring && %{$self->{deferred}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_is_deferring and %{$self->{"deferred"};}) 0 380 380
Condition66%2/3
not $self->_is_deferring and %{$self->{"deferred"};}
!ll&&!rl&&r
493310
MC/DC0%0/2
not $self->_is_deferring and %{$self->{"deferred"};}
not $self->_is_deferring %{$self->{"deferred"};}
Truth table66%2/3
not(A) and B
A= not $self->_is_deferringB= %{$self->{"deferred"};}
ABresult
0X0
100
111
1373 0 _ci_warn("deferred writing disabled, but deferbuffer nonempty");
1374 0 $good = 0;
1375 }
1376  
1377 # Any record in the deferbuffer should *not* be present in the readcache
1378 380 my $deferred_s = 0;
1379 380 ▶ while (my ($n, $r) = each %{$self->{deferred}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"deferred"};}) 87 380 467
1380 87 $deferred_s += length($r);
1381 87 ▶ if (defined $self->{cache}->_produce($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cache"}->_produce($n)) 0 87 87
1382 0 _ci_warn("record $n is in the deferbuffer *and* the readcache");
1383 0 $good = 0;
1384 }
1385 87 ▶ if (substr($r, -$rsl) ne $rs) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($r, -$rsl) ne $rs) 0 87 87
1386 0 _ci_warn("rec $n in the deferbuffer is missing the record separator");
1387 0 $good = 0;
1388 }
1389 }
1390  
1391 # Total size of deferbuffer should match internal total
1392 380 ▶ if ($deferred_s != $self->{deferred_s}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deferred_s != $self->{"deferred_s"}) 0 380 380
1393 0 _ci_warn("buffer size is $self->{deferred_s}, should be $deferred_s");
1394 0 $good = 0;
1395 }
1396  
1397 # Total size of deferbuffer should not exceed the specified limit
1398 380 ▶ if ($deferred_s > $self->{dw_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deferred_s > $self->{"dw_size"}) 0 380 380
1399 0 _ci_warn("buffer size is $self->{deferred_s} which exceeds the limit " .
1400 "of $self->{dw_size}");
1401 0 $good = 0;
1402 }
1403  
1404 # Total size of cached data should not exceed the specified limit
1405 380 ▶ if ($deferred_s + $cached > $self->{memory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deferred_s + $cached > $self->{"memory"}) 0 380 380
1406 0 my $total = $deferred_s + $cached;
1407 0 _ci_warn("total stored data size is $total which exceeds the limit " .
1408 "of $self->{memory}");
1409 0 $good = 0;
1410 }
1411  
1412 # Stuff related to autodeferment
1413 380 ▶ if (!$self->{autodefer} && @{$self->{ad_history}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"autodefer"} and @{$self->{"ad_history"};}) 0 380 380
Condition66%2/3
not $self->{"autodefer"} and @{$self->{"ad_history"};}
!ll&&!rl&&r
2511290
MC/DC0%0/2
not $self->{"autodefer"} and @{$self->{"ad_history"};}
not $self->{"autodefer"} @{$self->{"ad_history"};}
Truth table66%2/3
not(A) and B
A= not $self->{"autodefer"}B= @{$self->{"ad_history"};}
ABresult
0X0
100
111
1414 0 _ci_warn("autodefer is disabled, but ad_history is nonempty");
1415 0 $good = 0;
1416 }
1417 380 ▶ if ($self->{autodeferring} && $self->{defer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodeferring"} and $self->{"defer"}) 0 380 380
Condition66%2/3
$self->{"autodeferring"} and $self->{"defer"}
!ll&&!rl&&r
37190
MC/DC0%0/2
$self->{"autodeferring"} and $self->{"defer"}
$self->{"autodeferring"} $self->{"defer"}
Truth table66%2/3
A and B
A= $self->{"autodeferring"}B= $self->{"defer"}
ABresult
0X0
100
111
1418 0 _ci_warn("both autodeferring and explicit deferring are active");
1419 0 $good = 0;
1420 }
1421 380 ▶ if (@{$self->{ad_history}} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{'ad_history'};} == 0) { } 366 14 380
elsif (@{$self->{'ad_history'};} == 2) { } 14 0 14
1422 # That's OK, no additional tests required
1423 14 } elsif (@{$self->{ad_history}} == 2) {
1424 14 my @non_number = grep !/^-?\d+$/, @{$self->{ad_history}};
1425 14 ▶ if (@non_number) {
Branch: 2 branches
BranchTrueFalseTotal
if (@non_number) { } 0 14 14
elsif ($self->{'ad_history'}[1] < $self->{'ad_history'}[0]) { } 0 14 14
1426 0 my $msg;
1427 0 { local $" = ')(';
1428 0 $msg = "ad_history contains non-numbers (@{$self->{ad_history}})";
1429 }
1430 0 _ci_warn($msg);
1431 0 $good = 0;
1432 } elsif ($self->{ad_history}[1] < $self->{ad_history}[0]) {
1433 0 _ci_warn("ad_history has nonsensical values @{$self->{ad_history}}");
1434 0 $good = 0;
1435 }
1436 } else {
1437 0 _ci_warn("ad_history has bad length <@{$self->{ad_history}}>");
1438 0 $good = 0;
1439 }
1440  
1441 380 $good;
1442 }
1443  
1444 ################################################################
1445 #
1446 # Tie::File::Cache
1447 #
1448 # Read cache
1449  
1450 package Tie::File::Cache;
1451 38 $Tie::File::Cache::VERSION = $Tie::File::VERSION;
1452 38 ▶use Carp ':DEFAULT', 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
1453  
1454 sub HEAP () { 0 }
1455 sub HASH () { 1 }
1456 sub MAX () { 2 }
1457 sub BYTES() { 3 }
1458 #sub STAT () { 4 } # Array with request statistics for each record
1459 #sub MISS () { 5 } # Total number of cache misses
1460 #sub REQ () { 6 } # Total number of cache requests
1461 38 ▶use strict 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
1462  
1463 2988 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
1464 2988 my ($pack, $max) = @_;
1465 2988 local *_;
1466 2988 ▶ croak "missing argument to ->new" unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 2988 2988
1467 2988 my $self = [];
1468 2988 bless $self => $pack;
1469 2988 @$self = (Tie::File::Heap->new($self), {}, $max, 0);
1470 2988 $self;
1471 }
1472  
1473 77 ▶sub adj_limit {
Subroutine adj_limit: called CC=1 SCAR=0.0
1474 77 my ($self, $n) = @_;
1475 77 $self->[MAX] += $n;
1476 }
1477  
1478 60 ▶sub set_limit {
Subroutine set_limit: called CC=1 SCAR=0.0
1479 60 my ($self, $n) = @_;
1480 60 $self->[MAX] = $n;
1481 }
1482  
1483 # For internal use only
1484 # Will be called by the heap structure to notify us that a certain
1485 # piece of data has moved from one heap element to another.
1486 # $k is the hash key of the item
1487 # $n is the new index into the heap at which it is stored
1488 # If $n is undefined, the item has been removed from the heap.
1489 3598 ▶sub _heap_move {
Subroutine _heap_move: called CC=2 SCAR=6.9
1490 3598 my ($self, $k, $n) = @_;
1491 3598 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) { } 3165 433 3598
1492 3165 $self->[HASH]{$k} = $n;
1493 } else {
1494 433 delete $self->[HASH]{$k};
1495 }
1496 }
1497  
1498 1227 ▶sub insert {
Subroutine insert: called CC=7 SCAR=20.7
1499 1227 my ($self, $key, $val) = @_;
1500 1227 local *_;
1501 1227 ▶ croak "missing argument to ->insert" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 1227 1227
1502 1227 ▶ unless (defined $self->[MAX]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->[2]) 0 1227 1227
1503 0 confess "undefined max" ;
1504 }
1505 1227 ▶ confess "undefined val" unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 1227 1227
1506 1227 ▶ return if length($val) > $self->[MAX];
Branch: 1 branch
BranchTrueFalseTotal
if length $val > $self->[2] 83 1144 1227
1507  
1508 # if ($self->[STAT]) {
1509 # $self->[STAT][$key] = 1;
1510 # return;
1511 # }
1512  
1513 1144 my $oldnode = $self->[HASH]{$key};
1514 1144 ▶ if (defined $oldnode) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oldnode) { } 0 1144 1144
1515 0 my $oldval = $self->[HEAP]->set_val($oldnode, $val);
1516 0 $self->[BYTES] -= length($oldval);
1517 } else {
1518 1144 $self->[HEAP]->insert($key, $val);
1519 }
1520 1144 $self->[BYTES] += length($val);
1521 1144 ▶ $self->flush if $self->[BYTES] > $self->[MAX];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[3] > $self->[2] 21 1123 1144
1522 }
1523  
1524 56 ▶sub expire {
Subroutine expire: called CC=2 SCAR=6.9
1525 56 my $self = shift;
1526 56 my $old_data = $self->[HEAP]->popheap;
1527 56 ▶ return unless defined $old_data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_data 6 50 56
1528 50 $self->[BYTES] -= length $old_data;
1529 50 $old_data;
1530 }
1531  
1532 437 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.0
1533 437 my ($self, @keys) = @_;
1534 437 my @result;
1535  
1536 # if ($self->[STAT]) {
1537 # for my $key (@keys) {
1538 # $self->[STAT][$key] = 0;
1539 # }
1540 # return;
1541 # }
1542  
1543 437 for my $key (@keys) {
1544 438 ▶ next unless exists $self->[HASH]{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[1]{$key} 99 339 438
1545 339 my $old_data = $self->[HEAP]->remove($self->[HASH]{$key});
1546 339 $self->[BYTES] -= length $old_data;
1547 339 push @result, $old_data;
1548 }
1549 437 @result;
1550 }
1551  
1552 1878 ▶sub lookup {
Subroutine lookup: called CC=3 SCAR=11.0
1553 1878 my ($self, $key) = @_;
1554 1878 local *_;
1555 1878 ▶ croak "missing argument to ->lookup" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 1878 1878
1556  
1557 # if ($self->[STAT]) {
1558 # $self->[MISS]++ if $self->[STAT][$key]++ == 0;
1559 # $self->[REQ]++;
1560 # my $hit_rate = 1 - $self->[MISS] / $self->[REQ];
1561 # # Do some testing to determine this threshhold
1562 # $#$self = STAT - 1 if $hit_rate > 0.20;
1563 # }
1564  
1565 1878 ▶ if (exists $self->[HASH]{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[1]{$key}) { } 669 1209 1878
1566 669 $self->[HEAP]->lookup($self->[HASH]{$key});
1567 } else {
1568 1209 return;
1569 }
1570 }
1571  
1572 # For internal use only
1573 3204 ▶sub _produce {
Subroutine _produce: called CC=2 SCAR=6.9
1574 3204 my ($self, $key) = @_;
1575 3204 my $loc = $self->[HASH]{$key};
1576 3204 ▶ return unless defined $loc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $loc 1281 1923 3204
1577 1923 $self->[HEAP][$loc][2];
1578 }
1579  
1580 # For internal use only
1581 5 ▶sub _promote {
Subroutine _promote: called CC=1 SCAR=0.0
1582 5 my ($self, $key) = @_;
1583 5 $self->[HEAP]->promote($self->[HASH]{$key});
1584 }
1585  
1586 86 ▶sub empty {
Subroutine empty: called CC=1 SCAR=0.0
1587 86 my ($self) = @_;
1588 86 %{$self->[HASH]} = ();
1589 86 $self->[BYTES] = 0;
1590 86 $self->[HEAP]->empty;
1591 # @{$self->[STAT]} = ();
1592 # $self->[MISS] = 0;
1593 # $self->[REQ] = 0;
1594 }
1595  
1596 3 ▶sub is_empty {
Subroutine is_empty: called CC=1 SCAR=0.0
1597 3 my ($self) = @_;
1598 3 keys %{$self->[HASH]} == 0;
1599 }
1600  
1601 505 ▶sub update {
Subroutine update: called CC=6 SCAR=18.1
1602 505 my ($self, $key, $val) = @_;
1603 505 local *_;
1604 505 ▶ croak "missing argument to ->update" unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 505 505
1605 505 ▶ if (length($val) > $self->[MAX]) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $val > $self->[2]) { } 21 484 505
elsif (exists $self->[1]{$key}) { } 449 35 484
1606 21 my ($oldval) = $self->remove($key);
1607 21 ▶ $self->[BYTES] -= length($oldval) if defined $oldval;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldval 0 21 21
1608 } elsif (exists $self->[HASH]{$key}) {
1609 449 my $oldval = $self->[HEAP]->set_val($self->[HASH]{$key}, $val);
1610 449 $self->[BYTES] += length($val);
1611 449 ▶ $self->[BYTES] -= length($oldval) if defined $oldval;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldval 449 0 449
1612 } else {
1613 35 $self->[HEAP]->insert($key, $val);
1614 35 $self->[BYTES] += length($val);
1615 }
1616 505 $self->flush;
1617 }
1618  
1619 386 ▶sub rekey {
Subroutine rekey: called CC=5 SCAR=16.2
1620 386 my ($self, $okeys, $nkeys) = @_;
1621 386 local *_;
1622 386 my %map;
1623 386 @map{@$okeys} = @$nkeys;
1624 386 ▶ croak "missing argument to ->rekey" unless defined $nkeys;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nkeys 0 386 386
1625 386 ▶ croak "length mismatch in ->rekey arguments" unless @$nkeys == @$okeys;
Branch: 1 branch
BranchTrueFalseTotal
unless @$nkeys == @$okeys 0 386 386
1626 386 my %adjusted; # map new keys to heap indices
1627 # You should be able to cut this to one loop TODO XXX
1628 386 for (0 .. $#$okeys) {
1629 530 $adjusted{$nkeys->[$_]} = delete $self->[HASH]{$okeys->[$_]};
1630 }
1631 386 ▶ while (my ($nk, $ix) = each %adjusted) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %adjusted) 530 386 916
1632 # @{$self->[HASH]}{keys %adjusted} = values %adjusted;
1633 530 $self->[HEAP]->rekey($ix, $nk);
1634 530 $self->[HASH]{$nk} = $ix;
1635 }
1636 }
1637  
1638 795 ▶sub ckeys {
Subroutine ckeys: called CC=1 SCAR=0.0
1639 795 my $self = shift;
1640 795 my @a = keys %{$self->[HASH]};
1641 795 @a;
1642 }
1643  
1644 # Return total amount of cached data
1645 507 ▶sub bytes {
Subroutine bytes: called CC=1 SCAR=0.0
1646 507 my $self = shift;
1647 507 $self->[BYTES];
1648 }
1649  
1650 # Expire oldest item from cache until cache size is smaller than $max
1651 1131 ▶sub reduce_size_to {
Subroutine reduce_size_to: called CC=3 SCAR=11.0
1652 1131 my ($self, $max) = @_;
1653 1131 ▶ until ($self->[BYTES] <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[3] <= $max) 44 1131 1175
1654 # Note that Tie::File::Cache::expire has been inlined here
1655 44 my $old_data = $self->[HEAP]->popheap;
1656 44 ▶ return unless defined $old_data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_data 0 44 44
1657 44 $self->[BYTES] -= length $old_data;
1658 }
1659 }
1660  
1661 # Why not just $self->reduce_size_to($self->[MAX])?
1662 # Try this when things stabilize TODO XXX
1663 # If the cache is too full, expire the oldest records
1664 526 ▶sub flush {
Subroutine flush: called CC=2 SCAR=6.9
1665 526 my $self = shift;
1666 526 ▶ $self->reduce_size_to($self->[MAX]) if $self->[BYTES] > $self->[MAX];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[3] > $self->[2] 36 490 526
1667 }
1668  
1669 # For internal use only
1670 1 ▶sub _produce_lru {
Subroutine _produce_lru: called CC=1 SCAR=0.0
1671 1 my $self = shift;
1672 1 $self->[HEAP]->expire_order;
1673 }
1674  
1675 38 ▶BEGIN { *_ci_warn = \&Tie::File::_ci_warn }
Subroutine BEGIN: called CC=1 SCAR=0.0
1676  
1677 406 ▶sub _check_integrity { # For CACHE
Subroutine _check_integrity: called CC=11 SCAR=31.6
1678 406 my $self = shift;
1679 406 my $good = 1;
1680  
1681 # Test HEAP
1682 406 ▶ $self->[HEAP]->_check_integrity or $good = 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->[0]->_check_integrity or $good = 0 406 0 406
1683  
1684 # Test HASH
1685 406 my $bytes = 0;
1686 406 for my $k (keys %{$self->[HASH]}) {
1687 1063 ▶ if ($k ne '0' && $k !~ /^[1-9][0-9]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k ne 0 and not $k =~ /^[1-9][0-9]*$/) 0 1063 1063
Condition66%2/3
$k ne 0 and not $k =~ /^[1-9][0-9]*$/
!ll&&!rl&&r
1329310
MC/DC0%0/2
$k ne 0 and not $k =~ /^[1-9][0-9]*$/
$k ne 0 not $k =~ /^[1-9][0-9]*$/
Truth table66%2/3
A and not(B)
A= $k ne 0B= not $k =~ /^[1-9][0-9]*$/
ABresult
0X0
100
111
1688 0 $good = 0;
1689 0 _ci_warn "Cache hash key <$k> is non-numeric";
1690 }
1691  
1692 1063 my $h = $self->[HASH]{$k};
1693 1063 ▶ if (! defined $h) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $h) { } 0 1063 1063
elsif ($h == 0) { } 0 1063 1063
1694 0 $good = 0;
1695 0 _ci_warn "Heap index number for key $k is undefined";
1696 } elsif ($h == 0) {
1697 0 $good = 0;
1698 0 _ci_warn "Heap index number for key $k is zero";
1699 } else {
1700 1063 my $j = $self->[HEAP][$h];
1701 1063 ▶ if (! defined $j) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $j) { } 0 1063 1063
1702 0 $good = 0;
1703 0 _ci_warn "Heap contents key $k (=> $h) are undefined";
1704 } else {
1705 1063 $bytes += length($j->[2]);
1706 1063 ▶ if ($k ne $j->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k ne $j->[1]) 0 1063 1063
1707 0 $good = 0;
1708 0 _ci_warn "Heap contents key $k (=> $h) is $j->[1], should be $k";
1709 }
1710 }
1711 }
1712 }
1713  
1714 # Test BYTES
1715 406 ▶ if ($bytes != $self->[BYTES]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes != $self->[3]) 0 406 406
1716 0 $good = 0;
1717 0 _ci_warn "Total data in cache is $bytes, expected $self->[BYTES]";
1718 }
1719  
1720 # Test MAX
1721 406 ▶ if ($bytes > $self->[MAX]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes > $self->[2]) 0 406 406
1722 0 $good = 0;
1723 0 _ci_warn "Total data in cache is $bytes, exceeds maximum $self->[MAX]";
1724 }
1725  
1726 406 return $good;
1727 }
1728  
1729 2986 ▶sub delink {
Subroutine delink: called CC=1 SCAR=0.0
1730 2986 my $self = shift;
1731 2986 $self->[HEAP] = undef; # Bye bye heap
1732 }
1733  
1734 ################################################################
1735 #
1736 # Tie::File::Heap
1737 #
1738 # Heap data structure for use by cache LRU routines
1739  
1740 package Tie::File::Heap;
1741 38 ▶use Carp ':DEFAULT', 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
1742 38 $Tie::File::Heap::VERSION = $Tie::File::Cache::VERSION;
1743 sub SEQ () { 0 };
1744 sub KEY () { 1 };
1745 sub DAT () { 2 };
1746  
1747 2988 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
1748 2988 my ($pack, $cache) = @_;
1749 die "$pack: Parent cache object $cache does not support _heap_move method"
1750 2988 ▶ unless eval { $cache->can('_heap_move') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $cache->can("_heap_move") } } 0 2988 2988
1751 2988 my $self = [[0,$cache,0]];
1752 2988 bless $self => $pack;
1753 }
1754  
1755 # Allocate a new sequence number, larger than all previously allocated numbers
1756 2302 ▶sub _nseq {
Subroutine _nseq: called CC=1 SCAR=0.0
1757 2302 my $self = shift;
1758 2302 $self->[0][0]++;
1759 }
1760  
1761 0 ▶sub _cache {
Subroutine _cache: not called CC=1 SCAR=6.9
1762 0 my $self = shift;
1763 0 $self->[0][1];
1764 }
1765  
1766 0 ▶sub _nelts {
Subroutine _nelts: not called CC=1 SCAR=6.9
1767 0 my $self = shift;
1768 0 $self->[0][2];
1769 }
1770  
1771 1179 ▶sub _nelts_inc {
Subroutine _nelts_inc: called CC=1 SCAR=0.0
1772 1179 my $self = shift;
1773 1179 ++$self->[0][2];
1774 }
1775  
1776 433 ▶sub _nelts_dec {
Subroutine _nelts_dec: called CC=1 SCAR=0.0
1777 433 my $self = shift;
1778 433 --$self->[0][2];
1779 }
1780  
1781 0 ▶sub is_empty {
Subroutine is_empty: not called CC=1 SCAR=6.9
1782 0 my $self = shift;
1783 0 $self->_nelts == 0;
1784 }
1785  
1786 86 ▶sub empty {
Subroutine empty: called CC=1 SCAR=0.0
1787 86 my $self = shift;
1788 86 $#$self = 0;
1789 86 $self->[0][2] = 0;
1790 86 $self->[0][0] = 0; # might as well reset the sequence numbers
1791 }
1792  
1793 # notify the parent cache object that we moved something
1794 0 ▶sub _heap_move {
Subroutine _heap_move: not called CC=1 SCAR=6.9
1795 0 my $self = shift;
1796 0 $self->_cache->_heap_move(@_);
1797 }
1798  
1799 # Insert a piece of data into the heap with the indicated sequence number.
1800 # The item with the smallest sequence number is always at the top.
1801 # If no sequence number is specified, allocate a new one and insert the
1802 # item at the bottom.
1803 1179 ▶sub insert {
Subroutine insert: called CC=2 SCAR=7.1
1804 1179 my ($self, $key, $data, $seq) = @_;
1805 1179 ▶ $seq = $self->_nseq unless defined $seq;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seq 1179 0 1179
1806 1179 $self->_insert_new([$seq, $key, $data]);
1807 }
1808  
1809 # Insert a new, fresh item at the bottom of the heap
1810 1179 ▶sub _insert_new {
Subroutine _insert_new: called CC=2 SCAR=6.9
1811 1179 my ($self, $item) = @_;
1812 1179 my $i = @$self;
1813 1179 ▶ $i = int($i/2) until defined $self->[$i/2];
Branch: 1 branch
BranchTrueFalseTotal
defined $$self[$i / 2] or do { $i = int $i / 2 } 1179 380 1559
1814 1179 $self->[$i] = $item;
1815 1179 $self->[0][1]->_heap_move($self->[$i][KEY], $i);
1816 1179 $self->_nelts_inc;
1817 }
1818  
1819 # Insert [$data, $seq] pair at or below item $i in the heap.
1820 # If $i is omitted, default to 1 (the top element.)
1821 0 ▶sub _insert {
Subroutine _insert: not called CC=7 SCAR=40.3
1822 0 my ($self, $item, $i) = @_;
1823 # $self->_check_loc($i) if defined $i;
1824 0 ▶ $i = 1 unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 0 0
1825 0 ▶ until (! defined $self->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $self->[$i]) 0 0 0
1826 0 ▶ if ($self->[$i][SEQ] > $item->[SEQ]) { # inserted item is older
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[$i][0] > $item->[0]) 0 0 0
1827 0 ($self->[$i], $item) = ($item, $self->[$i]);
1828 0 $self->[0][1]->_heap_move($self->[$i][KEY], $i);
1829 }
1830 # If either is undefined, go that way. Otherwise, choose at random
1831 0 my $dir;
1832 0 ▶ $dir = 0 if !defined $self->[2*$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self[2 * $i] 0 0 0
1833 0 ▶ $dir = 1 if !defined $self->[2*$i+1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self[2 * $i + 1] 0 0 0
1834 0 ▶ $dir = int(rand(2)) unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 0 0
1835 0 $i = 2*$i + $dir;
1836 }
1837 0 $self->[$i] = $item;
1838 0 $self->[0][1]->_heap_move($self->[$i][KEY], $i);
1839 0 $self->_nelts_inc;
1840 }
1841  
1842 # Remove the item at node $i from the heap, moving child items upwards.
1843 # The item with the smallest sequence number is always at the top.
1844 # Moving items upwards maintains this condition.
1845 # Return the removed item. Return undef if there was no item at node $i.
1846 439 ▶sub remove {
Subroutine remove: called CC=9 SCAR=22.0
1847 439 my ($self, $i) = @_;
1848 439 ▶ $i = 1 unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 439 439
1849 439 my $top = $self->[$i];
1850 439 ▶ return unless defined $top;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $top 6 433 439
1851 433 while (1) {
1852 915 my $ii;
1853 915 my ($L, $R) = (2*$i, 2*$i+1);
1854  
1855 # If either is undefined, go the other way.
1856 # Otherwise, go towards the smallest.
1857 915 ▶ last unless defined $self->[$L] || defined $self->[$R];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$L] or defined $self->[$R] 433 482 915
Condition100%3/3
defined $self->[$L] or defined $self->[$R]
l!l&&r!l&&!r
351131433
MC/DC100%2/2
defined $self->[$L] or defined $self->[$R]
defined $self->[$L] defined $self->[$R]
Truth table100%3/3
A or B
A= defined $self->[$L]B= defined $self->[$R]
ABresult
1X1
011
000
1858 482 ▶ $ii = $R if not defined $self->[$L];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$L] 131 351 482
1859 482 ▶ $ii = $L if not defined $self->[$R];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$R] 193 289 482
1860 482 ▶ unless (defined $ii) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ii) 158 324 482
1861 158 ▶ $ii = $self->[$L][SEQ] < $self->[$R][SEQ] ? $L : $R;
Branch: 1 branch
BranchTrueFalseTotal
$self->[$L][0] < $self->[$R][0] ? : 112 46 158
1862 }
1863  
1864 482 $self->[$i] = $self->[$ii]; # Promote child to fill vacated spot
1865 482 $self->[0][1]->_heap_move($self->[$i][KEY], $i);
1866 482 $i = $ii; # Fill new vacated spot
1867 }
1868 433 $self->[0][1]->_heap_move($top->[KEY], undef);
1869 433 undef $self->[$i];
1870 433 $self->_nelts_dec;
1871 433 return $top->[DAT];
1872 }
1873  
1874 100 ▶sub popheap {
Subroutine popheap: called CC=1 SCAR=0.0
1875 100 my $self = shift;
1876 100 $self->remove(1);
1877 }
1878  
1879 # set the sequence number of the indicated item to a higher number
1880 # than any other item in the heap, and bubble the item down to the
1881 # bottom.
1882 1123 ▶sub promote {
Subroutine promote: called CC=9 SCAR=22.0
1883 1123 my ($self, $n) = @_;
1884 # $self->_check_loc($n);
1885 1123 $self->[$n][SEQ] = $self->_nseq;
1886 1123 my $i = $n;
1887 1123 while (1) {
1888 1875 my ($L, $R) = (2*$i, 2*$i+1);
1889 1875 my $dir;
1890 1875 ▶ last unless defined $self->[$L] || defined $self->[$R];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$L] or defined $self->[$R] 1123 752 1875
Condition100%3/3
defined $self->[$L] or defined $self->[$R]
l!l&&r!l&&!r
662901123
MC/DC100%2/2
defined $self->[$L] or defined $self->[$R]
defined $self->[$L] defined $self->[$R]
Truth table100%3/3
A or B
A= defined $self->[$L]B= defined $self->[$R]
ABresult
1X1
011
000
1891 752 ▶ $dir = $R unless defined $self->[$L];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$L] 90 662 752
1892 752 ▶ $dir = $L unless defined $self->[$R];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$R] 190 562 752
1893 752 ▶ unless (defined $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dir) 472 280 752
1894 472 ▶ $dir = $self->[$L][SEQ] < $self->[$R][SEQ] ? $L : $R;
Branch: 1 branch
BranchTrueFalseTotal
$self->[$L][0] < $self->[$R][0] ? : 291 181 472
1895 }
1896 752 @{$self}[$i, $dir] = @{$self}[$dir, $i];
1897 752 for ($i, $dir) {
1898 1504 ▶ $self->[0][1]->_heap_move($self->[$_][KEY], $_) if defined $self->[$_];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[$_] 1504 0 1504
1899 }
1900 752 $i = $dir;
1901 }
1902 }
1903  
1904 # Return item $n from the heap, promoting its LRU status
1905 669 ▶sub lookup {
Subroutine lookup: called CC=1 SCAR=0.0
1906 669 my ($self, $n) = @_;
1907 # $self->_check_loc($n);
1908 669 my $val = $self->[$n];
1909 669 $self->promote($n);
1910 669 $val->[DAT];
1911 }
1912  
1913  
1914 # Assign a new value for node $n, promoting it to the bottom of the heap
1915 449 ▶sub set_val {
Subroutine set_val: called CC=1 SCAR=0.0
1916 449 my ($self, $n, $val) = @_;
1917 # $self->_check_loc($n);
1918 449 my $oval = $self->[$n][DAT];
1919 449 $self->[$n][DAT] = $val;
1920 449 $self->promote($n);
1921 449 return $oval;
1922 }
1923  
1924 # The hash key has changed for an item;
1925 # alter the heap's record of the hash key
1926 530 ▶sub rekey {
Subroutine rekey: called CC=1 SCAR=0.0
1927 530 my ($self, $n, $new_key) = @_;
1928 # $self->_check_loc($n);
1929 530 $self->[$n][KEY] = $new_key;
1930 }
1931  
1932 0 ▶sub _check_loc {
Subroutine _check_loc: not called CC=1 SCAR=6.9
1933 0 my ($self, $n) = @_;
1934 0 unless (1 || defined $self->[$n]) {
1935 confess "_check_loc($n) failed";
1936 }
1937 }
1938  
1939 38 ▶BEGIN { *_ci_warn = \&Tie::File::_ci_warn }
Subroutine BEGIN: called CC=1 SCAR=0.0
1940  
1941 406 ▶sub _check_integrity {
Subroutine _check_integrity: called CC=8 SCAR=23.8
1942 406 my $self = shift;
1943 406 my $good = 1;
1944 406 my %seq;
1945  
1946 406 ▶ unless (eval {$self->[0][1]->isa("Tie::File::Cache")}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->[0][1]->isa("Tie::File::Cache") } }) 0 406 406
1947 0 _ci_warn "Element 0 of heap corrupt";
1948 0 $good = 0;
1949 }
1950 406 ▶ $good = 0 unless $self->_satisfies_heap_condition(1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_satisfies_heap_condition(1) 0 406 406
1951 406 for my $i (2 .. $#{$self}) {
1952 1933 my $p = int($i/2); # index of parent node
1953 1933 ▶ if (defined $self->[$i] && ! defined $self->[$p]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[$i] and not defined $self->[$p]) 0 1933 1933
Condition66%2/3
defined $self->[$i] and not defined $self->[$p]
!ll&&!rl&&r
11098240
MC/DC0%0/2
defined $self->[$i] and not defined $self->[$p]
defined $self->[$i] not defined $self->[$p]
Truth table66%2/3
A and not(B)
A= defined $self->[$i]B= not defined $self->[$p]
ABresult
0X0
100
111
1954 0 _ci_warn "Element $i of heap defined, but parent $p isn't";
1955 0 $good = 0;
1956 }
1957  
1958 1933 ▶ if (defined $self->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[$i]) 824 1109 1933
1959 824 ▶ if ($seq{$self->[$i][SEQ]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seq{$self->[$i][0]}) { } 0 824 824
1960 0 my $seq = $self->[$i][SEQ];
1961 0 _ci_warn "Nodes $i and $seq{$seq} both have SEQ=$seq";
1962 0 $good = 0;
1963 } else {
1964 824 $seq{$self->[$i][SEQ]} = $i;
1965 }
1966 }
1967 }
1968  
1969 406 return $good;
1970 }
1971  
1972 1230 ▶sub _satisfies_heap_condition {
Subroutine _satisfies_heap_condition: called CC=6 SCAR=19.0
1973 1230 my $self = shift;
1974 1230 ▶ my $n = shift || 1;
Condition50%1/2
shift() || 1
l!l
12300
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1975 1230 my $good = 1;
1976 1230 for (0, 1) {
1977 2460 my $c = $n*2 + $_;
1978 2460 ▶ next unless defined $self->[$c];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$c] 1636 824 2460
1979 824 ▶ if ($self->[$n][SEQ] >= $self->[$c]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[$n][0] >= $self->[$c]) 0 824 824
1980 0 _ci_warn "Node $n of heap does not predate node $c";
1981 0 $good = 0 ;
1982 }
1983 824 ▶ $good = 0 unless $self->_satisfies_heap_condition($c);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_satisfies_heap_condition($c) 0 824 824
1984 }
1985 1230 return $good;
1986 }
1987  
1988 # Return a list of all the values, sorted by expiration order
1989 1 ▶sub expire_order {
Subroutine expire_order: called CC=1 SCAR=0.0
1990 1 my $self = shift;
1991 1 my @nodes = sort {$a->[SEQ] <=> $b->[SEQ]} $self->_nodes;
1992 1 map { $_->[KEY] } @nodes;
1993 }
1994  
1995 7 ▶sub _nodes {
Subroutine _nodes: called CC=3 SCAR=11.0
1996 7 my $self = shift;
1997 7 ▶ my $i = shift || 1;
Condition100%2/2
shift() || 1
l!l
61
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1998 7 ▶ return unless defined $self->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[$i] 4 3 7
1999 3 ($self->[$i], $self->_nodes($i*2), $self->_nodes($i*2+1));
2000 }
2001  
2002 38 1;
2003  
2004 __END__
↑ Summary