6 use warnings FATAL => 'all';
8 our $VERSION = q(1.0015);
12 use DBM::Deep::Engine::DBI ();
13 use DBM::Deep::Engine::File ();
15 use DBM::Deep::SQL::Util;
16 use DBM::Deep::SQL::Array;
17 use DBM::Deep::SQL::Hash;
20 '""' => sub { overload::StrVal( $_[0] ) },
23 use constant DEBUG => 0;
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 };
52 # Class constructor method for Perl OO interface.
53 # Calls tie() and returns blessed reference to tied hash or array,
54 # providing a hybrid OO/tie interface.
57 my $args = $class->_get_args( @_ );
60 if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) {
61 $class = 'DBM::Deep::Array';
62 require DBM::Deep::Array;
63 tie @$self, $class, %$args;
66 $class = 'DBM::Deep::Hash';
67 require DBM::Deep::Hash;
68 tie %$self, $class, %$args;
71 return bless $self, $class;
74 # This initializer is called from the various TIE* methods. new() calls tie(),
75 # which allows for a single point of entry.
80 # locking implicitly enables autoflush
81 if ($args->{locking}) { $args->{autoflush} = 1; }
83 # These are the defaults to be optionally overridden below
91 unless ( exists $args->{engine} ) {
92 my $class = exists $args->{dbi}
93 ? 'DBM::Deep::Engine::DBI'
94 : 'DBM::Deep::Engine::File';
96 $args->{engine} = $class->new({
102 # Grab the parameters we want to use
103 foreach my $param ( keys %$self ) {
104 next unless exists $args->{$param};
105 $self->{$param} = $args->{$param};
109 local $SIG{'__DIE__'};
111 $self->lock_exclusive;
112 $self->_engine->setup( $self );
116 eval { local $SIG{'__DIE__'}; $self->unlock; };
125 require DBM::Deep::Hash;
126 return DBM::Deep::Hash->TIEHASH( @_ );
131 require DBM::Deep::Array;
132 return DBM::Deep::Array->TIEARRAY( @_ );
136 my $self = shift->_get_self;
137 return $self->_engine->lock_exclusive( $self, @_ );
139 *lock = \&lock_exclusive;
141 my $self = shift->_get_self;
142 return $self->_engine->lock_shared( $self, @_ );
146 my $self = shift->_get_self;
147 return $self->_engine->unlock( $self, @_ );
151 my $self = shift->_get_self;
152 my ($spot, $value) = @_;
158 my $r = Scalar::Util::reftype( $value );
160 if ( $r eq 'ARRAY' ) {
161 $tied = tied(@$value);
163 elsif ( $r eq 'HASH' ) {
164 $tied = tied(%$value);
167 __PACKAGE__->_throw_error( "Unknown type for '$value'" );
170 if ( eval { local $SIG{__DIE__}; $tied->isa( __PACKAGE__ ) } ) {
171 ${$spot} = $tied->_repr;
172 $tied->_copy_node( ${$spot} );
175 if ( $r eq 'ARRAY' ) {
176 ${$spot} = [ @{$value} ];
179 ${$spot} = { %{$value} };
183 my $c = Scalar::Util::blessed( $value );
184 if ( defined $c && !$c->isa( __PACKAGE__ ) ) {
185 ${$spot} = bless ${$spot}, $c
193 # die "Must be implemented in a child class\n";
197 # die "Must be implemented in a child class\n";
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 __PACKAGE__->_throw_error(
228 "Storage of references of type '$r' is not supported."
233 return if !ref $_[0]; # Perl calls import() on use -- ignore
235 my $self = shift->_get_self;
238 my $type = $self->_check_legality( $struct );
240 __PACKAGE__->_throw_error( "Cannot import a scalar" );
243 if ( substr( $type, 0, 1 ) ne $self->_type ) {
244 __PACKAGE__->_throw_error(
245 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
246 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
255 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
258 my $r = $self->_check_legality( $val );
259 if ( 'HASH' eq $r ) {
260 while ( my ($k, $v) = each %$val ) {
261 my $r = $self->_check_legality( $v );
263 my $temp = 'HASH' eq $r ? {} : [];
264 if ( my $c = Scalar::Util::blessed( $v ) ) {
267 $obj->put( $k, $temp );
268 $recurse->( $temp, $v );
275 elsif ( 'ARRAY' eq $r ) {
276 foreach my $k ( 0 .. $#$val ) {
278 my $r = $self->_check_legality( $v );
280 my $temp = 'HASH' eq $r ? {} : [];
281 if ( my $c = Scalar::Util::blessed( $v ) ) {
284 $obj->put( $k, $temp );
285 $recurse->( $temp, $v );
293 $recurse->( $self, $struct );
298 #XXX Need to keep track of who has a fh to this file in order to
299 #XXX close them all prior to optimize on Win32/cygwin
300 # Rebuild entire database into new file, then move
301 # it back on top of original.
303 my $self = shift->_get_self;
305 # Optimizing is only something we need to do when we're working with our
306 # own file format. Otherwise, let the other guy do the optimizations.
307 return unless $self->_engine->isa( 'DBM::Deep::Engine::File' );
309 #XXX Need to create a new test for this
310 # if ($self->_engine->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->_engine->storage->{file} . '.tmp';
318 my $db_temp = __PACKAGE__->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
328 $self->lock_exclusive;
329 $self->_engine->clear_cache;
330 $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 );
370 # Make copy of object and return
372 my $self = shift->_get_self;
374 return __PACKAGE__->new(
375 type => $self->_type,
376 base_offset => $self->_base_offset,
377 staleness => $self->_staleness,
378 engine => $self->_engine,
384 return $self->_engine->supports( @_ );
387 #XXX Migrate this to the engine, where it really belongs and go through some
388 # API - stop poking in the innards of someone else..
390 my %is_legal_filter = map {
393 store_key store_value
394 fetch_key fetch_value
398 my $self = shift->_get_self;
402 if ( $is_legal_filter{$type} ) {
403 $self->_engine->storage->{"filter_$type"} = $func;
410 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
411 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
412 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
413 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
417 my $self = shift->_get_self;
418 $self->lock_exclusive;
419 my $rv = eval { $self->_engine->begin_work( $self, @_ ) };
427 my $self = shift->_get_self;
428 $self->lock_exclusive;
429 my $rv = eval { $self->_engine->rollback( $self, @_ ) };
437 my $self = shift->_get_self;
438 $self->lock_exclusive;
439 my $rv = eval { $self->_engine->commit( $self, @_ ) };
448 my $self = $_[0]->_get_self;
449 return $self->{engine};
453 my $self = $_[0]->_get_self;
454 return $self->{type};
458 my $self = $_[0]->_get_self;
459 return $self->{base_offset};
463 my $self = $_[0]->_get_self;
464 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";
478 # Store single hash key/value or array element in database.
480 my $self = shift->_get_self;
481 my ($key, $value) = @_;
482 warn "STORE($self, '$key', '@{[defined$value?$value:'undef']}')\n" if DEBUG;
484 unless ( $self->_engine->storage->is_writable ) {
485 $self->_throw_error( 'Cannot write to a readonly filehandle' );
488 $self->lock_exclusive;
490 # User may be storing a complex value, in which case we do not want it run
491 # through the filtering system.
492 if ( !ref($value) && $self->_engine->storage->{filter_store_value} ) {
493 $value = $self->_engine->storage->{filter_store_value}->( $value );
496 $self->_engine->write_value( $self, $key, $value );
503 # Fetch single value or element given plain key or array index
505 my $self = shift->_get_self;
507 warn "FETCH($self, '$key')\n" if DEBUG;
511 my $result = $self->_engine->read_value( $self, $key );
515 # Filters only apply to scalar values, so the ref check is making
516 # sure the fetched bucket is a scalar, not a child hash or array.
517 return ($result && !ref($result) && $self->_engine->storage->{filter_fetch_value})
518 ? $self->_engine->storage->{filter_fetch_value}->($result)
522 # Delete single key/value pair or element given plain key or array index
524 my $self = shift->_get_self;
526 warn "DELETE($self, '$key')\n" if DEBUG;
528 unless ( $self->_engine->storage->is_writable ) {
529 $self->_throw_error( 'Cannot write to a readonly filehandle' );
532 $self->lock_exclusive;
537 my $value = $self->_engine->delete_key( $self, $key);
539 if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) {
540 $value = $self->_engine->storage->{filter_fetch_value}->($value);
548 # Check if a single key or element exists given plain key or array index
550 my $self = shift->_get_self;
552 warn "EXISTS($self, '$key')\n" if DEBUG;
556 my $result = $self->_engine->key_exists( $self, $key );
563 # Clear all keys from hash, or all elements from array.
565 my $self = shift->_get_self;
566 warn "CLEAR($self)\n" if DEBUG;
568 unless ( $self->_engine->storage->is_writable ) {
569 $self->_throw_error( 'Cannot write to a readonly filehandle' );
572 $self->lock_exclusive;
574 #XXX Rewrite this dreck to do it in the engine as a tight loop vs.
575 # iterating over keys - such a WASTE - is this required for transactional
576 # clearning?! Surely that can be detected in the engine ...
577 if ( $self->_type eq TYPE_HASH ) {
578 my $key = $self->first_key;
580 # Retrieve the key before deleting because we depend on next_key
581 my $next_key = $self->next_key( $key );
582 $self->_engine->delete_key( $self, $key, $key );
587 my $size = $self->FETCHSIZE;
588 for my $key ( 0 .. $size - 1 ) {
589 $self->_engine->delete_key( $self, $key, $key );
591 $self->STORESIZE( 0 );
599 # Public method aliases
600 sub put { (shift)->STORE( @_ ) }
601 sub get { (shift)->FETCH( @_ ) }
602 sub store { (shift)->STORE( @_ ) }
603 sub fetch { (shift)->FETCH( @_ ) }
604 sub delete { (shift)->DELETE( @_ ) }
605 sub exists { (shift)->EXISTS( @_ ) }
606 sub clear { (shift)->CLEAR( @_ ) }
608 sub _dump_file {shift->_get_self->_engine->_dump_file;}