6 use warnings FATAL => 'all';
7 no warnings 'recursion';
9 our $VERSION = q(1.0019_003);
14 '""' => sub { overload::StrVal( $_[0] ) },
17 use constant DEBUG => 0;
19 use DBM::Deep::Engine;
21 sub TYPE_HASH () { DBM::Deep::Engine->SIG_HASH }
22 sub TYPE_ARRAY () { DBM::Deep::Engine->SIG_ARRAY }
24 # This is used in all the children of this class in their TIE<type> methods.
31 $proto->_throw_error( "Odd number of parameters to " . (caller(1))[2] );
36 unless ( eval { local $SIG{'__DIE__'}; %{$_[0]} || 1 } ) {
37 $proto->_throw_error( "Not a hashref in args to " . (caller(1))[2] );
42 $args = { file => shift };
48 # Class constructor method for Perl OO interface.
49 # Calls tie() and returns blessed reference to tied hash or array,
50 # providing a hybrid OO/tie interface.
53 my $args = $class->_get_args( @_ );
56 if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) {
57 $class = 'DBM::Deep::Array';
58 require DBM::Deep::Array;
59 tie @$self, $class, %$args;
62 $class = 'DBM::Deep::Hash';
63 require DBM::Deep::Hash;
64 tie %$self, $class, %$args;
67 return bless $self, $class;
70 # This initializer is called from the various TIE* methods. new() calls tie(),
71 # which allows for a single point of entry.
76 # locking implicitly enables autoflush
77 if ($args->{locking}) { $args->{autoflush} = 1; }
79 # These are the defaults to be optionally overridden below
87 unless ( exists $args->{engine} ) {
88 my $class = exists $args->{dbi}
89 ? 'DBM::Deep::Engine::DBI'
90 : 'DBM::Deep::Engine::File';
92 eval "use $class"; die $@ if $@;
93 $args->{engine} = $class->new({
99 # Grab the parameters we want to use
100 foreach my $param ( keys %$self ) {
101 next unless exists $args->{$param};
102 $self->{$param} = $args->{$param};
106 local $SIG{'__DIE__'};
108 $self->lock_exclusive;
109 $self->_engine->setup( $self );
113 eval { local $SIG{'__DIE__'}; $self->unlock; };
122 require DBM::Deep::Hash;
123 return DBM::Deep::Hash->TIEHASH( @_ );
128 require DBM::Deep::Array;
129 return DBM::Deep::Array->TIEARRAY( @_ );
133 my $self = shift->_get_self;
134 return $self->_engine->lock_exclusive( $self, @_ );
136 *lock = \&lock_exclusive;
138 my $self = shift->_get_self;
139 use Carp qw( cluck ); use Data::Dumper;
140 cluck Dumper($self) unless $self->_engine;
141 return $self->_engine->lock_shared( $self, @_ );
145 my $self = shift->_get_self;
146 return $self->_engine->unlock( $self, @_ );
150 my $self = shift->_get_self;
151 my ($spot, $value) = @_;
157 my $r = Scalar::Util::reftype( $value );
159 if ( $r eq 'ARRAY' ) {
160 $tied = tied(@$value);
162 elsif ( $r eq 'HASH' ) {
163 $tied = tied(%$value);
166 __PACKAGE__->_throw_error( "Unknown type for '$value'" );
169 if ( eval { local $SIG{'__DIE__'}; $tied->isa( __PACKAGE__ ) } ) {
170 ${$spot} = $tied->_repr;
171 $tied->_copy_node( ${$spot} );
174 if ( $r eq 'ARRAY' ) {
175 ${$spot} = [ @{$value} ];
178 ${$spot} = { %{$value} };
182 my $c = Scalar::Util::blessed( $value );
183 if ( defined $c && !$c->isa( __PACKAGE__ ) ) {
184 ${$spot} = bless ${$spot}, $c
192 # die "Must be implemented in a child class\n";
196 # die "Must be implemented in a child class\n";
200 my $self = shift->_get_self;
202 my $temp = $self->_repr;
204 $self->lock_exclusive;
205 $self->_copy_node( $temp );
208 my $classname = $self->_engine->get_classname( $self );
209 if ( defined $classname ) {
210 bless $temp, $classname;
216 sub _check_legality {
220 my $r = Scalar::Util::reftype( $val );
222 return $r if !defined $r || '' eq $r;
223 return $r if 'HASH' eq $r;
224 return $r if 'ARRAY' eq $r;
226 __PACKAGE__->_throw_error(
227 "Storage of references of type '$r' is not supported."
232 return if !ref $_[0]; # Perl calls import() on use -- ignore
234 my $self = shift->_get_self;
237 my $type = $self->_check_legality( $struct );
239 __PACKAGE__->_throw_error( "Cannot import a scalar" );
242 if ( substr( $type, 0, 1 ) ne $self->_type ) {
243 __PACKAGE__->_throw_error(
244 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
245 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
254 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
257 my $r = $self->_check_legality( $val );
258 if ( 'HASH' eq $r ) {
259 while ( my ($k, $v) = each %$val ) {
260 my $r = $self->_check_legality( $v );
262 my $temp = 'HASH' eq $r ? {} : [];
263 if ( my $c = Scalar::Util::blessed( $v ) ) {
266 $obj->put( $k, $temp );
267 $recurse->( $temp, $v );
274 elsif ( 'ARRAY' eq $r ) {
275 foreach my $k ( 0 .. $#$val ) {
277 my $r = $self->_check_legality( $v );
279 my $temp = 'HASH' eq $r ? {} : [];
280 if ( my $c = Scalar::Util::blessed( $v ) ) {
283 $obj->put( $k, $temp );
284 $recurse->( $temp, $v );
292 $recurse->( $self, $struct );
297 #XXX Need to keep track of who has a fh to this file in order to
298 #XXX close them all prior to optimize on Win32/cygwin
299 # Rebuild entire database into new file, then move
300 # it back on top of original.
302 my $self = shift->_get_self;
304 # Optimizing is only something we need to do when we're working with our
305 # own file format. Otherwise, let the other guy do the optimizations.
306 return unless $self->_engine->isa( 'DBM::Deep::Engine::File' );
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 = __PACKAGE__->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 );
331 $db_temp->_engine->storage->close;
335 # Attempt to copy user, group and permissions over to new file
337 $self->_engine->storage->copy_stats( $temp_filename );
339 # q.v. perlport for more information on this variable
340 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
342 # Potential race condition when optmizing on Win32 with locking.
343 # The Windows filesystem requires that the filehandle be closed
344 # before it is overwritten with rename(). This could be redone
348 $self->_engine->storage->close;
351 if (!rename $temp_filename, $self->_engine->storage->{file}) {
352 unlink $temp_filename;
354 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
358 $self->_engine->storage->close;
360 $self->_engine->storage->open;
361 $self->lock_exclusive;
362 $self->_engine->setup( $self );
369 my $self = shift->_get_self;
371 return __PACKAGE__->new(
372 type => $self->_type,
373 base_offset => $self->_base_offset,
374 staleness => $self->_staleness,
375 engine => $self->_engine,
380 my $self = shift->_get_self;
381 return $self->_engine->supports( @_ );
384 #XXX Migrate this to the engine, where it really belongs and go through some
385 # API - stop poking in the innards of someone else..
387 my %is_legal_filter = map {
390 store_key store_value
391 fetch_key fetch_value
395 my $self = shift->_get_self;
399 if ( $is_legal_filter{$type} ) {
400 $self->_engine->storage->{"filter_$type"} = $func;
407 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
408 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
409 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
410 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
414 my $self = shift->_get_self;
415 $self->lock_exclusive;
417 local $SIG{'__DIE__'};
418 $self->_engine->begin_work( $self, @_ );
427 my $self = shift->_get_self;
429 $self->lock_exclusive;
431 local $SIG{'__DIE__'};
432 $self->_engine->rollback( $self, @_ );
441 my $self = shift->_get_self;
442 $self->lock_exclusive;
444 local $SIG{'__DIE__'};
445 $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};
478 my @caller = caller( ++$n );
479 next if $caller[0] =~ m/^DBM::Deep/;
481 die "DBM::Deep: $_[1] at $0 line $caller[2]\n";
485 # Store single hash key/value or array element in database.
487 my $self = shift->_get_self;
488 my ($key, $value) = @_;
489 warn "STORE($self, '$key', '@{[defined$value?$value:'undef']}')\n" if DEBUG;
491 unless ( $self->_engine->storage->is_writable ) {
492 $self->_throw_error( 'Cannot write to a readonly filehandle' );
495 $self->lock_exclusive;
497 # User may be storing a complex value, in which case we do not want it run
498 # through the filtering system.
499 if ( !ref($value) && $self->_engine->storage->{filter_store_value} ) {
500 $value = $self->_engine->storage->{filter_store_value}->( $value );
504 local $SIG{'__DIE__'};
505 $self->_engine->write_value( $self, $key, $value );
506 }; if ( my $e = $@ ) {
516 # Fetch single value or element given plain key or array index
518 my $self = shift->_get_self;
520 warn "FETCH($self, '$key')\n" if DEBUG;
524 my $result = $self->_engine->read_value( $self, $key );
528 # Filters only apply to scalar values, so the ref check is making
529 # sure the fetched bucket is a scalar, not a child hash or array.
530 return ($result && !ref($result) && $self->_engine->storage->{filter_fetch_value})
531 ? $self->_engine->storage->{filter_fetch_value}->($result)
535 # Delete single key/value pair or element given plain key or array index
537 my $self = shift->_get_self;
539 warn "DELETE($self, '$key')\n" if DEBUG;
541 unless ( $self->_engine->storage->is_writable ) {
542 $self->_throw_error( 'Cannot write to a readonly filehandle' );
545 $self->lock_exclusive;
550 my $value = $self->_engine->delete_key( $self, $key);
552 if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) {
553 $value = $self->_engine->storage->{filter_fetch_value}->($value);
561 # Check if a single key or element exists given plain key or array index
563 my $self = shift->_get_self;
565 warn "EXISTS($self, '$key')\n" if DEBUG;
569 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 my $engine = $self->_engine;
582 unless ( $engine->storage->is_writable ) {
583 $self->_throw_error( 'Cannot write to a readonly filehandle' );
586 $self->lock_exclusive;
588 local $SIG{'__DIE__'};
589 $engine->clear( $self );
601 # Public method aliases
602 sub put { (shift)->STORE( @_ ) }
603 sub get { (shift)->FETCH( @_ ) }
604 sub store { (shift)->STORE( @_ ) }
605 sub fetch { (shift)->FETCH( @_ ) }
606 sub delete { (shift)->DELETE( @_ ) }
607 sub exists { (shift)->EXISTS( @_ ) }
608 sub clear { (shift)->CLEAR( @_ ) }
610 sub _dump_file {shift->_get_self->_engine->_dump_file;}