On Thu, Sep 08, 2011 at 11:47:36AM -0700, Ashley Pond V wrote:
> I have a Moose class that is using attribute traits to automatically
> bundle up a lot of ordinary but complex KinoSearch/Lucy behavior.
> Something blocking me from automating the indexer -> add_doc x N ->
> commit cycle is that there seems to be no way (from the Perl
> interface) to get an indexer to say whether it has already been
> committed or not. The idea being the meta object has indexers,
> searchers, and the schema embedded in it so it knows how to compose
> all kinds of things like add_doc. For it to do it sanely it must know
> its current indexers state for every meta->add_doc call so it can
> reuse its indexer or instantiate a new one.
Sounds vaguely similar to the code in LucyX::Simple, so maybe check that out
if you're looking for ideas.
> Is there a way (or a best practice if there is more than one) for
> getting an index to give its status? It appears that ->commit clears
> the index lock so I suppose the short form for knowing if an indexer
> hasn't been committed is to see if it still has a lock. But there is
> more than one kind of lock and it also seems short on perl-side
> introspection so I'm guessing it's the wrong way to go.
You could check for the lock with something like this:
my $folder = Lucy::Store::FSFolder->new(
path => '/path/to/index',
);
my $lock_factory = Lucy::Store::LockFactory->new(
folder => $folder,
host => $hostname,
);
my $write_lock = $lock_factory->make_lock(
name => 'write',
timeout => 1000,
interval => 100,
);
my $locked = $write_lock->is_locked;
However, it seems to me that a better solution for your specific problem is to
subclass Indexer and use an inside-out field to track its state.
This code sample adds a method, $indexer->committed, which returns true after
$indexer->commit has been called:
package MyIndexer;
use base qw( Lucy::Index::Indexer );
our %committed;
sub new {
my $self = shift->SUPER::new(@_);
$committed{$self} = 0;
}
sub commit {
my $self = shift;
$self->SUPER::commit(@_);
$committed{$self} = 1;
}
sub DESTROY {
my $self = shift;
delete $committed{$self};
$self->SUPER::DESTROY;
}
sub committed {
my $self = shift;
return $committed{$self};
}
More sophisticated tracking could be accomplished by storing something other
than a boolean value.
Hope this helps,
Marvin Humphrey