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;
79 # This initializer is called from the various TIE* methods. new() calls tie(),
80 # which allows for a single point of entry.
85 $args->{storage} = DBM::Deep::File->new( $args )
86 unless exists $args->{storage};
88 # locking implicitly enables autoflush
89 if ($args->{locking}) { $args->{autoflush} = 1; }
91 # These are the defaults to be optionally overridden below
101 $args->{engine} = DBM::Deep::Engine->new( { %{$args}, obj => $self } )
102 unless exists $args->{engine};
104 # Grab the parameters we want to use
105 foreach my $param ( keys %$self ) {
106 next unless exists $args->{$param};
107 $self->{$param} = $args->{$param};
111 local $SIG{'__DIE__'};
113 $self->lock_exclusive;
114 $self->_engine->setup_fh( $self );
115 $self->_storage->set_inode;
119 eval { local $SIG{'__DIE__'}; $self->unlock; };
128 require DBM::Deep::Hash;
129 return DBM::Deep::Hash->TIEHASH( @_ );
134 require DBM::Deep::Array;
135 return DBM::Deep::Array->TIEARRAY( @_ );
139 my $self = shift->_get_self;
140 return $self->_storage->lock_exclusive( $self );
142 *lock = \&lock_exclusive;
144 my $self = shift->_get_self;
145 return $self->_storage->lock_shared( $self );
149 my $self = shift->_get_self;
150 return $self->_storage->unlock( $self );
154 my $self = shift->_get_self;
155 my ($spot, $value) = @_;
161 # This assumes hash or array only. This is a bad assumption moving forward.
163 my $r = Scalar::Util::reftype( $value );
165 if ( $r eq 'ARRAY' ) {
166 $tied = tied(@$value);
169 $tied = tied(%$value);
172 if ( eval { local $SIG{__DIE__}; $tied->isa( 'DBM::Deep' ) } ) {
173 ${$spot} = $tied->_repr;
174 $tied->_copy_node( ${$spot} );
177 if ( $r eq 'ARRAY' ) {
178 ${$spot} = [ @{$value} ];
181 ${$spot} = { %{$value} };
185 my $c = Scalar::Util::blessed( $value );
186 if ( defined $c && !$c->isa( 'DBM::Deep') ) {
187 ${$spot} = bless ${$spot}, $c
195 # die "Must be implemented in a child class\n";
199 # die "Must be implemented in a child class\n";
204 # Recursively export into standard Perl hashes and arrays.
206 my $self = shift->_get_self;
208 my $temp = $self->_repr;
210 $self->lock_exclusive;
211 $self->_copy_node( $temp );
214 my $classname = $self->_engine->get_classname( $self );
215 if ( defined $classname ) {
216 bless $temp, $classname;
222 sub _check_legality {
226 my $r = Scalar::Util::reftype( $val );
228 return $r if !defined $r || '' eq $r;
229 return $r if 'HASH' eq $r;
230 return $r if 'ARRAY' eq $r;
232 DBM::Deep->_throw_error(
233 "Storage of references of type '$r' is not supported."
238 # Perl calls import() on use -- ignore
239 return if !ref $_[0];
241 my $self = shift->_get_self;
244 my $type = $self->_check_legality( $struct );
246 DBM::Deep->_throw_error( "Cannot import a scalar" );
249 if ( substr( $type, 0, 1 ) ne $self->_type ) {
250 DBM::Deep->_throw_error(
251 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
252 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
261 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
264 my $r = $self->_check_legality( $val );
265 if ( 'HASH' eq $r ) {
266 while ( my ($k, $v) = each %$val ) {
267 my $r = $self->_check_legality( $v );
269 my $temp = 'HASH' eq $r ? {} : [];
270 if ( my $c = Scalar::Util::blessed( $v ) ) {
273 $obj->put( $k, $temp );
274 $recurse->( $temp, $v );
281 elsif ( 'ARRAY' eq $r ) {
282 foreach my $k ( 0 .. $#$val ) {
284 my $r = $self->_check_legality( $v );
286 my $temp = 'HASH' eq $r ? {} : [];
287 if ( my $c = Scalar::Util::blessed( $v ) ) {
290 $obj->put( $k, $temp );
291 $recurse->( $temp, $v );
299 $recurse->( $self, $struct );
304 #XXX Need to keep track of who has a fh to this file in order to
305 #XXX close them all prior to optimize on Win32/cygwin
308 # Rebuild entire database into new file, then move
309 # it back on top of original.
311 my $self = shift->_get_self;
313 #XXX Need to create a new test for this
314 # if ($self->_storage->{links} > 1) {
315 # $self->_throw_error("Cannot optimize: reference count is greater than 1");
318 #XXX Do we have to lock the tempfile?
320 #XXX Should we use tempfile() here instead of a hard-coded name?
321 my $temp_filename = $self->_storage->{file} . '.tmp';
322 my $db_temp = DBM::Deep->new(
323 file => $temp_filename,
324 type => $self->_type,
326 # Bring over all the parameters that we need to bring over
327 ( map { $_ => $self->_engine->$_ } qw(
328 byte_size max_buckets data_sector_size num_txns
332 $self->lock_exclusive;
333 $self->_engine->clear_cache;
334 $self->_copy_node( $db_temp );
335 $db_temp->_storage->close;
339 # Attempt to copy user, group and permissions over to new file
341 $self->_storage->copy_stats( $temp_filename );
343 # q.v. perlport for more information on this variable
344 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
346 # Potential race condition when optmizing on Win32 with locking.
347 # The Windows filesystem requires that the filehandle be closed
348 # before it is overwritten with rename(). This could be redone
352 $self->_storage->close;
355 if (!rename $temp_filename, $self->_storage->{file}) {
356 unlink $temp_filename;
358 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
362 $self->_storage->close;
364 $self->_storage->open;
365 $self->lock_exclusive;
366 $self->_engine->setup_fh( $self );
374 # Make copy of object and return
376 my $self = shift->_get_self;
378 return DBM::Deep->new(
379 type => $self->_type,
380 base_offset => $self->_base_offset,
381 staleness => $self->_staleness,
382 storage => $self->_storage,
383 engine => $self->_engine,
387 #XXX Migrate this to the engine, where it really belongs and go through some
388 # API - stop poking in the innards of someone else..
390 my %is_legal_filter = map {
393 store_key store_value
394 fetch_key fetch_value
398 my $self = shift->_get_self;
402 if ( $is_legal_filter{$type} ) {
403 $self->_storage->{"filter_$type"} = $func;
410 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
411 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
412 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
413 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
417 my $self = shift->_get_self;
418 return $self->_engine->begin_work( $self, @_ );
422 my $self = shift->_get_self;
423 return $self->_engine->rollback( $self, @_ );
427 my $self = shift->_get_self;
428 return $self->_engine->commit( $self, @_ );
436 my $self = $_[0]->_get_self;
437 return $self->{engine};
441 my $self = $_[0]->_get_self;
442 return $self->{storage};
446 my $self = $_[0]->_get_self;
447 return $self->{type};
451 my $self = $_[0]->_get_self;
452 return $self->{base_offset};
456 my $self = $_[0]->_get_self;
457 return $self->{staleness};
467 my @caller = caller( ++$n );
468 next if $caller[0] =~ m/^DBM::Deep/;
470 die "DBM::Deep: $_[1] at $0 line $caller[2]\n";
476 # Store single hash key/value or array element in database.
478 my $self = shift->_get_self;
479 my ($key, $value) = @_;
480 warn "STORE($self, $key, $value)\n" if DEBUG;
482 unless ( $self->_storage->is_writable ) {
483 $self->_throw_error( 'Cannot write to a readonly filehandle' );
486 $self->lock_exclusive;
488 # User may be storing a complex value, in which case we do not want it run
489 # through the filtering system.
490 if ( !ref($value) && $self->_storage->{filter_store_value} ) {
491 $value = $self->_storage->{filter_store_value}->( $value );
494 $self->_engine->write_value( $self, $key, $value);
503 # Fetch single value or element given plain key or array index
505 my $self = shift->_get_self;
507 warn "FETCH($self,$key)\n" if DEBUG;
511 my $result = $self->_engine->read_value( $self, $key);
515 # Filters only apply to scalar values, so the ref check is making
516 # sure the fetched bucket is a scalar, not a child hash or array.
517 return ($result && !ref($result) && $self->_storage->{filter_fetch_value})
518 ? $self->_storage->{filter_fetch_value}->($result)
524 # Delete single key/value pair or element given plain key or array index
526 my $self = shift->_get_self;
528 warn "DELETE($self,$key)\n" if DEBUG;
530 unless ( $self->_storage->is_writable ) {
531 $self->_throw_error( 'Cannot write to a readonly filehandle' );
534 $self->lock_exclusive;
539 my $value = $self->_engine->delete_key( $self, $key);
541 if (defined $value && !ref($value) && $self->_storage->{filter_fetch_value}) {
542 $value = $self->_storage->{filter_fetch_value}->($value);
552 # Check if a single key or element exists given plain key or array index
554 my $self = shift->_get_self;
556 warn "EXISTS($self,$key)\n" if DEBUG;
560 my $result = $self->_engine->key_exists( $self, $key );
569 # Clear all keys from hash, or all elements from array.
571 my $self = shift->_get_self;
572 warn "CLEAR($self)\n" if DEBUG;
574 unless ( $self->_storage->is_writable ) {
575 $self->_throw_error( 'Cannot write to a readonly filehandle' );
578 $self->lock_exclusive;
580 #XXX Rewrite this dreck to do it in the engine as a tight loop vs.
581 # iterating over keys - such a WASTE - is this required for transactional
582 # clearning?! Surely that can be detected in the engine ...
583 if ( $self->_type eq TYPE_HASH ) {
584 my $key = $self->first_key;
586 # Retrieve the key before deleting because we depend on next_key
587 my $next_key = $self->next_key( $key );
588 $self->_engine->delete_key( $self, $key, $key );
593 my $size = $self->FETCHSIZE;
594 for my $key ( 0 .. $size - 1 ) {
595 $self->_engine->delete_key( $self, $key, $key );
597 $self->STORESIZE( 0 );
606 # Public method aliases
608 sub put { (shift)->STORE( @_ ) }
609 sub store { (shift)->STORE( @_ ) }
610 sub get { (shift)->FETCH( @_ ) }
611 sub fetch { (shift)->FETCH( @_ ) }
612 sub delete { (shift)->DELETE( @_ ) }
613 sub exists { (shift)->EXISTS( @_ ) }
614 sub clear { (shift)->CLEAR( @_ ) }
616 sub _dump_file {shift->_get_self->_engine->_dump_file;}