| 1 | package XML::LibXML::Cache::Base; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 2 | $XML::LibXML::Cache::Base::VERSION = '0.12'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Base class for XML::LibXML caches | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use XML::LibXML 1.59; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our $input_callbacks = XML::LibXML::InputCallback->new(); | |||||||||||||||||||||||||||
| 13 | 2 | $input_callbacks->register_callbacks([ | |||||||||||||||||||||||||||
| 14 | \&_match_cb, | ||||||||||||||||||||||||||||
| 15 | \&_open_cb, | ||||||||||||||||||||||||||||
| 16 | \&_read_cb, | ||||||||||||||||||||||||||||
| 17 | \&_close_cb, | ||||||||||||||||||||||||||||
| 18 | ]); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | my $deps_found; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub new { | ||||||||||||||||||||||||||||
| 23 | 2 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | my $self = { | |||||||||||||||||||||||||||
| 26 | cache => {}, | ||||||||||||||||||||||||||||
| 27 | hits => 0, | ||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | return bless($self, $class); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub cache_hits { | ||||||||||||||||||||||||||||
| 34 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
cache_hits: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 3 | return $self->{hits}; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub _cache_lookup { | ||||||||||||||||||||||||||||
| 40 | 3 | ▶ | my ($self, $filename, $get_item) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_cache_lookup: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | my $item = $self->_cache_read($filename); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | if ($item) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 2 | ++$self->{hits}; | |||||||||||||||||||||||||||
| 46 | 2 | return $item; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | $deps_found = {}; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | $item = $get_item->($filename); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | $self->_cache_write($filename, $item); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | $deps_found = undef; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | return $item; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub _cache_read { | ||||||||||||||||||||||||||||
| 61 | 3 | ▶ | my ($self, $filename) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_cache_read: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 3 | ▶ | my $cache_rec = $self->{cache}{$filename} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | or return (); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | my ($item, $deps) = @$cache_rec; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # check sizes and mtimes of deps_found | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | ▶ | while (my ($path, $attrs) = each(%$deps)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 6 | my @stat = stat($path); | |||||||||||||||||||||||||||
| 72 | 6 | ▶ | my ($size, $mtime) = @stat ? ($stat[7], $stat[9]) : (-1, -1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 6 | ▶ | return () if $size != $attrs->[0] || $mtime != $attrs->[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$size != $attrs->[0] or $mtime != $attrs->[1]
MC/DC0%0/2
$size != $attrs->[0] or $mtime != $attrs->[1]
$size != $attrs->[0]
$mtime != $attrs->[1]
Truth table33%1/3
A or B
A= $size != $attrs->[0]B= $mtime != $attrs->[1]
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | return $item; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub _cache_write { | ||||||||||||||||||||||||||||
| 81 | 1 | ▶ | my ($self, $filename, $item) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_cache_write: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | my $cache = $self->{cache}; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | if ($deps_found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | $cache->{$filename} = [ $item, $deps_found ]; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | else { | ||||||||||||||||||||||||||||
| 89 | 0 | delete($cache->{$filename}); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Handling of dependencies | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # We register an input callback that never matches but records all URIs | ||||||||||||||||||||||||||||
| 96 | # that are accessed during parsing. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub _match_cb { | ||||||||||||||||||||||||||||
| 99 | 3 | ▶ | my $uri_str = shift; | ||||||||||||||||||||||||||
|
Subroutine
_match_cb: called CC=5 SCAR=17.0
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 3 | ▶ | return undef if !$deps_found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | my $uri = URI->new($uri_str, 'file'); | |||||||||||||||||||||||||||
| 104 | 3 | my $scheme = $uri->scheme; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | ▶ | 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"
| |||||||||||||||||||||||||||||
| 107 | 3 | my $path = $uri->path; | |||||||||||||||||||||||||||
| 108 | 3 | my @stat = stat($path); | |||||||||||||||||||||||||||
| 109 | 3 | ▶ | $deps_found->{$path} = @stat ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | [ $stat[7], $stat[9] ] : | ||||||||||||||||||||||||||||
| 111 | [ -1, -1 ]; | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | else { | ||||||||||||||||||||||||||||
| 114 | # Unsupported URI, disable caching | ||||||||||||||||||||||||||||
| 115 | 0 | $deps_found = undef; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | return undef; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # should never be called | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub _open_cb { die('open callback called unexpectedly'); } | ||||||||||||||||||||||||||
|
Subroutine
_open_cb: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub _read_cb { die('read callback called unexpectedly'); } | ||||||||||||||||||||||||||
|
Subroutine
_read_cb: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub _close_cb { die('close callback called unexpectedly'); } | ||||||||||||||||||||||||||
|
Subroutine
_close_cb: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 2 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =pod | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 NAME | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | XML::LibXML::Cache::Base - Base class for XML::LibXML caches | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 VERSION | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | version 0.12 | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Base class for the document and style sheet caches. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 METHODS | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 new | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Only used by subclasses. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 cache_hits | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | my $hits = $cache->cache_hits; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Return the number of cache hits. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Nick Wellnhofer <wellnhofer@aevum.de> | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | This software is copyright (c) 2012 by Nick Wellnhofer. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 165 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =cut | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||