blib/lib/App/Greple/xlate/Cache.pm

filter: Statementstmt 90.6% 175 / 193 Branchbran 67.8% 57 / 84 Conditioncond 50.0% 24 / 48 MC/DCmcdc 10.0% 3 / 30 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 14 totaltotal 72.1% 287 / 398 CC 68 SCAR 26.0
CC
68
Cov
79%
update
29.6
checkpoint
17.3
open
15.2
CRAP 13.4
« blib/lib/App/Greple/xlate/deepl.pm ↑ Summary lib/App/Greple/xlate/Mask.pm »
1 package App::Greple::xlate::Cache;
2  
3 39 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 39 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use List::Util qw(pairmap mesh);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 37 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
12 37 my $self = shift;
13 37 my $obj = $self->new(name => @_);
14 37 $obj;
15 }
16  
17 109 ▶sub EXISTS {
Subroutine EXISTS: called CC=2 SCAR=7.1
18 109 my($obj, $key) = @_;
19 109 $obj->access($key);
20 109 ▶ exists $obj->current->{$key} or exists $obj->saved->{$key};
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->current->{$key} or exists $obj->saved->{$key} 0 109 109
21 }
22  
23 132 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
24 132 my($obj, $key) = @_;
25 132 $obj->access($key);
26 132 $obj->get($key);
27 }
28  
29 152 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
30 152 my($obj, $key, $val) = @_;
31 152 $obj->access($key);
32 152 $obj->set($key, $val);
33 }
34  
35 11 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
36 11 my $obj = shift;
37 11 $obj->update;
38 }
39  
40 39 my %default = (
41 name => '', # cache filename
42 saved => undef, # saved hash
43 saved_order => [], # keys of saved data in file order
44 current => undef, # current using hash
45 clear => 0, # clean up cache data
46 accessed => {}, # accessed keys
47 order => [], # accessed keys in order
48 accumulate => 0, # do not delete unused entry
49 force_update => 0, # update cache file anyway
50 updated => 0, # number of updated entries
51 format => 'list', # saving cache file format
52 old_pos => undef, # memoized key-to-position map of saved_order
53 # NOTE: reference-valued defaults must get fresh copies in new()
54 seed => undef, # seed cache file for a fresh cache
55 seeded => 0, # true when the seed was actually loaded
56 readonly => 0, # suppress all cache file writes
57 );
58  
59 39 for my $key (keys %default) {
60 39 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2335 ▶ *{$key} = sub :lvalue { $_[0]->{$key} }
Subroutine __ANON__: called CC=1 SCAR=0.0
62 585 }
63  
64 48 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
65 48 my $class = shift;
66 48 my $obj = bless { %default }, $class;
67 48 $obj->{accessed} = {};
68 48 $obj->{order} = [];
69 48 $obj->{saved_order} = [];
70 48 lock_keys %{$obj};
71 48 ▶ pairmap { $obj->{$a} = $b } @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
72 48 ▶ $obj->open if $obj->name;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->name 48 0 48
73 48 $obj;
74 }
75  
76 412 ▶sub access {
Subroutine access: called CC=2 SCAR=6.9
Pod undocumented
77 412 my $obj = shift;
78 412 my $key = shift;
79 412 ▶ push @{$obj->order}, $key if not $obj->accessed->{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if not $obj->accessed->{$key}++ 128 284 412
80 }
81  
82 145 ▶sub get {
Subroutine get: called CC=2 SCAR=7.0
Pod undocumented
83 145 my $obj = shift;
84 145 my $key = shift;
85 145 ▶ $obj->current->{$key} //= delete $obj->saved->{$key};
Condition66%2/3
$obj->current->{$key} //= delete $obj->saved->{$key}
l!l&&r!l&&!r
102430
MC/DC0%0/2
$obj->current->{$key} //= delete $obj->saved->{$key}
$obj->current->{$key} delete $obj->saved->{$key}
Truth table66%2/3
A //= B
A= $obj->current->{$key}B= delete $obj->saved->{$key}
ABresult
1X1
011
000
86 }
87  
88 156 ▶sub set {
Subroutine set: called CC=1 SCAR=1.0
Pod undocumented
89 156 my $obj = shift;
90 156 ▶ pairmap {
Subroutine __ANON__: called CC=7 SCAR=27.0
91 156 ▶ if (ref $a eq 'ARRAY' and ref $b eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq 'ARRAY' and ref $b eq 'ARRAY') { } 0 156 156
Condition33%1/3
ref $a eq "ARRAY" and ref $b eq "ARRAY"
!ll&&!rl&&r
15600
MC/DC0%0/2
ref $a eq "ARRAY" and ref $b eq "ARRAY"
ref $a eq "ARRAY" ref $b eq "ARRAY"
Truth table33%1/3
A and B
A= ref $a eq "ARRAY"B= ref $b eq "ARRAY"
ABresult
0X0
100
111
92 0 ▶ @$a == @$b or die;
Branch: 1 branch
BranchTrueFalseTotal
@$a == @$b or die 0 0 0
93 0 $obj->set(mesh $a, $b);
94 } else {
95 156 ▶ my $c = $obj->current->{$a} //= delete $obj->saved->{$a};
Condition33%1/3
$obj->current->{$a} //= delete $obj->saved->{$a}
l!l&&r!l&&!r
00156
MC/DC0%0/2
$obj->current->{$a} //= delete $obj->saved->{$a}
$obj->current->{$a} delete $obj->saved->{$a}
Truth table33%1/3
A //= B
A= $obj->current->{$a}B= delete $obj->saved->{$a}
ABresult
1X1
011
000
96 156 ▶ if (not defined $c or $c ne $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $c or $c ne $b) 156 0 156
Condition33%1/3
not defined $c or $c ne $b
l!l&&r!l&&!r
15600
MC/DC0%0/2
not defined $c or $c ne $b
not defined $c $c ne $b
Truth table33%1/3
not(A) or B
A= not defined $cB= $c ne $b
ABresult
1X1
011
000
97 156 $obj->current->{$a} = $b;
98 156 $obj->updated++;
99 }
100 }
101 156 } @_;
102 156 $obj;
103 }
104  
105 67 ▶sub json {
Subroutine json: called CC=1 SCAR=0.0
Pod undocumented
106 67 JSON->new->utf8->canonical->pretty;
107 }
108  
109 48 ▶sub open {
Subroutine open: called CC=12 SCAR=27.2
Pod undocumented
110 48 my $obj = shift;
111 48 ▶ my $file = $obj->name || return;
Condition50%1/2
$obj->name || (return)
l!l
480
MC/DC0%0/1
$obj->name || (return)
$obj->name
Truth table50%1/2
A
A= $obj->name
Aresult
11
00
112 48 ▶ if ($obj->clear and not $obj->readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->clear and not $obj->readonly) 0 48 48
Condition66%2/3
$obj->clear and not $obj->readonly
!ll&&!rl&&r
4710
MC/DC0%0/2
$obj->clear and not $obj->readonly
$obj->clear not $obj->readonly
Truth table66%2/3
A and not(B)
A= $obj->clearB= not $obj->readonly
ABresult
0X0
100
111
113 0 ▶ warn "created $file\n" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
114 0 ▶ CORE::open my $fh, '>', $file or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open my $fh, ">", $file or die "${file}: $!\n" 0 0 0
115 0 print $fh "{}\n";
116 }
117 48 $obj->{saved} = {};
118 48 $obj->{saved_order} = [];
119 48 ▶ if (CORE::open my $fh, $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::open my $fh, $file) 48 0 48
120 48 my $data = do { local $/; <$fh> };
121 48 ▶ $obj->load_data($data) if $data ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $data ne "" 23 25 48
122 48 warn "read cache from $file\n";
123 }
124 48 ▶ if (my $seed = $obj->seed) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $seed = $obj->seed) 5 43 48
125 5 ▶ if (%{$obj->saved}) {
Branch: 2 branches
BranchTrueFalseTotal
if (%{$obj->saved;}) { } 2 3 5
elsif (CORE::open my $fh, $seed) { } 3 0 3
126 2 warn "$seed: seed ignored (cache exists)\n";
127 } elsif (CORE::open my $fh, $seed) {
128 3 my $data = do { local $/; <$fh> };
129 3 ▶ if ($data ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "") 3 0 3
130 3 $obj->load_data($data);
131 3 $obj->seeded = 1;
132 3 warn "seed cache from $seed\n";
133 }
134 } else {
135 0 warn "$seed: $!\n";
136 }
137 }
138 48 $obj;
139 }
140  
141 26 ▶sub load_data {
Subroutine load_data: called CC=3 SCAR=11.1
Pod undocumented
142 26 my($obj, $data) = @_;
143 26 my $json = &json->decode($data);
144 26 ▶ if (ref $json eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $json eq 'HASH') { } 1 25 26
elsif (ref $json eq 'ARRAY') { } 25 0 25
145 1 $obj->{saved} = $json;
146 1 $obj->{saved_order} = []; # legacy format: no order info
147 } elsif (ref $json eq 'ARRAY') {
148 25 $obj->{saved} = +{ map @{$_}[0,1], @$json };
149 25 $obj->{saved_order} = [ map $_->[0], @$json ];
150 } else {
151 0 die "unexpected json data.";
152 }
153 26 $obj->{old_pos} = undef; # invalidate memoized position map
154 }
155  
156 6 ▶sub old_size {
Subroutine old_size: called CC=1 SCAR=0.0
Pod undocumented
157 6 my $obj = shift;
158 6 scalar @{$obj->saved_order};
159 }
160  
161 28 ▶sub old_position {
Subroutine old_position: called CC=2 SCAR=7.0
Pod undocumented
162 28 my($obj, $key) = @_;
163 28 ▶ my $pos = $obj->{old_pos} //= do {
Condition66%2/3
$obj->{"old_pos"} //= do { my $order = $obj->saved_order; +{map({$order->[$_], $_;} 0 .. $order->$#*)} }
l!l&&r!l&&!r
13150
MC/DC0%0/2
$obj->{"old_pos"} //= do { my $order = $obj->saved_order; +{map({$order->[$_], $_;} 0 .. $order->$#*)} }
$obj->{"old_pos"} do { my $order = $obj->saved_order; +{map({$order->[$_], $_;} 0 .. $order->$#*)} }
Truth table66%2/3
A //= B
A= $obj->{"old_pos"}B= do { my $order = $obj->saved_order; +{map({$order->[$_], $_;} 0 .. $order->$#*)} }
ABresult
1X1
011
000
164 15 my $order = $obj->saved_order;
165 15 +{ map { $order->[$_] => $_ } 0 .. $#$order };
166 };
167 28 $pos->{$key};
168 }
169  
170 18 ▶sub old_entries_slice {
Subroutine old_entries_slice: called CC=6 SCAR=18.0
Pod undocumented
171 18 my($obj, $lo, $hi) = @_;
172 18 my $order = $obj->saved_order;
173 18 ▶ $lo = 0 if $lo < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $lo < 0 1 17 18
174 18 ▶ $hi = $#$order if $hi > $#$order;
Branch: 1 branch
BranchTrueFalseTotal
if $hi > $order->$#* 1 17 18
175 18 my @out;
176 18 for my $k (@$order[$lo .. $hi]) {
177 26 ▶ my $v = $obj->saved->{$k} // $obj->current->{$k};
Condition66%2/3
$obj->saved->{$k} // $obj->current->{$k}
l!l&&r!l&&!r
2510
MC/DC0%0/2
$obj->saved->{$k} // $obj->current->{$k}
$obj->saved->{$k} $obj->current->{$k}
Truth table66%2/3
A // B
A= $obj->saved->{$k}B= $obj->current->{$k}
ABresult
1X1
011
000
178 26 ▶ push @out, [ $k, $v ] if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 26 0 26
179 }
180 18 @out;
181 }
182  
183 18 ▶sub update {
Subroutine update: called CC=22 SCAR=33.9
Pod undocumented
184 18 my $obj = shift;
185 18 ▶ return if $obj->readonly;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->readonly 2 16 18
186 16 ▶ my $file = $obj->name || return;
Condition100%2/2
$obj->name || (return)
l!l
610
MC/DC100%1/1
$obj->name || (return)
$obj->name
Truth table100%2/2
A
A= $obj->name
Aresult
11
00
187 6 ▶ if (not $obj->force_update and $obj->updated == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $obj->force_update and $obj->updated == 0) 3 3 6
Condition66%2/3
not $obj->force_update and $obj->updated == 0
!ll&&!rl&&r
033
MC/DC50%1/2
not $obj->force_update and $obj->updated == 0
not $obj->force_update $obj->updated == 0
Truth table66%2/3
not(A) and B
A= not $obj->force_updateB= $obj->updated == 0
ABresult
0X0
100
111
188 # accumulate: nothing changed, disk content is already right.
189 # otherwise: return only when there is nothing to purge.
190 0 ▶ return if not $obj->seeded and ($obj->accumulate or %{$obj->saved} == 0);
Branch: 1 branch
BranchTrueFalseTotal
if not $obj->seeded and $obj->accumulate || %{$obj->saved;} == 0 1 2 3
Condition50%3/6
not $obj->seeded and $obj->accumulate || %{$obj->saved;} == 0
!ll&&!rl&&r
201
$obj->accumulate or %{$obj->saved;} == 0
l!l&&r!l&&!r
100
MC/DC33%1/3
not $obj->seeded and $obj->accumulate || %{$obj->saved;} == 0
not $obj->seeded $obj->accumulate %{$obj->saved;} == 0
Truth table50%2/4
not(A) and B or C
A= not $obj->seededB= $obj->accumulateC= %{$obj->saved;} == 0
ABCresult
0XX0
1000
11X1
1011
191 }
192 5 ▶ if ($obj->accumulate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->accumulate) 1 4 5
193 # POD promises unused entries survive: adopt them unconditionally
194 1 for my $k (@{$obj->saved_order}, sort keys %{$obj->saved}) {
195 9 ▶ next if $obj->accessed->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->accessed->{$k} 5 4 9
196 4 ▶ defined(my $v = delete $obj->saved->{$k}) or next;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $v = delete $obj->saved->{$k}) or next 4 0 4
197 4 ▶ $obj->current->{$k} //= $v;
Condition33%1/3
$obj->current->{$k} //= $v
l!l&&r!l&&!r
040
MC/DC0%0/2
$obj->current->{$k} //= $v
$obj->current->{$k} $v
Truth table33%1/3
A //= B
A= $obj->current->{$k}B= $v
ABresult
1X1
011
000
198 4 $obj->access($k);
199 }
200 }
201 # A key is "used" when it was accessed this run, even if its value
202 # was never FETCHed (e.g. the run died before the output callback
203 # could read it back). Adopt such entries so they survive.
204 5 for my $k (grep { $obj->accessed->{$_} } keys %{$obj->saved}) {
205 0 ▶ $obj->current->{$k} //= delete $obj->saved->{$k};
Condition0%0/3
$obj->current->{$k} //= delete $obj->saved->{$k}
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj->current->{$k} //= delete $obj->saved->{$k}
$obj->current->{$k} delete $obj->saved->{$k}
Truth table0%0/3
A //= B
A= $obj->current->{$k}B= delete $obj->saved->{$k}
ABresult
1X1
011
000
206 }
207 5 ▶ while (my($k, $v) = each %{$obj->current}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$obj->current;}) 15 5 20
208 15 ▶ delete $obj->current->{$k} if not defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 14 15
209 }
210 5 ▶ if (not %{$obj->current}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$obj->current;}) 1 4 5
211 # A seeded cache may end its run without any access (e.g.
212 # --xlate-cache=create dies right after setup); persist the
213 # seed content instead of leaving the created file empty.
214 1 ▶ return $obj->checkpoint if $obj->seeded;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->seeded 1 0 1
215 0 return;
216 }
217 4 ▶ my $json_obj //= &json; # this is necessary to be called from DESTROY
Condition33%1/3
my $json_obj //= &json
l!l&&r!l&&!r
040
MC/DC0%0/2
my $json_obj //= &json
my $json_obj &json
Truth table33%1/3
A //= B
A= my $json_objB= &json
ABresult
1X1
011
000
218 4 ▶ if (CORE::open my $fh, '>', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::open my $fh, '>', $file) { } 4 0 4
219 4 ▶ my $data = $obj->format eq 'list' ? $obj->list_data : $obj->hash_data;
Branch: 1 branch
BranchTrueFalseTotal
$obj->format eq 'list' ? : 4 0 4
220 4 my $json = $json_obj->encode($data);
221 4 print $fh $json;
222 4 warn "write cache to $file\n";
223 } else {
224 0 warn "$file: $!\n";
225 }
226 }
227  
228 ##
229 ## Write out the merged state (saved and current, current wins)
230 ## WITHOUT purging unused entries. Called after each translation
231 ## batch so that an interrupted run does not lose paid API results.
232 ## The final write (update) keeps the purging semantics.
233 ##
234 ## Keys stored without going through the tie interface (no access())
235 ## are not visible to checkpoint.
236 ##
237 38 ▶sub checkpoint {
Subroutine checkpoint: called CC=14 SCAR=28.5
Pod undocumented
238 38 my $obj = shift;
239 38 ▶ return if $obj->readonly;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->readonly 1 37 38
240 37 ▶ my $file = $obj->name || return;
Condition50%1/2
$obj->name || (return)
l!l
370
MC/DC0%0/1
$obj->name || (return)
$obj->name
Truth table50%1/2
A
A= $obj->name
Aresult
11
00
241 37 my(@list, %done);
242 37 for my $key (@{$obj->saved_order}) {
243 59 ▶ next if $done{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$key}++ 0 59 59
244 59 ▶ my $v = $obj->current->{$key} // $obj->saved->{$key};
Condition66%2/3
$obj->current->{$key} // $obj->saved->{$key}
l!l&&r!l&&!r
29300
MC/DC0%0/2
$obj->current->{$key} // $obj->saved->{$key}
$obj->current->{$key} $obj->saved->{$key}
Truth table66%2/3
A // B
A= $obj->current->{$key}B= $obj->saved->{$key}
ABresult
1X1
011
000
245 59 ▶ push @list, [ $key => $v ] if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 59 0 59
246 }
247 37 for my $key (@{$obj->order}) {
248 111 ▶ next if $done{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$key}++ 32 79 111
249 79 my $v = $obj->current->{$key};
250 79 ▶ push @list, [ $key => $v ] if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 73 6 79
251 }
252 37 for my $key (sort keys %{$obj->saved}) { # legacy HASH caches
253 30 ▶ next if $done{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$key}++ 30 0 30
254 0 my $v = $obj->saved->{$key};
255 0 ▶ push @list, [ $key => $v ] if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 0 0 0
256 }
257 37 ▶ if (CORE::open my $fh, '>', $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::open my $fh, '>', $file) { } 37 0 37
258 37 print $fh &json->encode(\@list);
259 } else {
260 0 warn "$file: $!\n";
261 }
262 }
263  
264 0 ▶sub hash_data {
Subroutine hash_data: not called CC=1 SCAR=6.9
Pod undocumented
265 0 my $obj = shift;
266 0 $obj->current;
267 }
268  
269 4 ▶sub list_data {
Subroutine list_data: called CC=5 SCAR=16.4
Pod undocumented
270 4 my $obj = shift;
271 4 my %hash = %{$obj->current};
272 4 my @list;
273 4 for my $key (@{$obj->order}) {
274 15 ▶ next unless exists $hash{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{$key} 1 14 15
275 14 push @list, [ $key => delete $hash{$key} ];
276 }
277 4 for my $key (sort keys %hash) {
278 0 warn "$key: not in order list.";
279 0 push @list, [ $key => delete $hash{$key} ];
280 }
281 4 ▶ die if %hash;
Branch: 1 branch
BranchTrueFalseTotal
if %hash 0 4 4
282 4 \@list;
283 }
284  
285 39 1;
« blib/lib/App/Greple/xlate/deepl.pm ↑ Summary lib/App/Greple/xlate/Mask.pm »