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