line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CHI::Driver::BerkeleyDB; |
2
|
1
|
|
|
1
|
|
22037
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
38
|
|
3
|
1
|
|
|
1
|
|
510
|
use BerkeleyDB; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use CHI::Util qw(read_dir); |
5
|
|
|
|
|
|
|
use File::Path qw(mkpath); |
6
|
|
|
|
|
|
|
use Moose; |
7
|
|
|
|
|
|
|
use strict; |
8
|
|
|
|
|
|
|
use warnings; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
extends 'CHI::Driver'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has 'db' => ( is => 'ro', lazy_build => 1 ); |
15
|
|
|
|
|
|
|
has 'db_class' => ( is => 'ro', default => 'BerkeleyDB::Hash' ); |
16
|
|
|
|
|
|
|
has 'dir_create_mode' => ( is => 'ro', isa => 'Int', default => oct(775) ); |
17
|
|
|
|
|
|
|
has 'env' => ( is => 'ro', lazy_build => 1 ); |
18
|
|
|
|
|
|
|
has 'filename' => ( is => 'ro', init_arg => undef, lazy_build => 1 ); |
19
|
|
|
|
|
|
|
has 'root_dir' => ( is => 'ro' ); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable(); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _build_filename { |
24
|
|
|
|
|
|
|
my $self = shift; |
25
|
|
|
|
|
|
|
return $self->escape_for_filename( $self->namespace ) . ".db"; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _build_env { |
29
|
|
|
|
|
|
|
my $self = shift; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my $root_dir = $self->root_dir; |
32
|
|
|
|
|
|
|
die "must specify one of env or root_dir" if !defined($root_dir); |
33
|
|
|
|
|
|
|
mkpath( $root_dir, 0, $self->dir_create_mode ) |
34
|
|
|
|
|
|
|
if !-d $root_dir; |
35
|
|
|
|
|
|
|
my $env = BerkeleyDB::Env->new( |
36
|
|
|
|
|
|
|
'-Home' => $self->root_dir, |
37
|
|
|
|
|
|
|
'-Config' => {}, |
38
|
|
|
|
|
|
|
'-Flags' => DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL |
39
|
|
|
|
|
|
|
) |
40
|
|
|
|
|
|
|
or die sprintf( "cannot open Berkeley DB environment in '%s': %s", |
41
|
|
|
|
|
|
|
$root_dir, $BerkeleyDB::Error ); |
42
|
|
|
|
|
|
|
return $env; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub _build_db { |
46
|
|
|
|
|
|
|
my $self = shift; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $filename = $self->filename; |
49
|
|
|
|
|
|
|
my $db = $self->db_class->new( |
50
|
|
|
|
|
|
|
'-Filename' => $filename, |
51
|
|
|
|
|
|
|
'-Flags' => DB_CREATE, |
52
|
|
|
|
|
|
|
'-Env' => $self->env |
53
|
|
|
|
|
|
|
) |
54
|
|
|
|
|
|
|
or die |
55
|
|
|
|
|
|
|
sprintf( "cannot open Berkeley DB file '%s' in environment '%s': %s", |
56
|
|
|
|
|
|
|
$filename, $self->root_dir, $BerkeleyDB::Error ); |
57
|
|
|
|
|
|
|
return $db; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub fetch { |
61
|
|
|
|
|
|
|
my ( $self, $key ) = @_; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $data; |
64
|
|
|
|
|
|
|
return ( $self->db->db_get( $key, $data ) == 0 ) ? $data : undef; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub store { |
68
|
|
|
|
|
|
|
my ( $self, $key, $data ) = @_; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$self->db->db_put( $key, $data ) == 0 |
71
|
|
|
|
|
|
|
or die $BerkeleyDB::Error; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub remove { |
75
|
|
|
|
|
|
|
my ( $self, $key ) = @_; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
$self->db->db_del($key) == 0 |
78
|
|
|
|
|
|
|
or die $BerkeleyDB::Error; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub clear { |
82
|
|
|
|
|
|
|
my ($self) = @_; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $count = 0; |
85
|
|
|
|
|
|
|
$self->db->truncate($count) == 0 |
86
|
|
|
|
|
|
|
or die $BerkeleyDB::Error; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub get_keys { |
90
|
|
|
|
|
|
|
my ($self) = @_; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my @keys; |
93
|
|
|
|
|
|
|
my $cursor = $self->db->db_cursor(); |
94
|
|
|
|
|
|
|
my ( $key, $value ) = ( "", "" ); |
95
|
|
|
|
|
|
|
while ( $cursor->c_get( $key, $value, BerkeleyDB::DB_NEXT() ) == 0 ) { |
96
|
|
|
|
|
|
|
push( @keys, $key ); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
return @keys; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub get_namespaces { |
102
|
|
|
|
|
|
|
my ($self) = @_; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
my @contents = read_dir( $self->root_dir ); |
105
|
|
|
|
|
|
|
my @namespaces = |
106
|
|
|
|
|
|
|
map { $self->unescape_for_filename( substr( $_, 0, -3 ) ) } |
107
|
|
|
|
|
|
|
grep { /\.db$/ } @contents; |
108
|
|
|
|
|
|
|
return @namespaces; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__END__ |