From: rkinyon Date: Tue, 30 Jan 2007 04:36:02 +0000 (+0000) Subject: Transactional staleness counters are down from 4 bytes to 2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ac6daed3ba8f98fec100f622f993e146bdad6995;p=dbsrgits%2FDBM-Deep.git Transactional staleness counters are down from 4 bytes to 2 --- diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index 13f29fa..4f4e286 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -26,7 +26,8 @@ sub SIG_INDEX () { 'I' } sub SIG_BLIST () { 'B' } sub SIG_FREE () { 'F' } sub SIG_SIZE () { 1 } -sub STALE_SIZE () { 1 } + +my $STALE_SIZE = 2; # Please refer to the pack() documentation for further information my %StP = ( @@ -390,7 +391,7 @@ sub rollback { + $self->hash_size + $self->byte_size + $self->byte_size - + ($self->trans_id - 1) * ( $self->byte_size + 4 ); + + ($self->trans_id - 1) * ( $self->byte_size + $STALE_SIZE ); my $data_loc = $self->storage->read_at( $read_loc, $self->byte_size ); $data_loc = unpack( $StP{$self->byte_size}, $data_loc ); @@ -429,7 +430,7 @@ sub commit { my $head_loc = $self->storage->read_at( $base, $self->byte_size ); $head_loc = unpack( $StP{$self->byte_size}, $head_loc ); - my $spot = $base + $self->byte_size + ($self->trans_id - 1) * ( $self->byte_size + 4 ); + my $spot = $base + $self->byte_size + ($self->trans_id - 1) * ( $self->byte_size + $STALE_SIZE ); my $trans_loc = $self->storage->read_at( $spot, $self->byte_size, ); @@ -437,7 +438,7 @@ sub commit { $self->storage->print_at( $base, $trans_loc ); $self->storage->print_at( $spot, - pack( $StP{$self->byte_size} . ' N', (0) x 2 ), + pack( $StP{$self->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ), ); if ( $head_loc > 1 ) { @@ -485,9 +486,9 @@ sub get_txn_staleness_counter { # Hardcode staleness of 0 for the HEAD return 0 unless $trans_id; - return unpack( 'N', + return unpack( $StP{$STALE_SIZE}, $self->storage->read_at( - $self->trans_loc + 4 + 4 * ($trans_id - 1), + $self->trans_loc + 4 + $STALE_SIZE * ($trans_id - 1), 4, ) ); @@ -501,8 +502,8 @@ sub inc_txn_staleness_counter { return unless $trans_id; $self->storage->print_at( - $self->trans_loc + 4 + 4 * ($trans_id - 1), - pack( 'N', $self->get_txn_staleness_counter( $trans_id ) + 1 ), + $self->trans_loc + 4 + $STALE_SIZE * ($trans_id - 1), + pack( $StP{$STALE_SIZE}, $self->get_txn_staleness_counter( $trans_id ) + 1 ), ); } @@ -553,7 +554,7 @@ sub clear_entries { my $nt = $self->num_txns; - my $header_var = 1 + 1 + 1 + 1 + 4 + 4 * ($nt - 1) + 3 * $self->byte_size; + my $header_var = 1 + 1 + 1 + 1 + 4 + $STALE_SIZE * ($nt - 1) + 3 * $self->byte_size; my $loc = $self->storage->request_space( $header_fixed + $header_var ); @@ -572,7 +573,7 @@ sub clear_entries { pack('C', $nt), #XXX This is a problem - it limits everything to 32 transactions pack('N', 0 ), # Transaction activeness bitfield - pack('N' . ($nt-1), 0 x ($nt-1) ), # Transaction staleness counters + pack($StP{$STALE_SIZE}.($nt-1), 0 x ($nt-1) ), # Transaction staleness counters pack($StP{$self->byte_size}, 0), # Start of free chain (blist size) pack($StP{$self->byte_size}, 0), # Start of free chain (data size) pack($StP{$self->byte_size}, 0), # Start of free chain (index size) @@ -580,7 +581,7 @@ sub clear_entries { #XXX Set these less fragilely $self->set_trans_loc( $header_fixed + 4 ); - $self->set_chains_loc( $header_fixed + 4 + 4 + 4 * ($nt-1) ); + $self->set_chains_loc( $header_fixed + 4 + 4 + $STALE_SIZE * ($nt-1) ); return; } @@ -628,14 +629,14 @@ sub clear_entries { $self->{max_buckets} += 1; $self->{data_sector_size} += 1; - my $header_var = scalar(@values) + 4 + 4 * ($self->num_txns - 1) + 3 * $self->byte_size; + my $header_var = scalar(@values) + 4 + $STALE_SIZE * ($self->num_txns - 1) + 3 * $self->byte_size; unless ( $size == $header_var ) { $self->storage->close; DBM::Deep->_throw_error( "Unexpected size found ($size <-> $header_var)." ); } $self->set_trans_loc( $header_fixed + scalar(@values) ); - $self->set_chains_loc( $header_fixed + scalar(@values) + 4 + 4 * ($self->num_txns - 1) ); + $self->set_chains_loc( $header_fixed + scalar(@values) + 4 + $STALE_SIZE * ($self->num_txns - 1) ); return length($buffer) + length($buffer2); } @@ -714,9 +715,9 @@ sub _add_free_sector { # Increment staleness. # XXX Can this increment+modulo be done by "&= 0x1" ? - my $staleness = unpack( $StP{STALE_SIZE()}, $storage->read_at( $offset + SIG_SIZE, STALE_SIZE ) ); - $staleness = ($staleness + 1 ) % ( 2 ** ( 8 * STALE_SIZE ) ); - $storage->print_at( $offset + SIG_SIZE, pack( $StP{STALE_SIZE()}, $staleness ) ); + my $staleness = unpack( $StP{$STALE_SIZE}, $storage->read_at( $offset + SIG_SIZE, $STALE_SIZE ) ); + $staleness = ($staleness + 1 ) % ( 2 ** ( 8 * $STALE_SIZE ) ); + $storage->print_at( $offset + SIG_SIZE, pack( $StP{$STALE_SIZE}, $staleness ) ); my $old_head = $storage->read_at( $self->chains_loc + $chains_offset, $self->byte_size ); @@ -725,7 +726,7 @@ sub _add_free_sector { ); # Record the old head in the new sector after the signature and staleness counter - $storage->print_at( $offset + SIG_SIZE + STALE_SIZE, $old_head ); + $storage->print_at( $offset + SIG_SIZE + $STALE_SIZE, $old_head ); } sub _request_blist_sector { shift->_request_sector( 0, @_ ) } @@ -753,10 +754,10 @@ sub _request_sector { } # Read the new head after the signature and the staleness counter - my $new_head = $self->storage->read_at( $loc + SIG_SIZE + STALE_SIZE, $self->byte_size ); + my $new_head = $self->storage->read_at( $loc + SIG_SIZE + $STALE_SIZE, $self->byte_size ); $self->storage->print_at( $self->chains_loc + $chains_offset, $new_head ); $self->storage->print_at( - $loc + SIG_SIZE + STALE_SIZE, + $loc + SIG_SIZE + $STALE_SIZE, pack( $StP{$self->byte_size}, 0 ), ); @@ -966,7 +967,7 @@ sub type { $_[0]{type} } sub base_size { my $self = shift; - return $self->engine->SIG_SIZE + $self->engine->STALE_SIZE; + return $self->engine->SIG_SIZE + $STALE_SIZE; } sub free { @@ -1179,8 +1180,8 @@ sub _init { } $self->{staleness} = unpack( - $StP{$e->STALE_SIZE}, - $e->storage->read_at( $self->offset + $e->SIG_SIZE, $e->STALE_SIZE ), + $StP{$STALE_SIZE}, + $e->storage->read_at( $self->offset + $e->SIG_SIZE, $STALE_SIZE ), ); return; @@ -1561,9 +1562,8 @@ sub bucket_size { my $self = shift; unless ( $self->{bucket_size} ) { my $e = $self->engine; - #XXX Convert the 4 to STALE_SIZE() # Key + head (location) + transactions (location + staleness-counter) - my $location_size = $e->byte_size + $e->byte_size + ($e->num_txns - 1) * ($e->byte_size + 4); + my $location_size = $e->byte_size + $e->byte_size + ($e->num_txns - 1) * ($e->byte_size + $STALE_SIZE); $self->{bucket_size} = $e->hash_size + $location_size; } return $self->{bucket_size}; @@ -1678,13 +1678,12 @@ sub write_md5 { + $engine->hash_size + $engine->byte_size; - #XXX Convert the 4 to STALE_SIZE() if ( $args->{trans_id} ) { - $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + 4 ); + $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE ); $engine->storage->print_at( $loc, pack( $StP{$engine->byte_size}, $args->{value}->offset ), - pack( 'N', $engine->get_txn_staleness_counter( $args->{trans_id} ) ), + pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ), ); } else { @@ -1710,13 +1709,12 @@ sub mark_deleted { + $engine->hash_size + $engine->byte_size; - #XXX Convert the 4 to STALE_SIZE() if ( $args->{trans_id} ) { - $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + 4 ); + $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE ); $engine->storage->print_at( $loc, pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted - pack( 'N', $engine->get_txn_staleness_counter( $args->{trans_id} ) ), + pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ), ); } else { @@ -1774,24 +1772,22 @@ sub get_data_location_for { + $e->hash_size + $e->byte_size; - #XXX Convert the 4 to STALE_SIZE() if ( $args->{trans_id} ) { - $spot += $e->byte_size + ($args->{trans_id} - 1) * ( $e->byte_size + 4 ); + $spot += $e->byte_size + ($args->{trans_id} - 1) * ( $e->byte_size + $STALE_SIZE ); } - #XXX Convert the 4 to STALE_SIZE() my $buffer = $e->storage->read_at( $spot, - $e->byte_size + 4, + $e->byte_size + $STALE_SIZE, ); - my ($loc, $staleness) = unpack( $StP{$e->byte_size} . ' N', $buffer ); + my ($loc, $staleness) = unpack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, $buffer ); if ( $args->{trans_id} ) { # We have found an entry that is old, so get rid of it if ( $staleness != (my $s = $e->get_txn_staleness_counter( $args->{trans_id} ) ) ) { $e->storage->print_at( $spot, - pack( $StP{$e->byte_size} . ' N', (0) x 2 ), + pack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ), ); $loc = 0; }