blib/lib/File/Mosaic.pm

filter: Statementstmt 93.7% 195 / 208 Branchbran 59.7% 43 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 74.4% 32 / 43 Podpod 100% 10 / 10 totaltotal 84.0% 280 / 333 CC 44 SCAR 11.7
CC
44
Cov
85%
insert_before
6.6
insert_after
6.6
new
5.2
CRAP 3.2
↑ Summary
1 # $Id: Mosaic.pm 2466 2006-06-14 22:30:52Z cboumeno $
2 ######################################################################
3 #
4 # This program is Copyright 2006-2007 by Christopher Boumenot
5 # <boumenot@gmail.com>.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the same license as Perl.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 #
14 ######################################################################
15  
16 package File::Mosaic;
17  
18 2 require 5.006;
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 our $VERSION = '0.03';
23  
24 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Log::Log4perl qw(:easy get_logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 8 ▶sub new {
Subroutine new: called CC=5 SCAR=16.4
Pod documented
34 8 my ($class, %options) = @_;
35  
36 8 ▶ unless (defined $options{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{"filename"}) 0 8 8
37 0 confess "%Error: the parameter 'filename' is mandatory!\n";
38 }
39  
40 8 ▶ unless (defined $options{mosaic_directory}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{"mosaic_directory"}) 0 8 8
41 0 confess "%Error: the paramter 'mosaic_directory' is mandatory\n";
42 }
43  
44 8 my $self = {
45 'mosaic_file' => '.mosaics',
46 '_mosaics' => {},
47 '_count' => 0,
48 '_is_closed' => 0,
49 %options,
50 };
51  
52 8 ▶ unless (-d $self->{mosaic_directory}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"mosaic_directory"}) 1 7 8
53 1 File::Path::mkpath($self->{mosaic_directory});
54 }
55  
56 8 $self->{mosaic_path} = File::Spec->catfile($self->{mosaic_directory}, $self->{mosaic_file});
57  
58 8 bless $self, $class;
59  
60 8 ▶ if (-f $self->{mosaic_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->{"mosaic_path"}) 7 1 8
61 7 $self->_load_mosaic_directory();
62 }
63  
64 8 return $self;
65 }
66  
67 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
68 8 my ($self) = @_;
69 8 ▶ unless ($self->{_is_closed}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_is_closed"}) 2 6 8
70 2 $self->close();
71 }
72 }
73  
74 5 ▶sub append {
Subroutine append: called CC=3 SCAR=11.2
Pod documented
75 5 my $self = shift;
76 5 my %options = (
77 tag => undef,
78 mosaic => undef,
79 @_,
80 );
81  
82 5 my $tag = $options{tag};
83 5 my $mosaic = $options{mosaic};
84  
85 5 ▶ confess "%Error: 'tag' is a mandatory parameter!\n" unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 5 5
86 5 ▶ confess "%Error: 'mosaic' is a mandatory parameter!\n" unless defined $mosaic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mosaic 0 5 5
87  
88 0 ▶ DEBUG( sub { "append: tag => $tag\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
89  
90 5 $self->{_mosaics}->{$tag}->{data} = $mosaic;
91 5 $self->{_mosaics}->{$tag}->{count} = $self->{_count}++;
92  
93 5 $self->_add_digest_tag($tag, $mosaic);
94 }
95  
96 1 ▶sub insert_before {
Subroutine insert_before: called CC=6 SCAR=18.8
Pod documented
97 1 my $self = shift;
98 1 my %options = (
99 tag => undef,
100 before_tag => undef,
101 mosaic => undef,
102 @_,
103 );
104  
105 1 my $tag = $options{tag};
106 1 my $mosaic = $options{mosaic};
107 1 my $before_tag = $options{before_tag};
108  
109 1 ▶ confess "%Error: 'tag' is a mandatory parameter!\n" unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 1 1
110 1 ▶ confess "%Error: 'mosaic' is a mandatory parameter!\n" unless defined $mosaic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mosaic 0 1 1
111 1 ▶ confess "%Error: 'before_tag' is a mandatory parameter!\n" unless defined $before_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $before_tag 0 1 1
112  
113 1 ▶ confess "%Error: the tag '$tag' already exist!" if $self->_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_valid_tag($tag) 0 1 1
114 1 ▶ confess "%Error: the tag '$before_tag' does not exists!" unless $self->_valid_tag($before_tag);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid_tag($before_tag) 0 1 1
115  
116 0 ▶ DEBUG( sub { "insert_before: tag => $tag, before_tag => $before_tag\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
117  
118 1 my $count = $self->{_mosaics}->{$before_tag}->{count};
119 1 $self->_insert($tag, $count, $mosaic);
120 }
121  
122 1 ▶sub insert_after {
Subroutine insert_after: called CC=6 SCAR=18.8
Pod documented
123 1 my $self = shift;
124 1 my %options = (
125 tag => undef,
126 after_tag => undef,
127 mosaic => undef,
128 @_,
129 );
130  
131 1 my $tag = $options{tag};
132 1 my $mosaic = $options{mosaic};
133 1 my $after_tag = $options{after_tag};
134  
135 1 ▶ confess "%Error: 'tag' is a mandatory parameter!\n" unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 1 1
136 1 ▶ confess "%Error: 'mosaic' is a mandatory parameter!\n" unless defined $mosaic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mosaic 0 1 1
137 1 ▶ confess "%Error: 'after_tag' is a mandatory parameter!\n" unless defined $after_tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $after_tag 0 1 1
138  
139 1 ▶ confess "%Error: the tag '$tag' already exist!" if $self->_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_valid_tag($tag) 0 1 1
140 1 ▶ confess "%Error: the tag '$after_tag' does not exist!" unless $self->_valid_tag($after_tag);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid_tag($after_tag) 0 1 1
141  
142 0 ▶ DEBUG( sub { "insert_after: tag => $tag, after_tag => $after_tag\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
143  
144 1 my $count = $self->{_mosaics}->{$after_tag}->{count} + 1;
145 1 $self->_insert($tag, $count, $mosaic);
146 }
147  
148 1 ▶sub replace {
Subroutine replace: called CC=4 SCAR=14.4
Pod documented
149 1 my $self = shift;
150 1 my %options = (
151 tag => undef,
152 mosaic => undef,
153 @_,
154 );
155  
156 1 my $tag = $options{tag};
157 1 my $mosaic = $options{mosaic};
158  
159 1 ▶ confess "%Error: 'tag' is a mandatory parameter!\n" unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 1 1
160 1 ▶ confess "%Error: 'mosaic' is a mandatory parameter!\n" unless defined $mosaic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mosaic 0 1 1
161 1 ▶ confess "%Error: the tag '$tag' does not exist!" unless $self->_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid_tag($tag) 0 1 1
162  
163 0 ▶ DEBUG( sub { "replace: tag => $tag\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
164  
165 1 $self->{_mosaics}->{$tag}->{data} = $mosaic;
166 1 $self->{_mosaics}->{$tag}->{sum} = $self->_digest_tag($tag, $mosaic);
167 }
168  
169 1 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.3
Pod documented
170 1 my $self = shift;
171 1 my %options = (
172 tag => undef,
173 @_,
174 );
175  
176 1 my $tag = $options{tag};
177  
178 1 ▶ confess "%Error: 'tag' is a mandatory parameter!\n" unless defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 1 1
179 1 ▶ confess "%Error: the tag '$tag' does not exist!" unless $self->_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid_tag($tag) 0 1 1
180  
181 0 ▶ DEBUG( sub { "remove: tag => $tag\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
182  
183 1 $self->{_count}--;
184  
185 1 delete $self->{_mosaics}->{$tag};
186  
187 1 $self->_increment_counts($tag, -1);
188 }
189  
190 17 ▶sub fetch {
Subroutine fetch: called CC=3 SCAR=11.4
Pod documented
191 17 my $self = shift;
192 17 my %options = (
193 tag => undef,
194 @_,
195 );
196  
197 17 ▶ my $tag = $options{tag} or confess "%Error: 'tag' is a mandatory parameter!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $tag = $options{"tag"} or confess("%Error: 'tag' is a mandatory parameter!\n") 17 0 17
198 17 ▶ confess "%Error: the tag '$tag' does not exist!" unless $self->_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid_tag($tag) 0 17 17
199  
200 0 ▶ DEBUG( sub { "fetch: tag => $tag\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
201  
202 17 my $count = $self->{_mosaics}->{$tag}->{count};
203 17 return $self->{_mosaics}->{$tag}->{data};
204 }
205  
206 3 ▶sub fetch_tags {
Subroutine fetch_tags: called CC=3 SCAR=11.0
Pod documented
207 3 my $self = shift;
208  
209 3 my @tags;
210 3 for (sort {$self->{_mosaics}->{$a}->{count} <=>
211 26 $self->{_mosaics}->{$b}->{count}} keys %{$self->{_mosaics}}) {
212 17 push @tags, $_;
213 }
214  
215 3 ▶ return (wantarray) ? @tags : \@tags;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
216 }
217  
218 1 ▶sub reorder_tags {
Subroutine reorder_tags: called CC=5 SCAR=16.3
Pod documented
219 1 my $self = shift;
220 1 my %options = (
221 tags => undef,
222 @_,
223 );
224  
225 1 ▶ my $tags = $options{tags} or confess "%Error: 'tags' is a mandatory parameter!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $tags = $options{"tags"} or confess("%Error: 'tags' is a mandatory parameter!\n") 1 0 1
226  
227 1 for my $tag (@$tags) {
228 6 ▶ confess "%Error: the tag '$tag' does not exist!" unless $self->_valid_tag($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_valid_tag($tag) 0 6 6
229 }
230  
231 1 for my $i (0..scalar(@$tags)-1) {
232 6 my $tag = $tags->[$i];
233 6 $self->{_mosaics}->{$tag}->{count} = $i;
234 }
235 }
236  
237  
238 8 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
239 8 my ($self) = @_;
240  
241 8 $self->_write_file();
242 8 $self->_write_mosaics();
243 8 $self->_write_mosaic_file();
244
245 8 $self->{_is_closed} = 1;
246 }
247  
248 ##################################################
249 ## PRIVATE
250 ##################################################
251  
252 8 ▶sub _write_file {
Subroutine _write_file: called CC=3 SCAR=11.3
253 8 my ($self) = @_;
254  
255 8 ▶ my $fouth = IO::File->new(">$self->{filename}") or
Branch: 1 branch
BranchTrueFalseTotal
my $fouth = "IO::File"->new(qq[>$self->{"filename"}]) or confess(qq[%Error: $! '$self->{"filename"}'!\n]) 8 0 8
256 confess "%Error: $! '$self->{filename}'!\n";
257  
258 0 ▶ DEBUG( sub { "_write_file:\n" . Dumper($self) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
259
260 8 for my $tag (sort { $self->{_mosaics}->{$a}->{count} <=>
261 75 $self->{_mosaics}->{$b}->{count} }
262 8 keys %{$self->{_mosaics}}) {
263
264 0 ▶ DEBUG( sub { "_write_file: tag => $tag\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
265 47 print $fouth $self->{_mosaics}->{$tag}->{data};
266 }
267  
268 8 $fouth->close();
269 }
270  
271 8 ▶sub _write_mosaics {
Subroutine _write_mosaics: called CC=3 SCAR=11.0
272 8 my ($self) = @_;
273  
274 8 for my $tag (keys %{$self->{_mosaics}}) {
275 47 my $mosaic = $self->{_mosaics}->{$tag}->{data};
276 47 my $sum = $self->_digest_tag($tag, $mosaic);
277 47 my $fn = File::Spec->catfile($self->{mosaic_directory}, $sum);
278  
279 47 ▶ my $fouth = IO::File->new(">$fn") or
Branch: 1 branch
BranchTrueFalseTotal
my $fouth = "IO::File"->new(">$fn") or confess("%Error: $! '${fn}'!\n") 47 0 47
280 confess "%Error: $! '$fn'!\n";
281  
282 47 print $fouth $mosaic;
283
284 47 $fouth->close();
285 }
286 }
287  
288 8 ▶sub _write_mosaic_file {
Subroutine _write_mosaic_file: called CC=1 SCAR=0.0
289 8 my ($self) = @_;
290 8 store $self->{_mosaics}, $self->{mosaic_path};
291 }
292  
293 29 ▶sub _valid_tag {
Subroutine _valid_tag: called CC=3 SCAR=11.0
294 29 my ($self, $tag) = @_;
295 29 ▶ my $rc = (defined $self->{_mosaics}->{$tag}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'_mosaics'}{$tag} ? : 27 2 29
296 0 ▶ DEBUG(sub { "_valid_tag: tag => $tag\n" . Dumper($self) }) unless $rc;
Subroutine __ANON__: not called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless $rc 2 27 29
297 29 return $rc;
298 }
299  
300 55 ▶sub _digest_tag {
Subroutine _digest_tag: called CC=1 SCAR=0.0
301 55 my ($self, $tag, $mosaic) = @_;
302  
303 55 my $ctx = Digest::MD5->new;
304 55 $ctx->add($tag);
305 55 $ctx->add($mosaic);
306 55 my $sum = $ctx->hexdigest;
307  
308 55 return $sum;
309 }
310  
311 7 ▶sub _add_digest_tag {
Subroutine _add_digest_tag: called CC=1 SCAR=0.0
312 7 my ($self, $tag, $mosaic) = @_;
313  
314 7 my $sum = $self->_digest_tag($tag, $mosaic);
315 7 $self->{_mosaics}->{$tag}->{sum} = $sum;
316 }
317  
318 2 ▶sub _insert {
Subroutine _insert: called CC=1 SCAR=0.0
319 2 my ($self, $tag, $count, $mosaic) = @_;
320  
321 0 ▶ DEBUG( sub { "_insert: tag => $tag, count => $count\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
322  
323 2 $self->_increment_counts($count);
324 2 $self->{_mosaics}->{$tag}->{count} = $count;
325 2 $self->{_mosaics}->{$tag}->{data} = $mosaic;
326  
327 2 $self->_add_digest_tag($tag, $mosaic);
328 }
329  
330 3 ▶sub _increment_counts {
Subroutine _increment_counts: called CC=4 SCAR=13.9
331 3 my ($self, $count, $offset) = @_;
332  
333 3 ▶ $offset = 1 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 2 1 3
334
335 3 for my $tag (keys %{$self->{_mosaics}}) {
336 17 ▶ if ($self->{_mosaics}->{$tag}->{count} >= $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_mosaics"}{$tag}{"count"} >= $count) 11 6 17
337 # DEBUG( sub { "_increment
338 11 $self->{_mosaics}->{$tag}->{count} += $offset;
339 }
340 }
341 }
342  
343 7 ▶sub _load_mosaic_directory {
Subroutine _load_mosaic_directory: called CC=3 SCAR=11.0
344 7 my ($self) = @_;
345  
346 7 $self->_load_mosaic_file();
347
348 7 my $count = 0;
349 7 for my $tag (sort { $self->{_mosaics}->{$a}->{count} <=>
350 66 $self->{_mosaics}->{$b}->{count} }
351 7 keys %{$self->{_mosaics}}) {
352  
353 41 my $count = $self->{_mosaics}->{$tag}->{count};
354 41 my $sum = $self->{_mosaics}->{$tag}->{sum};
355  
356 0 ▶ DEBUG( sub { "_load_mosaic_directory: tag => $tag, count => $count\n" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
357  
358 41 my $mfn = File::Spec->catfile($self->{mosaic_directory}, $sum);
359 41 ▶ confess "%Error: a mosaic file, '$mfn', is missing!\n" unless -f $mfn;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $mfn 0 41 41
360  
361 41 $self->{_mosaics}->{$tag}->{count} = $count;
362 41 $self->{_mosaics}->{$tag}->{data} = $self->_slurp_file($mfn);
363 }
364 }
365  
366 7 ▶sub _load_mosaic_file {
Subroutine _load_mosaic_file: called CC=2 SCAR=7.2
367 7 my ($self) = @_;
368  
369 7 ▶ $self->{_mosaics} = retrieve($self->{mosaic_path}) or
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_mosaics"} = retrieve($self->{"mosaic_path"}) or confess(qq[%Error: $! '$self->{"mosaic_path"}'!\n]) 7 0 7
370 confess "%Error: $! '$self->{mosaic_path}'!\n";
371 }
372  
373 41 ▶sub _slurp_file {
Subroutine _slurp_file: called CC=2 SCAR=7.0
374 41 my ($self, $fn) = @_;
375
376 41 local $/;
377 41 ▶ my $finh = IO::File->new($fn) or
Branch: 1 branch
BranchTrueFalseTotal
my $finh = "IO::File"->new($fn) or confess("%Error: $! '${fn}'!\n") 41 0 41
378 confess "%Error: $! '$fn'!\n";
379  
380 41 my $mosaic = <$finh>;
381  
382 41 $finh->close();
383  
384 41 return $mosaic;
385 }
386  
387  
388 #######################################################################
389 2 1;
390 __END__
↑ Summary