line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::Index::Store::BerkeleyDB; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Modules |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
8
|
|
|
|
|
|
|
|
9
|
9
|
|
|
9
|
|
102672
|
use BerkeleyDB; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Fcntl; |
11
|
|
|
|
|
|
|
use File::Path; |
12
|
|
|
|
|
|
|
use Carp; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
require HTML::Index::Store; |
15
|
|
|
|
|
|
|
use vars qw( @ISA ); |
16
|
|
|
|
|
|
|
@ISA = qw( HTML::Index::Store ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
19
|
|
|
|
|
|
|
# |
20
|
|
|
|
|
|
|
# Initialization public method |
21
|
|
|
|
|
|
|
# |
22
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub init |
25
|
|
|
|
|
|
|
{ |
26
|
|
|
|
|
|
|
my $self = shift; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
croak "No DB\n" unless defined $self->{DB}; |
29
|
|
|
|
|
|
|
unless ( -d $self->{DB} ) |
30
|
|
|
|
|
|
|
{ |
31
|
|
|
|
|
|
|
mkpath( $self->{DB} ) or croak "can't mkpath $self->{DB}: $!\n"; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
$self->{MODE} ||= 'rw'; |
34
|
|
|
|
|
|
|
$self->SUPER::init(); |
35
|
|
|
|
|
|
|
return $self; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub create_table |
39
|
|
|
|
|
|
|
{ |
40
|
|
|
|
|
|
|
my $self = shift; |
41
|
|
|
|
|
|
|
my $table = shift; |
42
|
|
|
|
|
|
|
my $type = shift; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$self->{TYPE}{$table} = $type; |
45
|
|
|
|
|
|
|
my $flags = $self->{MODE} eq 'r' ? DB_RDONLY : DB_CREATE; |
46
|
|
|
|
|
|
|
my $db_path = "$self->{DB}/$table.db"; |
47
|
|
|
|
|
|
|
if ( -e $db_path and $self->{REFRESH} ) |
48
|
|
|
|
|
|
|
{ |
49
|
|
|
|
|
|
|
unlink( $db_path ) or croak "Can't remove $db_path\n"; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
$self->{PATH}{$table} = $db_path; |
52
|
|
|
|
|
|
|
if ( $type eq 'ARRAY' ) |
53
|
|
|
|
|
|
|
{ |
54
|
|
|
|
|
|
|
$self->{$table} = new BerkeleyDB::Recno( |
55
|
|
|
|
|
|
|
'-Filename' => $db_path, |
56
|
|
|
|
|
|
|
'-Flags' => $flags, |
57
|
|
|
|
|
|
|
) or croak "Cannot tie to $db_path ($flags): $!\n"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
elsif ( $type eq 'HASH' ) |
60
|
|
|
|
|
|
|
{ |
61
|
|
|
|
|
|
|
$self->{$table} = new BerkeleyDB::Hash( |
62
|
|
|
|
|
|
|
'-Filename' => $db_path, |
63
|
|
|
|
|
|
|
'-Flags' => $flags, |
64
|
|
|
|
|
|
|
'-Pagesize' => 512, |
65
|
|
|
|
|
|
|
) or croak "Cannot tie to $db_path ($flags): $!\n"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
warn "$table of type $type - $self->{$table}\n" if $self->{VERBOSE}; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
71
|
|
|
|
|
|
|
# |
72
|
|
|
|
|
|
|
# Destructor |
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub DESTROY |
77
|
|
|
|
|
|
|
{ |
78
|
|
|
|
|
|
|
my $self = shift; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
for my $table ( keys %{$self->{PATH}} ) |
81
|
|
|
|
|
|
|
{ |
82
|
|
|
|
|
|
|
undef( $self->{$table} ); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
87
|
|
|
|
|
|
|
# |
88
|
|
|
|
|
|
|
# Public methods |
89
|
|
|
|
|
|
|
# |
90
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub put |
93
|
|
|
|
|
|
|
{ |
94
|
|
|
|
|
|
|
my $self = shift; |
95
|
|
|
|
|
|
|
my $table = shift; |
96
|
|
|
|
|
|
|
croak "put called before init\n" unless defined $self->{TYPE}; |
97
|
|
|
|
|
|
|
my $type = $self->{TYPE}{$table}; |
98
|
|
|
|
|
|
|
unless ( $type ) |
99
|
|
|
|
|
|
|
{ |
100
|
|
|
|
|
|
|
croak |
101
|
|
|
|
|
|
|
"Can't put $table (not one of ", |
102
|
|
|
|
|
|
|
join( ',', keys %{$self->{TYPE}}) , |
103
|
|
|
|
|
|
|
")\n" |
104
|
|
|
|
|
|
|
; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
my $key = shift; |
107
|
|
|
|
|
|
|
my $val = shift; |
108
|
|
|
|
|
|
|
croak "Putting undef into $table $key\n" unless defined $val; |
109
|
|
|
|
|
|
|
my $status = $self->{$table}->db_put( $key, $val ); |
110
|
|
|
|
|
|
|
croak "Can't db_put $val into the $key field of $table: $status\n" if $status; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub get |
114
|
|
|
|
|
|
|
{ |
115
|
|
|
|
|
|
|
my $self = shift; |
116
|
|
|
|
|
|
|
my $table = shift; |
117
|
|
|
|
|
|
|
croak "get called before init\n" unless defined $self->{TYPE}; |
118
|
|
|
|
|
|
|
my $type = $self->{TYPE}{$table}; |
119
|
|
|
|
|
|
|
unless ( $type ) |
120
|
|
|
|
|
|
|
{ |
121
|
|
|
|
|
|
|
croak |
122
|
|
|
|
|
|
|
"Can't get $table (not one of ", |
123
|
|
|
|
|
|
|
join( ',', keys %{$self->{TYPE}}) , |
124
|
|
|
|
|
|
|
")\n" |
125
|
|
|
|
|
|
|
; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
my $key = shift; |
128
|
|
|
|
|
|
|
my $val; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $status = $self->{$table}->db_get( $key, $val ); |
131
|
|
|
|
|
|
|
croak "Can't get $key key of $table: $status\n" |
132
|
|
|
|
|
|
|
unless |
133
|
|
|
|
|
|
|
$status == 0 || |
134
|
|
|
|
|
|
|
$status == DB_NOTFOUND |
135
|
|
|
|
|
|
|
; |
136
|
|
|
|
|
|
|
return $val; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub get_keys |
140
|
|
|
|
|
|
|
{ |
141
|
|
|
|
|
|
|
my $self = shift; |
142
|
|
|
|
|
|
|
my $table = shift; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
croak "each called before init\n" unless defined $self->{TYPE}; |
145
|
|
|
|
|
|
|
my $type = $self->{TYPE}; |
146
|
|
|
|
|
|
|
my $cursor = $self->{$table}->db_cursor(); |
147
|
|
|
|
|
|
|
my ( $key, $val ) = ( $type eq 'ARRAY' ? 1 : '', 0 ); |
148
|
|
|
|
|
|
|
my @keys; |
149
|
|
|
|
|
|
|
while ( $cursor->c_get( $key, $val, DB_NEXT ) == 0 ) |
150
|
|
|
|
|
|
|
{ |
151
|
|
|
|
|
|
|
push( @keys, $key ); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
return @keys; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
sub nkeys |
157
|
|
|
|
|
|
|
{ |
158
|
|
|
|
|
|
|
my $self = shift; |
159
|
|
|
|
|
|
|
my $table = shift; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
croak "nkeys called before init\n" unless defined $self->{TYPE}; |
162
|
|
|
|
|
|
|
my $db_stat = $self->{$table}->db_stat(); |
163
|
|
|
|
|
|
|
return $db_stat->{bt_nkeys} if defined $db_stat->{bt_nkeys}; |
164
|
|
|
|
|
|
|
return $db_stat->{hash_nkeys} if defined $db_stat->{hash_nkeys}; |
165
|
|
|
|
|
|
|
return $db_stat->{qs_nkeys} if defined $db_stat->{hash_nkeys}; |
166
|
|
|
|
|
|
|
return undef; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
170
|
|
|
|
|
|
|
# |
171
|
|
|
|
|
|
|
# True |
172
|
|
|
|
|
|
|
# |
173
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
1; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
__END__ |