From: rkinyon Date: Mon, 25 Dec 2006 04:03:37 +0000 (+0000) Subject: Fixed multiple transactions X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b4e1791990ce198e006a2294934e5e4e636253e0;p=dbsrgits%2FDBM-Deep.git Fixed multiple transactions --- diff --git a/lib/DBM/Deep/Engine3.pm b/lib/DBM/Deep/Engine3.pm index a64672e..0ba5add 100644 --- a/lib/DBM/Deep/Engine3.pm +++ b/lib/DBM/Deep/Engine3.pm @@ -32,7 +32,7 @@ sub SIG_SIZE () { 1 } # Please refer to the pack() documentation for further information my %StP = ( - 1 => 'C', # Unsigned char value + 1 => 'C', # Unsigned char value (no order specified, presumably ASCII) 2 => 'n', # Unsigned short in "network" (big-endian) order 4 => 'N', # Unsigned long in "network" (big-endian) order 8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent) @@ -379,9 +379,7 @@ sub commit { DBM::Deep->_throw_error( "Cannot commit without a transaction" ); } - #print "TID: " . $self->trans_id, $/; foreach my $entry (@{ $self->get_entries } ) { - #print "$entry\n"; # Overwrite the entry in head with the entry in trans_id my $base = $entry + $self->hash_size @@ -440,21 +438,28 @@ sub get_running_txn_ids { sub get_txn_staleness_counter { my $self = shift; my ($trans_id) = @_; + + # Hardcode staleness of 0 for the HEAD + return 0 unless $trans_id; + my $x = unpack( 'N', $self->storage->read_at( - $self->trans_loc + 4 * ($trans_id - 1), + $self->trans_loc + 4 * $trans_id, 4, ) ); - print "$trans_id => $x\n"; return $x; } sub inc_txn_staleness_counter { my $self = shift; my ($trans_id) = @_; + + # Hardcode staleness of 0 for the HEAD + return unless $trans_id; + $self->storage->print_at( - $self->trans_loc + 4 * ($trans_id - 1), + $self->trans_loc + 4 * $trans_id, pack( 'N', $self->get_txn_staleness_counter( $trans_id ) + 1 ), ); } @@ -468,15 +473,12 @@ sub add_entry { my $self = shift; my ($trans_id, $loc) = @_; - #print "$trans_id => $loc\n"; $self->{entries}{$trans_id} ||= {}; $self->{entries}{$trans_id}{$loc} = undef; - #use Data::Dumper;print "$self: " . Dumper $self->{entries}; } sub clear_entries { my $self = shift; - #print "Clearing\n"; delete $self->{entries}{$self->trans_id}; } @@ -500,8 +502,8 @@ sub clear_entries { # --- Above is $header_fixed. Below is $header_var pack('C', $self->byte_size), pack('C', $self->max_buckets), - pack('N', 0 ), # Transaction cctiveness bitfield - pack('N' . $self->num_txns, 0 ), # Transaction staleness counters + pack('N', 0 ), # Transaction activeness bitfield + pack('N' . $self->num_txns, 0 x $self->num_txns ), # 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) ); @@ -1024,8 +1026,10 @@ sub write_data { if ( @trans_ids ) { my $old_value = $blist->get_data_for; foreach my $other_trans_id ( @trans_ids ) { - next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 }); - #print "write_md5 to save a value\n"; + next if $blist->get_data_location_for({ + trans_id => $other_trans_id, + allow_head => 0, + }); $blist->write_md5({ trans_id => $other_trans_id, key => $args->{key}, @@ -1278,7 +1282,6 @@ sub write_md5 { $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id}; my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size; - #print "Adding $args->{trans_id} -> $spot\n"; $engine->add_entry( $args->{trans_id}, $spot ); unless ($self->{found}) { @@ -1385,7 +1388,6 @@ sub get_data_location_for { # 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} ) ) ) { - print "Killing $args->{trans_id} ( $staleness <-> $s )\n"; $e->storage->print_at( $spot, pack( $StP{$e->byte_size} . ' N', (0) x 2 ), diff --git a/t/35_transaction_multiple.t b/t/35_transaction_multiple.t index 8b52af4..e283a63 100644 --- a/t/35_transaction_multiple.t +++ b/t/35_transaction_multiple.t @@ -52,7 +52,7 @@ $db2->begin_work; is( $db1->{foo}, 'bar2', "After DB2 transaction begin, DB1's foo is still bar2" ); is( $db2->{foo}, 'bar', "After DB2 transaction begin, DB2's foo is still bar" ); is( $db3->{foo}, 'bar', "After DB2 transaction begin, DB3's foo is still bar" ); -__END__ + ok( exists $db1->{bar}, "After DB2 transaction begin, DB1's bar exists" ); ok( !exists $db2->{bar}, "After DB2 transaction begin, DB2's bar doesn't exist" ); ok( !exists $db3->{bar}, "After DB2 transaction begin, DB3's bar doesn't exist" );