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 $self->_get_self->_engine->flush;
88 # This initializer is called from the various TIE* methods. new() calls tie(),
89 # which allows for a single point of entry.
94 # locking implicitly enables autoflush
95 if ($args->{locking}) { $args->{autoflush} = 1; }
97 # These are the defaults to be optionally overridden below
100 base_offset => undef,
105 $args->{engine} = DBM::Deep::Engine->new( { %{$args}, obj => $self } )
106 unless exists $args->{engine};
108 # Grab the parameters we want to use
109 foreach my $param ( keys %$self ) {
110 next unless exists $args->{$param};
111 $self->{$param} = $args->{$param};
115 local $SIG{'__DIE__'};
117 $self->lock_exclusive;
118 $self->_engine->setup_fh( $self );
122 eval { local $SIG{'__DIE__'}; $self->unlock; };
131 require DBM::Deep::Hash;
132 return DBM::Deep::Hash->TIEHASH( @_ );
137 require DBM::Deep::Array;
138 return DBM::Deep::Array->TIEARRAY( @_ );
142 my $self = shift->_get_self;
143 return $self->_engine->lock_exclusive( $self );
145 *lock = \&lock_exclusive;
147 my $self = shift->_get_self;
148 return $self->_engine->lock_shared( $self );
152 my $self = shift->_get_self;
153 return $self->_engine->unlock( $self );
157 my $self = shift->_get_self;
158 my ($spot, $value) = @_;
164 # This assumes hash or array only. This is a bad assumption moving forward.
166 my $r = Scalar::Util::reftype( $value );
168 if ( $r eq 'ARRAY' ) {
169 $tied = tied(@$value);
172 $tied = tied(%$value);
175 if ( eval { local $SIG{__DIE__}; $tied->isa( 'DBM::Deep' ) } ) {
176 ${$spot} = $tied->_repr;
177 $tied->_copy_node( ${$spot} );
180 if ( $r eq 'ARRAY' ) {
181 ${$spot} = [ @{$value} ];
184 ${$spot} = { %{$value} };
188 my $c = Scalar::Util::blessed( $value );
189 if ( defined $c && !$c->isa( 'DBM::Deep') ) {
190 ${$spot} = bless ${$spot}, $c
198 # die "Must be implemented in a child class\n";
202 # die "Must be implemented in a child class\n";
207 # Recursively export into standard Perl hashes and arrays.
209 my $self = shift->_get_self;
211 my $temp = $self->_repr;
213 $self->lock_exclusive;
214 $self->_copy_node( $temp );
217 my $classname = $self->_engine->get_classname( $self );
218 if ( defined $classname ) {
219 bless $temp, $classname;
225 sub _check_legality {
229 my $r = Scalar::Util::reftype( $val );
231 return $r if !defined $r || '' eq $r;
232 return $r if 'HASH' eq $r;
233 return $r if 'ARRAY' eq $r;
235 DBM::Deep->_throw_error(
236 "Storage of references of type '$r' is not supported."
241 # Perl calls import() on use -- ignore
242 return if !ref $_[0];
244 my $self = shift->_get_self;
247 my $type = $self->_check_legality( $struct );
249 DBM::Deep->_throw_error( "Cannot import a scalar" );
252 if ( substr( $type, 0, 1 ) ne $self->_type ) {
253 DBM::Deep->_throw_error(
254 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
255 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
264 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
267 my $r = $self->_check_legality( $val );
268 if ( 'HASH' eq $r ) {
269 while ( my ($k, $v) = each %$val ) {
270 my $r = $self->_check_legality( $v );
272 my $temp = 'HASH' eq $r ? {} : [];
273 if ( my $c = Scalar::Util::blessed( $v ) ) {
276 $obj->put( $k, $temp );
277 $recurse->( $temp, $v );
284 elsif ( 'ARRAY' eq $r ) {
285 foreach my $k ( 0 .. $#$val ) {
287 my $r = $self->_check_legality( $v );
289 my $temp = 'HASH' eq $r ? {} : [];
290 if ( my $c = Scalar::Util::blessed( $v ) ) {
293 $obj->put( $k, $temp );
294 $recurse->( $temp, $v );
302 $recurse->( $self, $struct );
307 #XXX Need to keep track of who has a fh to this file in order to
308 #XXX close them all prior to optimize on Win32/cygwin
311 # Rebuild entire database into new file, then move
312 # it back on top of original.
314 my $self = shift->_get_self;
316 #XXX Need to create a new test for this
317 # if ($self->_engine->storage->{links} > 1) {
318 # $self->_throw_error("Cannot optimize: reference count is greater than 1");
321 #XXX Do we have to lock the tempfile?
323 #XXX Should we use tempfile() here instead of a hard-coded name?
324 my $temp_filename = $self->_engine->storage->{file} . '.tmp';
325 my $db_temp = DBM::Deep->new(
326 file => $temp_filename,
327 type => $self->_type,
329 # Bring over all the parameters that we need to bring over
330 ( map { $_ => $self->_engine->$_ } qw(
331 byte_size max_buckets data_sector_size num_txns
335 $self->lock_exclusive;
336 $self->_engine->clear_cache;
337 $self->_copy_node( $db_temp );
338 $db_temp->_engine->storage->close;
342 # Attempt to copy user, group and permissions over to new file
344 $self->_engine->storage->copy_stats( $temp_filename );
346 # q.v. perlport for more information on this variable
347 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
349 # Potential race condition when optmizing on Win32 with locking.
350 # The Windows filesystem requires that the filehandle be closed
351 # before it is overwritten with rename(). This could be redone
355 $self->_engine->storage->close;
358 if (!rename $temp_filename, $self->_engine->storage->{file}) {
359 unlink $temp_filename;
361 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
365 $self->_engine->storage->close;
367 $self->_engine->storage->open;
368 $self->lock_exclusive;
369 $self->_engine->setup_fh( $self );
377 # Make copy of object and return
379 my $self = shift->_get_self;
381 return DBM::Deep->new(
382 type => $self->_type,
383 base_offset => $self->_base_offset,
384 staleness => $self->_staleness,
385 engine => $self->_engine,
389 #XXX Migrate this to the engine, where it really belongs and go through some
390 # API - stop poking in the innards of someone else..
392 my %is_legal_filter = map {
395 store_key store_value
396 fetch_key fetch_value
400 my $self = shift->_get_self;
404 if ( $is_legal_filter{$type} ) {
405 $self->_engine->storage->{"filter_$type"} = $func;
412 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
413 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
414 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
415 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
419 my $self = shift->_get_self;
420 return $self->_engine->begin_work( $self, @_ );
424 my $self = shift->_get_self;
425 return $self->_engine->rollback( $self, @_ );
429 my $self = shift->_get_self;
430 return $self->_engine->commit( $self, @_ );
438 my $self = $_[0]->_get_self;
439 return $self->{engine};
443 my $self = $_[0]->_get_self;
444 return $self->{type};
448 my $self = $_[0]->_get_self;
449 return $self->{base_offset};
453 my $self = $_[0]->_get_self;
454 return $self->{staleness};
464 my @caller = caller( ++$n );
465 next if $caller[0] =~ m/^DBM::Deep/;
467 die "DBM::Deep: $_[1] at $0 line $caller[2]\n";
473 # Store single hash key/value or array element in database.
475 my $self = shift->_get_self;
476 my ($key, $value) = @_;
477 warn "STORE($self, $key, @{[defined$value?$value:'undef']})\n" if DEBUG;
479 unless ( $self->_engine->storage->is_writable ) {
480 $self->_throw_error( 'Cannot write to a readonly filehandle' );
483 $self->lock_exclusive;
485 # User may be storing a complex value, in which case we do not want it run
486 # through the filtering system.
487 if ( !ref($value) && $self->_engine->storage->{filter_store_value} ) {
488 $value = $self->_engine->storage->{filter_store_value}->( $value );
491 $self->_engine->write_value( $self, $key, $value);
500 # Fetch single value or element given plain key or array index
502 my $self = shift->_get_self;
504 warn "FETCH($self,$key)\n" if DEBUG;
508 my $result = $self->_engine->read_value( $self, $key);
512 # Filters only apply to scalar values, so the ref check is making
513 # sure the fetched bucket is a scalar, not a child hash or array.
514 return ($result && !ref($result) && $self->_engine->storage->{filter_fetch_value})
515 ? $self->_engine->storage->{filter_fetch_value}->($result)
521 # Delete single key/value pair or element given plain key or array index
523 my $self = shift->_get_self;
525 warn "DELETE($self,$key)\n" if DEBUG;
527 unless ( $self->_engine->storage->is_writable ) {
528 $self->_throw_error( 'Cannot write to a readonly filehandle' );
531 $self->lock_exclusive;
536 my $value = $self->_engine->delete_key( $self, $key);
538 if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) {
539 $value = $self->_engine->storage->{filter_fetch_value}->($value);
549 # Check if a single key or element exists given plain key or array index
551 my $self = shift->_get_self;
553 warn "EXISTS($self,$key)\n" if DEBUG;
557 my $result = $self->_engine->key_exists( $self, $key );
566 # Clear all keys from hash, or all elements from array.
568 my $self = shift->_get_self;
569 warn "CLEAR($self)\n" if DEBUG;
571 unless ( $self->_engine->storage->is_writable ) {
572 $self->_throw_error( 'Cannot write to a readonly filehandle' );
575 $self->lock_exclusive;
577 #XXX Rewrite this dreck to do it in the engine as a tight loop vs.
578 # iterating over keys - such a WASTE - is this required for transactional
579 # clearning?! Surely that can be detected in the engine ...
580 if ( $self->_type eq TYPE_HASH ) {
581 my $key = $self->first_key;
583 # Retrieve the key before deleting because we depend on next_key
584 my $next_key = $self->next_key( $key );
585 $self->_engine->delete_key( $self, $key, $key );
590 my $size = $self->FETCHSIZE;
591 for my $key ( 0 .. $size - 1 ) {
592 $self->_engine->delete_key( $self, $key, $key );
594 $self->STORESIZE( 0 );
603 # Public method aliases
605 sub put { (shift)->STORE( @_ ) }
606 sub store { (shift)->STORE( @_ ) }
607 sub get { (shift)->FETCH( @_ ) }
608 sub fetch { (shift)->FETCH( @_ ) }
609 sub delete { (shift)->DELETE( @_ ) }
610 sub exists { (shift)->EXISTS( @_ ) }
611 sub clear { (shift)->CLEAR( @_ ) }
613 sub _dump_file {shift->_get_self->_engine->_dump_file;}