6 use warnings FATAL => 'all';
8 our $VERSION = q(1.0014);
13 use DBM::Deep::Engine;
17 '""' => sub { overload::StrVal( $_[0] ) },
20 use constant DEBUG => 0;
23 # Setup constants for users to pass to new()
25 sub TYPE_HASH () { DBM::Deep::Engine->SIG_HASH }
26 sub TYPE_ARRAY () { DBM::Deep::Engine->SIG_ARRAY }
28 # This is used in all the children of this class in their TIE<type> methods.
35 $proto->_throw_error( "Odd number of parameters to " . (caller(1))[2] );
40 unless ( eval { local $SIG{'__DIE__'}; %{$_[0]} || 1 } ) {
41 $proto->_throw_error( "Not a hashref in args to " . (caller(1))[2] );
46 $args = { file => shift };
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.
59 my $args = $class->_get_args( @_ );
62 # Check if we want a tied hash or array.
65 if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) {
66 $class = 'DBM::Deep::Array';
67 require DBM::Deep::Array;
68 tie @$self, $class, %$args;
71 $class = 'DBM::Deep::Hash';
72 require DBM::Deep::Hash;
73 tie %$self, $class, %$args;
76 return bless $self, $class;
82 # If we have an error, don't flush - we might be flushing bad stuff. -RobK, 2008-06-26
85 #XXX For some reason, this causes an allocation error in the final scope close
86 # of t/08_deephash.t. -RobK, 2008-06-28
87 $self->_get_self->_engine->flush;
90 # This initializer is called from the various TIE* methods. new() calls tie(),
91 # which allows for a single point of entry.
96 # locking implicitly enables autoflush
97 if ($args->{locking}) { $args->{autoflush} = 1; }
99 # These are the defaults to be optionally overridden below
102 base_offset => undef,
107 $args->{engine} = DBM::Deep::Engine->new( { %{$args}, obj => $self } )
108 unless exists $args->{engine};
110 # Grab the parameters we want to use
111 foreach my $param ( keys %$self ) {
112 next unless exists $args->{$param};
113 $self->{$param} = $args->{$param};
117 local $SIG{'__DIE__'};
119 $self->lock_exclusive;
120 $self->_engine->setup_fh( $self );
124 eval { local $SIG{'__DIE__'}; $self->unlock; };
133 require DBM::Deep::Hash;
134 return DBM::Deep::Hash->TIEHASH( @_ );
139 require DBM::Deep::Array;
140 return DBM::Deep::Array->TIEARRAY( @_ );
144 my $self = shift->_get_self;
145 return $self->_engine->lock_exclusive( $self );
147 *lock = \&lock_exclusive;
149 my $self = shift->_get_self;
150 return $self->_engine->lock_shared( $self );
154 my $self = shift->_get_self;
155 return $self->_engine->unlock( $self );
159 my $self = shift->_get_self;
160 my ($spot, $value) = @_;
166 # This assumes hash or array only. This is a bad assumption moving forward.
168 my $r = Scalar::Util::reftype( $value );
170 if ( $r eq 'ARRAY' ) {
171 $tied = tied(@$value);
174 $tied = tied(%$value);
177 if ( eval { local $SIG{__DIE__}; $tied->isa( 'DBM::Deep' ) } ) {
178 ${$spot} = $tied->_repr;
179 $tied->_copy_node( ${$spot} );
182 if ( $r eq 'ARRAY' ) {
183 ${$spot} = [ @{$value} ];
186 ${$spot} = { %{$value} };
190 my $c = Scalar::Util::blessed( $value );
191 if ( defined $c && !$c->isa( 'DBM::Deep') ) {
192 ${$spot} = bless ${$spot}, $c
200 # die "Must be implemented in a child class\n";
204 # die "Must be implemented in a child class\n";
209 # Recursively export into standard Perl hashes and arrays.
211 my $self = shift->_get_self;
213 my $temp = $self->_repr;
215 $self->lock_exclusive;
216 $self->_copy_node( $temp );
219 my $classname = $self->_engine->get_classname( $self );
220 if ( defined $classname ) {
221 bless $temp, $classname;
227 sub _check_legality {
231 my $r = Scalar::Util::reftype( $val );
233 return $r if !defined $r || '' eq $r;
234 return $r if 'HASH' eq $r;
235 return $r if 'ARRAY' eq $r;
237 DBM::Deep->_throw_error(
238 "Storage of references of type '$r' is not supported."
243 # Perl calls import() on use -- ignore
244 return if !ref $_[0];
246 my $self = shift->_get_self;
249 my $type = $self->_check_legality( $struct );
251 DBM::Deep->_throw_error( "Cannot import a scalar" );
254 if ( substr( $type, 0, 1 ) ne $self->_type ) {
255 DBM::Deep->_throw_error(
256 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
257 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
266 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
269 my $r = $self->_check_legality( $val );
270 if ( 'HASH' eq $r ) {
271 while ( my ($k, $v) = each %$val ) {
272 my $r = $self->_check_legality( $v );
274 my $temp = 'HASH' eq $r ? {} : [];
275 if ( my $c = Scalar::Util::blessed( $v ) ) {
278 $obj->put( $k, $temp );
279 $recurse->( $temp, $v );
286 elsif ( 'ARRAY' eq $r ) {
287 foreach my $k ( 0 .. $#$val ) {
289 my $r = $self->_check_legality( $v );
291 my $temp = 'HASH' eq $r ? {} : [];
292 if ( my $c = Scalar::Util::blessed( $v ) ) {
295 $obj->put( $k, $temp );
296 $recurse->( $temp, $v );
304 $recurse->( $self, $struct );
309 #XXX Need to keep track of who has a fh to this file in order to
310 #XXX close them all prior to optimize on Win32/cygwin
313 # Rebuild entire database into new file, then move
314 # it back on top of original.
316 my $self = shift->_get_self;
318 #XXX Need to create a new test for this
319 # if ($self->_engine->storage->{links} > 1) {
320 # $self->_throw_error("Cannot optimize: reference count is greater than 1");
323 #XXX Do we have to lock the tempfile?
325 #XXX Should we use tempfile() here instead of a hard-coded name?
326 my $temp_filename = $self->_engine->storage->{file} . '.tmp';
327 my $db_temp = DBM::Deep->new(
328 file => $temp_filename,
329 type => $self->_type,
331 # Bring over all the parameters that we need to bring over
332 ( map { $_ => $self->_engine->$_ } qw(
333 byte_size max_buckets data_sector_size num_txns
337 $self->lock_exclusive;
338 $self->_engine->clear_cache;
339 $self->_copy_node( $db_temp );
340 $db_temp->_engine->storage->close;
344 # Attempt to copy user, group and permissions over to new file
346 $self->_engine->storage->copy_stats( $temp_filename );
348 # q.v. perlport for more information on this variable
349 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
351 # Potential race condition when optmizing on Win32 with locking.
352 # The Windows filesystem requires that the filehandle be closed
353 # before it is overwritten with rename(). This could be redone
357 $self->_engine->storage->close;
360 if (!rename $temp_filename, $self->_engine->storage->{file}) {
361 unlink $temp_filename;
363 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
367 $self->_engine->storage->close;
369 $self->_engine->storage->open;
370 $self->lock_exclusive;
371 $self->_engine->setup_fh( $self );
379 # Make copy of object and return
381 my $self = shift->_get_self;
383 return DBM::Deep->new(
384 type => $self->_type,
385 base_offset => $self->_base_offset,
386 staleness => $self->_staleness,
387 engine => $self->_engine,
391 #XXX Migrate this to the engine, where it really belongs and go through some
392 # API - stop poking in the innards of someone else..
394 my %is_legal_filter = map {
397 store_key store_value
398 fetch_key fetch_value
402 my $self = shift->_get_self;
406 if ( $is_legal_filter{$type} ) {
407 $self->_engine->storage->{"filter_$type"} = $func;
414 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
415 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
416 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
417 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
421 my $self = shift->_get_self;
422 $self->lock_exclusive;
423 my $rv = eval { $self->_engine->begin_work( $self, @_ ) };
431 my $self = shift->_get_self;
432 $self->lock_exclusive;
433 my $rv = eval { $self->_engine->rollback( $self, @_ ) };
441 my $self = shift->_get_self;
442 $self->lock_exclusive;
443 my $rv = eval { $self->_engine->commit( $self, @_ ) };
455 my $self = $_[0]->_get_self;
456 return $self->{engine};
460 my $self = $_[0]->_get_self;
461 return $self->{type};
465 my $self = $_[0]->_get_self;
466 return $self->{base_offset};
470 my $self = $_[0]->_get_self;
471 return $self->{staleness};
481 my @caller = caller( ++$n );
482 next if $caller[0] =~ m/^DBM::Deep/;
484 die "DBM::Deep: $_[1] at $0 line $caller[2]\n";
490 # Store single hash key/value or array element in database.
492 my $self = shift->_get_self;
493 my ($key, $value) = @_;
494 warn "STORE($self, $key, @{[defined$value?$value:'undef']})\n" if DEBUG;
496 unless ( $self->_engine->storage->is_writable ) {
497 $self->_throw_error( 'Cannot write to a readonly filehandle' );
500 $self->lock_exclusive;
502 # User may be storing a complex value, in which case we do not want it run
503 # through the filtering system.
504 if ( !ref($value) && $self->_engine->storage->{filter_store_value} ) {
505 $value = $self->_engine->storage->{filter_store_value}->( $value );
508 $self->_engine->write_value( $self, $key, $value);
517 # Fetch single value or element given plain key or array index
519 my $self = shift->_get_self;
521 warn "FETCH($self,$key)\n" if DEBUG;
525 my $result = $self->_engine->read_value( $self, $key);
529 # Filters only apply to scalar values, so the ref check is making
530 # sure the fetched bucket is a scalar, not a child hash or array.
531 return ($result && !ref($result) && $self->_engine->storage->{filter_fetch_value})
532 ? $self->_engine->storage->{filter_fetch_value}->($result)
538 # Delete single key/value pair or element given plain key or array index
540 my $self = shift->_get_self;
542 warn "DELETE($self,$key)\n" if DEBUG;
544 unless ( $self->_engine->storage->is_writable ) {
545 $self->_throw_error( 'Cannot write to a readonly filehandle' );
548 $self->lock_exclusive;
553 my $value = $self->_engine->delete_key( $self, $key);
555 if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) {
556 $value = $self->_engine->storage->{filter_fetch_value}->($value);
566 # Check if a single key or element exists given plain key or array index
568 my $self = shift->_get_self;
570 warn "EXISTS($self,$key)\n" if DEBUG;
574 my $result = $self->_engine->key_exists( $self, $key );
583 # Clear all keys from hash, or all elements from array.
585 my $self = shift->_get_self;
586 warn "CLEAR($self)\n" if DEBUG;
588 unless ( $self->_engine->storage->is_writable ) {
589 $self->_throw_error( 'Cannot write to a readonly filehandle' );
592 $self->lock_exclusive;
594 #XXX Rewrite this dreck to do it in the engine as a tight loop vs.
595 # iterating over keys - such a WASTE - is this required for transactional
596 # clearning?! Surely that can be detected in the engine ...
597 if ( $self->_type eq TYPE_HASH ) {
598 my $key = $self->first_key;
600 # Retrieve the key before deleting because we depend on next_key
601 my $next_key = $self->next_key( $key );
602 $self->_engine->delete_key( $self, $key, $key );
607 my $size = $self->FETCHSIZE;
608 for my $key ( 0 .. $size - 1 ) {
609 $self->_engine->delete_key( $self, $key, $key );
611 $self->STORESIZE( 0 );
620 # Public method aliases
622 sub put { (shift)->STORE( @_ ) }
623 sub store { (shift)->STORE( @_ ) }
624 sub get { (shift)->FETCH( @_ ) }
625 sub fetch { (shift)->FETCH( @_ ) }
626 sub delete { (shift)->DELETE( @_ ) }
627 sub exists { (shift)->EXISTS( @_ ) }
628 sub clear { (shift)->CLEAR( @_ ) }
630 sub _dump_file {shift->_get_self->_engine->_dump_file;}