DBIx::Class::Indexed - Index data via external indexing facilities.


DBIx-Class-Indexed documentation Contained in the DBIx-Class-Indexed distribution.

Index


Code Index:

NAME

Top

DBIx::Class::Indexed - Index data via external indexing facilities.

SYNOPSIS

Top

    package Foo;

    use base qw( DBIx::Class );

    __PACKAGE__->load_components( qw( Indexed Core ) );
    __PACKAGE__->set_indexer( 'WebService::Lucene', {
        server => 'http://localhost:8080/lucene/',
        index  => 'stuff',
    });

    __PACKAGE__->add_columns(
        foo_id => {
            data_type         => 'integer',
            is_auto_increment => 1,
        },
        name => {
            data_type => 'varchar',
            size      => 256,
            indexed   => 1,
        },
        description => {
            data_type => 'text',
            indexed  => 1,
        },
    );

ACCESSORS

Top

indexer_package( [ $indexer ] )

Sets which indexer will be responsible for indexing this class' data. Corresponds to the package name after the DBIx::Class::Indexer prefix.

indexer_connection_info( [ \%info ] )

Sets the extra information passed to the indexer on instantiation.

index_on_insert

Determines whether or not DBIx::Class::Indexed will index the document when it is inserted.

index_on_update

Determines whether or not DBIx::Class::Indexed will index the document when it is updated.

index_on_delete

Determines whether or not DBIx::Class::Indexed will remove the document when it is deleted.

METHODS

Top

indexer( )

Accessor for the indexer object; lazy loaded.

set_indexer( $name [, \%connection_info ] )

Set the indexer information. Connection information is stored in the indexer_connection_info accessor and the package name is stored in indexer_package.

insert( )

Sends the object to the indexer's insert method, if index_on_insert is true.

update( )

Sends the object to the indexer's update method, if index_on_update is true.

delete( )

Sends the object to the indexer's delete method, if index_on_delete is true.

register_column ( $column, \%info )

Overrides DBIx::Class's register_column. If %info contains the key 'indexed', calls register_field.

add_index_fields ( @fields )

Behaves similarly to DBIx::Class's add_columns. Calls register_field underneath.

register_field( $field, \%info )

Registers a field as indexed.

AUTHORS

Top

* Adam Paynter <adapay@cpan.org>
* Brian Cassidy <bricas@cpan.org>

COPYRIGHT AND LICENSE

Top


DBIx-Class-Indexed documentation Contained in the DBIx-Class-Indexed distribution.
package DBIx::Class::Indexed;

use strict;
use warnings;

use base qw( DBIx::Class );

our $VERSION = '0.02';

__PACKAGE__->mk_classdata( _indexer                => undef );
__PACKAGE__->mk_classdata( indexer_connection_info => {} );
__PACKAGE__->mk_classdata( indexer_package         => undef );
__PACKAGE__->mk_classdata( index_fields            => {} );
__PACKAGE__->mk_classdata( index_on_insert         => 1 );
__PACKAGE__->mk_classdata( index_on_update         => 1 );
__PACKAGE__->mk_classdata( index_on_delete         => 1 );

sub indexer {
    my $self    = shift;
    my $schema  = $self->result_source->schema;
    my $key     = $self->indexer_connection_info->{ storage_key } || $self->table;

    $schema->{ _indexers } = {} unless $schema->{ _indexers };     

    my $indexer = $schema->{ _indexers }->{ $key };
    
    # lazy load the indexer
    if( !$indexer ) {
        my $name    = $self->indexer_package;
        my $package = "DBIx::Class::Indexer::$name";
        
        # ensure the indexer package is loaded
        eval "require $package";
        die "Failed to load indexer: $@" if $@;

        $indexer = $package->new( $self->indexer_connection_info, ref $self );
        $schema->{ _indexers }->{ $key } = $indexer;
    }

    return $indexer;
}

sub set_indexer {
    my $class        = shift;
    my $name         = shift;
    my $connect_info = shift;
    
    $class->indexer_package( $name );
    $class->indexer_connection_info( $connect_info || {} );
}

sub insert {
    my $self   = shift;
    my $result = $self->next::method( @_ );
    
    if ( $self->index_on_insert and my $indexer = $self->indexer ) {
        $indexer->insert( $self, @_ );
        
        if ( $self->is_changed ) {
            $result = $self->next::method( @_ );
        }
    }
    
    return $result;
}

sub update {
    my $self   = shift;
    my $result = $self->next::method( @_ );
    
    if ( $self->index_on_update and my $indexer = $self->indexer ) {
        $indexer->update( $self, @_ );
        
        if ( $self->is_changed ) {
            $result = $self->next::method( @_ );
        }
    }
    
    return $result;
}

sub delete {
    my $self = shift;
    
    if ( $self->index_on_delete and my $indexer = $self->indexer ) {
        $indexer->delete( $self, @_ );
    }
    
    $self->next::method( @_ );
}

sub register_column {
    my( $class, $column, $info ) = @_;
    $class->next::method( $column, $info );
    
    if (exists $info->{ indexed }) {
        $class->register_field( $column => $info->{ indexed } );
    }
}

sub add_index_fields {
    my( $class, @fields ) = @_;
    my $fields = $class->index_fields;
    
    while ( my $field = shift @fields ) {
        # If next entry is { ... } use that for the column info, if not
        # use an empty hashref
        my $field_info = ref $fields[ 0 ] ? shift @fields : {};
        $class->register_field( $field, $field_info );
    }
}

sub register_field {
    my( $class, $field, $info ) = @_;
    $class->index_fields->{ $field } = $info;
}

1;