6 use warnings FATAL => 'all';
7 no warnings 'recursion';
9 our $VERSION = q(1.0015);
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 # locking implicitly enables autoflush
87 if ($args->{locking}) { $args->{autoflush} = 1; }
89 # These are the defaults to be optionally overridden below
97 $args->{engine} = DBM::Deep::Engine->new( { %{$args}, obj => $self } )
98 unless exists $args->{engine};
100 # Grab the parameters we want to use
101 foreach my $param ( keys %$self ) {
102 next unless exists $args->{$param};
103 $self->{$param} = $args->{$param};
107 local $SIG{'__DIE__'};
109 $self->lock_exclusive;
110 $self->_engine->setup_fh( $self );
114 eval { local $SIG{'__DIE__'}; $self->unlock; };
123 require DBM::Deep::Hash;
124 return DBM::Deep::Hash->TIEHASH( @_ );
129 require DBM::Deep::Array;
130 return DBM::Deep::Array->TIEARRAY( @_ );
134 my $self = shift->_get_self;
135 return $self->_engine->lock_exclusive( $self, @_ );
137 *lock = \&lock_exclusive;
139 my $self = shift->_get_self;
140 return $self->_engine->lock_shared( $self, @_ );
144 my $self = shift->_get_self;
145 return $self->_engine->unlock( $self, @_ );
149 my $self = shift->_get_self;
150 my ($spot, $value) = @_;
156 # This assumes hash or array only. This is a bad assumption moving forward.
158 my $r = Scalar::Util::reftype( $value );
160 if ( $r eq 'ARRAY' ) {
161 $tied = tied(@$value);
164 $tied = tied(%$value);
167 if ( eval { local $SIG{__DIE__}; $tied->isa( 'DBM::Deep' ) } ) {
168 ${$spot} = $tied->_repr;
169 $tied->_copy_node( ${$spot} );
172 if ( $r eq 'ARRAY' ) {
173 ${$spot} = [ @{$value} ];
176 ${$spot} = { %{$value} };
180 my $c = Scalar::Util::blessed( $value );
181 if ( defined $c && !$c->isa( 'DBM::Deep') ) {
182 ${$spot} = bless ${$spot}, $c
190 # die "Must be implemented in a child class\n";
194 # die "Must be implemented in a child class\n";
199 # Recursively export into standard Perl hashes and arrays.
201 my $self = shift->_get_self;
203 my $temp = $self->_repr;
205 $self->lock_exclusive;
206 $self->_copy_node( $temp );
209 my $classname = $self->_engine->get_classname( $self );
210 if ( defined $classname ) {
211 bless $temp, $classname;
217 sub _check_legality {
221 my $r = Scalar::Util::reftype( $val );
223 return $r if !defined $r || '' eq $r;
224 return $r if 'HASH' eq $r;
225 return $r if 'ARRAY' eq $r;
227 DBM::Deep->_throw_error(
228 "Storage of references of type '$r' is not supported."
233 # Perl calls import() on use -- ignore
234 return if !ref $_[0];
236 my $self = shift->_get_self;
239 my $type = $self->_check_legality( $struct );
241 DBM::Deep->_throw_error( "Cannot import a scalar" );
244 if ( substr( $type, 0, 1 ) ne $self->_type ) {
245 DBM::Deep->_throw_error(
246 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
247 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
256 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
259 my $r = $self->_check_legality( $val );
260 if ( 'HASH' eq $r ) {
261 while ( my ($k, $v) = each %$val ) {
262 my $r = $self->_check_legality( $v );
264 my $temp = 'HASH' eq $r ? {} : [];
265 if ( my $c = Scalar::Util::blessed( $v ) ) {
268 $obj->put( $k, $temp );
269 $recurse->( $temp, $v );
276 elsif ( 'ARRAY' eq $r ) {
277 foreach my $k ( 0 .. $#$val ) {
279 my $r = $self->_check_legality( $v );
281 my $temp = 'HASH' eq $r ? {} : [];
282 if ( my $c = Scalar::Util::blessed( $v ) ) {
285 $obj->put( $k, $temp );
286 $recurse->( $temp, $v );
294 $recurse->( $self, $struct );
299 #XXX Need to keep track of who has a fh to this file in order to
300 #XXX close them all prior to optimize on Win32/cygwin
303 # Rebuild entire database into new file, then move
304 # it back on top of original.
306 my $self = shift->_get_self;
308 #XXX Need to create a new test for this
309 # if ($self->_engine->storage->{links} > 1) {
310 # $self->_throw_error("Cannot optimize: reference count is greater than 1");
313 #XXX Do we have to lock the tempfile?
315 #XXX Should we use tempfile() here instead of a hard-coded name?
316 my $temp_filename = $self->_engine->storage->{file} . '.tmp';
317 my $db_temp = DBM::Deep->new(
318 file => $temp_filename,
319 type => $self->_type,
321 # Bring over all the parameters that we need to bring over
322 ( map { $_ => $self->_engine->$_ } qw(
323 byte_size max_buckets data_sector_size num_txns
327 $self->lock_exclusive;
328 $self->_engine->clear_cache;
329 $self->_copy_node( $db_temp );
330 $db_temp->_engine->storage->close;
334 # Attempt to copy user, group and permissions over to new file
336 $self->_engine->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->_engine->storage->close;
350 if (!rename $temp_filename, $self->_engine->storage->{file}) {
351 unlink $temp_filename;
353 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
357 $self->_engine->storage->close;
359 $self->_engine->storage->open;
360 $self->lock_exclusive;
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 engine => $self->_engine,
381 #XXX Migrate this to the engine, where it really belongs and go through some
382 # API - stop poking in the innards of someone else..
384 my %is_legal_filter = map {
387 store_key store_value
388 fetch_key fetch_value
392 my $self = shift->_get_self;
396 if ( $is_legal_filter{$type} ) {
397 $self->_engine->storage->{"filter_$type"} = $func;
404 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
405 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
406 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
407 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
411 my $self = shift->_get_self;
412 $self->lock_exclusive;
413 my $rv = eval { $self->_engine->begin_work( $self, @_ ) };
421 my $self = shift->_get_self;
422 $self->lock_exclusive;
423 my $rv = eval { $self->_engine->rollback( $self, @_ ) };
431 my $self = shift->_get_self;
432 $self->lock_exclusive;
433 my $rv = eval { $self->_engine->commit( $self, @_ ) };
445 my $self = $_[0]->_get_self;
446 return $self->{engine};
450 my $self = $_[0]->_get_self;
451 return $self->{type};
455 my $self = $_[0]->_get_self;
456 return $self->{base_offset};
460 my $self = $_[0]->_get_self;
461 return $self->{staleness};
471 my @caller = caller( ++$n );
472 next if $caller[0] =~ m/^DBM::Deep/;
474 die "DBM::Deep: $_[1] at $0 line $caller[2]\n";
480 # Store single hash key/value or array element in database.
482 my $self = shift->_get_self;
483 my ($key, $value) = @_;
484 warn "STORE($self, '$key', '@{[defined$value?$value:'undef']}')\n" if DEBUG;
486 unless ( $self->_engine->storage->is_writable ) {
487 $self->_throw_error( 'Cannot write to a readonly filehandle' );
490 $self->lock_exclusive;
492 # User may be storing a complex value, in which case we do not want it run
493 # through the filtering system.
494 if ( !ref($value) && $self->_engine->storage->{filter_store_value} ) {
495 $value = $self->_engine->storage->{filter_store_value}->( $value );
498 my $x = $self->_engine->write_value( $self, $key, $value);
507 # Fetch single value or element given plain key or array index
509 my $self = shift->_get_self;
511 warn "FETCH($self, '$key')\n" if DEBUG;
515 my $result = $self->_engine->read_value( $self, $key);
519 # Filters only apply to scalar values, so the ref check is making
520 # sure the fetched bucket is a scalar, not a child hash or array.
521 return ($result && !ref($result) && $self->_engine->storage->{filter_fetch_value})
522 ? $self->_engine->storage->{filter_fetch_value}->($result)
528 # Delete single key/value pair or element given plain key or array index
530 my $self = shift->_get_self;
532 warn "DELETE($self, '$key')\n" if DEBUG;
534 unless ( $self->_engine->storage->is_writable ) {
535 $self->_throw_error( 'Cannot write to a readonly filehandle' );
538 $self->lock_exclusive;
543 my $value = $self->_engine->delete_key( $self, $key);
545 if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) {
546 $value = $self->_engine->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;
564 my $result = $self->_engine->key_exists( $self, $key );
573 # Clear all keys from hash, or all elements from array.
575 my $self = shift->_get_self;
576 warn "CLEAR($self)\n" if DEBUG;
578 my $engine = $self->_engine;
579 unless ( $engine->storage->is_writable ) {
580 $self->_throw_error( 'Cannot write to a readonly filehandle' );
583 $self->lock_exclusive;
585 # Dispatch to the specific clearing functionality.
586 $engine->clear($self);
594 # Public method aliases
596 sub put { (shift)->STORE( @_ ) }
597 sub store { (shift)->STORE( @_ ) }
598 sub get { (shift)->FETCH( @_ ) }
599 sub fetch { (shift)->FETCH( @_ ) }
600 sub delete { (shift)->DELETE( @_ ) }
601 sub exists { (shift)->EXISTS( @_ ) }
602 sub clear { (shift)->CLEAR( @_ ) }
604 sub _dump_file {shift->_get_self->_engine->_dump_file;}