File Coverage

blib/lib/App/Milter/Limit/Plugin/BerkeleyDB.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package App::Milter::Limit::Plugin::BerkeleyDB;
2             our $VERSION = '0.51';
3              
4             # ABSTRACT: BerkeleyDB driver for App::Milter::Limit
5              
6 1     1   868 use strict;
  1         2  
  1         44  
7 1     1   5 use base qw(App::Milter::Limit::Plugin Class::Accessor);
  1         1  
  1         854  
8 1     1   9062 use App::Milter::Limit::Log;
  1         30521  
  1         101  
9 1     1   728 use BerkeleyDB qw(DB_CREATE DB_INIT_MPOOL DB_INIT_CDB);
  0            
  0            
10              
11             __PACKAGE__->mk_accessors(qw(_db));
12              
13             sub init {
14             my $self = shift;
15              
16             $self->init_defaults;
17              
18             App::Milter::Limit::Util::make_path($self->config_get('driver', 'home'));
19              
20             # db/env creation deferred until child_init
21             }
22              
23             sub init_defaults {
24             my $self = shift;
25              
26             $self->config_defaults('driver',
27             home => $self->config_get('global', 'state_dir'),
28             file => 'bdb-stats.db',
29             );
30             }
31              
32             # open BerkeleyDB handles in child_init handler.
33             sub child_init {
34             my $self = shift;
35              
36             my $conf = App::Milter::Limit::Config->section('driver');
37              
38             my $env = BerkeleyDB::Env->new(
39             -Home => $$conf{home},
40             -Flags => DB_CREATE | DB_INIT_MPOOL | DB_INIT_CDB)
41             or die "failed to open BerkeleyDB env: $!";
42              
43             my $db = BerkeleyDB::Hash->new(
44             -Filename => $$conf{file},
45             -Env => $env,
46             -Flags => DB_CREATE) or die "failed to open BerkeleyDB: $!";
47              
48             $self->_db($db);
49              
50             debug("BerkeleyDB connection initialized");
51             }
52              
53             sub query {
54             my ($self, $from) = @_;
55              
56             my $conf = App::Milter::Limit::Config->global;
57              
58             my $db = $self->_db;
59              
60             my $val;
61             $db->db_get($from, $val);
62              
63             unless (defined $val) {
64             # initialize new record for sender
65             $val = join ':', time, 0;
66             }
67              
68             my ($start, $count) = split ':', $val;
69              
70             # reset counter if it is expired
71             if ($start < time - $$conf{expire}) {
72             $count = 0;
73             $start = time;
74             }
75              
76             # update database for this sender.
77             $val = join ':', $start, ++$count;
78             $db->db_put($from, $val);
79              
80             return $count;
81             }
82              
83             1;
84              
85              
86              
87             =pod
88              
89             =head1 NAME
90              
91             App::Milter::Limit::Plugin::BerkeleyDB - BerkeleyDB driver for App::Milter::Limit
92              
93             =head1 VERSION
94              
95             version 0.51
96              
97             =head1 SYNOPSIS
98              
99             my $milter = App::Milter::Limit->instance('BerkeleyDB');
100              
101             =head1 DESCRIPTION
102              
103             This module implements the C backend using a BerkeleyDB data
104             store.
105              
106             =head1 CONFIGURATION
107              
108             The C<[driver]> section of the configuration file must specify the following items:
109              
110             =over 4
111              
112             =item home
113              
114             The directory where the database files should be stored.
115              
116             =item file [optional]
117              
118             The database filename (default bdb-stats.db)
119              
120             =item mode [optional]
121              
122             The file mode for the database files (default 0644).
123              
124             =back
125              
126             =for Pod::Coverage child_init
127             init_defaults
128              
129             =head1 AUTHOR
130              
131             Michael Schout
132              
133             =head1 COPYRIGHT AND LICENSE
134              
135             This software is copyright (c) 2010 by Michael Schout.
136              
137             This is free software; you can redistribute it and/or modify it under
138             the same terms as the Perl 5 programming language system itself.
139              
140             =cut
141              
142              
143             __END__