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;
}
});
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 {
);
}
-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 );
}
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" );
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
##