AnyEvent-Groonga

 view release on metacpan or  search on metacpan

lib/AnyEvent/Groonga.pm  view on Meta::CPAN

package AnyEvent::Groonga;
use strict;
use warnings;
use Carp;
use AnyEvent;
use AnyEvent::Util qw(run_cmd);
use AnyEvent::HTTP;
use AnyEvent::Groonga::Result;
use File::Which qw(which);
use List::MoreUtils qw(any);
use URI;
use URI::Escape;
use JSON;
use Try::Tiny;
use Encode;
use base qw(Class::Accessor::Fast);

our $VERSION = '0.08';

__PACKAGE__->mk_accessors($_)
    for qw( protocol host port groonga_path database_path command_list debug);

sub new {
    my $class = shift;
    my $self  = $class->SUPER::new(
        {   protocol      => 'gqtp',
            host          => 'localhost',
            port          => '10041',
            groonga_path  => which("groonga") || undef,
            database_path => undef,
            command_list  => [
                qw(
                    cache_limit
                    check
                    clearlock
                    column_create
                    column_list
                    column_remove
                    define_selector
                    defrag
                    delete
                    dump
                    load
                    log_level
                    log_put
                    log_reopen
                    quit
                    select
                    shutdown
                    status
                    suggest
                    table_create
                    table_list
                    table_remove
                    view_add
                    )
            ],
            @_
        }
    );
    return $self;
}

sub call {
    my $self     = shift;
    my $command  = shift;
    my $args_ref = shift;

    croak( $command . " is not supported command" )
        unless any { $command eq $_ } @{ $self->{command_list} };

    if ( $self->protocol eq 'http' ) {
        return $self->_post_to_http_server( $command, $args_ref );
    }
    elsif ( $self->protocol eq 'gqtp' ) {
        croak("can not find gronnga_path")
            if !$self->groonga_path
                or !-e $self->groonga_path;
        return $self->_post_to_gqtp_server( $command, $args_ref );
    }
    elsif ( $self->protocol eq 'local_db' ) {
        croak("can not find gronnga_path")
            if !$self->groonga_path
                or !-e $self->groonga_path;
        croak("can not find database_path")
            if !$self->database_path
                or !-e $self->database_path;
        return $self->_post_to_local_db( $command, $args_ref );
    }
    else {
        croak( $self->protocol . " is not supported protocol" );
        return undef;
    }
}

sub _set_timeout {
    my $self    = shift;
    my $cv      = shift;
    my $timeout = shift;
    AnyEvent->now_update;

lib/AnyEvent/Groonga.pm  view on Meta::CPAN


=item port

=item groonga_path

It is necessary if you set the protocol as gqtp or local_db.

=item database_path

It is necessary if you set the protocol as local_db.

=back

=head2 call ($command => \%args)

Call groonga command named $command with %args parameters.

It returns AnyEvent condvar object for response.

  # blocking interface
  my $result = $groonga->call(select => {
    table          => "Site",
    query          => 'title:@test',
    output_columns => [qw(_id _score title)],
    sortby         => '_score',
  })->recv;

  # result is AnyEvent::Groonga::Result::Select object
  print $result->dump;

  # non-blocking interface
  $groonga->call(select => {
    table          => "Site",
    query          => 'title:@test',
    output_columns => [qw(_id _score title)],
    sortby         => '_score',
  })->cb(
    sub {
      my $result = $_[0]->recv; 
    }
  );

Available commands are:

=over 4

=item cache_limit

=item check

=item clearlock

=item column_create

=item column_list

=item column_remove

=item define_selector

=item defrag

=item delete

=item dump

=item load

=item log_level

=item log_put

=item log_reopen

=item quit

=item select

=item shutdown

=item status

=item suggest

=item table_create

=item table_list

=item table_remove

=item view_add

See Groonga's official site.
L<http://groonga.org/>

=back

=head1 AUTHOR

Takeshi Miki E<lt>miki@cpan.orgE<gt>


=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut



( run in 1.129 second using v1.01-cache-2.11-cpan-b16cb0d3907 )