SWISH::API::Stat - reconnect to a SWISH::API handle if index file changes


SWISH-API-Stat documentation Contained in the SWISH-API-Stat distribution.

Index


Code Index:

NAME

Top

SWISH::API::Stat - reconnect to a SWISH::API handle if index file changes

SYNOPSIS

Top

  use SWISH::API::Stat;
  my $swish = SWISH::API::Stat->new(
                    log             => $filehandle, 
                    indexes         => [ 'path/to/index' ],
                    paranoia_level  => 1
                    );

  # use just like $swish handle in SWISH::API

DESCRIPTION

Top

SWISH::API::Stat will detect if the Swish-e index(es) to which you are connected have been modified and will automatically flush the stale handle and create a new one.

SWISH::API::Stat is most useful in long-running processes where the underlying index might be merged, renamed or replaced, as in a mod_perl setup.

METHODS

Top

SWISH::API::Stat is a subclass of SWISH::API::More. See that module's documentation.

new

Create a new object.

In addition the values supported by SWISH::API::More new(), the following key/value pairs are supported:

paranoia_level( 0|1|2|3|4 )

Sets the level at which an index file will be stat()'d to find out if it has changed. The default is 1 which means that every time you access $swish the index will be stat()'d. This is most useful for when you call new_search_object(), since the time lapse between one search object and the next is usually small compared to the lifetime of a long-running process.

It's usually safe to just stay with the default. At most you'll see one query go awry, if by some chance an index changes in the middle of a query (between the time you call new_search_object() and when it is destroyed). If your indexes change often enough that too many requests are failing due to stale filehandles, you can bump the paranoia_level up to 2, which will stat() the index whenever a Search object is execute()'d. If your paranoia level is higher than that, you should be helping the author work on Swish-e version 3, which will have stable incremental indexing and avoid the stale filehandle issue altogether.

paranoia( \%class_to_level )

Get/set the mapping of class names to paranoia_levels. If you really need this, read the source.

SEE ALSO

Top

SWISH::API, SWISH::API::More, Path::Class::File::Stat

AUTHOR

Top

Peter Karman, <karman@cpan.org>

Thanks to Atomic Learning for supporting some of the development of this module.

COPYRIGHT AND LICENSE

Top


SWISH-API-Stat documentation Contained in the SWISH-API-Stat distribution.

package SWISH::API::Stat;

use strict;
use warnings;
use base qw( SWISH::API::More );
use Path::Class::File::Stat;

our $VERSION = '0.06';

__PACKAGE__->mk_accessors(
    qw(
        paranoia_level
        paranoia
        )
);

my %paranoia = (
    Search  => 1,
    Execute => 2,
    Results => 3,
    Result  => 4
);

sub init {
    my $self = shift;

    $self->{paranoia} ||= {%paranoia};
    $self->paranoia_level(1) unless defined $self->paranoia_level;

    # new() will create our handle
    # so we just have to stash our stat()s.

    my @i;
    for my $f ( @{ $self->indexes } ) {
        push( @i, Path::Class::File::Stat->new($f) );
    }

    $self->indexes( \@i );
}

sub DESTROY {
    my $self = shift;
}

sub reconnect {
    my $self = shift;
    $self->logger("re-connecting to swish-e index");
    $self->handle( @{ $self->indexes } );
}

sub check_stat {
    my $self  = shift;
    my $reset = 0;
    for my $i ( @{ $self->indexes } ) {

        $self->logger("stat'ing $i") if $self->debug;
        $reset++ if $i->changed;
    }
    $self->reconnect if $reset;
}

sub search {
    my $self = shift;

    $self->check_stat
        if $self->paranoia->{Search} <= $self->paranoia_level;

    $self->SUPER::search(@_);
}

1;

package SWISH::API::Stat::Search;
use strict;
use warnings;
use base qw( SWISH::API::More::Search );

sub execute {
    my $self = shift;
    $self->base->check_stat
        if $self->base->paranoia->{Execute} <= $self->base->paranoia_level;

    return $self->SUPER::execute(@_);
}

1;

__END__