From: rkinyon Date: Wed, 27 Dec 2006 04:51:11 +0000 (+0000) Subject: Parameterized the freespace management to allow for new sector types more easily... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c0507636bb3275a41067bc1b6feb4ce822bc168a;p=dbsrgits%2FDBM-Deep.git Parameterized the freespace management to allow for new sector types more easily - clunky, but it works for now --- diff --git a/lib/DBM/Deep/Engine3.pm b/lib/DBM/Deep/Engine3.pm index 0da89a8..7270a67 100644 --- a/lib/DBM/Deep/Engine3.pm +++ b/lib/DBM/Deep/Engine3.pm @@ -642,24 +642,20 @@ sub _apply_digest { return $self->{digest}->(@_); } -#XXX Parameterize the staleness counter stuff here +sub _add_free_blist_sector { shift->_add_free_sector( 0, @_ ) } +sub _add_free_data_sector { shift->_add_free_sector( 1, @_ ) } +sub _add_free_index_sector { shift->_add_free_sector( 2, @_ ) } + sub _add_free_sector { my $self = shift; - my ($offset, $size) = @_; + my ($multiple, $offset, $size) = @_; - my $chains_offset; - # Data sector - if ( $size == 256 ) { - $chains_offset = $self->byte_size; - } - # Blist sector - else { - $chains_offset = 0; - } + my $chains_offset = $multiple * $self->byte_size; my $storage = $self->storage; # 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 ) ); @@ -674,19 +670,15 @@ sub _add_free_sector { $storage->print_at( $offset + SIG_SIZE + STALE_SIZE, $old_head ); } +sub _request_blist_sector { shift->_request_sector( 0, @_ ) } +sub _request_data_sector { shift->_request_sector( 1, @_ ) } +sub _request_index_sector { shift->_request_sector( 2, @_ ) } + sub _request_sector { my $self = shift; - my ($size) = @_; + my ($multiple, $size) = @_; - my $chains_offset; - # Data sector - if ( $size == 256 ) { - $chains_offset = $self->byte_size; - } - # Blist sector - else { - $chains_offset = 0; - } + my $chains_offset = $multiple * $self->byte_size; my $old_head = $self->storage->read_at( $self->chains_loc + $chains_offset, $self->byte_size ); my $loc = unpack( $StP{$self->byte_size}, $old_head ); @@ -842,9 +834,8 @@ sub free { chr(0) x ($self->size - $self->base_size), ); - $e->_add_free_sector( - $self->offset, $self->size, - ); + my $free_meth = $self->free_meth; + $e->$free_meth( $self->offset, $self->size ); return; } @@ -855,6 +846,7 @@ our @ISA = qw( DBM::Deep::Engine::Sector ); # This is in bytes sub size { return 256 } +sub free_meth { return '_add_free_data_sector' } sub clone { my $self = shift; @@ -892,7 +884,7 @@ sub _init { unless ( $self->offset ) { my $data_section = $self->size - $self->base_size - 1 * $engine->byte_size - 1; - $self->{offset} = $engine->_request_sector( $self->size ); + $self->{offset} = $engine->_request_data_sector( $self->size ); my $data = delete $self->{data}; my $dlen = length $data; @@ -909,7 +901,7 @@ sub _init { $chunk = substr( $data, 0, $this_len ); $dlen -= $data_section; - $next_offset = $engine->_request_sector( $self->size ); + $next_offset = $engine->_request_data_sector( $self->size ); $data = substr( $data, $this_len ); } else { @@ -989,7 +981,7 @@ sub _init { unless ( $self->offset ) { my $leftover = $self->size - $self->base_size - 1 * $engine->byte_size - 1; - $self->{offset} = $engine->_request_sector( $self->size ); + $self->{offset} = $engine->_request_data_sector( $self->size ); $engine->storage->print_at( $self->offset, $self->type ); # Sector type # Skip staleness counter $engine->storage->print_at( $self->offset + $self->base_size, @@ -1024,13 +1016,13 @@ sub _init { $class_offset = $class_sector->offset; } - $self->{offset} = $e->_request_sector( $self->size ); + $self->{offset} = $e->_request_data_sector( $self->size ); $e->storage->print_at( $self->offset, $self->type ); # Sector type # Skip staleness counter $e->storage->print_at( $self->offset + $self->base_size, pack( $StP{$e->byte_size}, 0 ), # Index/BList loc pack( $StP{$e->byte_size}, $class_offset ), # Classname loc - chr(0) x $leftover, # Zero-fill the rest + chr(0) x $leftover, # Zero-fill the rest ); } else { @@ -1263,7 +1255,7 @@ sub _init { unless ( $self->offset ) { my $leftover = $self->size - $self->base_size; - $self->{offset} = $engine->_request_sector( $self->size ); + $self->{offset} = $engine->_request_blist_sector( $self->size ); $engine->storage->print_at( $self->offset, $engine->SIG_BLIST ); # Sector type # Skip staleness counter $engine->storage->print_at( $self->offset + $self->base_size, @@ -1287,6 +1279,8 @@ sub size { return $self->{size}; } +sub free_meth { return '_add_free_blist_sector' } + sub bucket_size { my $self = shift; unless ( $self->{bucket_size} ) { @@ -1514,7 +1508,7 @@ sub _init { unless ( $self->offset ) { my $leftover = $self->size - $self->base_size; - $self->{offset} = $engine->_request_sector( $self->size ); + $self->{offset} = $engine->_request_index_sector( $self->size ); $engine->storage->print_at( $self->offset, $engine->SIG_BLIST ); # Sector type # Skip staleness counter $engine->storage->print_at( $self->offset + $self->base_size, @@ -1534,5 +1528,7 @@ sub size { return $self->{size}; } +sub free_meth { return '_add_free_index_sector' } + 1; __END__