DocSet/Cache.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 297 CC 38 SCAR 27.9
CC
38
Cov
0%
parent_node
42.0
get
30.0
unset
30.0
CRAP 16.3
1 package DocSet::Cache;
2  
3 use strict;
4 use warnings;
5  
6 use DocSet::RunTime;
7 use DocSet::Util;
8 use Storable;
9 use Carp;
10  
11 my %attrs = map {$_ => 1} qw(toc meta order child_cache_path);
12  
13 # $update == 1 marks the cache as dirty, so it'll be always written to the disk
14 # $purge == 1 deletes the existing cache file if such exists
15 sub new {
16 my ($class, $path, $update, $purge) = @_;
17  
18 die "no cache path specified" unless defined $path;
19  
20 my $self = bless {
21 path => $path,
22 dirty => 0,
23 cache => {},
24 read_error => '',
25 }, ref($class)||$class;
26  
27 if ($purge) {
28 $self->purge();
29 }
30 else {
31 $self->read();
32 }
33  
34 if ($purge || $update) {
35 # we will reconstruct the ids order to make sure to reflect the
36 # changes in added and removed items (and those who have changed
37 # their order)
38 $self->{cache}{_ordered_ids} = [];
39  
40 # XXX: because we rewrite _order_ids on each invocation, we have to sync
41 # the cache to the disk, even if it didn't really change. In the
42 # future we may do somechecksum check and write the file only if
43 # the checksum has changed, so at this moment the dirty mechanism
44 # does nothing
45 $self->{dirty} = 1;
46 }
47  
48 return $self;
49 }
50  
51 sub path {
52 my ($self) = @_;
53 $self->{path};
54 }
55  
56 # returns the read error if any
57 sub read_error {
58 shift->{read_error};
59 }
60  
61 # check whether the cache file exists and readable
62 sub can_read {
63 -e shift->{path} && -r _;
64 }
65  
66 sub purge {
67 my $self = shift;
68 if (-e $self->{path}) {
69 note "!!! Removing cache file $self->{path}";
70 unlink $self->{path};
71 }
72 }
73  
74 sub read {
75 my ($self) = @_;
76  
77 if (-w $self->{path} && DocSet::RunTime::has_storable_module()) {
78 note "+++ Reading cache from $self->{path}";
79 eval {
80 $self->{cache} = Storable::retrieve($self->{path});
81 };
82 if ($@) {
83 # nuke the cache file
84 note "failed to retrieve $self->{path}: $@";
85 $self->{read_error} = "$@";
86 $self->purge();
87 }
88 } else {
89 note "+++ Initializing a new cache for $self->{path}";
90 }
91 }
92  
93 sub write {
94 my ($self) = @_;
95  
96 if (DocSet::RunTime::has_storable_module()) {
97 note "+++ Storing the docset's cache to $self->{path}";
98 Storable::store($self->{cache}, $self->{path});
99 $self->{dirty} = 0; # mark as synced (clean)
100 }
101 }
102  
103 sub add {
104 my ($self, $id) = @_;
105 push @{ $self->{cache}{_ordered_ids} }, $id;
106 $self->{cache}{$id}{seq} = $#{ $self->{cache}{_ordered_ids} };
107 }
108  
109 # set a cache entry (overrides a prev entry if any exists)
110 sub set {
111 my ($self, $id, $attr, $data, $hidden) = @_;
112  
113 croak "must specify a unique id" unless defined $id;
114 croak "must specify an attribute" unless defined $attr;
115 croak "unknown attribute $attr" unless exists $attrs{$attr};
116  
117 # # remember the addition order (unless it's an update)
118 # unless (exists $self->{cache}{$id}) {
119 # push @{ $self->{cache}{_ordered_ids} }, $id;
120 # $self->{cache}{$id}{seq} = $#{ $self->{cache}{_ordered_ids} };
121 # }
122  
123 $self->{cache}{$id}{$attr} = $data;
124 $self->{cache}{$id}{_hidden} = $hidden;
125 $self->{dirty} = 1;
126 }
127  
128 # get a cache entry
129 sub get {
130 my ($self, $id, $attr) = @_;
131  
132 croak "must specify a unique id" unless defined $id;
133 croak "must specify an attribute" unless defined $attr;
134 croak "unknown attribute $attr" unless exists $attrs{$attr};
135  
136 if (exists $self->{cache}{$id} && exists $self->{cache}{$id}{$attr}) {
137 return $self->{cache}{$id}{$attr};
138 }
139 }
140  
141 # check whether a cached entry exists
142 sub is_cached {
143 my ($self, $id, $attr) = @_;
144  
145 croak "must specify a unique id" unless defined $id;
146 croak "must specify an attribute" unless defined $attr;
147 croak "unknown attribute $attr" unless exists $attrs{$attr};
148  
149 exists $self->{cache}{$id}{$attr};
150 }
151  
152 # invalidate cache (i.e. when a complete rebuild is forced)
153 sub invalidate {
154 my ($self) = @_;
155  
156 $self->{cache} = {};
157 }
158  
159 # delete an entry in the cache
160 sub unset {
161 my ($self, $id, $attr) = @_;
162  
163 croak "must specify a unique id" unless defined $id;
164 croak "must specify an attribute" unless defined $attr;
165 croak "unknown attribute $attr" unless exists $attrs{$attr};
166  
167 if (exists $self->{cache}{$id}{$attr}) {
168 delete $self->{cache}{$id}{$attr};
169 $self->{dirty} = 1;
170 }
171  
172 }
173  
174 sub is_hidden {
175 my ($self, $id) = @_;
176 #print "$id is hidden\n" if $self->{cache}{$id}{_hidden};
177 return $self->{cache}{$id}{_hidden};
178 }
179  
180 # return the sequence number of $id in the list of linked objects (0..N)
181 sub id2seq {
182 my ($self, $id) = @_;
183 croak "must specify a unique id" unless defined $id;
184 if (exists $self->{cache}{$id}) {
185 return $self->{cache}{$id}{seq};
186 }
187 else {
188 # this shouldn't happen!
189 Carp::croak("Cannot find '$id' in $self->{path} cache");
190 }
191  
192 }
193  
194 # return the $id at the place $seq in the list of linked objects (0..N)
195 sub seq2id {
196 my ($self, $seq) = @_;
197  
198 croak "must specify a seq number" unless defined $seq;
199 if ($self->{cache}{_ordered_ids} && defined $self->{cache}{_ordered_ids}->[$seq]) {
200 return $self->{cache}{_ordered_ids}->[$seq];
201 }
202 else {
203 Carp::croak("Cannot find '$seq' in $self->{path} cache");
204 }
205 }
206  
207  
208 sub ordered_ids {
209 my ($self) = @_;
210 return @{ $self->{cache}{_ordered_ids}||[] };
211 }
212  
213 sub total_ids {
214 my ($self) = @_;
215 return scalar @{ $self->{cache}{_ordered_ids}||[] };
216 }
217  
218 # remember the meta data of the index node
219 sub index_node {
220 my ($self) = shift;
221  
222 if (@_) {
223 my %args = @_;
224 my %required = map { $_ => 1} qw(id title stitle);
225  
226 for (keys %required) {
227 croak "must specify the index_node's $_" unless exists $args{$_};
228 }
229 # set
230 $self->{cache}{_index} = \%args;
231 }
232 else {
233 # get
234 return exists $self->{cache}{_index}
235 ? $self->{cache}{_index}
236 : undef;
237 }
238  
239 }
240  
241 # set/get the path to the parent cache
242 sub parent_node {
243 my ($self) = shift;
244  
245 if (@_) {
246 # set
247 my ($cache_path, $id, $rel_path) = @_;
248 croak "must specify a path to the parent cache"
249 unless defined $cache_path;
250 croak "must specify a path relative to parent docset"
251 unless defined $rel_path;
252 croak "must specify a parent id" unless defined $id;
253 $self->{cache}{_parent}{cache_path} = $cache_path;
254 $self->{cache}{_parent}{id} = $id;
255 $self->{cache}{_parent}{rel_path} = $rel_path;
256 }
257 else {
258 # get
259 return exists $self->{cache}{_parent}
260 ? ($self->{cache}{_parent}{cache_path},
261 $self->{cache}{_parent}{id},
262 $self->{cache}{_parent}{rel_path})
263 : (undef, undef, undef);
264 }
265 }
266  
267 # get the child node's cache if any
268 sub child_cache_path {
269 my ($self, $id) = @_;
270 croak "must specify a id" unless defined $id;
271  
272 return exists $self->{cache}{$id}{child_cache_path}
273 ? $self->{cache}{$id}{child_cache_path}
274 : undef;
275 }
276  
277  
278 # set/get the path to the node_groups cache
279 sub node_groups {
280 my ($self) = shift;
281  
282 if (@_) { # set
283 $self->{cache}{_node_groups} = shift;
284 }
285 else { # get
286 return $self->{cache}{_node_groups};
287 }
288 }
289  
290 sub is_dirty { shift->{dirty};}
291  
292 sub DESTROY {
293 my ($self) = @_;
294  
295 # flush the cache if destroyed before having a chance to sync to the disk
296 $self->write if $self->is_dirty;
297 }
298  
299 1;
300 __END__