lib/Mail/Decency/Helper/Database.pm

filter: Statementstmt 90.3% 75 / 83 Branchbran 50.0% 13 / 26 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 86.3% 19 / 22 Podpod 92.3% 12 / 13 totaltotal 80.6% 121 / 150 CC 16 SCAR 10.5
CC
16
Cov
80%
parse_data
5.9
create
5.3
do_lock
3.2
CRAP 2.9
« lib/Mail/Decency/Core/Meta/Maintenance.pm ↑ Summary lib/Mail/Decency/Helper/IP.pm »
1 package Mail::Decency::Helper::Database;
2  
3 25 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 ▶use IPC::SysV qw/ IPC_PRIVATE IPC_CREAT S_IRWXU /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use IPC::Semaphore;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 25 ▶use Time::HiRes qw/ usleep ualarm /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Carp qw/ confess /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Mail::Decency::Helper::Database
18  
19 =head1 DESCRIPTION
20  
21 Base class for all databases
22  
23 =head1 SYNPOSIS
24  
25 Create a new datbaase like this:
26  
27 Mail::Decency::Helper::Database->create( MongoDB => $config_ref );
28  
29  
30 =head1 CLASS ATTRIBUTES
31  
32 =head2 type : Str
33  
34 The type of the database (DBD, MongoDB)
35  
36 =cut
37  
38 25 has type => ( is => "rw", isa => "Str" );
39  
40 =head2 logger : CodeRef
41  
42 Log-Handler method
43  
44 =cut
45  
46 25 has logger => ( is => "rw", isa => "Mail::Decency::Helper::Logger" );
47  
48 =head2 locker : IPC::Semaphore
49  
50 =cut
51  
52 25 has locker => ( is => "ro", predicate => "use_lock" );
53  
54 =head2 locker_pid : Int
55  
56 PID of process creating the semaphore
57  
58 =cut
59  
60 25 has locker_pid => ( is => "rw", isa => 'Int' );
61  
62  
63 =head1 METHODS
64  
65  
66 =head2 create $type, $args_ref
67  
68 Returns a new instance of the create database object
69  
70 my $database = Mail::Decency::Helper::Database->create( DBD => $args_ref );
71  
72 =over
73  
74 =item * $type
75  
76 Either DBD or MongoDB for now
77  
78 =item * $args_ref
79  
80 HashRef of constrauctions variabels for the module's new-method
81  
82 =back
83  
84 =cut
85  
86 1 ▶sub create {
Subroutine create: called CC=5 SCAR=16.6
Pod documented
87 1 my ( $class, $type, $args_ref ) = @_;
88
89 1 my $module = "Mail::Decency::Helper::Database::$type";
90 1 ▶ my $ok = eval "use $module; 1";
Subroutine BEGIN: called CC=1 SCAR=0.0
91 1 ▶ unless ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 1 1
92 0 confess "Unsupported database '$type': $@\n";
93 }
94
95 # create locker
96 # get a free share..
97 1 ▶ my $locker = IPC::Semaphore->new( IPC_PRIVATE, 3, S_IRWXU | IPC_CREAT )
Branch: 1 branch
BranchTrueFalseTotal
my $locker = "IPC::Semaphore"->new(IPC_PRIVATE(), 3, S_IRWXU() | IPC_CREAT()) or die "Cannot create IPC Semaphore for locking: $!\n" 1 0 1
98 or die "Cannot create IPC Semaphore for locking: $!\n";
99 1 ▶ $locker->setall( (1) x 3 )
Branch: 1 branch
BranchTrueFalseTotal
$locker->setall((1) x 3) or die "Cannot initial unlock semaphores: $!\n" 1 0 1
100 or die "Cannot initial unlock semaphores: $!\n";
101
102 # create and return instance
103 1 my $obj;
104 1 eval {
105 1 $obj = $module->new(
106 %$args_ref,
107 type => $type,
108 locker => $locker,
109 locker_pid => $$
110 );
111 };
112 1 ▶ die "Connection error for '$type': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
113 1 return $obj;
114 }
115  
116  
117 =head2 DEMOLISH
118  
119 Remove locker
120  
121 =cut
122  
123 1 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=3 SCAR=11.4
Pod documented
124 1 my ( $self ) = @_;
125 1 ▶ $self->locker->remove if $self->locker_pid == $$; # remove semaphore with parent process only
Branch: 1 branch
BranchTrueFalseTotal
if $self->locker_pid == $$ 1 0 1
126 1 ▶ $self->db->disconnect if $self->db;
Branch: 1 branch
BranchTrueFalseTotal
if $self->db 1 0 1
127 1 delete $self->{ db };
128 }
129  
130  
131 =head2 get $schema, $table, $search_ref
132  
133 Searches and returns single entry from database
134  
135 See parse_data method for return contexts.
136  
137 =over
138  
139 =item * $schema
140  
141 The schema/context/prefix of the lookup.. eg "throttle" for throttle tables
142  
143 =item * $table
144  
145 The table/suffix of the lookup .. eg "sender_domain" for the "throttle_sender_domain" table
146  
147 =item * $search_ref
148  
149 HashRef of search attributes. Can be flat or nested
150  
151 $search_ref = { attribute => "value" }; # simple equals
152 $search_ref = { attribute => { ">" => 123 } }; # complex "greater then"
153  
154 =back
155  
156  
157 =head2 set $schema, $table, $search_ref, $data_ref
158  
159 Writes to database. Could affect multiple entries.
160  
161 =over
162  
163 =item * $schema, $table, $search_ref
164  
165 Set get method
166  
167 =item * $data_ref
168  
169 HashRef or scalar of the data to be saved. If scalar, it is will be converted into { data => "scalar" }
170  
171 =back
172  
173  
174 =head2 search $schema, $table, $search_ref
175  
176 Returns a list of search results (in opposite to the get method). In scalar contexts it returns an ArrayRef instead
177  
178 =over
179  
180 =item * $schema, $table, $search_ref
181  
182 Set get method
183  
184 =back
185  
186 =cut
187  
188  
189 =head2 update_data
190  
191 Transforms flat (scalar) values into { data => $value } hashrefs
192  
193 =cut
194  
195 1 ▶sub update_data {
Subroutine update_data: called CC=2 SCAR=8.1
Pod documented
196 1 my ( $self, $data ) = @_;
197 1 ▶ return $data if ref( $data );
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 1 0 1
198 0 return { data => $data };
199 }
200  
201 =head2 parse_data $data_ref
202  
203 Transforms hashref values in an array context from { value => $value } to ( $value )
204  
205 In array-context, it will return the content of the "data" field, if any
206  
207 Can be modified in derived modules.
208  
209 =cut
210  
211 4 ▶sub parse_data {
Subroutine parse_data: called CC=5 SCAR=17.8
Pod documented
212 4 my ( $self, $data ) = @_;
213 4 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 3 1 4
214 1 ▶ return wantarray ? ( $data ) : { data => $data } unless ref( $data );
Branch: 2 branches
BranchTrueFalseTotal
unless ref $data 0 1 1
wantarray ? : 0 0 0
215 1 ▶ return wantarray ? ( $data->{ data } ) : $data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
216 }
217  
218  
219  
220 =head2 update_query $query_ref
221  
222 Update method for search query. Can be overwritten/extended in derived modules.
223  
224 =cut
225  
226 9 ▶sub update_query {
Subroutine update_query: called CC=2 SCAR=8.1
Pod documented
227 9 my ( $self, $query_ref ) = @_;
228 9 ▶ return $query_ref if ref( $query_ref );
Branch: 1 branch
BranchTrueFalseTotal
if ref $query_ref 9 0 9
229 0 return { key => $query_ref };
230 }
231  
232  
233 =head2 do_lock
234  
235 Locks via flock file
236  
237 =cut
238  
239 7 ▶sub do_lock {
Subroutine do_lock: called CC=3 SCAR=11.7
Pod documented
240 7 my ( $self, $num ) = @_;
241 7 ▶ $num ||= 0;
Condition50%1/2
$num ||= 0
l!l
70
MC/DC0%0/1
$num ||= 0
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
242
243 7 my $locker = $self->locker;
244
245 # !! ATTENTION !!
246 # the purpose of this locking is to ensure increments in multi-forking
247 # environment work. The purpose is NOT to assure absolute mutual
248 # exclusion.
249 # worst case for data: some counter are not incremented
250 # worst case for process: slow response (not to speak of deadlock)
251 # the process needs overrule the (statistic) data needs.
252 # !! ATTENTION !!
253 7 my $deadlock = 1_500_000; # = 1.5 sec
254 7 eval {
255 0 ▶ $SIG{ ALRM } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
256 0 die "Deadlock timeout\n";
257 7 };
258 7 ualarm( $deadlock );
259 7 $locker->op( $num, -1, 0 );
260 7 ualarm( 0 );
261 };
262 7 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
263 0 $locker->setval( $num, 0 );
264 0 warn "Deadlock in $num blighted\n";
265 }
266 }
267  
268  
269 =head2 do_unlock
270  
271 Unlocks the flock
272  
273 =cut
274  
275 7 ▶sub do_unlock {
Subroutine do_unlock: called CC=2 SCAR=7.1
Pod documented
276 7 my ( $self, $num ) = @_;
277 7 ▶ $num ||= 0;
Condition50%1/2
$num ||= 0
l!l
70
MC/DC0%0/1
$num ||= 0
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
278 #$self->locker->write( 0, 0, 1 );
279 7 $self->locker->op( $num, 1, 0 );
280 }
281  
282 =head2 read_lock
283  
284 Do read lock
285  
286 =cut
287  
288 5 ▶sub read_lock {
Subroutine read_lock: called CC=1 SCAR=0.0
Pod documented
289 5 return shift->do_lock( 1 );
290 }
291  
292 =head2 read_unlock
293  
294 Do unlock read
295  
296 =cut
297  
298 5 ▶sub read_unlock {
Subroutine read_unlock: called CC=1 SCAR=0.0
Pod documented
299 5 return shift->do_unlock( 1 );
300 }
301  
302  
303  
304 =head2 write_lock
305  
306 Do read lock
307  
308 =cut
309  
310 2 ▶sub write_lock {
Subroutine write_lock: called CC=1 SCAR=0.0
Pod documented
311 2 my ( $self ) = @_;
312 2 $self->read_lock;
313 2 $self->do_lock( 2 );
314 2 return ;
315 }
316  
317 =head2 write_unlock
318  
319 Do unlock read
320  
321 =cut
322  
323 2 ▶sub write_unlock {
Subroutine write_unlock: called CC=1 SCAR=0.0
Pod documented
324 2 my ( $self ) = @_;
325 2 $self->do_unlock( 2 );
326 2 $self->read_unlock;
327 }
328  
329 =head2 usr_lock
330  
331 Custom locker
332  
333 =cut
334  
335 0 ▶sub usr_lock {
Subroutine usr_lock: not called CC=1 SCAR=6.9
Pod documented
336 0 return shift->do_lock( 0 );
337 }
338  
339 =head2 usr_lock
340  
341 Custom locker
342  
343 =cut
344  
345 0 ▶sub usr_unlock {
Subroutine usr_unlock: not called CC=1 SCAR=6.9
Pod undocumented
346 0 return shift->do_unlock( 0 );
347 }
348  
349  
350  
351 =head1 AUTHOR
352  
353 Ulrich Kautz <uk@fortrabbit.de>
354  
355 =head1 COPYRIGHT
356  
357 Copyright (c) 2010 the L</AUTHOR> as listed above
358  
359 =head1 LICENCSE
360  
361 This library is free software and may be distributed under the same terms as perl itself.
362  
363 =cut
364  
365  
366 25 1;
« lib/Mail/Decency/Core/Meta/Maintenance.pm ↑ Summary lib/Mail/Decency/Helper/IP.pm »