From: Rob Kinyon Date: Sat, 5 Dec 2009 02:21:50 +0000 (-0500) Subject: Converted ->load calls into Engine::->load_sector() calls in order to allow better... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d6ecf579f089dce02e0e872e65ad1d242bfe5018;p=dbsrgits%2FDBM-Deep.git Converted ->load calls into Engine::->load_sector() calls in order to allow better encapsulation and pluggability --- diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index 276df95..40895dc 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -337,5 +337,30 @@ sub flush { return; } +=head2 load_sector( $loc ) + +This takes an id/location/offset and loads the sector based on the engine's +defined sector type. + +=cut + +sub load_sector { $_[0]->sector_type->load( @_ ) } + +=head2 ACCESSORS + +The following are readonly attributes. + +=over 4 + +=item * storage + +=back + +=cut + +sub storage { $_[0]{storage} } + +sub sector_type { die "sector_type must be implemented in a child class" } + 1; __END__ diff --git a/lib/DBM/Deep/Engine/DBI.pm b/lib/DBM/Deep/Engine/DBI.pm index ebd5f52..434fafe 100644 --- a/lib/DBM/Deep/Engine/DBI.pm +++ b/lib/DBM/Deep/Engine/DBI.pm @@ -7,6 +7,13 @@ use warnings FATAL => 'all'; use base 'DBM::Deep::Engine'; +use DBM::Deep::Sector::DBI (); +use DBM::Deep::Storage::DBI (); + +sub sector_type { 'DBM::Deep::Sector::DBI' } + +__END__ + sub read_value { my $self = shift; my ($obj, $key) = @_; diff --git a/lib/DBM/Deep/Engine/File.pm b/lib/DBM/Deep/Engine/File.pm index 878f436..df8cc4e 100644 --- a/lib/DBM/Deep/Engine/File.pm +++ b/lib/DBM/Deep/Engine/File.pm @@ -13,6 +13,8 @@ use DBM::Deep::Null (); use DBM::Deep::Sector::File (); use DBM::Deep::Storage::File (); +sub sector_type { 'DBM::Deep::Sector::File' } + my $STALE_SIZE = 2; # Please refer to the pack() documentation for further information @@ -121,7 +123,7 @@ sub read_value { my ($obj, $key) = @_; # This will be a Reference sector - my $sector = DBM::Deep::Sector::File->load( $self, $obj->_base_offset ) + my $sector = $self->load_sector( $obj->_base_offset ) or return; if ( $sector->staleness != $obj->_staleness ) { @@ -156,7 +158,7 @@ sub get_classname { my ($obj) = @_; # This will be a Reference sector - my $sector = DBM::Deep::Sector::File->load( $self, $obj->_base_offset ) + my $sector = $self->load_sector( $obj->_base_offset ) or DBM::Deep->_throw_error( "How did get_classname fail (no sector for '$obj')?!" ); if ( $sector->staleness != $obj->_staleness ) { @@ -171,7 +173,7 @@ sub make_reference { my ($obj, $old_key, $new_key) = @_; # This will be a Reference sector - my $sector = DBM::Deep::Sector::File->load( $self, $obj->_base_offset ) + my $sector = $self->load_sector( $obj->_base_offset ) or DBM::Deep->_throw_error( "How did make_reference fail (no sector for '$obj')?!" ); if ( $sector->staleness != $obj->_staleness ) { @@ -222,7 +224,7 @@ sub key_exists { my ($obj, $key) = @_; # This will be a Reference sector - my $sector = DBM::Deep::Sector::File->load( $self, $obj->_base_offset ) + my $sector = $self->load_sector( $obj->_base_offset ) or return ''; if ( $sector->staleness != $obj->_staleness ) { @@ -242,7 +244,7 @@ sub delete_key { my $self = shift; my ($obj, $key) = @_; - my $sector = DBM::Deep::Sector::File->load( $self, $obj->_base_offset ) + my $sector = $self->load_sector( $obj->_base_offset ) or return; if ( $sector->staleness != $obj->_staleness ) { @@ -271,7 +273,7 @@ sub write_value { } # This will be a Reference sector - my $sector = DBM::Deep::Sector::File->load( $self, $obj->_base_offset ) + my $sector = $self->load_sector( $obj->_base_offset ) or DBM::Deep->_throw_error( "Cannot write to a deleted spot in DBM::Deep." ); if ( $sector->staleness != $obj->_staleness ) { @@ -313,7 +315,7 @@ sub write_value { } #XXX Can this use $loc? - my $value_sector = DBM::Deep::Sector::File->load( $self, $tmpvar->_base_offset ); + my $value_sector = $self->load_sector( $tmpvar->_base_offset ); $sector->write_data({ key => $key, key_md5 => $self->_apply_digest( $key ), @@ -480,7 +482,7 @@ sub rollback { $self->storage->print_at( $read_loc, pack( $StP{$self->byte_size}, 0 ) ); if ( $data_loc > 1 ) { - DBM::Deep::Sector::File->load( $self, $data_loc )->free; + $self->load_sector( $data_loc )->free; } } @@ -524,7 +526,7 @@ sub commit { ); if ( $head_loc > 1 ) { - DBM::Deep::Sector::File->load( $self, $head_loc )->free; + $self->load_sector( $head_loc )->free; } } @@ -948,8 +950,6 @@ The following are readonly attributes. =over 4 -=item * storage - =item * byte_size =item * hash_size @@ -970,7 +970,6 @@ The following are readonly attributes. =cut -sub storage { $_[0]{storage} } sub byte_size { $_[0]{byte_size} } sub hash_size { $_[0]{hash_size} } sub hash_chars { $_[0]{hash_chars} } @@ -1076,7 +1075,7 @@ sub _dump_file { SECTOR: while ( $spot < $self->storage->{end} ) { # Read each sector in order. - my $sector = DBM::Deep::Sector::File->load( $self, $spot ); + my $sector = $self->load_sector( $spot ); if ( !$sector ) { # Find it in the free-sectors that were found already foreach my $type ( keys %sectors ) { diff --git a/lib/DBM/Deep/Iterator.pm b/lib/DBM/Deep/Iterator.pm index 7c28b6f..26e81a2 100644 --- a/lib/DBM/Deep/Iterator.pm +++ b/lib/DBM/Deep/Iterator.pm @@ -78,7 +78,7 @@ sub get_sector_iterator { my $self = shift; my ($loc) = @_; - my $sector = DBM::Deep::Sector::File->load( $self->{engine}, $loc ) + my $sector = $self->{engine}->load_sector( $loc ) or return; if ( $sector->isa( 'DBM::Deep::Sector::File::Index' ) ) { @@ -110,7 +110,7 @@ sub get_next_key { unless ( @$crumbs ) { # This will be a Reference sector - my $sector = DBM::Deep::Sector::File->load( $e, $self->{base_offset} ) + my $sector = $e->load_sector( $self->{base_offset} ) # If no sector is found, this must have been deleted from under us. or return; diff --git a/lib/DBM/Deep/Sector/DBI.pm b/lib/DBM/Deep/Sector/DBI.pm new file mode 100644 index 0000000..8737207 --- /dev/null +++ b/lib/DBM/Deep/Sector/DBI.pm @@ -0,0 +1,11 @@ +package DBM::Deep::Sector::DBI; + +use 5.006_000; + +use strict; +use warnings FATAL => 'all'; + +use base 'DBM::Deep::Sector'; + +1; +__END__ diff --git a/lib/DBM/Deep/Sector/File/BucketList.pm b/lib/DBM/Deep/Sector/File/BucketList.pm index 349fa3a..d218d0d 100644 --- a/lib/DBM/Deep/Sector/File/BucketList.pm +++ b/lib/DBM/Deep/Sector/File/BucketList.pm @@ -68,7 +68,7 @@ sub free { # Delete the keysector my $l = unpack( $StP{$e->byte_size}, substr( $rest, $e->hash_size, $e->byte_size ) ); - my $s = DBM::Deep::Sector::File->load( $e, $l ); $s->free if $s; + my $s = $e->load_sector( $l ); $s->free if $s; # Delete the HEAD sector $l = unpack( $StP{$e->byte_size}, @@ -77,7 +77,7 @@ sub free { $e->byte_size, ), ); - $s = DBM::Deep::Sector::File->load( $e, $l ); $s->free if $s; + $s = $e->load_sector( $l ); $s->free if $s; foreach my $txn ( 0 .. $e->num_txns - 2 ) { my $l = unpack( $StP{$e->byte_size}, @@ -86,7 +86,7 @@ sub free { $e->byte_size, ), ); - my $s = DBM::Deep::Sector::File->load( $e, $l ); $s->free if $s; + my $s = $e->load_sector( $l ); $s->free if $s; } } @@ -283,7 +283,7 @@ sub delete_md5 { $key_sector->free; - my $data_sector = DBM::Deep::Sector::File->load( $self->engine, $location ); + my $data_sector = $self->engine->load_sector( $location ); my $data = $data_sector->data({ export => 1 }); $data_sector->free; @@ -350,7 +350,7 @@ sub get_data_for { my $location = $self->get_data_location_for({ allow_head => $args->{allow_head}, }); - return DBM::Deep::Sector::File->load( $self->engine, $location ); + return $self->engine->load_sector( $location ); } sub get_key_for { @@ -369,7 +369,7 @@ sub get_key_for { $location = unpack( $StP{$self->engine->byte_size}, $location ); DBM::Deep->_throw_error( "get_key_for: No location?" ) unless $location; - return DBM::Deep::Sector::File->load( $self->engine, $location ); + return $self->engine->load_sector( $location ); } 1; diff --git a/lib/DBM/Deep/Sector/File/Index.pm b/lib/DBM/Deep/Sector/File/Index.pm index de0fa73..7fd14f2 100644 --- a/lib/DBM/Deep/Sector/File/Index.pm +++ b/lib/DBM/Deep/Sector/File/Index.pm @@ -49,7 +49,7 @@ sub free { for my $i ( 0 .. $e->hash_chars - 1 ) { my $l = $self->get_entry( $i ) or next; - DBM::Deep::Sector::File->load( $e, $l )->free; + $e->load_sector( $l )->free; } $self->SUPER::free(); diff --git a/lib/DBM/Deep/Sector/File/Reference.pm b/lib/DBM/Deep/Sector/File/Reference.pm index e86bf23..e66a1f5 100644 --- a/lib/DBM/Deep/Sector/File/Reference.pm +++ b/lib/DBM/Deep/Sector/File/Reference.pm @@ -93,7 +93,7 @@ sub get_data_for { my $location = $self->get_data_location_for( $args ) or return; - return DBM::Deep::Sector::File->load( $self->engine, $location ); + return $self->engine->load_sector( $location ); } sub write_data { @@ -175,7 +175,7 @@ sub delete_key { my $location = $blist->get_data_location_for({ allow_head => 0, }); - my $old_value = $location && DBM::Deep::Sector::File->load( $self->engine, $location ); + my $old_value = $location && $self->engine->load_sector( $location ); my @trans_ids = $self->engine->get_running_txn_ids; @@ -246,7 +246,7 @@ sub get_bucket_list { return $blist; } - my $sector = DBM::Deep::Sector::File->load( $engine, $blist_loc ) + my $sector = $engine->load_sector( $blist_loc ) or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" ); my $i = 0; my $last_sector = undef; @@ -254,7 +254,7 @@ sub get_bucket_list { $blist_loc = $sector->get_entry( ord( substr( $args->{key_md5}, $i++, 1 ) ) ); $last_sector = $sector; if ( $blist_loc ) { - $sector = DBM::Deep::Sector::File->load( $engine, $blist_loc ) + $sector = $engine->load_sector( $blist_loc ) or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" ); } else { @@ -404,7 +404,7 @@ sub get_classname { return unless $class_offset; - return DBM::Deep::Sector::File->load( $self->engine, $class_offset )->data; + return $self->engine->load_sector( $class_offset )->data; } sub data { @@ -460,10 +460,10 @@ sub free { delete $self->engine->cache->{ $self->offset }; my $blist_loc = $self->get_blist_loc; - DBM::Deep::Sector::File->load( $self->engine, $blist_loc )->free if $blist_loc; + $self->engine->load_sector( $blist_loc )->free if $blist_loc; my $class_loc = $self->get_class_offset; - DBM::Deep::Sector::File->load( $self->engine, $class_loc )->free if $class_loc; + $self->engine->load_sector( $class_loc )->free if $class_loc; $self->SUPER::free(); } diff --git a/lib/DBM/Deep/Sector/File/Scalar.pm b/lib/DBM/Deep/Sector/File/Scalar.pm index bfbae22..7dfa041 100644 --- a/lib/DBM/Deep/Sector/File/Scalar.pm +++ b/lib/DBM/Deep/Sector/File/Scalar.pm @@ -25,7 +25,7 @@ sub free { $self->SUPER::free(); if ( $chain_loc ) { - DBM::Deep::Sector::File->load( $self->engine, $chain_loc )->free; + $self->engine->load_sector( $chain_loc )->free; } return; @@ -120,7 +120,7 @@ sub data { last unless $chain_loc; - $self = DBM::Deep::Sector::File->load( $self->engine, $chain_loc ); + $self = $self->engine->load_sector( $chain_loc ); } return $data;