blib/lib/ApacheLog/Parser/SkipList.pm

filter: Statementstmt 22.7% 37 / 163 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 40.0% 10 / 25 Podpod 100% 6 / 6 totaltotal 20.3% 53 / 261 CC 34 SCAR 30.9
CC
34
Cov
16%
_open_read
56.0
merge
42.0
set_config
30.0
CRAP 22.0
« bin/cron.loghack ↑ Summary
1 package ApacheLog::Parser::SkipList;
2 1 $VERSION = v0.0.1;
3  
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 ApacheLog::Parser::SkipList - a list of skippable lines
14  
15 =head1 SYNOPSIS
16  
17 my $skipper = ApacheLog::Parser::SkipList->new;
18 my %regexps = $skipper->set_config(\%conf);
19 # you'll typically build %regexps into some condition sub
20  
21 my $sw = $skipper->new_writer($skipfile);
22 my $counter = 0;
23 while(...) {
24 ...
25 $counter++;
26 $some_condition and $sw->skip($counter);
27 }
28  
29 Later, while reading a file with a prepared skiplist:
30  
31 my $skipper = ApacheLog::Parser::SkipList->new;
32 $skipper->set_config(\%conf);
33  
34 my $sr = $skipper->new_reader($skipfile);
35 my $skip = $sr->next_skip;
36 my $counter = 0;
37 while(my $line = <$fh>) {
38 if(++$counter == $skip) {
39 $counter += $sr->skip_lines($fh);
40 $skip = $sr->next_skip;
41 next;
42 }
43
44 # then do more expensive stuff
45 ...
46 }
47  
48 =cut
49  
50 =head2 new
51  
52 my $skipper = ApacheLog::Parser::SkipList->new;
53  
54 =cut
55  
56 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
57 0 my $package = shift;
58 0 ▶ my $class = ref($package) || $package;
Condition0%0/3
ref $package || $package
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $package || $package
ref $package $package
Truth table0%0/3
A || B
A= ref $packageB= $package
ABresult
1X1
011
000
59 0 my $self = {};
60 0 bless($self, $class);
61 0 return($self);
62 } # end subroutine new definition
63 ########################################################################
64  
65 =head2 set_config
66  
67 my %regexps = $skipper->set_config(\%conf);
68  
69 =cut
70  
71 0 ▶sub set_config {
Subroutine set_config: not called CC=5 SCAR=34.0
Pod documented
72 0 my $self = shift;
73 0 my ($conf) = (@_);
74  
75 my $handle = {
76 file => {
77 0 ▶ ext => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 my $s = join('|', @_);
79 0 return(qr/\.(?:$s)$/);
80 },
81 0 ▶ path => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my $s = join('|', @_);
83 0 return(qr/^(?:$s)/);
84 },
85 },
86 0 };
87 0 my %reg;
88 0 foreach my $k (keys(%$conf)) {
89 0 my $ref = $handle->{$k};
90 0 my @ans;
91 0 foreach my $bit (sort({$b cmp $a} keys(%{$conf->{$k}}))) {
92 0 ▶ $ref->{$bit} or croak("no handler for $k/$bit config");
Branch: 1 branch
BranchTrueFalseTotal
$ref->{$bit} or croak("no handler for $k/$bit config") 0 0 0
93 0 my $list = $conf->{$k}{$bit};
94 0 push(@ans, $ref->{$bit}->(@$list));
95 }
96 0 ▶ if(@ans) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ans) 0 0 0
97 0 my $s = join("|", @ans);
98 0 $reg{$k} = qr/(?:$s)/;
99 }
100 }
101  
102 0 $self->{config} = Digest::MD5::md5_hex(YAML::Dump($conf));
103 0 $self->{regexps} = \%reg;
104 0 return(%reg);
105 } # end subroutine set_config definition
106 ########################################################################
107  
108 =head2 get_matcher
109  
110 my $subref = $skipper->get_matcher;
111  
112 =cut
113  
114 0 ▶sub get_matcher {
Subroutine get_matcher: not called CC=4 SCAR=30.0
Pod documented
115 0 my $self = shift;
116  
117 0 my %re = %{$self->{regexps}};
118  
119 0 my $code = '';
120 0 foreach my $type (qw(file)) {
121 0 ▶ $re{$type} or next;
Branch: 1 branch
BranchTrueFalseTotal
$re{$type} or next 0 0 0
122 0 $code .= "(\$v->[$type] =~ m#$re{$type}#) and return(1);";
123 }
124 #die "compiling $code";
125 0 my $doskip = eval("
126 use ApacheLog::Parser qw(:fields);
127 my \$code = sub {my \$v = shift; $code};
128 no ApacheLog::Parser;
129 \$code");
130 0 ▶ $@ and die "cannot build doskip sub -- $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "cannot build doskip sub -- $@" 0 0 0
131 0 return($doskip);
132 } # end subroutine get_matcher definition
133 ########################################################################
134  
135 =head2 merge
136  
137 Merge existing files (adjusting the offsets.)
138  
139 $skipper->merge($dest, $file, $offset, $file);
140  
141 =cut
142  
143 1 ▶ use constant flag => 2**31;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 0 ▶sub merge {
Subroutine merge: not called CC=6 SCAR=37.4
Pod documented
146 0 my $self = shift;
147 0 my ($dest, @parts) = @_;
148  
149 0 my $outfh = $self->_open_write($dest);
150  
151 # just slurp the entire first bit
152 0 my $first_part = shift(@parts);
153 {
154 0 my $fh = $self->_open_read($first_part);
155 0 print $outfh readline($fh);
156 }
157  
158 0 ▶ while(@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 0 0 0
159 0 my ($offset, $part) = (shift(@parts), shift(@parts));
160 0 my $fh = $self->_open_read($part);
161  
162 0 ▶ while(not eof($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 0 0 0
163 0 my $v;
164 0 ▶ (read($fh, $v, 4) == 4) or die "gah";
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $v, 4) == 4 or die "gah" 0 0 0
165 0 my $n = unpack("N", $v);
166  
167 # if it is flagged, there's another byte
168 0 ▶ if($n & flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n & 2147483648) { } 0 0 0
169 0 my $val;
170 0 ▶ (read($fh, $val, 4) == 4) or die "gah";
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $val, 4) == 4 or die "gah" 0 0 0
171 0 $n &= ~flag; # de-mangle it
172 0 $n += $offset;
173 0 $n |= flag; # re-mangle
174 0 print $outfh pack('N', $n), $val;
175 }
176 else {
177 0 $n += $offset;
178 0 print $outfh pack('N', $n);
179 }
180 }
181 }
182 } # end subroutine merge definition
183 ########################################################################
184  
185 =head2 new_writer
186  
187 my $sw = $skipper->new_writer($skipfile);
188  
189 =cut
190  
191 0 ▶sub new_writer {
Subroutine new_writer: not called CC=1 SCAR=6.9
Pod documented
192 0 my $self = shift;
193 0 my ($filename) = @_;
194  
195 0 my $fh = $self->_open_write($filename);
196 0 my $writer = __PACKAGE__ . '::Writer';
197 0 return($writer->new($fh));
198 } # end subroutine new_writer definition
199 ########################################################################
200 0 ▶sub _open_write {
Subroutine _open_write: not called CC=3 SCAR=24.8
201 0 my $self = shift;
202 0 my ($filename) = @_;
203  
204 my $conf_check = $self->{config} or
205 0 ▶ croak("cannot make a writer without a config");
Branch: 1 branch
BranchTrueFalseTotal
my $conf_check = $self->{"config"} or croak("cannot make a writer without a config") 0 0 0
206  
207 0 ▶ open(my $fh, '>', $filename) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or croak("cannot open '${filename}' for writing $!") 0 0 0
208 croak("cannot open '$filename' for writing $!");
209  
210 0 print $fh $conf_check;
211 0 return($fh);
212 }
213  
214 =head2 new_reader
215  
216 my $sr = $skipper->new_reader($skipfile);
217  
218 =cut
219  
220 0 ▶sub new_reader {
Subroutine new_reader: not called CC=1 SCAR=6.9
Pod documented
221 0 my $self = shift;
222 0 my ($filename) = @_;
223  
224 0 my $fh = $self->_open_read($filename);
225 0 my $reader = __PACKAGE__ . '::Reader';
226 0 return($reader->new($fh));
227 } # end subroutine new_reader definition
228 ########################################################################
229 0 ▶sub _open_read {
Subroutine _open_read: not called CC=7 SCAR=40.3
230 0 my $self = shift;
231 0 my ($filename) = @_;
232 my $conf_check = $self->{config} or
233 0 ▶ croak("cannot make a reader without a config");
Branch: 1 branch
BranchTrueFalseTotal
my $conf_check = $self->{"config"} or croak("cannot make a reader without a config") 0 0 0
234  
235 0 ▶ open(my $fh, '<', $filename) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or croak("cannot open '${filename}' for reading $!") 0 0 0
236 croak("cannot open '$filename' for reading $!");
237  
238 0 my $verify;
239 0 my $ok = read($fh, $verify, 32);
240 0 ▶ (($ok||0) == 32) or
Branch: 2 branches
BranchTrueFalseTotal
($ok || 0) == 32 or croak("read error on $filename ", defined $ok ? "eof" : $!) 0 0 0
defined $ok ? : 0 0 0
Condition0%0/2
$ok || 0
l!l
00
MC/DC0%0/1
$ok || 0
$ok
Truth table0%0/2
A
A= $ok
Aresult
11
00
241 croak("read error on $filename ", (defined($ok) ? 'eof' : $!));
242 0 ▶ ($verify eq $conf_check) or
Branch: 1 branch
BranchTrueFalseTotal
$verify eq $conf_check or croak("the config has changed since this skiplist was created\n", " '${verify}' vs '${conf_check}'") 0 0 0
243 croak("the config has changed since this skiplist was created\n",
244 " '$verify' vs '$conf_check'");
245  
246 0 return($fh);
247 }
248  
249 {
250 package ApacheLog::Parser::SkipList::Base;
251 0 ▶ sub new {
Subroutine new: not called CC=2 SCAR=17.9
252 0 my $package = shift;
253 0 my ($fh) = @_;
254 0 ▶ my $class = ref($package) || $package;
Condition0%0/3
ref $package || $package
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $package || $package
ref $package $package
Truth table0%0/3
A || B
A= ref $packageB= $package
ABresult
1X1
011
000
255 0 my $self = [$fh, 0, -1];
256 0 bless($self, $class);
257 0 return($self);
258 }
259 }
260 {
261 1 package ApacheLog::Parser::SkipList::Writer;
262 1 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
263 1 our @ISA = qw(ApacheLog::Parser::SkipList::Base);
264 1 ▶ use constant flag => 2**31;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 0 ▶ sub skip {
Subroutine skip: not called CC=4 SCAR=30.0
266 0 my $self = shift;
267 0 my ($l) = @_;
268  
269 0 my $fh = $self->[0];
270 0 ▶ if($l == $self->[2]+1) { # contiguous
Branch: 1 branch
BranchTrueFalseTotal
if ($l == $self->[2] + 1) { } 0 0 0
271 0 $self->[2] = $l;
272 }
273 else {
274 # write-out
275 0 ▶ if(my $num = $self->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $self->[1]) 0 0 0
276 0 ▶ if(my $span = $self->[2] - $num) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $span = $self->[2] - $num) { } 0 0 0
277 0 $num |= flag;
278 0 print $fh pack('N2', $num, $span);
279 }
280 else {
281 0 print $fh pack('N', $num);
282 }
283 }
284  
285 0 $self->[1] = $self->[2] = $l;
286 }
287 }
288 0 ▶ sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
289 0 ▶ close($_[0]->[0]) or
Branch: 1 branch
BranchTrueFalseTotal
close $_[0][0] or croak("close file failed $!") 0 0 0
290 croak("close file failed $!");
291 0 @{$_[0]} = ();
292 }
293 }
294 {
295 1 package ApacheLog::Parser::SkipList::Reader;
296 1 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
297 1 our @ISA = qw(ApacheLog::Parser::SkipList::Base);
298 1 ▶ use constant flag => 2**31;
Subroutine BEGIN: called CC=1 SCAR=0.0
299  
300 # return the next skip value and setup the line counter
301 0 ▶ sub next_skip {
Subroutine next_skip: not called CC=5 SCAR=34.0
302 0 my $self = shift;
303  
304 0 my $fh = $self->[0];
305 0 ▶ eof($fh) and return(0);
Branch: 1 branch
BranchTrueFalseTotal
eof $fh and return 0 0 0 0
306  
307 0 my $v;
308 0 ▶ (read($fh, $v, 4) == 4) or die "gah";
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $v, 4) == 4 or die "gah" 0 0 0
309 0 my $n = unpack("N", $v);
310  
311 # if it is flagged, there's another byte
312 0 ▶ if($n & flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n & 2147483648) { } 0 0 0
313 0 my $val;
314 0 ▶ (read($fh, $val, 4) == 4) or die "gah";
Branch: 1 branch
BranchTrueFalseTotal
read($fh, $val, 4) == 4 or die "gah" 0 0 0
315 0 my $more = unpack("N", $val);
316 0 $self->[1] = $more;
317 0 $n &= ~flag; # de-mangle it
318 }
319 else {
320 # a single line
321 0 $self->[1] = 0;
322 }
323 0 return($self->[2] = $n);
324 }
325 0 ▶ sub skip_lines {
Subroutine skip_lines: not called CC=4 SCAR=30.0
326 0 my ($self, $fh) = @_;
327 0 ▶ my $n = $self->[1] or return(0);
Branch: 1 branch
BranchTrueFalseTotal
my $n = $self->[1] or return 0 0 0 0
328 0 my $q = 0;
329 0 ▶ while(<$fh>) { (++$q >= $n) and return($n); }
Branch: 2 branches
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
++$q >= $n and return $n 0 0 0
330 0 croak("eof while skipping");
331 }
332 }
333  
334 =head1 AUTHOR
335  
336 Eric Wilhelm @ <ewilhelm at cpan dot org>
337  
338 http://scratchcomputing.com/
339  
340 =head1 BUGS
341  
342 If you found this module on CPAN, please report any bugs or feature
343 requests through the web interface at L<http://rt.cpan.org>. I will be
344 notified, and then you'll automatically be notified of progress on your
345 bug as I make changes.
346  
347 If you pulled this development version from my /svn/, please contact me
348 directly.
349  
350 =head1 COPYRIGHT
351  
352 Copyright (C) 2007 Eric L. Wilhelm, All Rights Reserved.
353  
354 =head1 NO WARRANTY
355  
356 Absolutely, positively NO WARRANTY, neither express or implied, is
357 offered with this software. You use this software at your own risk. In
358 case of loss, no person or entity owes you anything whatsoever. You
359 have been warned.
360  
361 =head1 LICENSE
362  
363 This program is free software; you can redistribute it and/or modify it
364 under the same terms as Perl itself.
365  
366 =cut
367  
368 # vi:ts=2:sw=2:et:sta
369 1 1;
« bin/cron.loghack ↑ Summary