Branch Coverage

blib/lib/Cache/Mmap.pm
Criterion Covered Total %
branch 106 160 66.2


line true false branch
239 3 8 unless $options
246 0 44 unless defined $$self{$_} and $$self{$_} =~ /^[1-9]\d*$/s
249 0 11 unless $$self{'pagesize'} >= $maxheadsize
252 0 33 unless not $$self{$_} or ref $$self{$_} eq 'CODE'
260 0 11 if ((my $i_pages = int $n_pages) != $n_pages)
267 0 11 unless sysopen $fh, $filename, 66, $$self{'permissions'}
310 2 47 unless eval { do { local $SIG{'__DIE__'}; ($found, my($expired, $poff, $off, $size, $klen, $vlen, $flags)) = $self->_find($bucket, $key); my($new_found, $new_val); if (not $found or $expired) { my(@_read) = $$self{'read'} ? $$self{'read'}($key, $$self{'context'}) : (); ($new_found, $new_val) = @_read == 1 ? (defined $_read[0], $_read[0]) : @_read; $new_found = 0 unless defined $new_found; undef $new_val unless $new_found; if ($new_found == 3402498049) { $found = $expired; $expired = 0; $new_found = 0; } elsif ($new_found == 3402498051) { $found = $expired; $expired = 0; $new_found = 0; substr($$self{'_mmap'}, $off + 4, 4) = pack('l', time) if $found; } ; } ; if ($found) { { if ($expired and not $flags & 1) { my $b_end = $bucket + $$self{'bucketsize'}; substr($$self{'_mmap'}, $off, $b_end - $off) = substr($$self{'_mmap'}, $off + $size, $b_end - $off - $size) . "\000" x $size; my($filled) = unpack('l', substr($$self{'_mmap'}, $bucket, $bheadsize)); $filled -= $size; substr($$self{'_mmap'}, $bucket, $bheadsize) = substr(pack("lx$bheadsize", $filled), 0, $bheadsize); $found = 0; last; } ; if ($poff) { my $psize = $off - $poff; substr($$self{'_mmap'}, $poff, $psize + $size) = substr($$self{'_mmap'}, $off, $size) . substr($$self{'_mmap'}, $poff, $psize); $off = $poff; } ; $val = $self->_decode(substr($$self{'_mmap'}, $off + $eheadsize + $klen, $vlen), 0); } ; } ; unless ($found) { ($found, $val) = ($new_found, $new_val); if ($found or $$self{'cachenegative'}) { my $eval = $self->_encode($val, 0); $self->_insert($bucket, $ekey, $eval, 0); } ; } ; 1 } }
319 9 38 if (not $found or $expired)
320 4 5 $$self{'read'} ? :
322 0 9 @_read == 1 ? :
323 7 2 unless defined $new_found
324 7 2 unless $new_found
326 1 8 if ($new_found == 3402498049) { }
1 7 elsif ($new_found == 3402498051) { }
338 1 0 if $found
343 42 5 if ($found)
346 2 40 if ($expired and not $flags & 1)
359 6 34 if ($poff)
368 7 40 unless ($found)
373 0 7 if ($found or $$self{'cachenegative'})
385 2 47 if $err
406 28 0 if ($size <= $bsize) { }
0 0 elsif (my $wsub = $$self{'write'}) { }
411 0 28 unless eval { do { local $SIG{'__DIE__'}; my($found, $expired, $poff, $off, $_size, $_klen, $_vlen, $flags) = $self->_find($bucket, $key); if ($found) { my($filled) = unpack('l', substr($$self{'_mmap'}, $bucket, $bheadsize)); my $pre = substr($$self{'_mmap'}, $bucket + $bheadsize, $off - ($bucket + $bheadsize)); my $post = substr($$self{'_mmap'}, $off + $_size, $bucket + $bheadsize + $filled - $off - $_size); my $new_filled = length $pre . $post; my $bhead = substr(pack("lx$bheadsize", $new_filled), 0, $bheadsize); substr($$self{'_mmap'}, $bucket, $bheadsize + $new_filled) = $bhead . $pre . $post; } ; $self->_insert($bucket, $ekey, $eval, 1); if ($$self{'writethrough'} and my $write = $$self{'write'}) { &$write($key, $val, $$self{'context'}); } ; 1 } }
417 3 25 if ($found)
434 0 28 if ($$self{'writethrough'} and my $write = $$self{'write'})
441 0 28 if $err
472 0 3 unless eval { do { local $SIG{'__DIE__'}; ($found, my($expired, $poff, $off, $size, $klen, $vlen, $flags)) = $self->_find($bucket, $key); if ($found) { $val = $self->_decode(substr($$self{'_mmap'}, $off + $eheadsize + $klen, $vlen), 0); if (my $dsub = $$self{'delete'} and not $flags & 1) { &$dsub($key, $val, $$self{'context'}); } ; my($filled) = unpack('l', substr($$self{'_mmap'}, $bucket, $bheadsize)); my $new_filled = $filled - $size; substr($$self{'_mmap'}, $bucket, $bheadsize) = substr(pack("lx$bheadsize", $new_filled), 0, $bheadsize); my $fill_end = $bucket + $bheadsize + $filled; my $elem_end = $off + $size; substr($$self{'_mmap'}, $off, $fill_end - $elem_end) = substr($$self{'_mmap'}, $elem_end, $fill_end - $elem_end); } ; 1 } }
478 3 0 if ($found)
480 0 3 if (my $dsub = $$self{'delete'} and not $flags & 1)
499 0 3 if $err
546 0 65 unless eval { do { local $SIG{'__DIE__'}; my($filled) = unpack('l', substr($$self{'_mmap'}, $bucket, $bheadsize)); my $off = $bucket + $bheadsize; my $end = $off + $filled; my $size; while ($off < $end) { ($size, my($time, $klen, $vlen, $flags)) = unpack('l5', substr($$self{'_mmap'}, $off, $eheadsize)); unless ($size) { my $part = substr($$self{'_mmap'}, $off, $end - $off); $part =~ s/\\/\\\\/g; $part =~ s/([^\040-\176])/sprintf '\\%02x', ord $1;/eg; die "Zero-sized entry in $$self{'_filename'}, offset $off! Remaining bucket contents: $part"; } ; next if $expiry and time - $time > $expiry; my $key = $self->_decode(substr($$self{'_mmap'}, $off + $eheadsize, $klen), 1); if ($details) { push @entries, {'key', $key, 'time', $time, 'dirty', $flags & 1, $details > 1 ? ('value', $self->_decode(substr($$self{'_mmap'}, $off + $eheadsize + $klen, $vlen), 0)) : ()}; } else { push @entries, $key; } ; } continue { $off += $size; } ; 1 } }
556 0 20 unless ($size)
562 0 20 if $expiry and time - $time > $expiry
565 10 10 if ($details) { }
566 5 5 $details > 1 ? :
587 0 65 if $err
603 0 0 unless $self->_lock(0)
607 0 0 unless eval { do { local $SIG{'__DIE__'}; my $buckets = $self->buckets; my $bucketsize = $self->bucketsize; my $pagesize = $self->pagesize; my $empty = "\000" x $bucketsize; foreach $_ (0 .. $buckets - 1) { substr($$self{'_mmap'}, $pagesize + $bucketsize * $_, $bucketsize) = $empty; } ; 1 } }
624 0 0 if $err
687 0 0 @_ ? :
763 0 11 unless $self->_lock(0)
767 2 9 unless eval { do { local $SIG{'__DIE__'}; my $size_cur = -s $$self{'_fh'}; my $magic_ok; if ($size_cur >= $headsize) { my $head; if ((my $bytes = sysread($$self{'_fh'}, $head, $headsize)) != $headsize) { croak("Expecting $headsize bytes, read $bytes from cache header\n"); } ; my($mg, $buckets, $bucketsize, $pagesize, $flags, $format) = unpack('l6', $head); croak("$$self{'_filename'} is not a Cache::Mmap file") unless $mg == 22727374; croak("$$self{'_filename'} uses v$format data structures. Cache::Mmap $Cache::Mmap::VERSION only supports v" . 1 . ' data structures') unless ($format += 0) == 1; $$self{'buckets'} = $buckets; $$self{'bucketsize'} = $bucketsize; $$self{'pagesize'} = $pagesize; while (my($opt, $bit) = each %bool_opts) { do { $$self{$opt} = !!($flags & $bit) }; } ; $magic_ok = 1; } ; my $size = $$self{'pagesize'} + $$self{'buckets'} * $$self{'bucketsize'}; if ($size_cur < $size) { my $pad = "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"; croak("Can't seek to end of file: $!\n") unless sysseek $$self{'_fh'}, 2, 0; while ($size_cur < $size) { croak("Can't pad file: $!") unless my $len = syswrite($$self{'_fh'}, $pad, 1024); $size_cur += $len; } ; croak("Failed to set correct file size\n") unless -s $$self{'_fh'} >= $size; } ; unless ($magic_ok) { my $flags = 0; while (my($opt, $bit) = each %bool_opts) { do { $flags |= $bit if $$self{$opt} }; } ; my $head = pack("l6x$headsize", 22727374, @$self{'buckets', 'bucketsize', 'pagesize'}, $flags, 1); croak("Can't seek to beginning: $!") unless sysseek $$self{'_fh'}, 0, 0; croak("Can't write file header: $!") unless syswrite($$self{'_fh'}, $head, $headsize) == $headsize; } ; $self->_unlock; unless (&mmap(, $size, $$self{'_fh'})) { delete $$self{'_mmap'}; croak("Can't mmap $$self{'_filename'}: $!"); } ; unless (length $$self{'_mmap'} eq $size) { delete $$self{'_mmap'}; croak("mmap() failed silently: $!"); } ; 1 } }
773 3 8 if ($size_cur >= $headsize)
775 0 3 if ((my $bytes = sysread($$self{'_fh'}, $head, $headsize)) != $headsize)
779 1 2 unless $mg == 22727374
781 1 1 unless ($format += 0) == 1
795 8 1 if ($size_cur < $size)
797 0 8 unless sysseek $$self{'_fh'}, 2, 0
800 0 48 unless my $len = syswrite($$self{'_fh'}, $pad, 1024)
804 0 8 unless -s $$self{'_fh'} >= $size
809 8 1 unless ($magic_ok)
812 7 1 if $$self{$opt}
817 0 8 unless sysseek $$self{'_fh'}, 0, 0
819 0 8 unless syswrite($$self{'_fh'}, $head, $headsize) == $headsize
827 0 9 unless (&mmap(, $size, $$self{'_fh'}))
832 0 9 unless (length $$self{'_mmap'} eq $size)
844 2 9 if $err
856 9 2 if exists $$self{'_mmap'}
871 145 11 $offset ? :
903 0 28 if $size > $bsize
914 1 27 if ($filled > $bsize)
919 0 1 if $poff >= $filled
925 0 1 if (my $wsub = $$self{'write'} && !$$self{'writethrough'})
929 0 0 unless ($size)
937 0 0 if ($flags & 1)
993 1 71 if ($off >= $b_end)
998 1 70 unless ($size)
1003 0 1 if ($poff)
1009 48 22 if ($self->_decode(substr($$self{'_mmap'}, $off + $eheadsize, $klen), 1) eq $key)
1017 30 48 unless $found
1020 8 40 if ($found and my $exp = $self->expiry)
1036 0 105 if (not defined $value) { }
92 13 elsif ($$self{'strings'} or $is_key) { }
1039 92 0 if ('has_utf8', 1) { }
1041 92 0 if ($eval eq $value) { }
1063 0 138 if ($value eq '') { }
1068 0 138 if ($code eq 'U') { }
108 30 elsif ($$self{'strings'} or $is_key) { }
1069 0 0 if ('has_utf8', 1) { }