8 our $VERSION = q(1.0010);
11 use Fcntl qw( :flock );
14 use DBM::Deep::Engine;
18 '""' => sub { overload::StrVal( $_[0] ) },
21 use constant DEBUG => 0;
24 # Setup constants for users to pass to new()
26 sub TYPE_HASH () { DBM::Deep::Engine->SIG_HASH }
27 sub TYPE_ARRAY () { DBM::Deep::Engine->SIG_ARRAY }
29 # This is used in all the children of this class in their TIE<type> methods.
36 $proto->_throw_error( "Odd number of parameters to " . (caller(1))[2] );
41 unless ( eval { local $SIG{'__DIE__'}; %{$_[0]} || 1 } ) {
42 $proto->_throw_error( "Not a hashref in args to " . (caller(1))[2] );
47 $args = { file => shift };
55 # Class constructor method for Perl OO interface.
56 # Calls tie() and returns blessed reference to tied hash or array,
57 # providing a hybrid OO/tie interface.
60 my $args = $class->_get_args( @_ );
63 # Check if we want a tied hash or array.
66 if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) {
67 $class = 'DBM::Deep::Array';
68 require DBM::Deep::Array;
69 tie @$self, $class, %$args;
72 $class = 'DBM::Deep::Hash';
73 require DBM::Deep::Hash;
74 tie %$self, $class, %$args;
77 return bless $self, $class;
80 # This initializer is called from the various TIE* methods. new() calls tie(),
81 # which allows for a single point of entry.
86 $args->{storage} = DBM::Deep::File->new( $args )
87 unless exists $args->{storage};
89 # locking implicitly enables autoflush
90 if ($args->{locking}) { $args->{autoflush} = 1; }
92 # These are the defaults to be optionally overridden below
102 $args->{engine} = DBM::Deep::Engine->new( { %{$args}, obj => $self } )
103 unless exists $args->{engine};
105 # Grab the parameters we want to use
106 foreach my $param ( keys %$self ) {
107 next unless exists $args->{$param};
108 $self->{$param} = $args->{$param};
112 local $SIG{'__DIE__'};
115 $self->_engine->setup_fh( $self );
116 $self->_storage->set_inode;
120 eval { local $SIG{'__DIE__'}; $self->unlock; };
129 require DBM::Deep::Hash;
130 return DBM::Deep::Hash->TIEHASH( @_ );
135 require DBM::Deep::Array;
136 return DBM::Deep::Array->TIEARRAY( @_ );
140 my $self = shift->_get_self;
141 return $self->_storage->lock( $self, @_ );
145 my $self = shift->_get_self;
146 return $self->_storage->unlock( $self, @_ );
150 my $self = shift->_get_self;
151 my ($spot, $value) = @_;
157 # This assumes hash or array only. This is a bad assumption moving forward.
159 my $r = Scalar::Util::reftype( $value );
161 if ( $r eq 'ARRAY' ) {
162 $tied = tied(@$value);
165 $tied = tied(%$value);
168 if ( eval { local $SIG{__DIE__}; $tied->isa( 'DBM::Deep' ) } ) {
169 ${$spot} = $tied->_repr;
170 $tied->_copy_node( ${$spot} );
173 if ( $r eq 'ARRAY' ) {
174 ${$spot} = [ @{$value} ];
177 ${$spot} = { %{$value} };
181 my $c = Scalar::Util::blessed( $value );
182 if ( defined $c && !$c->isa( 'DBM::Deep') ) {
183 ${$spot} = bless ${$spot}, $c
191 # die "Must be implemented in a child class\n";
195 # die "Must be implemented in a child class\n";
200 # Recursively export into standard Perl hashes and arrays.
202 my $self = shift->_get_self;
204 my $temp = $self->_repr;
207 $self->_copy_node( $temp );
210 my $classname = $self->_engine->get_classname( $self );
211 if ( defined $classname ) {
212 bless $temp, $classname;
218 sub _check_legality {
222 my $r = Scalar::Util::reftype( $val );
224 return $r if !defined $r || '' eq $r;
225 return $r if 'HASH' eq $r;
226 return $r if 'ARRAY' eq $r;
228 DBM::Deep->_throw_error(
229 "Storage of references of type '$r' is not supported."
234 # Perl calls import() on use -- ignore
235 return if !ref $_[0];
237 my $self = shift->_get_self;
240 my $type = $self->_check_legality( $struct );
242 DBM::Deep->_throw_error( "Cannot import a scalar" );
245 if ( substr( $type, 0, 1 ) ne $self->_type ) {
246 DBM::Deep->_throw_error(
247 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
248 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
257 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
260 my $r = $self->_check_legality( $val );
261 if ( 'HASH' eq $r ) {
262 while ( my ($k, $v) = each %$val ) {
263 my $r = $self->_check_legality( $v );
265 my $temp = 'HASH' eq $r ? {} : [];
266 if ( my $c = Scalar::Util::blessed( $v ) ) {
269 $obj->put( $k, $temp );
270 $recurse->( $temp, $v );
277 elsif ( 'ARRAY' eq $r ) {
278 foreach my $k ( 0 .. $#$val ) {
280 my $r = $self->_check_legality( $v );
282 my $temp = 'HASH' eq $r ? {} : [];
283 if ( my $c = Scalar::Util::blessed( $v ) ) {
286 $obj->put( $k, $temp );
287 $recurse->( $temp, $v );
295 $recurse->( $self, $struct );
300 #XXX Need to keep track of who has a fh to this file in order to
301 #XXX close them all prior to optimize on Win32/cygwin
304 # Rebuild entire database into new file, then move
305 # it back on top of original.
307 my $self = shift->_get_self;
309 #XXX Need to create a new test for this
310 # if ($self->_storage->{links} > 1) {
311 # $self->_throw_error("Cannot optimize: reference count is greater than 1");
314 #XXX Do we have to lock the tempfile?
316 #XXX Should we use tempfile() here instead of a hard-coded name?
317 my $temp_filename = $self->_storage->{file} . '.tmp';
318 my $db_temp = DBM::Deep->new(
319 file => $temp_filename,
320 type => $self->_type,
322 # Bring over all the parameters that we need to bring over
323 ( map { $_ => $self->_engine->$_ } qw(
324 byte_size max_buckets data_sector_size num_txns
329 $self->_engine->clear_cache;
330 $self->_copy_node( $db_temp );
334 # Attempt to copy user, group and permissions over to new file
336 $self->_storage->copy_stats( $temp_filename );
338 # q.v. perlport for more information on this variable
339 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
341 # Potential race condition when optmizing on Win32 with locking.
342 # The Windows filesystem requires that the filehandle be closed
343 # before it is overwritten with rename(). This could be redone
347 $self->_storage->close;
350 if (!rename $temp_filename, $self->_storage->{file}) {
351 unlink $temp_filename;
353 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
357 $self->_storage->close;
359 $self->_storage->open;
361 $self->_engine->setup_fh( $self );
369 # Make copy of object and return
371 my $self = shift->_get_self;
373 return DBM::Deep->new(
374 type => $self->_type,
375 base_offset => $self->_base_offset,
376 staleness => $self->_staleness,
377 storage => $self->_storage,
378 engine => $self->_engine,
382 #XXX Migrate this to the engine, where it really belongs and go through some
383 # API - stop poking in the innards of someone else..
385 my %is_legal_filter = map {
388 store_key store_value
389 fetch_key fetch_value
393 my $self = shift->_get_self;
397 if ( $is_legal_filter{$type} ) {
398 $self->_storage->{"filter_$type"} = $func;
405 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
406 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
407 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
408 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
412 my $self = shift->_get_self;
413 return $self->_engine->begin_work( $self, @_ );
417 my $self = shift->_get_self;
418 return $self->_engine->rollback( $self, @_ );
422 my $self = shift->_get_self;
423 return $self->_engine->commit( $self, @_ );
431 my $self = $_[0]->_get_self;
432 return $self->{engine};
436 my $self = $_[0]->_get_self;
437 return $self->{storage};
441 my $self = $_[0]->_get_self;
442 return $self->{type};
446 my $self = $_[0]->_get_self;
447 return $self->{base_offset};
451 my $self = $_[0]->_get_self;
452 return $self->{staleness};
462 my @caller = caller( ++$n );
463 next if $caller[0] =~ m/^DBM::Deep/;
465 die "DBM::Deep: $_[1] at $0 line $caller[2]\n";
471 # Store single hash key/value or array element in database.
473 my $self = shift->_get_self;
474 my ($key, $value) = @_;
475 warn "STORE($self, $key, $value)\n" if DEBUG;
477 unless ( $self->_storage->is_writable ) {
478 $self->_throw_error( 'Cannot write to a readonly filehandle' );
482 # Request exclusive lock for writing
484 $self->lock( LOCK_EX );
486 # User may be storing a complex value, in which case we do not want it run
487 # through the filtering system.
488 if ( !ref($value) && $self->_storage->{filter_store_value} ) {
489 $value = $self->_storage->{filter_store_value}->( $value );
492 $self->_engine->write_value( $self, $key, $value);
501 # Fetch single value or element given plain key or array index
503 my $self = shift->_get_self;
505 warn "FETCH($self,$key)\n" if DEBUG;
508 # Request shared lock for reading
510 $self->lock( LOCK_SH );
512 my $result = $self->_engine->read_value( $self, $key);
516 # Filters only apply to scalar values, so the ref check is making
517 # sure the fetched bucket is a scalar, not a child hash or array.
518 return ($result && !ref($result) && $self->_storage->{filter_fetch_value})
519 ? $self->_storage->{filter_fetch_value}->($result)
525 # Delete single key/value pair or element given plain key or array index
527 my $self = shift->_get_self;
529 warn "DELETE($self,$key)\n" if DEBUG;
531 unless ( $self->_storage->is_writable ) {
532 $self->_throw_error( 'Cannot write to a readonly filehandle' );
536 # Request exclusive lock for writing
538 $self->lock( LOCK_EX );
543 my $value = $self->_engine->delete_key( $self, $key);
545 if (defined $value && !ref($value) && $self->_storage->{filter_fetch_value}) {
546 $value = $self->_storage->{filter_fetch_value}->($value);
556 # Check if a single key or element exists given plain key or array index
558 my $self = shift->_get_self;
560 warn "EXISTS($self,$key)\n" if DEBUG;
563 # Request shared lock for reading
565 $self->lock( LOCK_SH );
567 my $result = $self->_engine->key_exists( $self, $key );
576 # Clear all keys from hash, or all elements from array.
578 my $self = shift->_get_self;
579 warn "CLEAR($self)\n" if DEBUG;
581 unless ( $self->_storage->is_writable ) {
582 $self->_throw_error( 'Cannot write to a readonly filehandle' );
586 # Request exclusive lock for writing
588 $self->lock( LOCK_EX );
590 #XXX Rewrite this dreck to do it in the engine as a tight loop vs.
591 # iterating over keys - such a WASTE - is this required for transactional
592 # clearning?! Surely that can be detected in the engine ...
593 if ( $self->_type eq TYPE_HASH ) {
594 my $key = $self->first_key;
596 # Retrieve the key before deleting because we depend on next_key
597 my $next_key = $self->next_key( $key );
598 $self->_engine->delete_key( $self, $key, $key );
603 my $size = $self->FETCHSIZE;
604 for my $key ( 0 .. $size - 1 ) {
605 $self->_engine->delete_key( $self, $key, $key );
607 $self->STORESIZE( 0 );
616 # Public method aliases
618 sub put { (shift)->STORE( @_ ) }
619 sub store { (shift)->STORE( @_ ) }
620 sub get { (shift)->FETCH( @_ ) }
621 sub fetch { (shift)->FETCH( @_ ) }
622 sub delete { (shift)->DELETE( @_ ) }
623 sub exists { (shift)->EXISTS( @_ ) }
624 sub clear { (shift)->CLEAR( @_ ) }
626 sub _dump_file {shift->_get_self->_engine->_dump_file;}