From: rkinyon Date: Wed, 29 Nov 2006 04:24:15 +0000 (+0000) Subject: Added delete X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e86cef3618b604b18989c11f9b473028805c6769;p=dbsrgits%2FDBM-Deep.git Added delete --- diff --git a/lib/DBM/Deep/Engine3.pm b/lib/DBM/Deep/Engine3.pm index cb9fd0e..6c4341b 100644 --- a/lib/DBM/Deep/Engine3.pm +++ b/lib/DBM/Deep/Engine3.pm @@ -119,6 +119,7 @@ sub read_value { die "How did this fail (no blist)?!\n" unless $blist; my $value_sector = $blist->get_data_for( $key_md5 ); + return if ! $value_sector; return $value_sector->data; } @@ -139,12 +140,26 @@ sub key_exists { }); die "How did this fail (no blist)?!\n" unless $blist; - my $value_sector = $blist->get_data_for( $key_md5 ); + # exists() returns 1 or '' for true/false. + return $blist->has_md5( $key_md5 ) ? 1 : ''; } sub delete_key { my $self = shift; my ($trans_id, $base_offset, $key) = @_; + + my $sector = $self->_load_sector( $base_offset ); + die "How did this fail (no sector for '$base_offset')?!\n" unless $sector; + + my $key_md5 = $self->_apply_digest( $key ); + + # XXX What should happen if this fails? + my $blist = $sector->get_bucket_list({ + key_md5 => $key_md5, + }); + die "How did this fail (no blist)?!\n" unless $blist; + + return $blist->delete_md5( $key_md5 ); } sub write_value { @@ -591,16 +606,39 @@ sub write_md5 { ); } -sub get_data_for { +sub delete_md5 { my $self = shift; my ($md5) = @_; + my $engine = $self->engine; my ($found, $idx) = $self->find_md5( $md5 ); - return unless $found; + return 1 unless $found; + my $location = $self->get_location_for( $idx ); + $engine->storage->print_at( $self->offset + $self->base_size + $idx * $self->bucket_size, + $self->engine->blank_md5, # The blank MD5 + pack( $StP{$engine->byte_size}, 0 ), # The pointer to the data in the HEAD + ); + + return 1; +} + +sub get_location_for { + my $self = shift; + my ($idx) = @_; + my $location = $self->engine->storage->read_at( $self->offset + $self->base_size + $idx * $self->bucket_size + $self->engine->hash_size, $self->engine->byte_size, ); - $location = unpack( $StP{$self->engine->byte_size}, $location ); + return unpack( $StP{$self->engine->byte_size}, $location ); +} + +sub get_data_for { + my $self = shift; + my ($md5) = @_; + + my ($found, $idx) = $self->find_md5( $md5 ); + return unless $found; + my $location = $self->get_location_for( $idx ); return $self->engine->_load_sector( $location ); } diff --git a/t/02_hash.t b/t/02_hash.t index da9f221..19e26de 100644 --- a/t/02_hash.t +++ b/t/02_hash.t @@ -33,7 +33,6 @@ is( $db->{key3}, 'value3', "... and hash-access also works" ); is( $db->{key1}, "value1", "Key1 is still correct" ); is( $db->{key2}, undef, "Key2 is still correct" ); is( $db->{key3}, 'value3', "Key3 is still correct" ); -__END__ ok( $db->exists("key1"), "exists() function works" ); ok( exists $db->{key2}, "exists() works against tied hash" ); @@ -44,9 +43,10 @@ TODO: { local $TODO = "Autovivification isn't correct yet"; ok( exists $db->{key4}, "Autovivified key4 now exists" ); } + delete $db->{key4}; ok( !exists $db->{key4}, "And key4 doesn't exists anymore" ); - +__END__ ## # count keys ##