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' ) {
lib/AnyEvent/Groonga.pm view on Meta::CPAN
my @array;
while ( my ( $key, $value ) = each %$args_ref ) {
if ( $command eq 'load' && $key eq 'values' ) {
$value = $self->_load_filter($value);
}
elsif ( ref $value eq 'ARRAY' ) {
$value = join( ",", @$value );
}
$key = uri_escape($key);
$value = uri_escape($value);
push @array, $key . '=' . $value;
}
$uri->query( join( "&", @array ) );
return $uri->as_string;
}
sub _generate_groonga_command {
my $self = shift;
my $command = shift;
my $args_ref = shift;
my $groonga_command;
if ( $self->protocol eq 'gqtp' ) {
$groonga_command = join( " ",
$self->groonga_path, '-p', $self->port, '-c', $self->host );
}
else {
$groonga_command
= join( " ", $self->groonga_path, $self->database_path );
}
$groonga_command .= ' "' . $command . ' ';
my @array;
while ( my ( $key, $value ) = each %$args_ref ) {
if ( $command eq 'load' && $key eq 'values' ) {
$value = $self->_load_filter($value);
}
elsif (
$command eq 'select'
&& ( $key eq 'query'
|| $key eq 'filter'
|| $key eq 'sortby'
|| $key eq 'scorer' )
)
{
if ( ref $value eq 'ARRAY' ) {
$value = join( ",", @$value );
}
$value = $self->_select_filter($value);
}
elsif ( ref $value eq 'ARRAY' ) {
$value = join( ",", @$value );
}
$key = '--' . $key;
push @array, ( $key, $value );
}
$groonga_command .= join( " ", @array ) . '"';
warn($groonga_command) if $self->debug;
return $groonga_command;
}
sub _select_filter {
my $self = shift;
my $data = shift;
$data = decode( "utf8", $data ) unless utf8::is_utf8($data);
$data =~ /(^|[^\\])"|'/;
if ($1) {
$data =~ s/(^|[^\\])"|'/$1\\"/g;
}
else {
$data =~ s/(^|[^\\])"|'/\\"/g;
}
return '\'' . $data . '\'';
}
sub _load_filter {
my $self = shift;
my $data = shift;
my $json = JSON->new->latin1->encode($data);
if ( $self->protocol ne 'http' ) {
$json =~ s/\\/\\\\\\\\/g;
$json =~ s/'/\\'/g;
$json =~ s/"/\\"/g;
}
if ( ref $data ne 'ARRAY' ) {
$json = '[' . $json . ']';
}
$json = '\'' . $json . '\'' if $self->protocol ne 'http';
return $json;
}
1;
__END__
=head1 NAME
AnyEvent::Groonga - Groonga client for AnyEvent
=head1 SYNOPSIS
use AnyEvent::Groonga;
my $groonga = AnyEvent::Groonga->new(
protocol => 'http',
host => 'localhost,
port => '10041',
);
# 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;
( run in 0.901 second using v1.01-cache-2.11-cpan-39bf76dae61 )