From: rkinyon Date: Sun, 4 Feb 2007 04:44:14 +0000 (+0000) Subject: Cleaned up a little bit X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=37d693fa325c3bfe61cf8353ed6a25297f3aa2cd;p=dbsrgits%2FDBM-Deep.git Cleaned up a little bit --- diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index 6792fa0..663a0f0 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -465,25 +465,18 @@ sub commit { sub read_txn_slots { my $self = shift; - my $bitfield_len = ($self->num_txns) / 8; - if ( $bitfield_len > int( $bitfield_len ) ) { - $bitfield_len = int( $bitfield_len ) + 1; - } - my $num_bits = $bitfield_len * 8; + my $bl = $self->txn_bitfield_len; + my $num_bits = $bl * 8; return split '', unpack( 'b'.$num_bits, $self->storage->read_at( - $self->trans_loc, $bitfield_len, + $self->trans_loc, $bl, ) ); } sub write_txn_slots { my $self = shift; - my $bitfield_len = ($self->num_txns) / 8; - if ( $bitfield_len > int( $bitfield_len ) ) { - $bitfield_len = int( $bitfield_len ) + 1; - } - my $num_bits = $bitfield_len * 8; + my $num_bits = $self->txn_bitfield_len * 8; $self->storage->print_at( $self->trans_loc, pack( 'b'.$num_bits, join('', @_) ), ); @@ -569,12 +562,9 @@ sub clear_entries { my $self = shift; my $nt = $self->num_txns; - my $bitfield_len = ($nt) / 8; - if ( $bitfield_len > int( $bitfield_len ) ) { - $bitfield_len = int( $bitfield_len ) + 1; - } + my $bl = $self->txn_bitfield_len; - my $header_var = 1 + 1 + 1 + 1 + $bitfield_len + $STALE_SIZE * ($nt - 1) + 3 * $self->byte_size; + my $header_var = 1 + 1 + 1 + 1 + $bl + $STALE_SIZE * ($nt - 1) + 3 * $self->byte_size; my $loc = $self->storage->request_space( $header_fixed + $header_var ); @@ -591,7 +581,7 @@ sub clear_entries { pack('C', $self->data_sector_size - 1), pack('C', $nt), - pack('C' . $bitfield_len, 0 ), # Transaction activeness bitfield + pack('C' . $bl, 0 ), # Transaction activeness bitfield 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) @@ -600,7 +590,7 @@ sub clear_entries { #XXX Set these less fragilely $self->set_trans_loc( $header_fixed + 4 ); - $self->set_chains_loc( $header_fixed + 4 + $bitfield_len + $STALE_SIZE * ($nt-1) ); + $self->set_chains_loc( $header_fixed + 4 + $bl + $STALE_SIZE * ($nt-1) ); return; } @@ -648,19 +638,16 @@ sub clear_entries { $self->{max_buckets} += 1; $self->{data_sector_size} += 1; - my $bitfield_len = ($self->num_txns) / 8; - if ( $bitfield_len > int( $bitfield_len ) ) { - $bitfield_len = int( $bitfield_len ) + 1; - } + my $bl = $self->txn_bitfield_len; - my $header_var = scalar(@values) + $bitfield_len + $STALE_SIZE * ($self->num_txns - 1) + 3 * $self->byte_size; + my $header_var = scalar(@values) + $bl + $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) + $bitfield_len + $STALE_SIZE * ($self->num_txns - 1) ); + $self->set_chains_loc( $header_fixed + scalar(@values) + $bl + $STALE_SIZE * ($self->num_txns - 1) ); return length($buffer) + length($buffer2); } @@ -799,6 +786,19 @@ sub max_buckets { $_[0]{max_buckets} } sub blank_md5 { chr(0) x $_[0]->hash_size } sub data_sector_size { $_[0]{data_sector_size} } +# This is a calculated value +sub txn_bitfield_len { + my $self = shift; + unless ( exists $self->{txn_bitfield_len} ) { + my $temp = ($self->num_txns) / 8; + if ( $temp > int( $temp ) ) { + $temp = int( $temp ) + 1; + } + $self->{txn_bitfield_len} = $temp; + } + return $self->{txn_bitfield_len}; +} + sub trans_id { $_[0]{trans_id} } sub set_trans_id { $_[0]{trans_id} = $_[1] } diff --git a/t/43_transaction_maximum.t b/t/43_transaction_maximum.t index 397e6be..e8462b3 100644 --- a/t/43_transaction_maximum.t +++ b/t/43_transaction_maximum.t @@ -32,7 +32,7 @@ for my $n (0 .. $num) { $dbs[$n]->begin_work } "DB $n can begin_work"; - my $trans_id = $dbs[$n]->_engine->trans_id, $/; + my $trans_id = $dbs[$n]->_engine->trans_id; ok( !exists $trans_ids{ $trans_id }, "DB $n has a unique transaction ID ($trans_id)" ); $trans_ids{ $trans_id } = $n; }