Subject: | Secondary indices not cleared aftter deleting objects (KiokudDB::Backend::DBD::GIN) |
Secondary indices not cleared aftter deleting objects (KiokudDB::Backend::DBD::GIN)
perl version: 5.8.8
os version: Linux fireball 2.6.18-6-686 #1 SMP Tue Jun 17 21:31:27 UTC 2008 i686 GNU/Linux
KiokuDB version: 0.27
KiokiDB::Backend::BDB version: 0.14
BerkeleyDB (perl package) version: 0.38
BerkeleyDB version: 4.7.25
Execute following code:
{
package App::Object;
use Moose;
has indexed_field => (
is => 'rw',
isa => 'Int'
);
__PACKAGE__->meta->make_immutable;
1;
}
package main;
use KiokuDB;
use Search::GIN::Extract::Callback;
my $dir = KiokuDB->connect(
"bdb-gin:dir=~/tmp/db/",
create => 1,
extract => Search::GIN::Extract::Callback->new(extract =>
sub {
my ( $object, $extractor, @args) = @_;
my $index = {};
if ( $object->isa('App::Object') ){
$index->{indexed_field} = $object->indexed_field;
}
return $index;
})
);
{
my $scope = $dir->new_scope;
my $obj = new App::Object indexed_field => 123;
$dir->store($obj);
$dir->delete($obj);
}
Then in shell:
# ~/bin/db_dump -p gin_index
VERSION=3
format=print
type=btree
duplicates=1
dupsort=1
db_pagesize=4096
HEADER=END
indexed_field:123
E41BB608-4A08-11DE-8F2D-9288FFEE0A87
DATA=END
# ~/bin/db_dump -p gin_index
VERSION=3
format=print
type=btree
duplicates=1
dupsort=1
db_pagesize=4096
HEADER=END
indexed_field:123
E41BB608-4A08-11DE-8F2D-9288FFEE0A87
DATA=END
Simplest way i found to fix this bug is to add method modifiler (before delete).
In KiokuDB::Backend::DBD::GIN :
before delete => sub
{
my ( $self, @entries ) = @_;
if ( $self->extract ) {
foreach my $entry ( @entries ) {
if ($entry->has_object){
my @keys = $self->extract_values( $entry->object, entry => $entry );
if ( @keys ) {
my $d = $entry->backend_data || $entry->backend_data({});
$d->{keys} = \@keys;
$keys{$entry->id} = \@keys;
}
}
}
}
};
Possible there is better way to fix this problem.