blib/lib/Email/Fingerprint/Cache/AnyDBM.pm

filter: Statementstmt 97.3% 73 / 75 Branchbran 87.5% 21 / 24 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 93.1% 122 / 131 SCAR 27.1
CC
15
Cov
93%
lock
6.2
is_open
4.2
open
4.0
CRAP 15.1
« blib/lib/Email/Fingerprint/Cache.pm ↑ Summary build_lib/Test/Stdout.pm »
1 package Email::Fingerprint::Cache::AnyDBM;
2 3 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use AnyDBM_File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use LockFile::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Email::Fingerprint::Cache::AnyDBM - AnyDBM backend for Email::Fingerprint::Cache
16  
17 =head1 VERSION
18  
19 Version 0.01
20  
21 =cut
22  
23 3 our $VERSION = '0.01';
24  
25 =head1 SYNOPSIS
26  
27 use Email::Fingerprint::Cache;
28  
29 my $foo = Email::Fingerprint::Cache->new({
30 backend => 'AnyDBM',
31 });
32 ...
33  
34 You never want to use this class directly; you always want to access it
35 through Email::Fingerpint::Cache.
36  
37 =head1 ATTRIBUTES
38  
39 =cut
40  
41 3 my %file :ATTR( :init_arg<file>, :set<file> ) = ();
42 3 my %hash :ATTR( :init_arg<hash> ) = ();
43 3 my %lock :ATTR = ();
44 3 my %mgr :ATTR = ();
45  
46 =head1 FUNCTIONS
47  
48 =head2 new
49  
50 $cache = new Email::Fingerprint::Cache::AnyDBM({
51 file => $filename, # Mandatory
52 });
53  
54 Method created automatically by C<Class::Std>.
55  
56 =head2 BUILD
57  
58 Internal helper method; never called directly by users.
59  
60 =cut
61  
62 sub BUILD {
63 11 ▶ my ( $self, $ident, $args ) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
64  
65 11 $mgr{$ident} = LockFile::Simple->make(
66 -nfs => 1,
67 -warn => 0,
68 -efunc => undef,
69 -autoclean => 1,
70 );
71 }
72  
73 =head2 open
74  
75 $cache->open or die;
76  
77 Open the associated file, and tie it to our hash. This method does not
78 lock the file, nor unlock it on failure. See C<lock> and C<unlock>.
79  
80 =cut
81  
82 sub open {
83 10 ▶ my $self = shift;
Subroutine open: called CC=4 SCAR=13.9
Pod documented
84  
85 10 ▶ my $file = $file{ ident $self } || '';
Condition100%2/2
$file{builtin::refaddr($self)} || ''
l!l
91
MC/DC100%1/1
$file{builtin::refaddr($self)} || ''
$file{builtin::refaddr($self)}
Truth table100%2/2
A
A= $file{builtin::refaddr($self)}
Aresult
11
00
86 10 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 1 9 10
87  
88 9 my $hash = $self->get_hash;
89  
90 9 tie %$hash, 'AnyDBM_File', $file, O_CREAT|O_RDWR, oct(600);
91  
92 9 ▶ if ( not $self->is_open ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_open) 1 8 9
93 1 carp "Couldn't open $file";
94 1 return;
95 }
96  
97 8 1;
98 }
99  
100 =head2 close
101  
102 Unties the hash, which causes the underlying DB file to be written and
103 closed.
104  
105 =cut
106  
107 sub close {
108 9 ▶ my $self = shift;
Subroutine close: called CC=2 SCAR=6.9
Pod documented
109  
110 9 ▶ return unless $self->is_open;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_open 1 8 9
111  
112 8 untie %{ $self->get_hash };
113 }
114  
115 =head2 is_open
116  
117 Returns true if the cache is open; false otherwise.
118  
119 =cut
120  
121 sub is_open {
122 35 ▶ my $self = shift;
Subroutine is_open: called CC=4 SCAR=14.3
Pod documented
123 35 my $hash = $self->get_hash;
124  
125 35 ▶ return 0 unless defined $hash and ref $hash eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash and ref $hash eq "HASH" 0 35 35
Condition33%1/3
defined $hash and ref $hash eq "HASH"
!ll&&!rl&&r
0035
MC/DC0%0/2
defined $hash and ref $hash eq "HASH"
defined $hash ref $hash eq "HASH"
Truth table33%1/3
A and B
A= defined $hashB= ref $hash eq "HASH"
ABresult
0X0
100
111
126 35 ▶ return 0 unless tied %{ $hash };
Branch: 1 branch
BranchTrueFalseTotal
unless tied %{$hash;} 15 20 35
127 20 return 1;
128 }
129  
130 =head2 is_locked
131  
132 Returns true if the cache is locked; false otherwise.
133  
134 =cut
135  
136 sub is_locked {
137 7 ▶ my $self = shift;
Subroutine is_locked: called CC=2 SCAR=6.9
Pod documented
138 7 ▶ return defined $lock{ ident $self } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $lock{builtin::refaddr($self)} ? : 2 5 7
139 }
140  
141 =head2 lock
142  
143 $cache->lock or die; # returns immediately
144 $cache->lock( block => 1 ) or die; # Waits for a lock
145  
146 Lock the DB file. Returns false on failure, true on success.
147  
148 =cut
149  
150 sub lock {
151 8 ▶ my $self = shift;
Subroutine lock: called CC=6 SCAR=18.2
Pod documented
152 8 my %opts = @_;
153  
154 8 ▶ return 1 if exists $lock{ ident $self }; # Success if already locked
Branch: 1 branch
BranchTrueFalseTotal
if exists $lock{builtin::refaddr($self)} 1 7 8
155  
156 7 ▶ return unless defined $file{ ident $self }; # Can't lock nothing!
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file{builtin::refaddr($self)} 1 6 7
157 6 my $file = $file{ ident $self };
158  
159 6 my $mgr = $mgr{ ident $self };
160  
161 # Minor validation that LockFile::Simple doesn't perform
162 6 ▶ if (not -w dirname($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w dirname($file)) 0 6 6
163 0 warn "Directory " . dirname($file) . " is not writable\n";
164 0 return;
165 }
166  
167 # Perform the lock
168 my $lock
169 = $opts{block}
170 6 ▶ ? $mgr->lock($file)
Branch: 1 branch
BranchTrueFalseTotal
$opts{'block'} ? : 1 5 6
171 : $mgr->trylock($file);
172 6 ▶ return unless $lock;
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 0 6 6
173  
174 # Remember the lock
175 6 $lock{ ident $self } = $lock;
176  
177 6 1;
178 }
179  
180 =head2 unlock
181  
182 $cache->unlock or cluck "Unlock failed";
183  
184 Unlocks the DB file. Returns false on failure, true on success.
185  
186 =cut
187  
188 sub unlock {
189 16 ▶ my $self = shift;
Subroutine unlock: called CC=2 SCAR=6.9
Pod documented
190 16 ▶ my $lock = delete $lock{ ident $self } or return 1; # Success if no lock
Branch: 1 branch
BranchTrueFalseTotal
my $lock = delete $lock{builtin::refaddr($self)} or return 1 10 6 16
191  
192 6 $lock->release();
193  
194 6 1;
195 }
196  
197 =head1 PRIVATE METHODS
198  
199 =head2 get_hash
200  
201 Returns a reference to the hash which is tied to the backend storage.
202  
203 =cut
204  
205 sub get_hash : PRIVATE {
206 52 ▶ my $self = shift;
Subroutine get_hash: called CC=1 SCAR=0.0
207 52 return $hash{ ident $self };
208 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
209  
210 =head1 AUTHOR
211  
212 Len Budney, C<< <lbudney at pobox.com> >>
213  
214 =head1 BUGS
215  
216 Please report any bugs or feature requests to
217 C<bug-email-fingerprint at rt.cpan.org>, or through the web interface at
218 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Email-Fingerprint>.
219 I will be notified, and then you'll automatically be notified of progress on
220 your bug as I make changes.
221  
222 =head1 SUPPORT
223  
224 You can find documentation for this module with the perldoc command.
225  
226 perldoc Email::Fingerprint::Cache::AnyDBM
227  
228 You can also look for information at:
229  
230 =over 4
231  
232 =item * AnnoCPAN: Annotated CPAN documentation
233  
234 L<http://annocpan.org/dist/Email-Fingerprint>
235  
236 =item * CPAN Ratings
237  
238 L<http://cpanratings.perl.org/d/Email-Fingerprint>
239  
240 =item * RT: CPAN's request tracker
241  
242 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Email-Fingerprint>
243  
244 =item * Search CPAN
245  
246 L<http://search.cpan.org/dist/Email-Fingerprint>
247  
248 =back
249  
250 =head1 ACKNOWLEDGEMENTS
251  
252 Email::Fingerprint::Cache is based on caching code in the
253 C<eliminate_dups> script by Peter Samuel and available at
254 L<http://www.qmail.org/>.
255  
256 =head1 COPYRIGHT & LICENSE
257  
258 Copyright 2006-2011 Len Budney, all rights reserved.
259  
260 This program is free software; you can redistribute it and/or modify it
261 under the same terms as Perl itself.
262  
263 =cut
264  
265 3 1;
« blib/lib/Email/Fingerprint/Cache.pm ↑ Summary build_lib/Test/Stdout.pm »