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