| 1 | package Email::AutoReply::DB::BerkeleyDB; | ||
| 2 | our $rcsid = '$Id: BerkeleyDB.pm 3002 2008-06-05 20:23:24Z adam $'; | ||
| 3 | |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Email::AutoReply::DB '-Base'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Email::AutoReply::Recipient; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use BerkeleyDB; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Carp qw(confess); | ||
| 11 | |||
| 12 | field 'email_autoreply_settings_dir'; | ||
| 13 | field 'cachedb_file' => "replied_cache.db"; | ||
| 14 | field 'cachedb_path'; # a path, not ending in a path separator | ||
| 15 | field '_db'; # the reference to the actual tied hash | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | $self = super; | ||
| 19 | $self->_check_path_available; | ||
| 20 | $self->_init_db; | ||
| 21 | return $self; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _check_path_available { | ||
| 25 | my $dir = $self->email_autoreply_settings_dir; | ||
| 26 | defined($dir) or confess "must pass in email_autoreply_settings_dir"; | ||
| 27 | $self->cachedb_path($self->email_autoreply_settings_dir); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _init_db { | ||
| 31 | my %autoreply_cache; | ||
| 32 | my $filename = $self->cachedb_path . '/' . $self->cachedb_file; | ||
| 33 | tie %autoreply_cache, 'BerkeleyDB::Hash', | ||
| 34 | -Filename => $filename, | ||
| 35 | -Flags => DB_CREATE|DB_INIT_LOCK, | ||
| 36 | or die "Cannot open file $filename: $! $BerkeleyDB::Error\n"; | ||
| 37 | $self->_db(\%autoreply_cache); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub store { | ||
| 41 | my $input_type = 'Email::AutoReply::Recipient'; | ||
| 42 | ref $_[0] eq $input_type or confess "input object must be an $input_type"; | ||
| 43 | $_[0]->email && $_[0]->timestamp or confess "invalid input"; | ||
| 44 | $self->_db->{$_[0]->email} = $_[0]->timestamp; | ||
| 45 | } | ||
| 46 | |||
| 47 | # INPUT: string to search for | ||
| 48 | # OUTPUT: Email::AutoReply::Recipient object, or zero | ||
| 49 | sub fetch { | ||
| 50 | my $timestamp = $self->_db->{$_[0]}; | ||
| 51 | my $rv = 0; | ||
| 52 | if ($timestamp) { | ||
| 53 | $rv = Email::AutoReply::Recipient->new( | ||
| 54 | email => $_[0], timestamp => $timestamp | ||
| 55 | ); | ||
| 56 | } | ||
| 57 | return $rv; | ||
| 58 | } | ||
| 59 | |||
| 60 | # INPUT: | ||
| 61 | # OUTPUT: list of Email::AutoReply::Recipient objects, or an empty list | ||
| 62 | sub fetch_all { | ||
| 63 | return keys %{ $self->_db }; | ||
| 64 | } | ||
| 65 | |||
| 66 | return 1; | ||
| 67 | __END__ | ||