| 1 | package XML::LibXML::Cache::Base; | ||||||||||||||||||||||||||||
| 2 | 2 | $XML::LibXML::Cache::Base::VERSION = '0.14'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Base class for XML::LibXML caches | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use XML::LibXML 1.59; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $input_callbacks = XML::LibXML::InputCallback->new(); | |||||||||||||||||||||||||||
| 12 | 2 | $input_callbacks->register_callbacks([ | |||||||||||||||||||||||||||
| 13 | \&_match_cb, | ||||||||||||||||||||||||||||
| 14 | \&_open_cb, | ||||||||||||||||||||||||||||
| 15 | \&_read_cb, | ||||||||||||||||||||||||||||
| 16 | \&_close_cb, | ||||||||||||||||||||||||||||
| 17 | ]); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | my $deps_found; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | my $self = { | |||||||||||||||||||||||||||
| 25 | cache => {}, | ||||||||||||||||||||||||||||
| 26 | hits => 0, | ||||||||||||||||||||||||||||
| 27 | }; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | return bless($self, $class); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | sub cache_hits { | ||||||||||||||||||||||||||
|
Subroutine
cache_hits: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | return $self->{hits}; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 3 | ▶ | sub _cache_lookup { | ||||||||||||||||||||||||||
|
Subroutine
_cache_lookup: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 3 | my ($self, $filename, $get_item) = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | my $item = $self->_cache_read($filename); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | if ($item) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 1 | ++$self->{hits}; | |||||||||||||||||||||||||||
| 45 | 1 | return $item; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | $deps_found = {}; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | $item = $get_item->($filename); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | $self->_cache_write($filename, $item); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | $deps_found = undef; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | return $item; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | sub _cache_read { | ||||||||||||||||||||||||||
|
Subroutine
_cache_read: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 60 | 3 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | my $cache_rec = $self->{cache}{$filename} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | or return (); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | my ($item, $deps) = @$cache_rec; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # check sizes and mtimes of deps_found | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | while (my ($path, $attrs) = each(%$deps)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 6 | my @stat = stat($path); | |||||||||||||||||||||||||||
| 71 | 6 | ▶ | my ($size, $mtime) = @stat ? ($stat[7], $stat[9]) : (-1, -1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 6 | ▶ | return () if $size != $attrs->[0] || $mtime != $attrs->[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$size != $attrs->[0] or $mtime != $attrs->[1]
MC/DC50%1/2
$size != $attrs->[0] or $mtime != $attrs->[1]
$size != $attrs->[0]
$mtime != $attrs->[1]
Truth table66%2/3
A or B
A= $size != $attrs->[0]B= $mtime != $attrs->[1]
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | return $item; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | sub _cache_write { | ||||||||||||||||||||||||||
|
Subroutine
_cache_write: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 80 | 2 | my ($self, $filename, $item) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 2 | ▶ | if ($deps_found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | $cache->{$filename} = [ $item, $deps_found ]; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 0 | delete($cache->{$filename}); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Handling of dependencies | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # We register an input callback that never matches but records all URIs | ||||||||||||||||||||||||||||
| 95 | # that are accessed during parsing. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 10 | ▶ | sub _match_cb { | ||||||||||||||||||||||||||
|
Subroutine
_match_cb: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||
| 98 | 10 | my $uri_str = shift; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 10 | ▶ | return undef if !$deps_found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 10 | my $uri = URI->new($uri_str, 'file'); | |||||||||||||||||||||||||||
| 103 | 10 | my $scheme = $uri->scheme; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 10 | ▶ | if (!defined($scheme) || $scheme eq 'file') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $scheme or $scheme eq "file"
MC/DC0%0/2
not defined $scheme or $scheme eq "file"
not defined $scheme
$scheme eq "file"
Truth table33%1/3
not(A) or B
A= not defined $schemeB= $scheme eq "file"
| |||||||||||||||||||||||||||||
| 106 | 10 | my $path = $uri->path; | |||||||||||||||||||||||||||
| 107 | 10 | my @stat = stat($path); | |||||||||||||||||||||||||||
| 108 | 10 | ▶ | $deps_found->{$path} = @stat ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | [ $stat[7], $stat[9] ] : | ||||||||||||||||||||||||||||
| 110 | [ -1, -1 ]; | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||
| 113 | # Unsupported URI, disable caching | ||||||||||||||||||||||||||||
| 114 | 0 | $deps_found = undef; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 10 | return undef; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # should never be called | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub _open_cb { die('open callback called unexpectedly'); } | ||||||||||||||||||||||||||
|
Subroutine
_open_cb: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub _read_cb { die('read callback called unexpectedly'); } | ||||||||||||||||||||||||||
|
Subroutine
_read_cb: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub _close_cb { die('close callback called unexpectedly'); } | ||||||||||||||||||||||||||
|
Subroutine
_close_cb: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||