6 use warnings FATAL => 'all';
8 our $VERSION = q(1.0019_002);
13 '""' => sub { overload::StrVal( $_[0] ) },
16 use constant DEBUG => 0;
18 use DBM::Deep::Engine;
20 sub TYPE_HASH () { DBM::Deep::Engine->SIG_HASH }
21 sub TYPE_ARRAY () { DBM::Deep::Engine->SIG_ARRAY }
23 # This is used in all the children of this class in their TIE<type> methods.
30 $proto->_throw_error( "Odd number of parameters to " . (caller(1))[2] );
35 unless ( eval { local $SIG{'__DIE__'}; %{$_[0]} || 1 } ) {
36 $proto->_throw_error( "Not a hashref in args to " . (caller(1))[2] );
41 $args = { file => shift };
47 # Class constructor method for Perl OO interface.
48 # Calls tie() and returns blessed reference to tied hash or array,
49 # providing a hybrid OO/tie interface.
52 my $args = $class->_get_args( @_ );
55 if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) {
56 $class = 'DBM::Deep::Array';
57 require DBM::Deep::Array;
58 tie @$self, $class, %$args;
61 $class = 'DBM::Deep::Hash';
62 require DBM::Deep::Hash;
63 tie %$self, $class, %$args;
66 return bless $self, $class;
69 # This initializer is called from the various TIE* methods. new() calls tie(),
70 # which allows for a single point of entry.
75 # locking implicitly enables autoflush
76 if ($args->{locking}) { $args->{autoflush} = 1; }
78 # These are the defaults to be optionally overridden below
86 unless ( exists $args->{engine} ) {
87 my $class = exists $args->{dbi}
88 ? 'DBM::Deep::Engine::DBI'
89 : 'DBM::Deep::Engine::File';
91 eval "use $class"; die $@ if $@;
92 $args->{engine} = $class->new({
98 # Grab the parameters we want to use
99 foreach my $param ( keys %$self ) {
100 next unless exists $args->{$param};
101 $self->{$param} = $args->{$param};
105 local $SIG{'__DIE__'};
107 $self->lock_exclusive;
108 $self->_engine->setup( $self );
112 eval { local $SIG{'__DIE__'}; $self->unlock; };
121 require DBM::Deep::Hash;
122 return DBM::Deep::Hash->TIEHASH( @_ );
127 require DBM::Deep::Array;
128 return DBM::Deep::Array->TIEARRAY( @_ );
132 my $self = shift->_get_self;
133 return $self->_engine->lock_exclusive( $self, @_ );
135 *lock = \&lock_exclusive;
137 my $self = shift->_get_self;
138 return $self->_engine->lock_shared( $self, @_ );
142 my $self = shift->_get_self;
143 return $self->_engine->unlock( $self, @_ );
147 my $self = shift->_get_self;
148 my ($spot, $value) = @_;
154 my $r = Scalar::Util::reftype( $value );
156 if ( $r eq 'ARRAY' ) {
157 $tied = tied(@$value);
159 elsif ( $r eq 'HASH' ) {
160 $tied = tied(%$value);
163 __PACKAGE__->_throw_error( "Unknown type for '$value'" );
166 if ( eval { local $SIG{__DIE__}; $tied->isa( __PACKAGE__ ) } ) {
167 ${$spot} = $tied->_repr;
168 $tied->_copy_node( ${$spot} );
171 if ( $r eq 'ARRAY' ) {
172 ${$spot} = [ @{$value} ];
175 ${$spot} = { %{$value} };
179 my $c = Scalar::Util::blessed( $value );
180 if ( defined $c && !$c->isa( __PACKAGE__ ) ) {
181 ${$spot} = bless ${$spot}, $c
189 # die "Must be implemented in a child class\n";
193 # die "Must be implemented in a child class\n";
197 my $self = shift->_get_self;
199 my $temp = $self->_repr;
201 $self->lock_exclusive;
202 $self->_copy_node( $temp );
205 my $classname = $self->_engine->get_classname( $self );
206 if ( defined $classname ) {
207 bless $temp, $classname;
213 sub _check_legality {
217 my $r = Scalar::Util::reftype( $val );
219 return $r if !defined $r || '' eq $r;
220 return $r if 'HASH' eq $r;
221 return $r if 'ARRAY' eq $r;
223 __PACKAGE__->_throw_error(
224 "Storage of references of type '$r' is not supported."
229 return if !ref $_[0]; # Perl calls import() on use -- ignore
231 my $self = shift->_get_self;
234 my $type = $self->_check_legality( $struct );
236 __PACKAGE__->_throw_error( "Cannot import a scalar" );
239 if ( substr( $type, 0, 1 ) ne $self->_type ) {
240 __PACKAGE__->_throw_error(
241 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
242 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
251 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
254 my $r = $self->_check_legality( $val );
255 if ( 'HASH' eq $r ) {
256 while ( my ($k, $v) = each %$val ) {
257 my $r = $self->_check_legality( $v );
259 my $temp = 'HASH' eq $r ? {} : [];
260 if ( my $c = Scalar::Util::blessed( $v ) ) {
263 $obj->put( $k, $temp );
264 $recurse->( $temp, $v );
271 elsif ( 'ARRAY' eq $r ) {
272 foreach my $k ( 0 .. $#$val ) {
274 my $r = $self->_check_legality( $v );
276 my $temp = 'HASH' eq $r ? {} : [];
277 if ( my $c = Scalar::Util::blessed( $v ) ) {
280 $obj->put( $k, $temp );
281 $recurse->( $temp, $v );
289 $recurse->( $self, $struct );
294 #XXX Need to keep track of who has a fh to this file in order to
295 #XXX close them all prior to optimize on Win32/cygwin
296 # Rebuild entire database into new file, then move
297 # it back on top of original.
299 my $self = shift->_get_self;
301 # Optimizing is only something we need to do when we're working with our
302 # own file format. Otherwise, let the other guy do the optimizations.
303 return unless $self->_engine->isa( 'DBM::Deep::Engine::File' );
305 #XXX Need to create a new test for this
306 # if ($self->_engine->storage->{links} > 1) {
307 # $self->_throw_error("Cannot optimize: reference count is greater than 1");
310 #XXX Do we have to lock the tempfile?
312 #XXX Should we use tempfile() here instead of a hard-coded name?
313 my $temp_filename = $self->_engine->storage->{file} . '.tmp';
314 my $db_temp = __PACKAGE__->new(
315 file => $temp_filename,
316 type => $self->_type,
318 # Bring over all the parameters that we need to bring over
319 ( map { $_ => $self->_engine->$_ } qw(
320 byte_size max_buckets data_sector_size num_txns
324 $self->lock_exclusive;
325 $self->_engine->clear_cache;
326 $self->_copy_node( $db_temp );
327 $db_temp->_engine->storage->close;
331 # Attempt to copy user, group and permissions over to new file
333 $self->_engine->storage->copy_stats( $temp_filename );
335 # q.v. perlport for more information on this variable
336 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
338 # Potential race condition when optmizing on Win32 with locking.
339 # The Windows filesystem requires that the filehandle be closed
340 # before it is overwritten with rename(). This could be redone
344 $self->_engine->storage->close;
347 if (!rename $temp_filename, $self->_engine->storage->{file}) {
348 unlink $temp_filename;
350 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
354 $self->_engine->storage->close;
356 $self->_engine->storage->open;
357 $self->lock_exclusive;
358 $self->_engine->setup( $self );
366 # Make copy of object and return
368 my $self = shift->_get_self;
370 return __PACKAGE__->new(
371 type => $self->_type,
372 base_offset => $self->_base_offset,
373 staleness => $self->_staleness,
374 engine => $self->_engine,
380 return $self->_engine->supports( @_ );
383 #XXX Migrate this to the engine, where it really belongs and go through some
384 # API - stop poking in the innards of someone else..
386 my %is_legal_filter = map {
389 store_key store_value
390 fetch_key fetch_value
394 my $self = shift->_get_self;
398 if ( $is_legal_filter{$type} ) {
399 $self->_engine->storage->{"filter_$type"} = $func;
406 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
407 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
408 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
409 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
413 my $self = shift->_get_self;
414 $self->lock_exclusive;
415 my $rv = eval { $self->_engine->begin_work( $self, @_ ) };
423 my $self = shift->_get_self;
424 $self->lock_exclusive;
425 my $rv = eval { $self->_engine->rollback( $self, @_ ) };
433 my $self = shift->_get_self;
434 $self->lock_exclusive;
435 my $rv = eval { $self->_engine->commit( $self, @_ ) };
444 my $self = $_[0]->_get_self;
445 return $self->{engine};
449 my $self = $_[0]->_get_self;
450 return $self->{type};
454 my $self = $_[0]->_get_self;
455 return $self->{base_offset};
459 my $self = $_[0]->_get_self;
460 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";
474 # Store single hash key/value or array element in database.
476 my $self = shift->_get_self;
477 my ($key, $value) = @_;
478 warn "STORE($self, '$key', '@{[defined$value?$value:'undef']}')\n" if DEBUG;
480 unless ( $self->_engine->storage->is_writable ) {
481 $self->_throw_error( 'Cannot write to a readonly filehandle' );
484 $self->lock_exclusive;
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->_engine->storage->{filter_store_value} ) {
489 $value = $self->_engine->storage->{filter_store_value}->( $value );
493 $self->_engine->write_value( $self, $key, $value );
494 }; if ( my $e = $@ ) {
504 # Fetch single value or element given plain key or array index
506 my $self = shift->_get_self;
508 warn "FETCH($self, '$key')\n" if DEBUG;
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->_engine->storage->{filter_fetch_value})
519 ? $self->_engine->storage->{filter_fetch_value}->($result)
523 # Delete single key/value pair or element given plain key or array index
525 my $self = shift->_get_self;
527 warn "DELETE($self, '$key')\n" if DEBUG;
529 unless ( $self->_engine->storage->is_writable ) {
530 $self->_throw_error( 'Cannot write to a readonly filehandle' );
533 $self->lock_exclusive;
538 my $value = $self->_engine->delete_key( $self, $key);
540 if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) {
541 $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 );
564 # Clear all keys from hash, or all elements from array.
566 my $self = shift->_get_self;
567 warn "CLEAR($self)\n" if DEBUG;
569 unless ( $self->_engine->storage->is_writable ) {
570 $self->_throw_error( 'Cannot write to a readonly filehandle' );
573 $self->lock_exclusive;
575 #XXX Rewrite this dreck to do it in the engine as a tight loop vs.
576 # iterating over keys - such a WASTE - is this required for transactional
577 # clearning?! Surely that can be detected in the engine ...
578 if ( $self->_type eq TYPE_HASH ) {
579 my $key = $self->first_key;
581 # Retrieve the key before deleting because we depend on next_key
582 my $next_key = $self->next_key( $key );
583 $self->_engine->delete_key( $self, $key, $key );
588 my $size = $self->FETCHSIZE;
589 for my $key ( 0 .. $size - 1 ) {
590 $self->_engine->delete_key( $self, $key, $key );
592 $self->STORESIZE( 0 );
600 # Public method aliases
601 sub put { (shift)->STORE( @_ ) }
602 sub get { (shift)->FETCH( @_ ) }
603 sub store { (shift)->STORE( @_ ) }
604 sub fetch { (shift)->FETCH( @_ ) }
605 sub delete { (shift)->DELETE( @_ ) }
606 sub exists { (shift)->EXISTS( @_ ) }
607 sub clear { (shift)->CLEAR( @_ ) }
609 sub _dump_file {shift->_get_self->_engine->_dump_file;}