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 | |
9c7d9738 |
8 | our $VERSION = q(1.0014); |
86867f3a |
9 | |
edd45134 |
10 | use Data::Dumper (); |
ffed8b01 |
11 | use Scalar::Util (); |
ffed8b01 |
12 | |
2120a181 |
13 | use DBM::Deep::Engine; |
460b1067 |
14 | use DBM::Deep::File; |
95967a5e |
15 | |
4f0f6fff |
16 | use DBM::Deep::SQL::Util; |
17 | use DBM::Deep::SQL::Array; |
18 | use DBM::Deep::SQL::Hash; |
19 | |
c57b19c6 |
20 | use overload |
21 | '""' => sub { overload::StrVal( $_[0] ) }, |
22 | fallback => 1; |
23 | |
6e6789b0 |
24 | use constant DEBUG => 0; |
25 | |
ffed8b01 |
26 | ## |
27 | # Setup constants for users to pass to new() |
28 | ## |
2120a181 |
29 | sub TYPE_HASH () { DBM::Deep::Engine->SIG_HASH } |
30 | sub TYPE_ARRAY () { DBM::Deep::Engine->SIG_ARRAY } |
ffed8b01 |
31 | |
2120a181 |
32 | # This is used in all the children of this class in their TIE<type> methods. |
0ca7ea98 |
33 | sub _get_args { |
34 | my $proto = shift; |
35 | |
36 | my $args; |
37 | if (scalar(@_) > 1) { |
38 | if ( @_ % 2 ) { |
39 | $proto->_throw_error( "Odd number of parameters to " . (caller(1))[2] ); |
40 | } |
41 | $args = {@_}; |
42 | } |
d0b74c17 |
43 | elsif ( ref $_[0] ) { |
4d35d856 |
44 | unless ( eval { local $SIG{'__DIE__'}; %{$_[0]} || 1 } ) { |
0ca7ea98 |
45 | $proto->_throw_error( "Not a hashref in args to " . (caller(1))[2] ); |
46 | } |
47 | $args = $_[0]; |
48 | } |
d0b74c17 |
49 | else { |
0ca7ea98 |
50 | $args = { file => shift }; |
51 | } |
52 | |
53 | return $args; |
54 | } |
55 | |
ffed8b01 |
56 | sub new { |
d0b74c17 |
57 | ## |
58 | # Class constructor method for Perl OO interface. |
59 | # Calls tie() and returns blessed reference to tied hash or array, |
60 | # providing a hybrid OO/tie interface. |
61 | ## |
62 | my $class = shift; |
63 | my $args = $class->_get_args( @_ ); |
4f0f6fff |
64 | my $self; |
65 | |
66 | ## |
67 | # Check for SQL storage |
68 | ## |
69 | if (exists $args->{dbi}) { |
70 | eval { |
71 | require DBIx::Abstract; |
72 | }; if ( $@ ) { |
73 | DBM::Deep->_throw_error('DBIx::Abstract not installed. You cannot use the SQL mode.'); |
74 | } |
75 | unless (UNIVERSAL::isa($args->{dbi}, 'DBIx::Abstract')) { |
76 | $args->{dbi} = DBIx::Abstract->connect($args->{dbi}); |
77 | } |
78 | |
79 | if (defined $args->{id}) { |
80 | unless ($args->{id} =~ /^\d+$/ && $args->{id} > 0) { |
81 | DBM::Deep->_throw_error('Invalid SQL record id'); |
82 | } |
83 | my $util = {dbi => $args->{dbi}}; |
84 | bless $util, 'DBM::Deep::SQL::Util'; |
85 | my $q = $util->_select( |
86 | table => 'rec_item', |
87 | fields => 'item_type', |
88 | where => {id => $args->{id}}, |
89 | ); |
90 | if ($q->[0]->[0] eq 'array') { |
91 | $args->{type} = TYPE_ARRAY; |
92 | } |
93 | elsif ($q->[0]->[0] eq 'hash') { |
94 | $args->{type} = TYPE_HASH; |
95 | } |
96 | else { |
97 | DBM::Deep->_throw_error('Unknown SQL record id'); |
98 | } |
99 | } |
100 | else { |
101 | my $util = {dbi => $args->{dbi}}; |
102 | bless $util, 'DBM::Deep::SQL::Util'; |
103 | if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) { |
104 | $args->{id} = $util->_create('array'); |
105 | } |
106 | else { |
107 | $args->{id} = $util->_create('hash'); |
108 | } |
109 | } |
110 | |
111 | if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) { |
112 | $class = 'DBM::Deep::SQL::Array'; |
113 | require DBM::Deep::SQL::Array; |
114 | tie @$self, $class, %$args; |
115 | if ($args->{prefetch}) { |
116 | (tied(@$self))->_prefetch(); |
117 | } |
118 | return bless $self, $class; |
119 | } |
120 | else { |
121 | $class = 'DBM::Deep::SQL::Hash'; |
122 | require DBM::Deep::SQL::Hash; |
123 | tie %$self, $class, %$args; |
124 | if ($args->{prefetch}) { |
125 | (tied(%$self))->_prefetch(); |
126 | } |
127 | return bless $self, $class; |
128 | } |
129 | } |
d0b74c17 |
130 | |
131 | ## |
132 | # Check if we want a tied hash or array. |
133 | ## |
d0b74c17 |
134 | if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) { |
6fe26b29 |
135 | $class = 'DBM::Deep::Array'; |
136 | require DBM::Deep::Array; |
d0b74c17 |
137 | tie @$self, $class, %$args; |
138 | } |
139 | else { |
6fe26b29 |
140 | $class = 'DBM::Deep::Hash'; |
141 | require DBM::Deep::Hash; |
d0b74c17 |
142 | tie %$self, $class, %$args; |
143 | } |
ffed8b01 |
144 | |
d0b74c17 |
145 | return bless $self, $class; |
ffed8b01 |
146 | } |
147 | |
96041a25 |
148 | # This initializer is called from the various TIE* methods. new() calls tie(), |
149 | # which allows for a single point of entry. |
0795f290 |
150 | sub _init { |
0795f290 |
151 | my $class = shift; |
994ccd8e |
152 | my ($args) = @_; |
0795f290 |
153 | |
460b1067 |
154 | # locking implicitly enables autoflush |
155 | if ($args->{locking}) { $args->{autoflush} = 1; } |
156 | |
0795f290 |
157 | # These are the defaults to be optionally overridden below |
158 | my $self = bless { |
95967a5e |
159 | type => TYPE_HASH, |
e06824f8 |
160 | base_offset => undef, |
2120a181 |
161 | staleness => undef, |
2120a181 |
162 | engine => undef, |
0795f290 |
163 | }, $class; |
2120a181 |
164 | |
165 | $args->{engine} = DBM::Deep::Engine->new( { %{$args}, obj => $self } ) |
166 | unless exists $args->{engine}; |
8db25060 |
167 | |
fde3db1a |
168 | # Grab the parameters we want to use |
0795f290 |
169 | foreach my $param ( keys %$self ) { |
170 | next unless exists $args->{$param}; |
3e9498a1 |
171 | $self->{$param} = $args->{$param}; |
ffed8b01 |
172 | } |
d0b74c17 |
173 | |
2120a181 |
174 | eval { |
175 | local $SIG{'__DIE__'}; |
0795f290 |
176 | |
5c0756fc |
177 | $self->lock_exclusive; |
2120a181 |
178 | $self->_engine->setup_fh( $self ); |
2120a181 |
179 | $self->unlock; |
180 | }; if ( $@ ) { |
181 | my $e = $@; |
182 | eval { local $SIG{'__DIE__'}; $self->unlock; }; |
183 | die $e; |
184 | } |
359a01ac |
185 | |
0795f290 |
186 | return $self; |
ffed8b01 |
187 | } |
188 | |
ffed8b01 |
189 | sub TIEHASH { |
6fe26b29 |
190 | shift; |
191 | require DBM::Deep::Hash; |
192 | return DBM::Deep::Hash->TIEHASH( @_ ); |
ffed8b01 |
193 | } |
194 | |
195 | sub TIEARRAY { |
6fe26b29 |
196 | shift; |
197 | require DBM::Deep::Array; |
198 | return DBM::Deep::Array->TIEARRAY( @_ ); |
ffed8b01 |
199 | } |
200 | |
5c0756fc |
201 | sub lock_exclusive { |
994ccd8e |
202 | my $self = shift->_get_self; |
9c87a079 |
203 | return $self->_engine->lock_exclusive( $self, @_ ); |
5c0756fc |
204 | } |
205 | *lock = \&lock_exclusive; |
206 | sub lock_shared { |
207 | my $self = shift->_get_self; |
9c87a079 |
208 | return $self->_engine->lock_shared( $self, @_ ); |
ffed8b01 |
209 | } |
210 | |
211 | sub unlock { |
994ccd8e |
212 | my $self = shift->_get_self; |
9c87a079 |
213 | return $self->_engine->unlock( $self, @_ ); |
ffed8b01 |
214 | } |
215 | |
906c8e01 |
216 | sub _copy_value { |
217 | my $self = shift->_get_self; |
218 | my ($spot, $value) = @_; |
219 | |
220 | if ( !ref $value ) { |
221 | ${$spot} = $value; |
222 | } |
906c8e01 |
223 | else { |
edd45134 |
224 | # This assumes hash or array only. This is a bad assumption moving forward. |
225 | # -RobK, 2008-05-27 |
906c8e01 |
226 | my $r = Scalar::Util::reftype( $value ); |
edd45134 |
227 | my $tied; |
906c8e01 |
228 | if ( $r eq 'ARRAY' ) { |
edd45134 |
229 | $tied = tied(@$value); |
906c8e01 |
230 | } |
231 | else { |
edd45134 |
232 | $tied = tied(%$value); |
233 | } |
234 | |
235 | if ( eval { local $SIG{__DIE__}; $tied->isa( 'DBM::Deep' ) } ) { |
236 | ${$spot} = $tied->_repr; |
237 | $tied->_copy_node( ${$spot} ); |
238 | } |
239 | else { |
240 | if ( $r eq 'ARRAY' ) { |
241 | ${$spot} = [ @{$value} ]; |
242 | } |
243 | else { |
244 | ${$spot} = { %{$value} }; |
245 | } |
246 | } |
247 | |
248 | my $c = Scalar::Util::blessed( $value ); |
249 | if ( defined $c && !$c->isa( 'DBM::Deep') ) { |
250 | ${$spot} = bless ${$spot}, $c |
906c8e01 |
251 | } |
906c8e01 |
252 | } |
253 | |
254 | return 1; |
255 | } |
256 | |
2120a181 |
257 | #sub _copy_node { |
258 | # die "Must be implemented in a child class\n"; |
259 | #} |
260 | # |
261 | #sub _repr { |
262 | # die "Must be implemented in a child class\n"; |
263 | #} |
ffed8b01 |
264 | |
265 | sub export { |
d0b74c17 |
266 | ## |
267 | # Recursively export into standard Perl hashes and arrays. |
268 | ## |
994ccd8e |
269 | my $self = shift->_get_self; |
d0b74c17 |
270 | |
f9c33187 |
271 | my $temp = $self->_repr; |
d0b74c17 |
272 | |
5c0756fc |
273 | $self->lock_exclusive; |
d0b74c17 |
274 | $self->_copy_node( $temp ); |
5c0756fc |
275 | $self->unlock; |
d0b74c17 |
276 | |
2120a181 |
277 | my $classname = $self->_engine->get_classname( $self ); |
278 | if ( defined $classname ) { |
279 | bless $temp, $classname; |
68f943b3 |
280 | } |
281 | |
d0b74c17 |
282 | return $temp; |
ffed8b01 |
283 | } |
284 | |
e00d0eb3 |
285 | sub _check_legality { |
286 | my $self = shift; |
287 | my ($val) = @_; |
288 | |
289 | my $r = Scalar::Util::reftype( $val ); |
290 | |
291 | return $r if !defined $r || '' eq $r; |
292 | return $r if 'HASH' eq $r; |
293 | return $r if 'ARRAY' eq $r; |
294 | |
295 | DBM::Deep->_throw_error( |
296 | "Storage of references of type '$r' is not supported." |
297 | ); |
298 | } |
299 | |
ffed8b01 |
300 | sub import { |
e00d0eb3 |
301 | # Perl calls import() on use -- ignore |
302 | return if !ref $_[0]; |
d0b74c17 |
303 | |
994ccd8e |
304 | my $self = shift->_get_self; |
305 | my ($struct) = @_; |
d0b74c17 |
306 | |
e00d0eb3 |
307 | my $type = $self->_check_legality( $struct ); |
308 | if ( !$type ) { |
309 | DBM::Deep->_throw_error( "Cannot import a scalar" ); |
d0b74c17 |
310 | } |
311 | |
e00d0eb3 |
312 | if ( substr( $type, 0, 1 ) ne $self->_type ) { |
313 | DBM::Deep->_throw_error( |
314 | "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array') |
315 | . " into " . ('HASH' eq $type ? 'an array' : 'a hash') |
316 | ); |
7a960a12 |
317 | } |
318 | |
e00d0eb3 |
319 | my %seen; |
320 | my $recurse; |
321 | $recurse = sub { |
322 | my ($db, $val) = @_; |
323 | |
324 | my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db); |
325 | $obj ||= $db; |
326 | |
327 | my $r = $self->_check_legality( $val ); |
328 | if ( 'HASH' eq $r ) { |
329 | while ( my ($k, $v) = each %$val ) { |
330 | my $r = $self->_check_legality( $v ); |
331 | if ( $r ) { |
332 | my $temp = 'HASH' eq $r ? {} : []; |
333 | if ( my $c = Scalar::Util::blessed( $v ) ) { |
334 | bless $temp, $c; |
335 | } |
336 | $obj->put( $k, $temp ); |
337 | $recurse->( $temp, $v ); |
338 | } |
339 | else { |
340 | $obj->put( $k, $v ); |
341 | } |
342 | } |
343 | } |
344 | elsif ( 'ARRAY' eq $r ) { |
345 | foreach my $k ( 0 .. $#$val ) { |
346 | my $v = $val->[$k]; |
347 | my $r = $self->_check_legality( $v ); |
348 | if ( $r ) { |
349 | my $temp = 'HASH' eq $r ? {} : []; |
350 | if ( my $c = Scalar::Util::blessed( $v ) ) { |
351 | bless $temp, $c; |
352 | } |
353 | $obj->put( $k, $temp ); |
354 | $recurse->( $temp, $v ); |
355 | } |
356 | else { |
357 | $obj->put( $k, $v ); |
358 | } |
359 | } |
360 | } |
361 | }; |
362 | $recurse->( $self, $struct ); |
363 | |
7a960a12 |
364 | return 1; |
ffed8b01 |
365 | } |
366 | |
13ff93d5 |
367 | #XXX Need to keep track of who has a fh to this file in order to |
368 | #XXX close them all prior to optimize on Win32/cygwin |
ffed8b01 |
369 | sub optimize { |
d0b74c17 |
370 | ## |
371 | # Rebuild entire database into new file, then move |
372 | # it back on top of original. |
373 | ## |
994ccd8e |
374 | my $self = shift->_get_self; |
cc4bef86 |
375 | |
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'; |
d0b74c17 |
385 | my $db_temp = DBM::Deep->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; |
72e315ac |
429 | $self->_engine->setup_fh( $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 | |
441 | return DBM::Deep->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 | ## |
509 | # Accessor methods |
510 | ## |
511 | |
72e315ac |
512 | sub _engine { |
513 | my $self = $_[0]->_get_self; |
514 | return $self->{engine}; |
515 | } |
516 | |
4d35d856 |
517 | sub _type { |
2ac02042 |
518 | my $self = $_[0]->_get_self; |
d0b74c17 |
519 | return $self->{type}; |
ffed8b01 |
520 | } |
521 | |
4d35d856 |
522 | sub _base_offset { |
2ac02042 |
523 | my $self = $_[0]->_get_self; |
d0b74c17 |
524 | return $self->{base_offset}; |
ffed8b01 |
525 | } |
526 | |
2120a181 |
527 | sub _staleness { |
528 | my $self = $_[0]->_get_self; |
529 | return $self->{staleness}; |
530 | } |
531 | |
ffed8b01 |
532 | ## |
533 | # Utility methods |
534 | ## |
535 | |
261d1296 |
536 | sub _throw_error { |
807f63a7 |
537 | my $n = 0; |
538 | while( 1 ) { |
539 | my @caller = caller( ++$n ); |
540 | next if $caller[0] =~ m/^DBM::Deep/; |
541 | |
542 | die "DBM::Deep: $_[1] at $0 line $caller[2]\n"; |
807f63a7 |
543 | } |
ffed8b01 |
544 | } |
545 | |
ffed8b01 |
546 | sub STORE { |
d0b74c17 |
547 | ## |
548 | # Store single hash key/value or array element in database. |
549 | ## |
550 | my $self = shift->_get_self; |
2120a181 |
551 | my ($key, $value) = @_; |
c803879b |
552 | warn "STORE($self, '$key', '@{[defined$value?$value:'undef']}')\n" if DEBUG; |
81d3d316 |
553 | |
f1879fdc |
554 | unless ( $self->_engine->storage->is_writable ) { |
acd4faf2 |
555 | $self->_throw_error( 'Cannot write to a readonly filehandle' ); |
556 | } |
d0b74c17 |
557 | |
5c0756fc |
558 | $self->lock_exclusive; |
d0b74c17 |
559 | |
0cb639bd |
560 | # User may be storing a complex value, in which case we do not want it run |
561 | # through the filtering system. |
f1879fdc |
562 | if ( !ref($value) && $self->_engine->storage->{filter_store_value} ) { |
563 | $value = $self->_engine->storage->{filter_store_value}->( $value ); |
d0b74c17 |
564 | } |
565 | |
4f0f6fff |
566 | $self->_engine->write_value( $self, $key, $value); |
d0b74c17 |
567 | |
5c0756fc |
568 | $self->unlock; |
d0b74c17 |
569 | |
86867f3a |
570 | return 1; |
ffed8b01 |
571 | } |
572 | |
573 | sub FETCH { |
d0b74c17 |
574 | ## |
575 | # Fetch single value or element given plain key or array index |
576 | ## |
cb79ec85 |
577 | my $self = shift->_get_self; |
2120a181 |
578 | my ($key) = @_; |
c803879b |
579 | warn "FETCH($self, '$key')\n" if DEBUG; |
ffed8b01 |
580 | |
5c0756fc |
581 | $self->lock_shared; |
d0b74c17 |
582 | |
2120a181 |
583 | my $result = $self->_engine->read_value( $self, $key); |
d0b74c17 |
584 | |
5c0756fc |
585 | $self->unlock; |
d0b74c17 |
586 | |
a86430bd |
587 | # Filters only apply to scalar values, so the ref check is making |
588 | # sure the fetched bucket is a scalar, not a child hash or array. |
f1879fdc |
589 | return ($result && !ref($result) && $self->_engine->storage->{filter_fetch_value}) |
590 | ? $self->_engine->storage->{filter_fetch_value}->($result) |
cb79ec85 |
591 | : $result; |
ffed8b01 |
592 | } |
593 | |
594 | sub DELETE { |
d0b74c17 |
595 | ## |
596 | # Delete single key/value pair or element given plain key or array index |
597 | ## |
a97c8f67 |
598 | my $self = shift->_get_self; |
2120a181 |
599 | my ($key) = @_; |
c803879b |
600 | warn "DELETE($self, '$key')\n" if DEBUG; |
d0b74c17 |
601 | |
f1879fdc |
602 | unless ( $self->_engine->storage->is_writable ) { |
a86430bd |
603 | $self->_throw_error( 'Cannot write to a readonly filehandle' ); |
604 | } |
d0b74c17 |
605 | |
5c0756fc |
606 | $self->lock_exclusive; |
d0b74c17 |
607 | |
d0b74c17 |
608 | ## |
609 | # Delete bucket |
610 | ## |
2120a181 |
611 | my $value = $self->_engine->delete_key( $self, $key); |
a86430bd |
612 | |
f1879fdc |
613 | if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) { |
614 | $value = $self->_engine->storage->{filter_fetch_value}->($value); |
3b6a5056 |
615 | } |
616 | |
5c0756fc |
617 | $self->unlock; |
d0b74c17 |
618 | |
619 | return $value; |
ffed8b01 |
620 | } |
621 | |
622 | sub EXISTS { |
d0b74c17 |
623 | ## |
624 | # Check if a single key or element exists given plain key or array index |
625 | ## |
a97c8f67 |
626 | my $self = shift->_get_self; |
627 | my ($key) = @_; |
c803879b |
628 | warn "EXISTS($self, '$key')\n" if DEBUG; |
d0b74c17 |
629 | |
5c0756fc |
630 | $self->lock_shared; |
d0b74c17 |
631 | |
2120a181 |
632 | my $result = $self->_engine->key_exists( $self, $key ); |
d0b74c17 |
633 | |
5c0756fc |
634 | $self->unlock; |
d0b74c17 |
635 | |
636 | return $result; |
ffed8b01 |
637 | } |
638 | |
639 | sub CLEAR { |
d0b74c17 |
640 | ## |
641 | # Clear all keys from hash, or all elements from array. |
642 | ## |
a97c8f67 |
643 | my $self = shift->_get_self; |
6e6789b0 |
644 | warn "CLEAR($self)\n" if DEBUG; |
ffed8b01 |
645 | |
f1879fdc |
646 | unless ( $self->_engine->storage->is_writable ) { |
a86430bd |
647 | $self->_throw_error( 'Cannot write to a readonly filehandle' ); |
648 | } |
649 | |
5c0756fc |
650 | $self->lock_exclusive; |
d0b74c17 |
651 | |
2120a181 |
652 | #XXX Rewrite this dreck to do it in the engine as a tight loop vs. |
653 | # iterating over keys - such a WASTE - is this required for transactional |
654 | # clearning?! Surely that can be detected in the engine ... |
f9a320bb |
655 | if ( $self->_type eq TYPE_HASH ) { |
656 | my $key = $self->first_key; |
657 | while ( $key ) { |
83c43bb5 |
658 | # Retrieve the key before deleting because we depend on next_key |
f9a320bb |
659 | my $next_key = $self->next_key( $key ); |
2120a181 |
660 | $self->_engine->delete_key( $self, $key, $key ); |
f9a320bb |
661 | $key = $next_key; |
662 | } |
663 | } |
664 | else { |
665 | my $size = $self->FETCHSIZE; |
c3aafc14 |
666 | for my $key ( 0 .. $size - 1 ) { |
2120a181 |
667 | $self->_engine->delete_key( $self, $key, $key ); |
f9a320bb |
668 | } |
669 | $self->STORESIZE( 0 ); |
670 | } |
d0b74c17 |
671 | |
5c0756fc |
672 | $self->unlock; |
d0b74c17 |
673 | |
674 | return 1; |
ffed8b01 |
675 | } |
676 | |
ffed8b01 |
677 | ## |
678 | # Public method aliases |
679 | ## |
7f441181 |
680 | sub put { (shift)->STORE( @_ ) } |
681 | sub store { (shift)->STORE( @_ ) } |
682 | sub get { (shift)->FETCH( @_ ) } |
683 | sub fetch { (shift)->FETCH( @_ ) } |
baa27ab6 |
684 | sub delete { (shift)->DELETE( @_ ) } |
685 | sub exists { (shift)->EXISTS( @_ ) } |
686 | sub clear { (shift)->CLEAR( @_ ) } |
ffed8b01 |
687 | |
888453b9 |
688 | sub _dump_file {shift->_get_self->_engine->_dump_file;} |
689 | |
ffed8b01 |
690 | 1; |
ffed8b01 |
691 | __END__ |