Commit | Line | Data |
ffed8b01 |
1 | package DBM::Deep; |
2 | |
2120a181 |
3 | use 5.006_000; |
460b1067 |
4 | |
ffed8b01 |
5 | use strict; |
065b45be |
6 | use warnings FATAL => 'all'; |
8b957036 |
7 | |
d426259c |
8 | our $VERSION = q(1.0015); |
86867f3a |
9 | |
ffed8b01 |
10 | use Scalar::Util (); |
ffed8b01 |
11 | |
a4d36ff6 |
12 | use DBM::Deep::Engine::DBI (); |
51e9eee3 |
13 | use DBM::Deep::Engine::File (); |
95967a5e |
14 | |
4f0f6fff |
15 | use DBM::Deep::SQL::Util; |
16 | use DBM::Deep::SQL::Array; |
17 | use DBM::Deep::SQL::Hash; |
18 | |
c57b19c6 |
19 | use overload |
20 | '""' => sub { overload::StrVal( $_[0] ) }, |
21 | fallback => 1; |
22 | |
6e6789b0 |
23 | use constant DEBUG => 0; |
24 | |
2120a181 |
25 | sub TYPE_HASH () { DBM::Deep::Engine->SIG_HASH } |
26 | sub TYPE_ARRAY () { DBM::Deep::Engine->SIG_ARRAY } |
ffed8b01 |
27 | |
2120a181 |
28 | # This is used in all the children of this class in their TIE<type> methods. |
0ca7ea98 |
29 | sub _get_args { |
30 | my $proto = shift; |
31 | |
32 | my $args; |
33 | if (scalar(@_) > 1) { |
34 | if ( @_ % 2 ) { |
35 | $proto->_throw_error( "Odd number of parameters to " . (caller(1))[2] ); |
36 | } |
37 | $args = {@_}; |
38 | } |
d0b74c17 |
39 | elsif ( ref $_[0] ) { |
4d35d856 |
40 | unless ( eval { local $SIG{'__DIE__'}; %{$_[0]} || 1 } ) { |
0ca7ea98 |
41 | $proto->_throw_error( "Not a hashref in args to " . (caller(1))[2] ); |
42 | } |
43 | $args = $_[0]; |
44 | } |
d0b74c17 |
45 | else { |
0ca7ea98 |
46 | $args = { file => shift }; |
47 | } |
48 | |
49 | return $args; |
50 | } |
51 | |
d426259c |
52 | # Class constructor method for Perl OO interface. |
53 | # Calls tie() and returns blessed reference to tied hash or array, |
54 | # providing a hybrid OO/tie interface. |
ffed8b01 |
55 | sub new { |
d0b74c17 |
56 | my $class = shift; |
57 | my $args = $class->_get_args( @_ ); |
4f0f6fff |
58 | my $self; |
59 | |
a4d36ff6 |
60 | =pod |
4f0f6fff |
61 | if (exists $args->{dbi}) { |
62 | eval { |
63 | require DBIx::Abstract; |
64 | }; if ( $@ ) { |
2c70efe1 |
65 | __PACKAGE__->_throw_error('DBIx::Abstract not installed. You cannot use the SQL mode.'); |
4f0f6fff |
66 | } |
67 | unless (UNIVERSAL::isa($args->{dbi}, 'DBIx::Abstract')) { |
68 | $args->{dbi} = DBIx::Abstract->connect($args->{dbi}); |
69 | } |
70 | |
71 | if (defined $args->{id}) { |
72 | unless ($args->{id} =~ /^\d+$/ && $args->{id} > 0) { |
2c70efe1 |
73 | __PACKAGE__->_throw_error('Invalid SQL record id'); |
4f0f6fff |
74 | } |
75 | my $util = {dbi => $args->{dbi}}; |
76 | bless $util, 'DBM::Deep::SQL::Util'; |
77 | my $q = $util->_select( |
78 | table => 'rec_item', |
79 | fields => 'item_type', |
80 | where => {id => $args->{id}}, |
81 | ); |
82 | if ($q->[0]->[0] eq 'array') { |
83 | $args->{type} = TYPE_ARRAY; |
84 | } |
85 | elsif ($q->[0]->[0] eq 'hash') { |
86 | $args->{type} = TYPE_HASH; |
87 | } |
88 | else { |
89 | DBM::Deep->_throw_error('Unknown SQL record id'); |
90 | } |
91 | } |
92 | else { |
93 | my $util = {dbi => $args->{dbi}}; |
94 | bless $util, 'DBM::Deep::SQL::Util'; |
95 | if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) { |
96 | $args->{id} = $util->_create('array'); |
97 | } |
98 | else { |
99 | $args->{id} = $util->_create('hash'); |
100 | } |
101 | } |
102 | |
103 | if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) { |
104 | $class = 'DBM::Deep::SQL::Array'; |
105 | require DBM::Deep::SQL::Array; |
106 | tie @$self, $class, %$args; |
107 | if ($args->{prefetch}) { |
108 | (tied(@$self))->_prefetch(); |
109 | } |
110 | return bless $self, $class; |
111 | } |
112 | else { |
113 | $class = 'DBM::Deep::SQL::Hash'; |
114 | require DBM::Deep::SQL::Hash; |
115 | tie %$self, $class, %$args; |
116 | if ($args->{prefetch}) { |
117 | (tied(%$self))->_prefetch(); |
118 | } |
119 | return bless $self, $class; |
120 | } |
121 | } |
a4d36ff6 |
122 | =cut |
d0b74c17 |
123 | |
124 | ## |
125 | # Check if we want a tied hash or array. |
126 | ## |
d0b74c17 |
127 | if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) { |
6fe26b29 |
128 | $class = 'DBM::Deep::Array'; |
129 | require DBM::Deep::Array; |
d0b74c17 |
130 | tie @$self, $class, %$args; |
131 | } |
132 | else { |
6fe26b29 |
133 | $class = 'DBM::Deep::Hash'; |
134 | require DBM::Deep::Hash; |
d0b74c17 |
135 | tie %$self, $class, %$args; |
136 | } |
ffed8b01 |
137 | |
d0b74c17 |
138 | return bless $self, $class; |
ffed8b01 |
139 | } |
140 | |
96041a25 |
141 | # This initializer is called from the various TIE* methods. new() calls tie(), |
142 | # which allows for a single point of entry. |
0795f290 |
143 | sub _init { |
0795f290 |
144 | my $class = shift; |
994ccd8e |
145 | my ($args) = @_; |
0795f290 |
146 | |
460b1067 |
147 | # locking implicitly enables autoflush |
148 | if ($args->{locking}) { $args->{autoflush} = 1; } |
149 | |
0795f290 |
150 | # These are the defaults to be optionally overridden below |
151 | my $self = bless { |
95967a5e |
152 | type => TYPE_HASH, |
e06824f8 |
153 | base_offset => undef, |
2120a181 |
154 | staleness => undef, |
2120a181 |
155 | engine => undef, |
0795f290 |
156 | }, $class; |
2120a181 |
157 | |
2c70efe1 |
158 | unless ( exists $args->{engine} ) { |
159 | my $class = exists $args->{dbi} |
160 | ? 'DBM::Deep::Engine::DBI' |
161 | : 'DBM::Deep::Engine::File'; |
162 | |
163 | $args->{engine} = $class->new({ |
164 | %{$args}, |
165 | obj => $self, |
166 | }); |
167 | } |
8db25060 |
168 | |
fde3db1a |
169 | # Grab the parameters we want to use |
0795f290 |
170 | foreach my $param ( keys %$self ) { |
171 | next unless exists $args->{$param}; |
3e9498a1 |
172 | $self->{$param} = $args->{$param}; |
ffed8b01 |
173 | } |
d0b74c17 |
174 | |
2120a181 |
175 | eval { |
2c70efe1 |
176 | local $SIG{'__DIE__'}; |
0795f290 |
177 | |
2c70efe1 |
178 | $self->lock_exclusive; |
179 | $self->_engine->setup( $self ); |
180 | $self->unlock; |
2120a181 |
181 | }; if ( $@ ) { |
2c70efe1 |
182 | my $e = $@; |
183 | eval { local $SIG{'__DIE__'}; $self->unlock; }; |
184 | die $e; |
2120a181 |
185 | } |
359a01ac |
186 | |
0795f290 |
187 | return $self; |
ffed8b01 |
188 | } |
189 | |
ffed8b01 |
190 | sub TIEHASH { |
6fe26b29 |
191 | shift; |
192 | require DBM::Deep::Hash; |
193 | return DBM::Deep::Hash->TIEHASH( @_ ); |
ffed8b01 |
194 | } |
195 | |
196 | sub TIEARRAY { |
6fe26b29 |
197 | shift; |
198 | require DBM::Deep::Array; |
199 | return DBM::Deep::Array->TIEARRAY( @_ ); |
ffed8b01 |
200 | } |
201 | |
5c0756fc |
202 | sub lock_exclusive { |
994ccd8e |
203 | my $self = shift->_get_self; |
9c87a079 |
204 | return $self->_engine->lock_exclusive( $self, @_ ); |
5c0756fc |
205 | } |
206 | *lock = \&lock_exclusive; |
207 | sub lock_shared { |
208 | my $self = shift->_get_self; |
9c87a079 |
209 | return $self->_engine->lock_shared( $self, @_ ); |
ffed8b01 |
210 | } |
211 | |
212 | sub unlock { |
994ccd8e |
213 | my $self = shift->_get_self; |
9c87a079 |
214 | return $self->_engine->unlock( $self, @_ ); |
ffed8b01 |
215 | } |
216 | |
906c8e01 |
217 | sub _copy_value { |
218 | my $self = shift->_get_self; |
219 | my ($spot, $value) = @_; |
220 | |
221 | if ( !ref $value ) { |
222 | ${$spot} = $value; |
223 | } |
906c8e01 |
224 | else { |
225 | my $r = Scalar::Util::reftype( $value ); |
edd45134 |
226 | my $tied; |
906c8e01 |
227 | if ( $r eq 'ARRAY' ) { |
edd45134 |
228 | $tied = tied(@$value); |
906c8e01 |
229 | } |
2c70efe1 |
230 | elsif ( $r eq 'HASH' ) { |
edd45134 |
231 | $tied = tied(%$value); |
232 | } |
2c70efe1 |
233 | else { |
234 | __PACKAGE__->_throw_error( "Unknown type for '$value'" ); |
235 | } |
edd45134 |
236 | |
2c70efe1 |
237 | if ( eval { local $SIG{__DIE__}; $tied->isa( __PACKAGE__ ) } ) { |
edd45134 |
238 | ${$spot} = $tied->_repr; |
239 | $tied->_copy_node( ${$spot} ); |
240 | } |
241 | else { |
242 | if ( $r eq 'ARRAY' ) { |
243 | ${$spot} = [ @{$value} ]; |
244 | } |
245 | else { |
246 | ${$spot} = { %{$value} }; |
247 | } |
248 | } |
249 | |
250 | my $c = Scalar::Util::blessed( $value ); |
2c70efe1 |
251 | if ( defined $c && !$c->isa( __PACKAGE__ ) ) { |
edd45134 |
252 | ${$spot} = bless ${$spot}, $c |
906c8e01 |
253 | } |
906c8e01 |
254 | } |
255 | |
256 | return 1; |
257 | } |
258 | |
2120a181 |
259 | #sub _copy_node { |
260 | # die "Must be implemented in a child class\n"; |
261 | #} |
262 | # |
263 | #sub _repr { |
264 | # die "Must be implemented in a child class\n"; |
265 | #} |
ffed8b01 |
266 | |
267 | sub export { |
994ccd8e |
268 | my $self = shift->_get_self; |
d0b74c17 |
269 | |
f9c33187 |
270 | my $temp = $self->_repr; |
d0b74c17 |
271 | |
5c0756fc |
272 | $self->lock_exclusive; |
d0b74c17 |
273 | $self->_copy_node( $temp ); |
5c0756fc |
274 | $self->unlock; |
d0b74c17 |
275 | |
2120a181 |
276 | my $classname = $self->_engine->get_classname( $self ); |
277 | if ( defined $classname ) { |
278 | bless $temp, $classname; |
68f943b3 |
279 | } |
280 | |
d0b74c17 |
281 | return $temp; |
ffed8b01 |
282 | } |
283 | |
e00d0eb3 |
284 | sub _check_legality { |
285 | my $self = shift; |
286 | my ($val) = @_; |
287 | |
288 | my $r = Scalar::Util::reftype( $val ); |
289 | |
290 | return $r if !defined $r || '' eq $r; |
291 | return $r if 'HASH' eq $r; |
292 | return $r if 'ARRAY' eq $r; |
293 | |
2c70efe1 |
294 | __PACKAGE__->_throw_error( |
e00d0eb3 |
295 | "Storage of references of type '$r' is not supported." |
296 | ); |
297 | } |
298 | |
ffed8b01 |
299 | sub import { |
d426259c |
300 | return if !ref $_[0]; # Perl calls import() on use -- ignore |
d0b74c17 |
301 | |
994ccd8e |
302 | my $self = shift->_get_self; |
303 | my ($struct) = @_; |
d0b74c17 |
304 | |
e00d0eb3 |
305 | my $type = $self->_check_legality( $struct ); |
306 | if ( !$type ) { |
2c70efe1 |
307 | __PACKAGE__->_throw_error( "Cannot import a scalar" ); |
d0b74c17 |
308 | } |
309 | |
e00d0eb3 |
310 | if ( substr( $type, 0, 1 ) ne $self->_type ) { |
2c70efe1 |
311 | __PACKAGE__->_throw_error( |
e00d0eb3 |
312 | "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array') |
313 | . " into " . ('HASH' eq $type ? 'an array' : 'a hash') |
314 | ); |
7a960a12 |
315 | } |
316 | |
e00d0eb3 |
317 | my %seen; |
318 | my $recurse; |
319 | $recurse = sub { |
320 | my ($db, $val) = @_; |
321 | |
322 | my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db); |
323 | $obj ||= $db; |
324 | |
325 | my $r = $self->_check_legality( $val ); |
326 | if ( 'HASH' eq $r ) { |
327 | while ( my ($k, $v) = each %$val ) { |
328 | my $r = $self->_check_legality( $v ); |
329 | if ( $r ) { |
330 | my $temp = 'HASH' eq $r ? {} : []; |
331 | if ( my $c = Scalar::Util::blessed( $v ) ) { |
332 | bless $temp, $c; |
333 | } |
334 | $obj->put( $k, $temp ); |
335 | $recurse->( $temp, $v ); |
336 | } |
337 | else { |
338 | $obj->put( $k, $v ); |
339 | } |
340 | } |
341 | } |
342 | elsif ( 'ARRAY' eq $r ) { |
343 | foreach my $k ( 0 .. $#$val ) { |
344 | my $v = $val->[$k]; |
345 | my $r = $self->_check_legality( $v ); |
346 | if ( $r ) { |
347 | my $temp = 'HASH' eq $r ? {} : []; |
348 | if ( my $c = Scalar::Util::blessed( $v ) ) { |
349 | bless $temp, $c; |
350 | } |
351 | $obj->put( $k, $temp ); |
352 | $recurse->( $temp, $v ); |
353 | } |
354 | else { |
355 | $obj->put( $k, $v ); |
356 | } |
357 | } |
358 | } |
359 | }; |
360 | $recurse->( $self, $struct ); |
361 | |
7a960a12 |
362 | return 1; |
ffed8b01 |
363 | } |
364 | |
13ff93d5 |
365 | #XXX Need to keep track of who has a fh to this file in order to |
366 | #XXX close them all prior to optimize on Win32/cygwin |
d426259c |
367 | # Rebuild entire database into new file, then move |
368 | # it back on top of original. |
ffed8b01 |
369 | sub optimize { |
994ccd8e |
370 | my $self = shift->_get_self; |
cc4bef86 |
371 | |
d426259c |
372 | # Optimizing is only something we need to do when we're working with our |
373 | # own file format. Otherwise, let the other guy do the optimizations. |
51e9eee3 |
374 | return unless $self->_engine->isa( 'DBM::Deep::Engine::File' ); |
d426259c |
375 | |
cc4bef86 |
376 | #XXX Need to create a new test for this |
f1879fdc |
377 | # if ($self->_engine->storage->{links} > 1) { |
1400a48e |
378 | # $self->_throw_error("Cannot optimize: reference count is greater than 1"); |
d0b74c17 |
379 | # } |
380 | |
7a960a12 |
381 | #XXX Do we have to lock the tempfile? |
382 | |
e00d0eb3 |
383 | #XXX Should we use tempfile() here instead of a hard-coded name? |
f1879fdc |
384 | my $temp_filename = $self->_engine->storage->{file} . '.tmp'; |
2c70efe1 |
385 | my $db_temp = __PACKAGE__->new( |
6e6789b0 |
386 | file => $temp_filename, |
2120a181 |
387 | type => $self->_type, |
388 | |
389 | # Bring over all the parameters that we need to bring over |
888453b9 |
390 | ( map { $_ => $self->_engine->$_ } qw( |
391 | byte_size max_buckets data_sector_size num_txns |
392 | )), |
d0b74c17 |
393 | ); |
d0b74c17 |
394 | |
5c0756fc |
395 | $self->lock_exclusive; |
c57b19c6 |
396 | $self->_engine->clear_cache; |
d0b74c17 |
397 | $self->_copy_node( $db_temp ); |
f1879fdc |
398 | $db_temp->_engine->storage->close; |
d0b74c17 |
399 | undef $db_temp; |
400 | |
401 | ## |
402 | # Attempt to copy user, group and permissions over to new file |
403 | ## |
f1879fdc |
404 | $self->_engine->storage->copy_stats( $temp_filename ); |
d0b74c17 |
405 | |
ffed8b01 |
406 | # q.v. perlport for more information on this variable |
90f93b43 |
407 | if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) { |
d0b74c17 |
408 | ## |
409 | # Potential race condition when optmizing on Win32 with locking. |
410 | # The Windows filesystem requires that the filehandle be closed |
411 | # before it is overwritten with rename(). This could be redone |
412 | # with a soft copy. |
413 | ## |
5c0756fc |
414 | $self->unlock; |
f1879fdc |
415 | $self->_engine->storage->close; |
d0b74c17 |
416 | } |
417 | |
f1879fdc |
418 | if (!rename $temp_filename, $self->_engine->storage->{file}) { |
6e6789b0 |
419 | unlink $temp_filename; |
5c0756fc |
420 | $self->unlock; |
1400a48e |
421 | $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!"); |
d0b74c17 |
422 | } |
423 | |
5c0756fc |
424 | $self->unlock; |
f1879fdc |
425 | $self->_engine->storage->close; |
2120a181 |
426 | |
f1879fdc |
427 | $self->_engine->storage->open; |
5c0756fc |
428 | $self->lock_exclusive; |
f4d0ac97 |
429 | $self->_engine->setup( $self ); |
5c0756fc |
430 | $self->unlock; |
70b55428 |
431 | |
d0b74c17 |
432 | return 1; |
ffed8b01 |
433 | } |
434 | |
435 | sub clone { |
d0b74c17 |
436 | ## |
437 | # Make copy of object and return |
438 | ## |
994ccd8e |
439 | my $self = shift->_get_self; |
d0b74c17 |
440 | |
2c70efe1 |
441 | return __PACKAGE__->new( |
c3aafc14 |
442 | type => $self->_type, |
d0b74c17 |
443 | base_offset => $self->_base_offset, |
2120a181 |
444 | staleness => $self->_staleness, |
2120a181 |
445 | engine => $self->_engine, |
d0b74c17 |
446 | ); |
ffed8b01 |
447 | } |
448 | |
2120a181 |
449 | #XXX Migrate this to the engine, where it really belongs and go through some |
450 | # API - stop poking in the innards of someone else.. |
ffed8b01 |
451 | { |
452 | my %is_legal_filter = map { |
453 | $_ => ~~1, |
454 | } qw( |
455 | store_key store_value |
456 | fetch_key fetch_value |
457 | ); |
458 | |
459 | sub set_filter { |
994ccd8e |
460 | my $self = shift->_get_self; |
461 | my $type = lc shift; |
462 | my $func = shift; |
d0b74c17 |
463 | |
ffed8b01 |
464 | if ( $is_legal_filter{$type} ) { |
f1879fdc |
465 | $self->_engine->storage->{"filter_$type"} = $func; |
ffed8b01 |
466 | return 1; |
467 | } |
468 | |
469 | return; |
470 | } |
888453b9 |
471 | |
472 | sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); } |
473 | sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); } |
474 | sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); } |
475 | sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); } |
ffed8b01 |
476 | } |
477 | |
fee0243f |
478 | sub begin_work { |
479 | my $self = shift->_get_self; |
db2eb673 |
480 | $self->lock_exclusive; |
c65299b4 |
481 | my $rv = eval { $self->_engine->begin_work( $self, @_ ) }; |
482 | my $e = $@; |
db2eb673 |
483 | $self->unlock; |
c65299b4 |
484 | die $e if $e; |
db2eb673 |
485 | return $rv; |
fee0243f |
486 | } |
487 | |
488 | sub rollback { |
489 | my $self = shift->_get_self; |
db2eb673 |
490 | $self->lock_exclusive; |
c65299b4 |
491 | my $rv = eval { $self->_engine->rollback( $self, @_ ) }; |
492 | my $e = $@; |
db2eb673 |
493 | $self->unlock; |
c65299b4 |
494 | die $e if $e; |
db2eb673 |
495 | return $rv; |
fee0243f |
496 | } |
497 | |
359a01ac |
498 | sub commit { |
499 | my $self = shift->_get_self; |
db2eb673 |
500 | $self->lock_exclusive; |
c65299b4 |
501 | my $rv = eval { $self->_engine->commit( $self, @_ ) }; |
502 | my $e = $@; |
db2eb673 |
503 | $self->unlock; |
c65299b4 |
504 | die $e if $e; |
db2eb673 |
505 | return $rv; |
359a01ac |
506 | } |
fee0243f |
507 | |
ffed8b01 |
508 | # Accessor methods |
72e315ac |
509 | sub _engine { |
510 | my $self = $_[0]->_get_self; |
511 | return $self->{engine}; |
512 | } |
513 | |
4d35d856 |
514 | sub _type { |
2ac02042 |
515 | my $self = $_[0]->_get_self; |
d0b74c17 |
516 | return $self->{type}; |
ffed8b01 |
517 | } |
518 | |
4d35d856 |
519 | sub _base_offset { |
2ac02042 |
520 | my $self = $_[0]->_get_self; |
d0b74c17 |
521 | return $self->{base_offset}; |
ffed8b01 |
522 | } |
523 | |
2120a181 |
524 | sub _staleness { |
525 | my $self = $_[0]->_get_self; |
526 | return $self->{staleness}; |
527 | } |
528 | |
ffed8b01 |
529 | # Utility methods |
261d1296 |
530 | sub _throw_error { |
807f63a7 |
531 | my $n = 0; |
532 | while( 1 ) { |
533 | my @caller = caller( ++$n ); |
534 | next if $caller[0] =~ m/^DBM::Deep/; |
535 | |
536 | die "DBM::Deep: $_[1] at $0 line $caller[2]\n"; |
807f63a7 |
537 | } |
ffed8b01 |
538 | } |
539 | |
d426259c |
540 | # Store single hash key/value or array element in database. |
ffed8b01 |
541 | sub STORE { |
d0b74c17 |
542 | my $self = shift->_get_self; |
2120a181 |
543 | my ($key, $value) = @_; |
c803879b |
544 | warn "STORE($self, '$key', '@{[defined$value?$value:'undef']}')\n" if DEBUG; |
81d3d316 |
545 | |
f1879fdc |
546 | unless ( $self->_engine->storage->is_writable ) { |
acd4faf2 |
547 | $self->_throw_error( 'Cannot write to a readonly filehandle' ); |
548 | } |
d0b74c17 |
549 | |
5c0756fc |
550 | $self->lock_exclusive; |
d0b74c17 |
551 | |
0cb639bd |
552 | # User may be storing a complex value, in which case we do not want it run |
553 | # through the filtering system. |
f1879fdc |
554 | if ( !ref($value) && $self->_engine->storage->{filter_store_value} ) { |
555 | $value = $self->_engine->storage->{filter_store_value}->( $value ); |
d0b74c17 |
556 | } |
557 | |
2c70efe1 |
558 | $self->_engine->write_value( $self, $key, $value ); |
d0b74c17 |
559 | |
5c0756fc |
560 | $self->unlock; |
d0b74c17 |
561 | |
86867f3a |
562 | return 1; |
ffed8b01 |
563 | } |
564 | |
d426259c |
565 | # Fetch single value or element given plain key or array index |
ffed8b01 |
566 | sub FETCH { |
cb79ec85 |
567 | my $self = shift->_get_self; |
2120a181 |
568 | my ($key) = @_; |
c803879b |
569 | warn "FETCH($self, '$key')\n" if DEBUG; |
ffed8b01 |
570 | |
5c0756fc |
571 | $self->lock_shared; |
d0b74c17 |
572 | |
2c70efe1 |
573 | my $result = $self->_engine->read_value( $self, $key ); |
d0b74c17 |
574 | |
5c0756fc |
575 | $self->unlock; |
d0b74c17 |
576 | |
a86430bd |
577 | # Filters only apply to scalar values, so the ref check is making |
578 | # sure the fetched bucket is a scalar, not a child hash or array. |
f1879fdc |
579 | return ($result && !ref($result) && $self->_engine->storage->{filter_fetch_value}) |
580 | ? $self->_engine->storage->{filter_fetch_value}->($result) |
cb79ec85 |
581 | : $result; |
ffed8b01 |
582 | } |
583 | |
d426259c |
584 | # Delete single key/value pair or element given plain key or array index |
ffed8b01 |
585 | sub DELETE { |
a97c8f67 |
586 | my $self = shift->_get_self; |
2120a181 |
587 | my ($key) = @_; |
c803879b |
588 | warn "DELETE($self, '$key')\n" if DEBUG; |
d0b74c17 |
589 | |
f1879fdc |
590 | unless ( $self->_engine->storage->is_writable ) { |
a86430bd |
591 | $self->_throw_error( 'Cannot write to a readonly filehandle' ); |
592 | } |
d0b74c17 |
593 | |
5c0756fc |
594 | $self->lock_exclusive; |
d0b74c17 |
595 | |
d0b74c17 |
596 | ## |
597 | # Delete bucket |
598 | ## |
2120a181 |
599 | my $value = $self->_engine->delete_key( $self, $key); |
a86430bd |
600 | |
f1879fdc |
601 | if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) { |
602 | $value = $self->_engine->storage->{filter_fetch_value}->($value); |
3b6a5056 |
603 | } |
604 | |
5c0756fc |
605 | $self->unlock; |
d0b74c17 |
606 | |
607 | return $value; |
ffed8b01 |
608 | } |
609 | |
d426259c |
610 | # Check if a single key or element exists given plain key or array index |
ffed8b01 |
611 | sub EXISTS { |
a97c8f67 |
612 | my $self = shift->_get_self; |
613 | my ($key) = @_; |
c803879b |
614 | warn "EXISTS($self, '$key')\n" if DEBUG; |
d0b74c17 |
615 | |
5c0756fc |
616 | $self->lock_shared; |
d0b74c17 |
617 | |
2120a181 |
618 | my $result = $self->_engine->key_exists( $self, $key ); |
d0b74c17 |
619 | |
5c0756fc |
620 | $self->unlock; |
d0b74c17 |
621 | |
622 | return $result; |
ffed8b01 |
623 | } |
624 | |
d426259c |
625 | # Clear all keys from hash, or all elements from array. |
ffed8b01 |
626 | sub CLEAR { |
a97c8f67 |
627 | my $self = shift->_get_self; |
6e6789b0 |
628 | warn "CLEAR($self)\n" if DEBUG; |
ffed8b01 |
629 | |
f1879fdc |
630 | unless ( $self->_engine->storage->is_writable ) { |
a86430bd |
631 | $self->_throw_error( 'Cannot write to a readonly filehandle' ); |
632 | } |
633 | |
5c0756fc |
634 | $self->lock_exclusive; |
d0b74c17 |
635 | |
2120a181 |
636 | #XXX Rewrite this dreck to do it in the engine as a tight loop vs. |
637 | # iterating over keys - such a WASTE - is this required for transactional |
638 | # clearning?! Surely that can be detected in the engine ... |
f9a320bb |
639 | if ( $self->_type eq TYPE_HASH ) { |
640 | my $key = $self->first_key; |
641 | while ( $key ) { |
83c43bb5 |
642 | # Retrieve the key before deleting because we depend on next_key |
f9a320bb |
643 | my $next_key = $self->next_key( $key ); |
2120a181 |
644 | $self->_engine->delete_key( $self, $key, $key ); |
f9a320bb |
645 | $key = $next_key; |
646 | } |
647 | } |
648 | else { |
649 | my $size = $self->FETCHSIZE; |
c3aafc14 |
650 | for my $key ( 0 .. $size - 1 ) { |
2120a181 |
651 | $self->_engine->delete_key( $self, $key, $key ); |
f9a320bb |
652 | } |
653 | $self->STORESIZE( 0 ); |
654 | } |
d0b74c17 |
655 | |
5c0756fc |
656 | $self->unlock; |
d0b74c17 |
657 | |
658 | return 1; |
ffed8b01 |
659 | } |
660 | |
ffed8b01 |
661 | # Public method aliases |
d426259c |
662 | sub put { (shift)->STORE( @_ ) } |
663 | sub get { (shift)->FETCH( @_ ) } |
664 | sub store { (shift)->STORE( @_ ) } |
665 | sub fetch { (shift)->FETCH( @_ ) } |
baa27ab6 |
666 | sub delete { (shift)->DELETE( @_ ) } |
667 | sub exists { (shift)->EXISTS( @_ ) } |
d426259c |
668 | sub clear { (shift)->CLEAR( @_ ) } |
ffed8b01 |
669 | |
888453b9 |
670 | sub _dump_file {shift->_get_self->_engine->_dump_file;} |
671 | |
ffed8b01 |
672 | 1; |
ffed8b01 |
673 | __END__ |