1 package DBM::Deep::Engine;
8 our $VERSION = q(0.99_03);
10 use Fcntl qw( :DEFAULT :flock );
14 # * To add to bucket_size, make sure you modify the following:
17 # - add_bucket() - where the buckets are printed
19 # * Every method in here assumes that the _fileobj has been appropriately
20 # safeguarded. This can be anything from flock() to some sort of manual
21 # mutex. But, it's the caller's responsability to make sure that this has
25 # Setup file and tag signatures. These should never change.
27 sub SIG_FILE () { 'DPDB' }
28 sub SIG_HEADER () { 'h' }
29 sub SIG_INTERNAL () { 'i' }
30 sub SIG_HASH () { 'H' }
31 sub SIG_ARRAY () { 'A' }
32 sub SIG_NULL () { 'N' }
33 sub SIG_DATA () { 'D' }
34 sub SIG_INDEX () { 'I' }
35 sub SIG_BLIST () { 'B' }
36 sub SIG_FREE () { 'F' }
37 sub SIG_KEYS () { 'K' }
40 ################################################################################
42 # This is new code. It is a complete rewrite of the engine based on a new API
44 ################################################################################
48 my ($offset, $key, $value, $orig_key) = @_;
50 my $dig_key = $self->apply_digest( $key );
51 my $tag = $self->find_blist( $offset, $dig_key, { create => 1 } );
52 return $self->add_bucket( $tag, $dig_key, $key, $value, undef, $orig_key );
57 my ($offset, $key, $orig_key) = @_;
59 my $dig_key = $self->apply_digest( $key );
60 my $tag = $self->find_blist( $offset, $dig_key ) or return;
61 return $self->get_bucket_value( $tag, $dig_key, $orig_key );
66 my ($offset, $key, $orig_key) = @_;
68 my $dig_key = $self->apply_digest( $key );
69 my $tag = $self->find_blist( $offset, $dig_key ) or return;
70 my $value = $self->get_bucket_value( $tag, $dig_key, $orig_key );
71 $self->delete_bucket( $tag, $dig_key, $orig_key );
77 my ($offset, $key) = @_;
79 my $dig_key = $self->apply_digest( $key );
80 # exists() returns the empty string, not undef
81 my $tag = $self->find_blist( $offset, $dig_key ) or return '';
82 return $self->bucket_exists( $tag, $dig_key, $key );
89 # If the previous key was not specifed, start at the top and
90 # return the first one found.
94 prev_md5 => $self->apply_digest($_[1]),
100 prev_md5 => chr(0) x $self->{hash_size},
105 return $self->traverse_index( $temp, $offset, 0 );
108 ################################################################################
110 # Below here is the old code. It will be folded into the code above as it can.
112 ################################################################################
124 digest => \&Digest::MD5::md5,
125 hash_size => 16, # In bytes
128 # Number of buckets per blist before another level of indexing is
129 # done. Increase this value for slightly greater speed, but larger database
130 # files. DO NOT decrease this value below 16, due to risk of recursive
139 if ( defined $args->{pack_size} ) {
140 if ( lc $args->{pack_size} eq 'small' ) {
141 $args->{long_size} = 2;
142 $args->{long_pack} = 'n';
144 elsif ( lc $args->{pack_size} eq 'medium' ) {
145 $args->{long_size} = 4;
146 $args->{long_pack} = 'N';
148 elsif ( lc $args->{pack_size} eq 'large' ) {
149 $args->{long_size} = 8;
150 $args->{long_pack} = 'Q';
153 die "Unknown pack_size value: '$args->{pack_size}'\n";
157 # Grab the parameters we want to use
158 foreach my $param ( keys %$self ) {
159 next unless exists $args->{$param};
160 $self->{$param} = $args->{$param};
162 Scalar::Util::weaken( $self->{obj} ) if $self->{obj};
164 if ( $self->{max_buckets} < 16 ) {
165 warn "Floor of max_buckets is 16. Setting it to 16 from '$self->{max_buckets}'\n";
166 $self->{max_buckets} = 16;
172 sub _fileobj { return $_[0]{fileobj} }
176 return $self->{digest}->(@_);
179 sub calculate_sizes {
182 # The 2**8 here indicates the number of different characters in the
183 # current hashing algorithm
184 #XXX Does this need to be updated with different hashing algorithms?
185 $self->{hash_chars_used} = (2**8);
186 $self->{index_size} = $self->{hash_chars_used} * $self->{long_size};
188 $self->{bucket_size} = $self->{hash_size} + $self->{long_size} * 2;
189 $self->{bucket_list_size} = $self->{max_buckets} * $self->{bucket_size};
191 $self->{key_size} = $self->{long_size} * 2;
192 $self->{keyloc_size} = $self->{max_buckets} * $self->{key_size};
197 sub write_file_header {
200 my $loc = $self->_fileobj->request_space( length( SIG_FILE ) + 33 );
202 $self->_fileobj->print_at( $loc,
205 pack('N', 1), # header version
206 pack('N', 24), # header size
207 pack('N4', 0, 0, 0, 0), # currently running transaction IDs
208 pack('n', $self->{long_size}),
209 pack('A', $self->{long_pack}),
210 pack('n', $self->{data_size}),
211 pack('A', $self->{data_pack}),
212 pack('n', $self->{max_buckets}),
215 $self->_fileobj->set_transaction_offset( 13 );
220 sub read_file_header {
223 my $buffer = $self->_fileobj->read_at( 0, length(SIG_FILE) + 9 );
224 return unless length($buffer);
226 my ($file_signature, $sig_header, $header_version, $size) = unpack(
230 unless ( $file_signature eq SIG_FILE ) {
231 $self->_fileobj->close;
232 $self->_throw_error( "Signature not found -- file is not a Deep DB" );
235 unless ( $sig_header eq SIG_HEADER ) {
236 $self->_fileobj->close;
237 $self->_throw_error( "Old file version found." );
240 my $buffer2 = $self->_fileobj->read_at( undef, $size );
241 my ($a1, $a2, $a3, $a4, @values) = unpack( 'N4 n A n A n', $buffer2 );
243 $self->_fileobj->set_transaction_offset( 13 );
245 if ( @values < 5 || grep { !defined } @values ) {
246 $self->_fileobj->close;
247 $self->_throw_error("Corrupted file - bad header");
250 #XXX Add warnings if values weren't set right
251 @{$self}{qw(long_size long_pack data_size data_pack max_buckets)} = @values;
253 return length($buffer) + length($buffer2);
260 # Need to remove use of $fh here
261 my $fh = $self->_fileobj->{fh};
264 #XXX The duplication of calculate_sizes needs to go away
265 unless ( $obj->{base_offset} ) {
266 my $bytes_read = $self->read_file_header;
268 $self->calculate_sizes;
271 # File is empty -- write header and master index
274 $self->_fileobj->audit( "# Database created on" );
276 $self->write_file_header;
278 $obj->{base_offset} = $self->_fileobj->request_space(
279 $self->tag_size( $self->{index_size} ),
283 $obj->_base_offset, $obj->_type,
284 chr(0)x$self->{index_size},
287 # Flush the filehandle
288 my $old_fh = select $fh;
289 my $old_af = $|; $| = 1; $| = $old_af;
293 $obj->{base_offset} = $bytes_read;
296 # Get our type from master index header
298 my $tag = $self->load_tag($obj->_base_offset);
301 $self->_throw_error("Corrupted file, no master index record");
304 unless ($obj->_type eq $tag->{signature}) {
306 $self->_throw_error("File type mismatch");
311 $self->calculate_sizes;
314 #XXX We have to make sure we don't mess up when autoflush isn't turned on
315 $self->_fileobj->set_inode;
325 return SIG_SIZE + $self->{data_size} + $size;
330 # Given offset, signature and content, create tag and write to disk
333 my ($offset, $sig, $content) = @_;
334 my $size = length( $content );
336 $self->_fileobj->print_at(
338 $sig, pack($self->{data_pack}, $size), $content,
341 return unless defined $offset;
345 #XXX Is this even used?
347 offset => $offset + SIG_SIZE + $self->{data_size},
354 # Given offset, load single tag and return signature, size and data
359 my $fileobj = $self->_fileobj;
361 my ($sig, $size) = unpack(
362 "A $self->{data_pack}",
363 $fileobj->read_at( $offset, SIG_SIZE + $self->{data_size} ),
368 size => $size, #XXX Is this even used?
369 offset => $offset + SIG_SIZE + $self->{data_size},
370 content => $fileobj->read_at( undef, $size ),
376 my ($tag, $transaction_id) = @_;
377 $transaction_id = $self->_fileobj->transaction_id
378 unless defined $transaction_id;
380 for ( my $i = 0; $i < $self->{max_buckets}; $i++ ) {
381 my ($loc, $trans_id, $is_deleted) = unpack(
382 "$self->{long_pack} C C",
383 substr( $tag->{content}, $i * $self->{key_size}, $self->{key_size} ),
387 return ( $loc, $is_deleted, $i * $self->{key_size} );
390 next if $transaction_id != $trans_id;
392 return ( $loc, $is_deleted, $i * $self->{key_size} );
400 # Adds one key/value pair to bucket list, given offset, MD5 digest of key,
401 # plain (undigested) key and value.
404 my ($tag, $md5, $plain_key, $value, $deleted, $orig_key) = @_;
406 # This verifies that only supported values will be stored.
408 my $r = Scalar::Util::reftype( $value );
411 last if $r eq 'HASH';
412 last if $r eq 'ARRAY';
415 "Storage of references of type '$r' is not supported."
419 my $fileobj = $self->_fileobj;
421 #ACID - This is a mutation. Must only find the exact transaction
422 my ($keyloc, $offset) = $self->_find_in_buckets( $tag, $md5, 1 );
425 if ( $fileobj->transaction_id == 0 ) {
426 @transactions = $fileobj->current_transactions;
429 # $self->_release_space( $size, $subloc );
430 #XXX This needs updating to use _release_space
433 my $size = $self->_length_needed( $value, $plain_key );
435 # Updating a known md5
437 my $keytag = $self->load_tag( $keyloc );
438 my ($subloc, $is_deleted, $offset) = $self->find_keyloc( $keytag );
440 if ( $subloc && !$is_deleted && @transactions ) {
441 my $old_value = $self->read_from_loc( $subloc, $orig_key );
442 my $old_size = $self->_length_needed( $old_value, $plain_key );
444 for my $trans_id ( @transactions ) {
445 my ($loc, $is_deleted, $offset2) = $self->find_keyloc( $keytag, $trans_id );
447 my $location2 = $fileobj->request_space( $old_size );
448 $fileobj->print_at( $keytag->{offset} + $offset2,
449 pack($self->{long_pack}, $location2 ),
450 pack( 'C C', $trans_id, 0 ),
452 $self->_write_value( $location2, $plain_key, $old_value, $orig_key );
457 $location = $self->_fileobj->request_space( $size );
458 #XXX This needs to be transactionally-aware in terms of which keytag->{offset} to use
459 $fileobj->print_at( $keytag->{offset} + $offset,
460 pack($self->{long_pack}, $location ),
461 pack( 'C C', $fileobj->transaction_id, 0 ),
466 my $keyloc = $fileobj->request_space( $self->tag_size( $self->{keyloc_size} ) );
468 # The bucket fit into list
469 if ( defined $offset ) {
470 $fileobj->print_at( $tag->{offset} + $offset,
471 $md5, pack( $self->{long_pack}, $keyloc ),
474 # If bucket didn't fit into list, split into a new index level
476 $self->split_index( $tag, $md5, $keyloc );
479 my $keytag = $self->write_tag(
480 $keyloc, SIG_KEYS, chr(0)x$self->{keyloc_size},
483 $location = $self->_fileobj->request_space( $size );
484 $fileobj->print_at( $keytag->{offset},
485 pack( $self->{long_pack}, $location ),
486 pack( 'C C', $fileobj->transaction_id, 0 ),
490 for my $trans_id ( @transactions ) {
491 $fileobj->print_at( $keytag->{offset} + $self->{key_size} * $offset++,
492 pack( $self->{long_pack}, 0 ),
493 pack( 'C C', $trans_id, 1 ),
498 $self->_write_value( $location, $plain_key, $value, $orig_key );
505 my ($location, $key, $value, $orig_key) = @_;
507 my $fileobj = $self->_fileobj;
509 my $dbm_deep_obj = _get_dbm_object( $value );
510 if ( $dbm_deep_obj && $dbm_deep_obj->_fileobj ne $fileobj ) {
511 $self->_throw_error( "Cannot cross-reference. Use export() instead" );
515 # Write signature based on content type, set content length and write
518 my $r = Scalar::Util::reftype( $value ) || '';
519 if ( $dbm_deep_obj ) {
520 $self->write_tag( $location, SIG_INTERNAL,pack($self->{long_pack}, $dbm_deep_obj->_base_offset) );
522 elsif ($r eq 'HASH') {
523 if ( !$dbm_deep_obj && tied %{$value} ) {
524 $self->_throw_error( "Cannot store something that is tied" );
526 $self->write_tag( $location, SIG_HASH, chr(0)x$self->{index_size} );
528 elsif ($r eq 'ARRAY') {
529 if ( !$dbm_deep_obj && tied @{$value} ) {
530 $self->_throw_error( "Cannot store something that is tied" );
532 $self->write_tag( $location, SIG_ARRAY, chr(0)x$self->{index_size} );
534 elsif (!defined($value)) {
535 $self->write_tag( $location, SIG_NULL, '' );
538 $self->write_tag( $location, SIG_DATA, $value );
542 # Plain key is stored AFTER value, as keys are typically fetched less often.
544 $fileobj->print_at( undef, pack($self->{data_pack}, length($key)) . $key );
546 # Internal references don't care about autobless
547 return 1 if $dbm_deep_obj;
550 # If value is blessed, preserve class name
552 if ( $fileobj->{autobless} ) {
553 if ( defined( my $c = Scalar::Util::blessed($value) ) ) {
554 $fileobj->print_at( undef, chr(1), pack($self->{data_pack}, length($c)) . $c );
557 $fileobj->print_at( undef, chr(0) );
562 # Tie the passed in reference so that changes to it are reflected in the
563 # datafile. The use of $location as the base_offset will act as the
564 # the linkage between parent and child.
566 # The overall assignment is a hack around the fact that just tying doesn't
567 # store the values. This may not be the wrong thing to do.
571 tie %$value, 'DBM::Deep', {
572 base_offset => $location,
574 parent => $self->{obj},
575 parent_key => $orig_key,
578 bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value );
580 elsif ($r eq 'ARRAY') {
582 tie @$value, 'DBM::Deep', {
583 base_offset => $location,
585 parent => $self->{obj},
586 parent_key => $orig_key,
589 bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value );
597 my ($tag, $md5, $keyloc) = @_;
599 my $fileobj = $self->_fileobj;
601 my $loc = $fileobj->request_space(
602 $self->tag_size( $self->{index_size} ),
605 $fileobj->print_at( $tag->{ref_loc}, pack($self->{long_pack}, $loc) );
607 my $index_tag = $self->write_tag(
609 chr(0)x$self->{index_size},
612 my $keys = $tag->{content}
613 . $md5 . pack($self->{long_pack}, $keyloc);
617 # The <= here is deliberate - we have max_buckets+1 keys to iterate
618 # through, unlike every other loop that uses max_buckets as a stop.
619 for (my $i = 0; $i <= $self->{max_buckets}; $i++) {
620 my ($key, $old_subloc) = $self->_get_key_subloc( $keys, $i );
622 die "[INTERNAL ERROR]: No key in split_index()\n" unless $key;
623 die "[INTERNAL ERROR]: No subloc in split_index()\n" unless $old_subloc;
625 my $num = ord(substr($key, $tag->{ch} + 1, 1));
628 my $subkeys = $fileobj->read_at( $newloc[$num], $self->{bucket_list_size} );
630 # This is looking for the first empty spot
631 my ($subloc, $offset) = $self->_find_in_buckets(
632 { content => $subkeys }, '',
636 $newloc[$num] + $offset,
637 $key, pack($self->{long_pack}, $old_subloc),
643 my $loc = $fileobj->request_space(
644 $self->tag_size( $self->{bucket_list_size} ),
648 $index_tag->{offset} + ($num * $self->{long_size}),
649 pack($self->{long_pack}, $loc),
652 my $blist_tag = $self->write_tag(
654 chr(0)x$self->{bucket_list_size},
657 $fileobj->print_at( $blist_tag->{offset}, $key . pack($self->{long_pack}, $old_subloc) );
659 $newloc[$num] = $blist_tag->{offset};
662 $self->_release_space(
663 $self->tag_size( $self->{bucket_list_size} ),
664 $tag->{offset} - SIG_SIZE - $self->{data_size},
672 my ($subloc, $orig_key) = @_;
674 my $fileobj = $self->_fileobj;
676 my $signature = $fileobj->read_at( $subloc, SIG_SIZE );
679 # If value is a hash or array, return new DBM::Deep object with correct offset
681 if (($signature eq SIG_HASH) || ($signature eq SIG_ARRAY)) {
682 #XXX This needs to be a singleton
685 # if ( $signature eq SIG_HASH ) {
687 # tie %$new_obj, 'DBM::Deep', {
688 # base_offset => $subloc,
689 # fileobj => $self->_fileobj,
690 # parent => $self->{obj},
691 # parent_key => $orig_key,
693 # $is_autobless = tied(%$new_obj)->_fileobj->{autobless};
697 # tie @$new_obj, 'DBM::Deep', {
698 # base_offset => $subloc,
699 # fileobj => $self->_fileobj,
700 # parent => $self->{obj},
701 # parent_key => $orig_key,
703 # $is_autobless = tied(@$new_obj)->_fileobj->{autobless};
706 # if ($is_autobless) {
708 my $new_obj = DBM::Deep->new({
710 base_offset => $subloc,
711 fileobj => $self->_fileobj,
712 parent => $self->{obj},
713 parent_key => $orig_key,
716 if ($new_obj->_fileobj->{autobless}) {
718 # Skip over value and plain key to see if object needs
721 $fileobj->increment_pointer( $self->{data_size} + $self->{index_size} );
723 my $size = $fileobj->read_at( undef, $self->{data_size} );
724 $size = unpack($self->{data_pack}, $size);
725 if ($size) { $fileobj->increment_pointer( $size ); }
727 my $bless_bit = $fileobj->read_at( undef, 1 );
728 if ( ord($bless_bit) ) {
731 $fileobj->read_at( undef, $self->{data_size} ),
735 $new_obj = bless $new_obj, $fileobj->read_at( undef, $size );
742 elsif ( $signature eq SIG_INTERNAL ) {
743 my $size = $fileobj->read_at( undef, $self->{data_size} );
744 $size = unpack($self->{data_pack}, $size);
747 my $new_loc = $fileobj->read_at( undef, $size );
748 $new_loc = unpack( $self->{long_pack}, $new_loc );
749 return $self->read_from_loc( $new_loc, $orig_key );
756 # Otherwise return actual value
758 elsif ( $signature eq SIG_DATA ) {
759 my $size = $fileobj->read_at( undef, $self->{data_size} );
760 $size = unpack($self->{data_pack}, $size);
762 my $value = $size ? $fileobj->read_at( undef, $size ) : '';
767 # Key exists, but content is null
772 sub get_bucket_value {
774 # Fetch single value given tag and MD5 digested key.
777 my ($tag, $md5, $orig_key) = @_;
779 #ACID - This is a read. Can find exact or HEAD
780 my ($keyloc, $offset) = $self->_find_in_buckets( $tag, $md5 );
783 #XXX Need to use real key
784 # $self->add_bucket( $tag, $md5, $orig_key, undef, $orig_key );
787 # elsif ( !$is_deleted ) {
789 my $keytag = $self->load_tag( $keyloc );
790 my ($subloc, $is_deleted) = $self->find_keyloc( $keytag );
791 if (!$subloc && !$is_deleted) {
792 ($subloc, $is_deleted) = $self->find_keyloc( $keytag, 0 );
794 if ( $subloc && !$is_deleted ) {
795 return $self->read_from_loc( $subloc, $orig_key );
804 # Delete single key/value pair given tag and MD5 digested key.
807 my ($tag, $md5, $orig_key) = @_;
809 #ACID - Although this is a mutation, we must find any transaction.
810 # This is because we need to mark something as deleted that is in the HEAD.
811 my ($keyloc, $offset) = $self->_find_in_buckets( $tag, $md5 );
815 my $fileobj = $self->_fileobj;
818 if ( $fileobj->transaction_id == 0 ) {
819 @transactions = $fileobj->current_transactions;
822 if ( $fileobj->transaction_id == 0 ) {
823 my $keytag = $self->load_tag( $keyloc );
825 my ($subloc, $is_deleted, $offset) = $self->find_keyloc( $keytag );
826 return if !$subloc || $is_deleted;
828 my $value = $self->read_from_loc( $subloc, $orig_key );
830 my $size = $self->_length_needed( $value, $orig_key );
832 for my $trans_id ( @transactions ) {
833 my ($loc, $is_deleted, $offset2) = $self->find_keyloc( $keytag, $trans_id );
835 my $location2 = $fileobj->request_space( $size );
836 $fileobj->print_at( $keytag->{offset} + $offset2,
837 pack($self->{long_pack}, $location2 ),
838 pack( 'C C', $trans_id, 0 ),
840 $self->_write_value( $location2, $orig_key, $value, $orig_key );
844 $keytag = $self->load_tag( $keyloc );
845 ($subloc, $is_deleted, $offset) = $self->find_keyloc( $keytag );
846 $fileobj->print_at( $keytag->{offset} + $offset,
847 substr( $keytag->{content}, $offset + $self->{key_size} ),
848 chr(0) x $self->{key_size},
852 my $keytag = $self->load_tag( $keyloc );
854 my ($subloc, $is_deleted, $offset) = $self->find_keyloc( $keytag );
856 $fileobj->print_at( $keytag->{offset} + $offset,
857 pack($self->{long_pack}, 0 ),
858 pack( 'C C', $fileobj->transaction_id, 1 ),
867 # Check existence of single key given tag and MD5 digested key.
870 my ($tag, $md5) = @_;
872 #ACID - This is a read. Can find exact or HEAD
873 my ($keyloc) = $self->_find_in_buckets( $tag, $md5 );
874 my $keytag = $self->load_tag( $keyloc );
875 my ($subloc, $is_deleted, $offset) = $self->find_keyloc( $keytag );
876 if ( !$subloc && !$is_deleted ) {
877 ($subloc, $is_deleted, $offset) = $self->find_keyloc( $keytag, 0 );
879 return ($subloc && !$is_deleted) && 1;
884 # Locate offset for bucket list, given digested key
887 my ($offset, $md5, $args) = @_;
888 $args = {} unless $args;
891 # Locate offset for bucket list using digest index system
893 my $tag = $self->load_tag( $offset )
894 or $self->_throw_error( "INTERNAL ERROR - Cannot find tag" );
897 while ($tag->{signature} ne SIG_BLIST) {
898 my $num = ord substr($md5, $ch, 1);
900 my $ref_loc = $tag->{offset} + ($num * $self->{long_size});
901 $tag = $self->index_lookup( $tag, $num );
904 return if !$args->{create};
906 my $loc = $self->_fileobj->request_space(
907 $self->tag_size( $self->{bucket_list_size} ),
910 $self->_fileobj->print_at( $ref_loc, pack($self->{long_pack}, $loc) );
912 $tag = $self->write_tag(
914 chr(0)x$self->{bucket_list_size},
917 $tag->{ref_loc} = $ref_loc;
924 $tag->{ref_loc} = $ref_loc;
932 # Given index tag, lookup single entry in index and return .
935 my ($tag, $index) = @_;
937 my $location = unpack(
941 $index * $self->{long_size},
946 if (!$location) { return; }
948 return $self->load_tag( $location );
953 # Scan index and recursively step into deeper levels, looking for next key.
956 my ($xxxx, $offset, $ch, $force_return_next) = @_;
958 my $tag = $self->load_tag( $offset );
960 if ($tag->{signature} ne SIG_BLIST) {
961 my $start = $xxxx->{return_next} ? 0 : ord(substr($xxxx->{prev_md5}, $ch, 1));
963 for (my $idx = $start; $idx < $self->{hash_chars_used}; $idx++) {
968 $idx * $self->{long_size},
974 my $result = $self->traverse_index(
975 $xxxx, $subloc, $ch + 1, $force_return_next,
978 if (defined $result) { return $result; }
982 $xxxx->{return_next} = 1;
984 # This is the bucket list
986 my $keys = $tag->{content};
987 if ($force_return_next) { $xxxx->{return_next} = 1; }
990 # Iterate through buckets, looking for a key match
992 my $transaction_id = $self->_fileobj->transaction_id;
993 for (my $i = 0; $i < $self->{max_buckets}; $i++) {
994 my ($key, $keyloc) = $self->_get_key_subloc( $keys, $i );
996 # End of bucket list -- return to outer loop
998 $xxxx->{return_next} = 1;
1001 # Located previous key -- return next one found
1002 elsif ($key eq $xxxx->{prev_md5}) {
1003 $xxxx->{return_next} = 1;
1006 # Seek to bucket location and skip over signature
1007 elsif ($xxxx->{return_next}) {
1008 my $fileobj = $self->_fileobj;
1010 my $keytag = $self->load_tag( $keyloc );
1011 my ($subloc, $is_deleted) = $self->find_keyloc( $keytag );
1012 if ( $subloc == 0 && !$is_deleted ) {
1013 ($subloc, $is_deleted) = $self->find_keyloc( $keytag, 0 );
1015 next if $is_deleted;
1017 # Skip over value to get to plain key
1018 my $sig = $fileobj->read_at( $subloc, SIG_SIZE );
1020 my $size = $fileobj->read_at( undef, $self->{data_size} );
1021 $size = unpack($self->{data_pack}, $size);
1022 if ($size) { $fileobj->increment_pointer( $size ); }
1024 # Read in plain key and return as scalar
1025 $size = $fileobj->read_at( undef, $self->{data_size} );
1026 $size = unpack($self->{data_pack}, $size);
1029 if ($size) { $plain_key = $fileobj->read_at( undef, $size); }
1034 $xxxx->{return_next} = 1;
1042 sub _get_key_subloc {
1044 my ($keys, $idx) = @_;
1047 # This is 'a', not 'A'. Please read the pack() documentation for the
1048 # difference between the two and why it's important.
1049 "a$self->{hash_size} $self->{long_pack}",
1052 ($idx * $self->{bucket_size}),
1053 $self->{bucket_size},
1058 sub _find_in_buckets {
1060 my ($tag, $md5) = @_;
1063 for ( my $i = 0; $i < $self->{max_buckets}; $i++ ) {
1064 my ($key, $subloc) = $self->_get_key_subloc(
1065 $tag->{content}, $i,
1068 my @rv = ($subloc, $i * $self->{bucket_size});
1070 unless ( $subloc ) {
1074 next BUCKET if $key ne $md5;
1082 sub _release_space {
1084 my ($size, $loc) = @_;
1088 $self->_fileobj->print_at( $loc,
1090 pack($self->{long_pack}, $size ),
1091 pack($self->{long_pack}, $next_loc ),
1098 die "DBM::Deep: $_[1]\n";
1101 sub _get_dbm_object {
1105 local $SIG{__DIE__};
1106 if ($item->isa( 'DBM::Deep' )) {
1111 return $obj if $obj;
1113 my $r = Scalar::Util::reftype( $item ) || '';
1114 if ( $r eq 'HASH' ) {
1116 local $SIG{__DIE__};
1117 my $obj = tied(%$item);
1118 if ($obj->isa( 'DBM::Deep' )) {
1123 return $obj if $obj;
1125 elsif ( $r eq 'ARRAY' ) {
1127 local $SIG{__DIE__};
1128 my $obj = tied(@$item);
1129 if ($obj->isa( 'DBM::Deep' )) {
1134 return $obj if $obj;
1140 sub _length_needed {
1142 my ($value, $key) = @_;
1144 my $is_dbm_deep = eval {
1145 local $SIG{'__DIE__'};
1146 $value->isa( 'DBM::Deep' );
1150 + $self->{data_size} # size for value
1151 + $self->{data_size} # size for key
1152 + length( $key ); # length of key
1154 if ( $is_dbm_deep && $value->_fileobj eq $self->_fileobj ) {
1155 # long_size is for the internal reference
1156 return $len + $self->{long_size};
1159 if ( $self->_fileobj->{autobless} ) {
1160 # This is for the bit saying whether or not this thing is blessed.
1164 my $r = Scalar::Util::reftype( $value ) || '';
1165 unless ( $r eq 'HASH' || $r eq 'ARRAY' ) {
1166 if ( defined $value ) {
1167 $len += length( $value );
1172 $len += $self->{index_size};
1174 # if autobless is enabled, must also take into consideration
1175 # the class name as it is stored after the key.
1176 if ( $self->_fileobj->{autobless} ) {
1177 my $c = Scalar::Util::blessed($value);
1178 if ( defined $c && !$is_dbm_deep ) {
1179 $len += $self->{data_size} + length($c);