1 package DBM::Deep::Engine;
8 our $VERSION = q(1.0007);
13 # * Every method in here assumes that the storage has been appropriately
14 # safeguarded. This can be anything from flock() to some sort of manual
15 # mutex. But, it's the caller's responsability to make sure that this has
18 # Setup file and tag signatures. These should never change.
19 sub SIG_FILE () { 'DPDB' }
20 sub SIG_HEADER () { 'h' }
21 sub SIG_HASH () { 'H' }
22 sub SIG_ARRAY () { 'A' }
23 sub SIG_NULL () { 'N' }
24 sub SIG_DATA () { 'D' }
25 sub SIG_INDEX () { 'I' }
26 sub SIG_BLIST () { 'B' }
27 sub SIG_FREE () { 'F' }
32 # Please refer to the pack() documentation for further information
34 1 => 'C', # Unsigned char value (no order needed as it's just one byte)
35 2 => 'n', # Unsigned short in "network" (big-endian) order
36 4 => 'N', # Unsigned long in "network" (big-endian) order
37 8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent)
40 ################################################################################
50 hash_size => 16, # In bytes
51 hash_chars => 256, # Number of chars the algorithm uses per byte
53 num_txns => 1, # The HEAD
54 trans_id => 0, # Default to the HEAD
56 data_sector_size => 64, # Size in bytes of each data sector
58 entries => {}, # This is the list of entries for transactions
62 # Never allow byte_size to be set directly.
63 delete $args->{byte_size};
64 if ( defined $args->{pack_size} ) {
65 if ( lc $args->{pack_size} eq 'small' ) {
66 $args->{byte_size} = 2;
68 elsif ( lc $args->{pack_size} eq 'medium' ) {
69 $args->{byte_size} = 4;
71 elsif ( lc $args->{pack_size} eq 'large' ) {
72 $args->{byte_size} = 8;
75 DBM::Deep->_throw_error( "Unknown pack_size value: '$args->{pack_size}'" );
79 # Grab the parameters we want to use
80 foreach my $param ( keys %$self ) {
81 next unless exists $args->{$param};
82 $self->{$param} = $args->{$param};
86 max_buckets => { floor => 16, ceil => 256 },
87 num_txns => { floor => 1, ceil => 255 },
88 data_sector_size => { floor => 32, ceil => 256 },
91 while ( my ($attr, $c) = each %validations ) {
92 if ( !defined $self->{$attr}
93 || !length $self->{$attr}
94 || $self->{$attr} =~ /\D/
95 || $self->{$attr} < $c->{floor}
97 $self->{$attr} = '(undef)' if !defined $self->{$attr};
98 warn "Floor of $attr is $c->{floor}. Setting it to $c->{floor} from '$self->{$attr}'\n";
99 $self->{$attr} = $c->{floor};
101 elsif ( $self->{$attr} > $c->{ceil} ) {
102 warn "Ceiling of $attr is $c->{ceil}. Setting it to $c->{ceil} from '$self->{$attr}'\n";
103 $self->{$attr} = $c->{ceil};
107 if ( !$self->{digest} ) {
109 $self->{digest} = \&Digest::MD5::md5;
115 ################################################################################
119 my ($obj, $key) = @_;
121 # This will be a Reference sector
122 my $sector = $self->_load_sector( $obj->_base_offset )
125 if ( $sector->staleness != $obj->_staleness ) {
129 my $key_md5 = $self->_apply_digest( $key );
131 my $value_sector = $sector->get_data_for({
136 unless ( $value_sector ) {
137 $value_sector = DBM::Deep::Engine::Sector::Null->new({
142 $sector->write_data({
145 value => $value_sector,
149 return $value_sector->data;
156 # This will be a Reference sector
157 my $sector = $self->_load_sector( $obj->_base_offset )
158 or DBM::Deep->_throw_error( "How did get_classname fail (no sector for '$obj')?!" );
160 if ( $sector->staleness != $obj->_staleness ) {
164 return $sector->get_classname;
169 my ($obj, $old_key, $new_key) = @_;
171 # This will be a Reference sector
172 my $sector = $self->_load_sector( $obj->_base_offset )
173 or DBM::Deep->_throw_error( "How did get_classname fail (no sector for '$obj')?!" );
175 if ( $sector->staleness != $obj->_staleness ) {
179 my $old_md5 = $self->_apply_digest( $old_key );
181 my $value_sector = $sector->get_data_for({
186 unless ( $value_sector ) {
187 $value_sector = DBM::Deep::Engine::Sector::Null->new({
192 $sector->write_data({
195 value => $value_sector,
199 if ( $value_sector->isa( 'DBM::Deep::Engine::Sector::Reference' ) ) {
200 $sector->write_data({
202 key_md5 => $self->_apply_digest( $new_key ),
203 value => $value_sector,
205 $value_sector->increment_refcount;
208 $sector->write_data({
210 key_md5 => $self->_apply_digest( $new_key ),
211 value => $value_sector->clone,
218 my ($obj, $key) = @_;
220 # This will be a Reference sector
221 my $sector = $self->_load_sector( $obj->_base_offset )
224 if ( $sector->staleness != $obj->_staleness ) {
228 my $data = $sector->get_data_for({
229 key_md5 => $self->_apply_digest( $key ),
233 # exists() returns 1 or '' for true/false.
234 return $data ? 1 : '';
239 my ($obj, $key) = @_;
241 my $sector = $self->_load_sector( $obj->_base_offset )
244 if ( $sector->staleness != $obj->_staleness ) {
248 return $sector->delete_key({
249 key_md5 => $self->_apply_digest( $key ),
256 my ($obj, $key, $value) = @_;
258 my $r = Scalar::Util::reftype( $value ) || '';
261 last if $r eq 'HASH';
262 last if $r eq 'ARRAY';
264 DBM::Deep->_throw_error(
265 "Storage of references of type '$r' is not supported."
269 # This will be a Reference sector
270 my $sector = $self->_load_sector( $obj->_base_offset )
271 or DBM::Deep->_throw_error( "Cannot write to a deleted spot in DBM::Deep." );
273 if ( $sector->staleness != $obj->_staleness ) {
274 DBM::Deep->_throw_error( "Cannot write to a deleted spot in DBM::Deep.n" );
278 if ( !defined $value ) {
279 $class = 'DBM::Deep::Engine::Sector::Null';
281 elsif ( $r eq 'ARRAY' || $r eq 'HASH' ) {
282 my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $value->isa( 'DBM::Deep' ); };
283 if ( $is_dbm_deep ) {
284 if ( $value->_engine->storage == $self->storage ) {
285 my $value_sector = $self->_load_sector( $value->_base_offset );
286 $sector->write_data({
288 key_md5 => $self->_apply_digest( $key ),
289 value => $value_sector,
291 $value_sector->increment_refcount;
295 DBM::Deep->_throw_error( "Cannot store values across DBM::Deep files. Please use export() instead." );
297 if ( $r eq 'ARRAY' && tied(@$value) ) {
298 DBM::Deep->_throw_error( "Cannot store something that is tied." );
300 if ( $r eq 'HASH' && tied(%$value) ) {
301 DBM::Deep->_throw_error( "Cannot store something that is tied." );
303 $class = 'DBM::Deep::Engine::Sector::Reference';
304 $type = substr( $r, 0, 1 );
307 if ( tied($value) ) {
308 DBM::Deep->_throw_error( "Cannot store something that is tied." );
310 $class = 'DBM::Deep::Engine::Sector::Scalar';
313 # Create this after loading the reference sector in case something bad happens.
314 # This way, we won't allocate value sector(s) needlessly.
315 my $value_sector = $class->new({
321 $sector->write_data({
323 key_md5 => $self->_apply_digest( $key ),
324 value => $value_sector,
327 # This code is to make sure we write all the values in the $value to the disk
328 # and to make sure all changes to $value after the assignment are reflected
329 # on disk. This may be counter-intuitive at first, but it is correct dwimmery.
330 # NOTE - simply tying $value won't perform a STORE on each value. Hence, the
331 # copy to a temp value.
332 if ( $r eq 'ARRAY' ) {
334 tie @$value, 'DBM::Deep', {
335 base_offset => $value_sector->offset,
336 staleness => $value_sector->staleness,
337 storage => $self->storage,
341 bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value );
343 elsif ( $r eq 'HASH' ) {
345 tie %$value, 'DBM::Deep', {
346 base_offset => $value_sector->offset,
347 staleness => $value_sector->staleness,
348 storage => $self->storage,
353 bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value );
359 # XXX Add staleness here
362 my ($obj, $prev_key) = @_;
364 # XXX Need to add logic about resetting the iterator if any key in the reference has changed
365 unless ( $prev_key ) {
366 $obj->{iterator} = DBM::Deep::Iterator->new({
367 base_offset => $obj->_base_offset,
372 return $obj->{iterator}->get_next_key( $obj );
375 ################################################################################
381 # We're opening the file.
382 unless ( $obj->_base_offset ) {
383 my $bytes_read = $self->_read_file_header;
385 # Creating a new file
386 unless ( $bytes_read ) {
387 $self->_write_file_header;
389 # 1) Create Array/Hash entry
390 my $initial_reference = DBM::Deep::Engine::Sector::Reference->new({
394 $obj->{base_offset} = $initial_reference->offset;
395 $obj->{staleness} = $initial_reference->staleness;
397 $self->storage->flush;
399 # Reading from an existing file
401 $obj->{base_offset} = $bytes_read;
402 my $initial_reference = DBM::Deep::Engine::Sector::Reference->new({
404 offset => $obj->_base_offset,
406 unless ( $initial_reference ) {
407 DBM::Deep->_throw_error("Corrupted file, no master index record");
410 unless ($obj->_type eq $initial_reference->type) {
411 DBM::Deep->_throw_error("File type mismatch");
414 $obj->{staleness} = $initial_reference->staleness;
425 if ( $self->trans_id ) {
426 DBM::Deep->_throw_error( "Cannot begin_work within an active transaction" );
429 my @slots = $self->read_txn_slots;
431 for my $i ( 0 .. $#slots ) {
435 $self->set_trans_id( $i + 1 );
440 DBM::Deep->_throw_error( "Cannot allocate transaction ID" );
442 $self->write_txn_slots( @slots );
444 if ( !$self->trans_id ) {
445 DBM::Deep->_throw_error( "Cannot begin_work - no available transactions" );
455 if ( !$self->trans_id ) {
456 DBM::Deep->_throw_error( "Cannot rollback without an active transaction" );
459 # Each entry is the file location for a bucket that has a modification for
460 # this transaction. The entries need to be expunged.
461 foreach my $entry (@{ $self->get_entries } ) {
462 # Remove the entry here
463 my $read_loc = $entry
467 + ($self->trans_id - 1) * ( $self->byte_size + $STALE_SIZE );
469 my $data_loc = $self->storage->read_at( $read_loc, $self->byte_size );
470 $data_loc = unpack( $StP{$self->byte_size}, $data_loc );
471 $self->storage->print_at( $read_loc, pack( $StP{$self->byte_size}, 0 ) );
473 if ( $data_loc > 1 ) {
474 $self->_load_sector( $data_loc )->free;
478 $self->clear_entries;
480 my @slots = $self->read_txn_slots;
481 $slots[$self->trans_id-1] = 0;
482 $self->write_txn_slots( @slots );
483 $self->inc_txn_staleness_counter( $self->trans_id );
484 $self->set_trans_id( 0 );
493 if ( !$self->trans_id ) {
494 DBM::Deep->_throw_error( "Cannot commit without an active transaction" );
497 foreach my $entry (@{ $self->get_entries } ) {
498 # Overwrite the entry in head with the entry in trans_id
503 my $head_loc = $self->storage->read_at( $base, $self->byte_size );
504 $head_loc = unpack( $StP{$self->byte_size}, $head_loc );
506 my $spot = $base + $self->byte_size + ($self->trans_id - 1) * ( $self->byte_size + $STALE_SIZE );
507 my $trans_loc = $self->storage->read_at(
508 $spot, $self->byte_size,
511 $self->storage->print_at( $base, $trans_loc );
512 $self->storage->print_at(
514 pack( $StP{$self->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ),
517 if ( $head_loc > 1 ) {
518 $self->_load_sector( $head_loc )->free;
522 $self->clear_entries;
524 my @slots = $self->read_txn_slots;
525 $slots[$self->trans_id-1] = 0;
526 $self->write_txn_slots( @slots );
527 $self->inc_txn_staleness_counter( $self->trans_id );
528 $self->set_trans_id( 0 );
535 my $bl = $self->txn_bitfield_len;
536 my $num_bits = $bl * 8;
537 return split '', unpack( 'b'.$num_bits,
538 $self->storage->read_at(
539 $self->trans_loc, $bl,
544 sub write_txn_slots {
546 my $num_bits = $self->txn_bitfield_len * 8;
547 $self->storage->print_at( $self->trans_loc,
548 pack( 'b'.$num_bits, join('', @_) ),
552 sub get_running_txn_ids {
554 my @transactions = $self->read_txn_slots;
555 my @trans_ids = map { $_+1} grep { $transactions[$_] } 0 .. $#transactions;
558 sub get_txn_staleness_counter {
562 # Hardcode staleness of 0 for the HEAD
563 return 0 unless $trans_id;
565 return unpack( $StP{$STALE_SIZE},
566 $self->storage->read_at(
567 $self->trans_loc + $self->txn_bitfield_len + $STALE_SIZE * ($trans_id - 1),
573 sub inc_txn_staleness_counter {
577 # Hardcode staleness of 0 for the HEAD
578 return 0 unless $trans_id;
580 $self->storage->print_at(
581 $self->trans_loc + $self->txn_bitfield_len + $STALE_SIZE * ($trans_id - 1),
582 pack( $StP{$STALE_SIZE}, $self->get_txn_staleness_counter( $trans_id ) + 1 ),
588 return [ keys %{ $self->{entries}{$self->trans_id} ||= {} } ];
593 my ($trans_id, $loc) = @_;
595 $self->{entries}{$trans_id} ||= {};
596 $self->{entries}{$trans_id}{$loc} = undef;
599 # If the buckets are being relocated because of a reindexing, the entries
600 # mechanism needs to be made aware of it.
603 my ($old_loc, $new_loc) = @_;
606 while ( my ($trans_id, $locs) = each %{ $self->{entries} } ) {
607 foreach my $orig_loc ( keys %{ $locs } ) {
608 if ( $orig_loc == $old_loc ) {
609 delete $locs->{orig_loc};
610 $locs->{$new_loc} = undef;
619 delete $self->{entries}{$self->trans_id};
622 ################################################################################
625 my $header_fixed = length( SIG_FILE ) + 1 + 4 + 4;
626 my $this_file_version = 3;
628 sub _write_file_header {
631 my $nt = $self->num_txns;
632 my $bl = $self->txn_bitfield_len;
634 my $header_var = 1 + 1 + 1 + 1 + $bl + $STALE_SIZE * ($nt - 1) + 3 * $self->byte_size;
636 my $loc = $self->storage->request_space( $header_fixed + $header_var );
638 $self->storage->print_at( $loc,
641 pack('N', $this_file_version), # At this point, we're at 9 bytes
642 pack('N', $header_var), # header size
643 # --- Above is $header_fixed. Below is $header_var
644 pack('C', $self->byte_size),
646 # These shenanigans are to allow a 256 within a C
647 pack('C', $self->max_buckets - 1),
648 pack('C', $self->data_sector_size - 1),
651 pack('C' . $bl, 0 ), # Transaction activeness bitfield
652 pack($StP{$STALE_SIZE}.($nt-1), 0 x ($nt-1) ), # Transaction staleness counters
653 pack($StP{$self->byte_size}, 0), # Start of free chain (blist size)
654 pack($StP{$self->byte_size}, 0), # Start of free chain (data size)
655 pack($StP{$self->byte_size}, 0), # Start of free chain (index size)
658 #XXX Set these less fragilely
659 $self->set_trans_loc( $header_fixed + 4 );
660 $self->set_chains_loc( $header_fixed + 4 + $bl + $STALE_SIZE * ($nt-1) );
665 sub _read_file_header {
668 my $buffer = $self->storage->read_at( 0, $header_fixed );
669 return unless length($buffer);
671 my ($file_signature, $sig_header, $file_version, $size) = unpack(
675 unless ( $file_signature eq SIG_FILE ) {
676 $self->storage->close;
677 DBM::Deep->_throw_error( "Signature not found -- file is not a Deep DB" );
680 unless ( $sig_header eq SIG_HEADER ) {
681 $self->storage->close;
682 DBM::Deep->_throw_error( "Pre-1.00 file version found" );
685 unless ( $file_version == $this_file_version ) {
686 $self->storage->close;
687 DBM::Deep->_throw_error(
688 "Wrong file version found - " . $file_version .
689 " - expected " . $this_file_version
693 my $buffer2 = $self->storage->read_at( undef, $size );
694 my @values = unpack( 'C C C C', $buffer2 );
696 if ( @values != 4 || grep { !defined } @values ) {
697 $self->storage->close;
698 DBM::Deep->_throw_error("Corrupted file - bad header");
701 #XXX Add warnings if values weren't set right
702 @{$self}{qw(byte_size max_buckets data_sector_size num_txns)} = @values;
704 # These shenangians are to allow a 256 within a C
705 $self->{max_buckets} += 1;
706 $self->{data_sector_size} += 1;
708 my $bl = $self->txn_bitfield_len;
710 my $header_var = scalar(@values) + $bl + $STALE_SIZE * ($self->num_txns - 1) + 3 * $self->byte_size;
711 unless ( $size == $header_var ) {
712 $self->storage->close;
713 DBM::Deep->_throw_error( "Unexpected size found ($size <-> $header_var)." );
716 $self->set_trans_loc( $header_fixed + scalar(@values) );
717 $self->set_chains_loc( $header_fixed + scalar(@values) + $bl + $STALE_SIZE * ($self->num_txns - 1) );
719 return length($buffer) + length($buffer2);
727 # Add a catch for offset of 0 or 1
728 return if !$offset || $offset <= 1;
730 my $type = $self->storage->read_at( $offset, 1 );
731 return if $type eq chr(0);
733 if ( $type eq $self->SIG_ARRAY || $type eq $self->SIG_HASH ) {
734 return DBM::Deep::Engine::Sector::Reference->new({
740 # XXX Don't we need key_md5 here?
741 elsif ( $type eq $self->SIG_BLIST ) {
742 return DBM::Deep::Engine::Sector::BucketList->new({
748 elsif ( $type eq $self->SIG_INDEX ) {
749 return DBM::Deep::Engine::Sector::Index->new({
755 elsif ( $type eq $self->SIG_NULL ) {
756 return DBM::Deep::Engine::Sector::Null->new({
762 elsif ( $type eq $self->SIG_DATA ) {
763 return DBM::Deep::Engine::Sector::Scalar->new({
769 # This was deleted from under us, so just return and let the caller figure it out.
770 elsif ( $type eq $self->SIG_FREE ) {
774 DBM::Deep->_throw_error( "'$offset': Don't know what to do with type '$type'" );
779 return $self->{digest}->(@_);
782 sub _add_free_blist_sector { shift->_add_free_sector( 0, @_ ) }
783 sub _add_free_data_sector { shift->_add_free_sector( 1, @_ ) }
784 sub _add_free_index_sector { shift->_add_free_sector( 2, @_ ) }
786 sub _add_free_sector {
788 my ($multiple, $offset, $size) = @_;
790 my $chains_offset = $multiple * $self->byte_size;
792 my $storage = $self->storage;
794 # Increment staleness.
795 # XXX Can this increment+modulo be done by "&= 0x1" ?
796 my $staleness = unpack( $StP{$STALE_SIZE}, $storage->read_at( $offset + SIG_SIZE, $STALE_SIZE ) );
797 $staleness = ($staleness + 1 ) % ( 2 ** ( 8 * $STALE_SIZE ) );
798 $storage->print_at( $offset + SIG_SIZE, pack( $StP{$STALE_SIZE}, $staleness ) );
800 my $old_head = $storage->read_at( $self->chains_loc + $chains_offset, $self->byte_size );
802 $storage->print_at( $self->chains_loc + $chains_offset,
803 pack( $StP{$self->byte_size}, $offset ),
806 # Record the old head in the new sector after the signature and staleness counter
807 $storage->print_at( $offset + SIG_SIZE + $STALE_SIZE, $old_head );
810 sub _request_blist_sector { shift->_request_sector( 0, @_ ) }
811 sub _request_data_sector { shift->_request_sector( 1, @_ ) }
812 sub _request_index_sector { shift->_request_sector( 2, @_ ) }
814 sub _request_sector {
816 my ($multiple, $size) = @_;
818 my $chains_offset = $multiple * $self->byte_size;
820 my $old_head = $self->storage->read_at( $self->chains_loc + $chains_offset, $self->byte_size );
821 my $loc = unpack( $StP{$self->byte_size}, $old_head );
823 # We don't have any free sectors of the right size, so allocate a new one.
825 my $offset = $self->storage->request_space( $size );
827 # Zero out the new sector. This also guarantees correct increases
829 $self->storage->print_at( $offset, chr(0) x $size );
834 # Read the new head after the signature and the staleness counter
835 my $new_head = $self->storage->read_at( $loc + SIG_SIZE + $STALE_SIZE, $self->byte_size );
836 $self->storage->print_at( $self->chains_loc + $chains_offset, $new_head );
837 $self->storage->print_at(
838 $loc + SIG_SIZE + $STALE_SIZE,
839 pack( $StP{$self->byte_size}, 0 ),
845 ################################################################################
847 sub storage { $_[0]{storage} }
848 sub byte_size { $_[0]{byte_size} }
849 sub hash_size { $_[0]{hash_size} }
850 sub hash_chars { $_[0]{hash_chars} }
851 sub num_txns { $_[0]{num_txns} }
852 sub max_buckets { $_[0]{max_buckets} }
853 sub blank_md5 { chr(0) x $_[0]->hash_size }
854 sub data_sector_size { $_[0]{data_sector_size} }
856 # This is a calculated value
857 sub txn_bitfield_len {
859 unless ( exists $self->{txn_bitfield_len} ) {
860 my $temp = ($self->num_txns) / 8;
861 if ( $temp > int( $temp ) ) {
862 $temp = int( $temp ) + 1;
864 $self->{txn_bitfield_len} = $temp;
866 return $self->{txn_bitfield_len};
869 sub trans_id { $_[0]{trans_id} }
870 sub set_trans_id { $_[0]{trans_id} = $_[1] }
872 sub trans_loc { $_[0]{trans_loc} }
873 sub set_trans_loc { $_[0]{trans_loc} = $_[1] }
875 sub chains_loc { $_[0]{chains_loc} }
876 sub set_chains_loc { $_[0]{chains_loc} = $_[1] }
878 sub cache { $_[0]{cache} ||= {} }
879 sub clear_cache { %{$_[0]->cache} = () }
885 my $spot = $self->_read_file_header();
894 'D' => $self->data_sector_size,
895 'B' => DBM::Deep::Engine::Sector::BucketList->new({engine=>$self,offset=>1})->size,
896 'I' => DBM::Deep::Engine::Sector::Index->new({engine=>$self,offset=>1})->size,
902 $return .= "NumTxns: " . $self->num_txns . $/;
904 # Read the free sector chains
906 foreach my $multiple ( 0 .. 2 ) {
907 $return .= "Chains($types{$multiple}):";
908 my $old_loc = $self->chains_loc + $multiple * $self->byte_size;
911 $StP{$self->byte_size},
912 $self->storage->read_at( $old_loc, $self->byte_size ),
915 # We're now out of free sectors of this kind.
920 $sectors{ $types{$multiple} }{ $loc } = undef;
921 $old_loc = $loc + SIG_SIZE + $STALE_SIZE;
928 while ( $spot < $self->storage->{end} ) {
929 # Read each sector in order.
930 my $sector = $self->_load_sector( $spot );
932 # Find it in the free-sectors that were found already
933 foreach my $type ( keys %sectors ) {
934 if ( exists $sectors{$type}{$spot} ) {
935 my $size = $sizes{$type};
936 $return .= sprintf "%08d: %s %04d\n", $spot, 'F' . $type, $size;
942 die "********\n$return\nDidn't find free sector for $spot in chains\n********\n";
945 $return .= sprintf "%08d: %s %04d", $spot, $sector->type, $sector->size;
946 if ( $sector->type eq 'D' ) {
947 $return .= ' ' . $sector->data;
949 elsif ( $sector->type eq 'A' || $sector->type eq 'H' ) {
950 $return .= ' REF: ' . $sector->get_refcount;
952 elsif ( $sector->type eq 'B' ) {
953 foreach my $bucket ( $sector->chopped_up ) {
955 $return .= sprintf "%08d", unpack($StP{$self->byte_size},
956 substr( $bucket->[-1], $self->hash_size, $self->byte_size),
958 my $l = unpack( $StP{$self->byte_size},
959 substr( $bucket->[-1],
960 $self->hash_size + $self->byte_size,
964 $return .= sprintf " %08d", $l;
965 foreach my $txn ( 0 .. $self->num_txns - 2 ) {
966 my $l = unpack( $StP{$self->byte_size},
967 substr( $bucket->[-1],
968 $self->hash_size + 2 * $self->byte_size + $txn * ($self->byte_size + $STALE_SIZE),
972 $return .= sprintf " %08d", $l;
978 $spot += $sector->size;
985 ################################################################################
987 package DBM::Deep::Iterator;
995 engine => $args->{engine},
996 base_offset => $args->{base_offset},
999 Scalar::Util::weaken( $self->{engine} );
1004 sub reset { $_[0]{breadcrumbs} = [] }
1006 sub get_sector_iterator {
1010 my $sector = $self->{engine}->_load_sector( $loc )
1013 if ( $sector->isa( 'DBM::Deep::Engine::Sector::Index' ) ) {
1014 return DBM::Deep::Iterator::Index->new({
1019 elsif ( $sector->isa( 'DBM::Deep::Engine::Sector::BucketList' ) ) {
1020 return DBM::Deep::Iterator::BucketList->new({
1026 DBM::Deep->_throw_error( "get_sector_iterator(): Why did $loc make a $sector?" );
1033 my $crumbs = $self->{breadcrumbs};
1034 my $e = $self->{engine};
1036 unless ( @$crumbs ) {
1037 # This will be a Reference sector
1038 my $sector = $e->_load_sector( $self->{base_offset} )
1039 # If no sector is found, thist must have been deleted from under us.
1042 if ( $sector->staleness != $obj->_staleness ) {
1046 my $loc = $sector->get_blist_loc
1049 push @$crumbs, $self->get_sector_iterator( $loc );
1054 unless ( @$crumbs ) {
1059 my $iterator = $crumbs->[-1];
1061 # This level is done.
1062 if ( $iterator->at_end ) {
1067 if ( $iterator->isa( 'DBM::Deep::Iterator::Index' ) ) {
1068 # If we don't have any more, it will be caught at the
1070 if ( my $next = $iterator->get_next_iterator ) {
1071 push @$crumbs, $next;
1076 unless ( $iterator->isa( 'DBM::Deep::Iterator::BucketList' ) ) {
1077 DBM::Deep->_throw_error(
1078 "Should have a bucketlist iterator here - instead have $iterator"
1082 # At this point, we have a BucketList iterator
1083 my $key = $iterator->get_next_key;
1084 if ( defined $key ) {
1087 #XXX else { $iterator->set_to_end() } ?
1089 # We hit the end of the bucketlist iterator, so redo
1093 DBM::Deep->_throw_error( "get_next_key(): How did we get here?" );
1096 package DBM::Deep::Iterator::Index;
1099 my $self = bless $_[1] => $_[0];
1100 $self->{curr_index} = 0;
1106 return $self->{curr_index} >= $self->{iterator}{engine}->hash_chars;
1109 sub get_next_iterator {
1114 return if $self->at_end;
1115 $loc = $self->{sector}->get_entry( $self->{curr_index}++ );
1118 return $self->{iterator}->get_sector_iterator( $loc );
1121 package DBM::Deep::Iterator::BucketList;
1124 my $self = bless $_[1] => $_[0];
1125 $self->{curr_index} = 0;
1131 return $self->{curr_index} >= $self->{iterator}{engine}->max_buckets;
1137 return if $self->at_end;
1139 my $idx = $self->{curr_index}++;
1141 my $data_loc = $self->{sector}->get_data_location_for({
1146 #XXX Do we want to add corruption checks here?
1147 return $self->{sector}->get_key_for( $idx )->data;
1150 package DBM::Deep::Engine::Sector;
1153 my $self = bless $_[1], $_[0];
1154 Scalar::Util::weaken( $self->{engine} );
1160 #sub clone { DBM::Deep->_throw_error( "Must be implemented in the child class" ); }
1162 sub engine { $_[0]{engine} }
1163 sub offset { $_[0]{offset} }
1164 sub type { $_[0]{type} }
1168 return $self->engine->SIG_SIZE + $STALE_SIZE;
1174 my $e = $self->engine;
1176 $e->storage->print_at( $self->offset, $e->SIG_FREE );
1177 # Skip staleness counter
1178 $e->storage->print_at( $self->offset + $self->base_size,
1179 chr(0) x ($self->size - $self->base_size),
1182 my $free_meth = $self->free_meth;
1183 $e->$free_meth( $self->offset, $self->size );
1188 package DBM::Deep::Engine::Sector::Data;
1190 our @ISA = qw( DBM::Deep::Engine::Sector );
1193 sub size { $_[0]{engine}->data_sector_size }
1194 sub free_meth { return '_add_free_data_sector' }
1198 return ref($self)->new({
1199 engine => $self->engine,
1200 type => $self->type,
1201 data => $self->data,
1205 package DBM::Deep::Engine::Sector::Scalar;
1207 our @ISA = qw( DBM::Deep::Engine::Sector::Data );
1212 my $chain_loc = $self->chain_loc;
1214 $self->SUPER::free();
1217 $self->engine->_load_sector( $chain_loc )->free;
1223 sub type { $_[0]{engine}->SIG_DATA }
1227 my $engine = $self->engine;
1229 unless ( $self->offset ) {
1230 my $data_section = $self->size - $self->base_size - $engine->byte_size - 1;
1232 $self->{offset} = $engine->_request_data_sector( $self->size );
1234 my $data = delete $self->{data};
1235 my $dlen = length $data;
1237 my $curr_offset = $self->offset;
1238 while ( $continue ) {
1240 my $next_offset = 0;
1242 my ($leftover, $this_len, $chunk);
1243 if ( $dlen > $data_section ) {
1245 $this_len = $data_section;
1246 $chunk = substr( $data, 0, $this_len );
1248 $dlen -= $data_section;
1249 $next_offset = $engine->_request_data_sector( $self->size );
1250 $data = substr( $data, $this_len );
1253 $leftover = $data_section - $dlen;
1260 $engine->storage->print_at( $curr_offset, $self->type ); # Sector type
1262 $engine->storage->print_at( $curr_offset + $self->base_size,
1263 pack( $StP{$engine->byte_size}, $next_offset ), # Chain loc
1264 pack( $StP{1}, $this_len ), # Data length
1265 $chunk, # Data to be stored in this sector
1266 chr(0) x $leftover, # Zero-fill the rest
1269 $curr_offset = $next_offset;
1279 my $buffer = $self->engine->storage->read_at(
1280 $self->offset + $self->base_size + $self->engine->byte_size, 1
1283 return unpack( $StP{1}, $buffer );
1289 $StP{$self->engine->byte_size},
1290 $self->engine->storage->read_at(
1291 $self->offset + $self->base_size,
1292 $self->engine->byte_size,
1302 my $chain_loc = $self->chain_loc;
1304 $data .= $self->engine->storage->read_at(
1305 $self->offset + $self->base_size + $self->engine->byte_size + 1, $self->data_length,
1308 last unless $chain_loc;
1310 $self = $self->engine->_load_sector( $chain_loc );
1316 package DBM::Deep::Engine::Sector::Null;
1318 our @ISA = qw( DBM::Deep::Engine::Sector::Data );
1320 sub type { $_[0]{engine}->SIG_NULL }
1321 sub data_length { 0 }
1327 my $engine = $self->engine;
1329 unless ( $self->offset ) {
1330 my $leftover = $self->size - $self->base_size - 1 * $engine->byte_size - 1;
1332 $self->{offset} = $engine->_request_data_sector( $self->size );
1333 $engine->storage->print_at( $self->offset, $self->type ); # Sector type
1334 # Skip staleness counter
1335 $engine->storage->print_at( $self->offset + $self->base_size,
1336 pack( $StP{$engine->byte_size}, 0 ), # Chain loc
1337 pack( $StP{1}, $self->data_length ), # Data length
1338 chr(0) x $leftover, # Zero-fill the rest
1345 package DBM::Deep::Engine::Sector::Reference;
1347 our @ISA = qw( DBM::Deep::Engine::Sector::Data );
1352 my $e = $self->engine;
1354 unless ( $self->offset ) {
1355 my $classname = Scalar::Util::blessed( delete $self->{data} );
1356 my $leftover = $self->size - $self->base_size - 3 * $e->byte_size;
1358 my $class_offset = 0;
1359 if ( defined $classname ) {
1360 my $class_sector = DBM::Deep::Engine::Sector::Scalar->new({
1364 $class_offset = $class_sector->offset;
1367 $self->{offset} = $e->_request_data_sector( $self->size );
1368 $e->storage->print_at( $self->offset, $self->type ); # Sector type
1369 # Skip staleness counter
1370 $e->storage->print_at( $self->offset + $self->base_size,
1371 pack( $StP{$e->byte_size}, 0 ), # Index/BList loc
1372 pack( $StP{$e->byte_size}, $class_offset ), # Classname loc
1373 pack( $StP{$e->byte_size}, 1 ), # Initial refcount
1374 chr(0) x $leftover, # Zero-fill the rest
1378 $self->{type} = $e->storage->read_at( $self->offset, 1 );
1381 $self->{staleness} = unpack(
1383 $e->storage->read_at( $self->offset + $e->SIG_SIZE, $STALE_SIZE ),
1389 sub staleness { $_[0]{staleness} }
1395 # Assume that the head is not allowed unless otherwise specified.
1396 $args->{allow_head} = 0 unless exists $args->{allow_head};
1398 # Assume we don't create a new blist location unless otherwise specified.
1399 $args->{create} = 0 unless exists $args->{create};
1401 my $blist = $self->get_bucket_list({
1402 key_md5 => $args->{key_md5},
1403 key => $args->{key},
1404 create => $args->{create},
1406 return unless $blist && $blist->{found};
1408 # At this point, $blist knows where the md5 is. What it -doesn't- know yet
1409 # is whether or not this transaction has this key. That's part of the next
1411 my $location = $blist->get_data_location_for({
1412 allow_head => $args->{allow_head},
1415 return $self->engine->_load_sector( $location );
1422 my $blist = $self->get_bucket_list({
1423 key_md5 => $args->{key_md5},
1424 key => $args->{key},
1426 }) or DBM::Deep->_throw_error( "How did write_data fail (no blist)?!" );
1428 # Handle any transactional bookkeeping.
1429 if ( $self->engine->trans_id ) {
1430 if ( ! $blist->has_md5 ) {
1431 $blist->mark_deleted({
1437 my @trans_ids = $self->engine->get_running_txn_ids;
1438 if ( $blist->has_md5 ) {
1440 my $old_value = $blist->get_data_for;
1441 foreach my $other_trans_id ( @trans_ids ) {
1442 next if $blist->get_data_location_for({
1443 trans_id => $other_trans_id,
1447 trans_id => $other_trans_id,
1448 key => $args->{key},
1449 key_md5 => $args->{key_md5},
1450 value => $old_value->clone,
1457 foreach my $other_trans_id ( @trans_ids ) {
1458 #XXX This doesn't seem to possible to ever happen . . .
1459 next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 });
1460 $blist->mark_deleted({
1461 trans_id => $other_trans_id,
1468 #XXX Is this safe to do transactionally?
1469 # Free the place we're about to write to.
1470 if ( $blist->get_data_location_for({ allow_head => 0 }) ) {
1471 $blist->get_data_for({ allow_head => 0 })->free;
1475 key => $args->{key},
1476 key_md5 => $args->{key_md5},
1477 value => $args->{value},
1485 # XXX What should happen if this fails?
1486 my $blist = $self->get_bucket_list({
1487 key_md5 => $args->{key_md5},
1488 }) or DBM::Deep->_throw_error( "How did delete_key fail (no blist)?!" );
1490 # Save the location so that we can free the data
1491 my $location = $blist->get_data_location_for({
1494 my $old_value = $location && $self->engine->_load_sector( $location );
1496 my @trans_ids = $self->engine->get_running_txn_ids;
1498 # If we're the HEAD and there are running txns, then we need to clone this value to the other
1499 # transactions to preserve Isolation.
1500 if ( $self->engine->trans_id == 0 ) {
1502 foreach my $other_trans_id ( @trans_ids ) {
1503 next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 });
1505 trans_id => $other_trans_id,
1506 key => $args->{key},
1507 key_md5 => $args->{key_md5},
1508 value => $old_value->clone,
1516 $blist->mark_deleted( $args );
1519 $data = $old_value->data;
1524 $data = $blist->delete_md5( $args );
1533 my $e = $self->engine;
1534 my $blist_loc = $e->storage->read_at( $self->offset + $self->base_size, $e->byte_size );
1535 return unpack( $StP{$e->byte_size}, $blist_loc );
1538 sub get_bucket_list {
1543 # XXX Add in check here for recycling?
1545 my $engine = $self->engine;
1547 my $blist_loc = $self->get_blist_loc;
1549 # There's no index or blist yet
1550 unless ( $blist_loc ) {
1551 return unless $args->{create};
1553 my $blist = DBM::Deep::Engine::Sector::BucketList->new({
1555 key_md5 => $args->{key_md5},
1558 $engine->storage->print_at( $self->offset + $self->base_size,
1559 pack( $StP{$engine->byte_size}, $blist->offset ),
1565 my $sector = $engine->_load_sector( $blist_loc )
1566 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
1568 my $last_sector = undef;
1569 while ( $sector->isa( 'DBM::Deep::Engine::Sector::Index' ) ) {
1570 $blist_loc = $sector->get_entry( ord( substr( $args->{key_md5}, $i++, 1 ) ) );
1571 $last_sector = $sector;
1573 $sector = $engine->_load_sector( $blist_loc )
1574 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
1582 # This means we went through the Index sector(s) and found an empty slot
1583 unless ( $sector ) {
1584 return unless $args->{create};
1586 DBM::Deep->_throw_error( "No last_sector when attempting to build a new entry" )
1587 unless $last_sector;
1589 my $blist = DBM::Deep::Engine::Sector::BucketList->new({
1591 key_md5 => $args->{key_md5},
1594 $last_sector->set_entry( ord( substr( $args->{key_md5}, $i - 1, 1 ) ) => $blist->offset );
1599 $sector->find_md5( $args->{key_md5} );
1601 # See whether or not we need to reindex the bucketlist
1602 # Yes, the double-braces are there for a reason. if() doesn't create a redo-able block,
1603 # so we have to create a bare block within the if() for redo-purposes. Patch and idea
1604 # submitted by sprout@cpan.org. -RobK, 2008-01-09
1605 if ( !$sector->has_md5 && $args->{create} && $sector->{idx} == -1 ) {{
1608 my $new_index = DBM::Deep::Engine::Sector::Index->new({
1613 #XXX q.v. the comments for this function.
1614 foreach my $entry ( $sector->chopped_up ) {
1615 my ($spot, $md5) = @{$entry};
1616 my $idx = ord( substr( $md5, $i, 1 ) );
1618 # XXX This is inefficient
1619 my $blist = $blist_cache{$idx}
1620 ||= DBM::Deep::Engine::Sector::BucketList->new({
1624 $new_index->set_entry( $idx => $blist->offset );
1626 my $new_spot = $blist->write_at_next_open( $md5 );
1627 $engine->reindex_entry( $spot => $new_spot );
1630 # Handle the new item separately.
1632 my $idx = ord( substr( $args->{key_md5}, $i, 1 ) );
1634 # If all the previous blist's items have been thrown into one
1635 # blist and the new item belongs in there too, we need
1637 if ( keys %blist_cache == 1 and each %blist_cache == $idx ) {
1640 my $blist = $blist_cache{$idx}
1641 ||= DBM::Deep::Engine::Sector::BucketList->new({
1645 $new_index->set_entry( $idx => $blist->offset );
1648 $blist->find_md5( $args->{key_md5} );
1650 key => $args->{key},
1651 key_md5 => $args->{key_md5},
1652 value => DBM::Deep::Engine::Sector::Null->new({
1658 # my $blist = $blist_cache{$idx}
1659 # ||= DBM::Deep::Engine::Sector::BucketList->new({
1660 # engine => $engine,
1663 # $new_index->set_entry( $idx => $blist->offset );
1665 # #XXX THIS IS HACKY!
1666 # $blist->find_md5( $args->{key_md5} );
1667 # $blist->write_md5({
1668 # key => $args->{key},
1669 # key_md5 => $args->{key_md5},
1670 # value => DBM::Deep::Engine::Sector::Null->new({
1671 # engine => $engine,
1677 if ( $last_sector ) {
1678 $last_sector->set_entry(
1679 ord( substr( $args->{key_md5}, $i - 1, 1 ) ),
1683 $engine->storage->print_at( $self->offset + $self->base_size,
1684 pack( $StP{$engine->byte_size}, $new_index->offset ),
1692 (undef, $sector) = %blist_cache;
1693 $last_sector = $new_index;
1697 $sector = $blist_cache{ ord( substr( $args->{key_md5}, $i, 1 ) ) };
1698 $sector->find_md5( $args->{key_md5} );
1704 sub get_class_offset {
1707 my $e = $self->engine;
1709 $StP{$e->byte_size},
1710 $e->storage->read_at(
1711 $self->offset + $self->base_size + 1 * $e->byte_size, $e->byte_size,
1719 my $class_offset = $self->get_class_offset;
1721 return unless $class_offset;
1723 return $self->engine->_load_sector( $class_offset )->data;
1729 unless ( $self->engine->cache->{ $self->offset } ) {
1730 my $new_obj = DBM::Deep->new({
1731 type => $self->type,
1732 base_offset => $self->offset,
1733 staleness => $self->staleness,
1734 storage => $self->engine->storage,
1735 engine => $self->engine,
1738 if ( $self->engine->storage->{autobless} ) {
1739 my $classname = $self->get_classname;
1740 if ( defined $classname ) {
1741 bless $new_obj, $classname;
1745 $self->engine->cache->{$self->offset} = $new_obj;
1747 return $self->engine->cache->{$self->offset};
1753 # We're not ready to be removed yet.
1754 if ( $self->decrement_refcount > 0 ) {
1758 # Rebless the object into DBM::Deep::Null.
1759 eval { %{ $self->engine->cache->{ $self->offset } } = (); };
1760 eval { @{ $self->engine->cache->{ $self->offset } } = (); };
1761 bless $self->engine->cache->{ $self->offset }, 'DBM::Deep::Null';
1762 delete $self->engine->cache->{ $self->offset };
1764 my $blist_loc = $self->get_blist_loc;
1765 $self->engine->_load_sector( $blist_loc )->free if $blist_loc;
1767 my $class_loc = $self->get_class_offset;
1768 $self->engine->_load_sector( $class_loc )->free if $class_loc;
1770 $self->SUPER::free();
1773 sub increment_refcount {
1776 my $refcount = $self->get_refcount;
1780 $self->write_refcount( $refcount );
1785 sub decrement_refcount {
1788 my $refcount = $self->get_refcount;
1792 $self->write_refcount( $refcount );
1800 my $e = $self->engine;
1802 $StP{$e->byte_size},
1803 $e->storage->read_at(
1804 $self->offset + $self->base_size + 2 * $e->byte_size, $e->byte_size,
1809 sub write_refcount {
1813 my $e = $self->engine;
1814 $e->storage->print_at(
1815 $self->offset + $self->base_size + 2 * $e->byte_size,
1816 pack( $StP{$e->byte_size}, $num ),
1820 package DBM::Deep::Engine::Sector::BucketList;
1822 our @ISA = qw( DBM::Deep::Engine::Sector );
1827 my $engine = $self->engine;
1829 unless ( $self->offset ) {
1830 my $leftover = $self->size - $self->base_size;
1832 $self->{offset} = $engine->_request_blist_sector( $self->size );
1833 $engine->storage->print_at( $self->offset, $engine->SIG_BLIST ); # Sector type
1834 # Skip staleness counter
1835 $engine->storage->print_at( $self->offset + $self->base_size,
1836 chr(0) x $leftover, # Zero-fill the data
1840 if ( $self->{key_md5} ) {
1849 $self->engine->storage->print_at( $self->offset + $self->base_size,
1850 chr(0) x ($self->size - $self->base_size), # Zero-fill the data
1856 unless ( $self->{size} ) {
1857 my $e = $self->engine;
1858 # Base + numbuckets * bucketsize
1859 $self->{size} = $self->base_size + $e->max_buckets * $self->bucket_size;
1861 return $self->{size};
1864 sub free_meth { return '_add_free_blist_sector' }
1869 my $e = $self->engine;
1870 foreach my $bucket ( $self->chopped_up ) {
1871 my $rest = $bucket->[-1];
1873 # Delete the keysector
1874 my $l = unpack( $StP{$e->byte_size}, substr( $rest, $e->hash_size, $e->byte_size ) );
1875 my $s = $e->_load_sector( $l ); $s->free if $s;
1877 # Delete the HEAD sector
1878 $l = unpack( $StP{$e->byte_size},
1880 $e->hash_size + $e->byte_size,
1884 $s = $e->_load_sector( $l ); $s->free if $s;
1886 foreach my $txn ( 0 .. $e->num_txns - 2 ) {
1887 my $l = unpack( $StP{$e->byte_size},
1889 $e->hash_size + 2 * $e->byte_size + $txn * ($e->byte_size + $STALE_SIZE),
1893 my $s = $e->_load_sector( $l ); $s->free if $s;
1897 $self->SUPER::free();
1902 unless ( $self->{bucket_size} ) {
1903 my $e = $self->engine;
1904 # Key + head (location) + transactions (location + staleness-counter)
1905 my $location_size = $e->byte_size + $e->byte_size + ($e->num_txns - 1) * ($e->byte_size + $STALE_SIZE);
1906 $self->{bucket_size} = $e->hash_size + $location_size;
1908 return $self->{bucket_size};
1911 # XXX This is such a poor hack. I need to rethink this code.
1915 my $e = $self->engine;
1918 foreach my $idx ( 0 .. $e->max_buckets - 1 ) {
1919 my $spot = $self->offset + $self->base_size + $idx * $self->bucket_size;
1920 my $md5 = $e->storage->read_at( $spot, $e->hash_size );
1922 #XXX If we're chopping, why would we ever have the blank_md5?
1923 last if $md5 eq $e->blank_md5;
1925 my $rest = $e->storage->read_at( undef, $self->bucket_size - $e->hash_size );
1926 push @buckets, [ $spot, $md5 . $rest ];
1932 sub write_at_next_open {
1936 #XXX This is such a hack!
1937 $self->{_next_open} = 0 unless exists $self->{_next_open};
1939 my $spot = $self->offset + $self->base_size + $self->{_next_open}++ * $self->bucket_size;
1940 $self->engine->storage->print_at( $spot, $entry );
1947 unless ( exists $self->{found} ) {
1950 return $self->{found};
1956 $self->{found} = undef;
1960 $self->{key_md5} = shift;
1963 # If we don't have an MD5, then what are we supposed to do?
1964 unless ( exists $self->{key_md5} ) {
1965 DBM::Deep->_throw_error( "Cannot find_md5 without a key_md5 set" );
1968 my $e = $self->engine;
1969 foreach my $idx ( 0 .. $e->max_buckets - 1 ) {
1970 my $potential = $e->storage->read_at(
1971 $self->offset + $self->base_size + $idx * $self->bucket_size, $e->hash_size,
1974 if ( $potential eq $e->blank_md5 ) {
1975 $self->{idx} = $idx;
1979 if ( $potential eq $self->{key_md5} ) {
1981 $self->{idx} = $idx;
1993 DBM::Deep->_throw_error( "write_md5: no key" ) unless exists $args->{key};
1994 DBM::Deep->_throw_error( "write_md5: no key_md5" ) unless exists $args->{key_md5};
1995 DBM::Deep->_throw_error( "write_md5: no value" ) unless exists $args->{value};
1997 my $engine = $self->engine;
1999 $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id};
2001 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
2002 $engine->add_entry( $args->{trans_id}, $spot );
2004 unless ($self->{found}) {
2005 my $key_sector = DBM::Deep::Engine::Sector::Scalar->new({
2007 data => $args->{key},
2010 $engine->storage->print_at( $spot,
2012 pack( $StP{$engine->byte_size}, $key_sector->offset ),
2017 + $engine->hash_size
2018 + $engine->byte_size;
2020 if ( $args->{trans_id} ) {
2021 $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE );
2023 $engine->storage->print_at( $loc,
2024 pack( $StP{$engine->byte_size}, $args->{value}->offset ),
2025 pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ),
2029 $engine->storage->print_at( $loc,
2030 pack( $StP{$engine->byte_size}, $args->{value}->offset ),
2040 my $engine = $self->engine;
2042 $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id};
2044 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
2045 $engine->add_entry( $args->{trans_id}, $spot );
2048 + $engine->hash_size
2049 + $engine->byte_size;
2051 if ( $args->{trans_id} ) {
2052 $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE );
2054 $engine->storage->print_at( $loc,
2055 pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted
2056 pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ),
2060 $engine->storage->print_at( $loc,
2061 pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted
2071 my $engine = $self->engine;
2072 return undef unless $self->{found};
2074 # Save the location so that we can free the data
2075 my $location = $self->get_data_location_for({
2078 my $key_sector = $self->get_key_for;
2080 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
2081 $engine->storage->print_at( $spot,
2082 $engine->storage->read_at(
2083 $spot + $self->bucket_size,
2084 $self->bucket_size * ( $engine->max_buckets - $self->{idx} - 1 ),
2086 chr(0) x $self->bucket_size,
2091 my $data_sector = $self->engine->_load_sector( $location );
2092 my $data = $data_sector->data;
2098 sub get_data_location_for {
2103 $args->{allow_head} = 0 unless exists $args->{allow_head};
2104 $args->{trans_id} = $self->engine->trans_id unless exists $args->{trans_id};
2105 $args->{idx} = $self->{idx} unless exists $args->{idx};
2107 my $e = $self->engine;
2109 my $spot = $self->offset + $self->base_size
2110 + $args->{idx} * $self->bucket_size
2114 if ( $args->{trans_id} ) {
2115 $spot += $e->byte_size + ($args->{trans_id} - 1) * ( $e->byte_size + $STALE_SIZE );
2118 my $buffer = $e->storage->read_at(
2120 $e->byte_size + $STALE_SIZE,
2122 my ($loc, $staleness) = unpack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, $buffer );
2124 if ( $args->{trans_id} ) {
2125 # We have found an entry that is old, so get rid of it
2126 if ( $staleness != (my $s = $e->get_txn_staleness_counter( $args->{trans_id} ) ) ) {
2127 $e->storage->print_at(
2129 pack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ),
2135 # If we're in a transaction and we never wrote to this location, try the
2137 if ( $args->{trans_id} && !$loc && $args->{allow_head} ) {
2138 return $self->get_data_location_for({
2141 idx => $args->{idx},
2144 return $loc <= 1 ? 0 : $loc;
2152 return unless $self->{found};
2153 my $location = $self->get_data_location_for({
2154 allow_head => $args->{allow_head},
2156 return $self->engine->_load_sector( $location );
2162 $idx = $self->{idx} unless defined $idx;
2164 if ( $idx >= $self->engine->max_buckets ) {
2165 DBM::Deep->_throw_error( "get_key_for(): Attempting to retrieve $idx" );
2168 my $location = $self->engine->storage->read_at(
2169 $self->offset + $self->base_size + $idx * $self->bucket_size + $self->engine->hash_size,
2170 $self->engine->byte_size,
2172 $location = unpack( $StP{$self->engine->byte_size}, $location );
2173 DBM::Deep->_throw_error( "get_key_for: No location?" ) unless $location;
2175 return $self->engine->_load_sector( $location );
2178 package DBM::Deep::Engine::Sector::Index;
2180 our @ISA = qw( DBM::Deep::Engine::Sector );
2185 my $engine = $self->engine;
2187 unless ( $self->offset ) {
2188 my $leftover = $self->size - $self->base_size;
2190 $self->{offset} = $engine->_request_index_sector( $self->size );
2191 $engine->storage->print_at( $self->offset, $engine->SIG_INDEX ); # Sector type
2192 # Skip staleness counter
2193 $engine->storage->print_at( $self->offset + $self->base_size,
2194 chr(0) x $leftover, # Zero-fill the rest
2204 unless ( $self->{size} ) {
2205 my $e = $self->engine;
2206 $self->{size} = $self->base_size + $e->byte_size * $e->hash_chars;
2208 return $self->{size};
2211 sub free_meth { return '_add_free_index_sector' }
2215 my $e = $self->engine;
2217 for my $i ( 0 .. $e->hash_chars - 1 ) {
2218 my $l = $self->get_entry( $i ) or next;
2219 $e->_load_sector( $l )->free;
2222 $self->SUPER::free();
2228 return $self->offset + $self->base_size + $idx * $self->engine->byte_size;
2235 my $e = $self->engine;
2237 DBM::Deep->_throw_error( "get_entry: Out of range ($idx)" )
2238 if $idx < 0 || $idx >= $e->hash_chars;
2241 $StP{$e->byte_size},
2242 $e->storage->read_at( $self->_loc_for( $idx ), $e->byte_size ),
2248 my ($idx, $loc) = @_;
2250 my $e = $self->engine;
2252 DBM::Deep->_throw_error( "set_entry: Out of range ($idx)" )
2253 if $idx < 0 || $idx >= $e->hash_chars;
2255 $self->engine->storage->print_at(
2256 $self->_loc_for( $idx ),
2257 pack( $StP{$e->byte_size}, $loc ),
2261 # This was copied from MARCEL's Class::Null. However, I couldn't use it because
2262 # I need an undef value, not an implementation of the Null Class pattern.
2263 package DBM::Deep::Null;
2266 'bool' => sub { undef },
2267 '""' => sub { undef },
2268 '0+' => sub { undef },
2270 nomethod => 'AUTOLOAD';
2272 sub AUTOLOAD { return; }