AnyEvent-Groonga
view release on metacpan or search on metacpan
lib/AnyEvent/Groonga.pm view on Meta::CPAN
AnyEvent->now_update;
my $timer;
$timer = AnyEvent->timer(
after => $timeout,
cb => sub {
my $data = [ [ 0, undef, undef, ], ['timeout'] ];
my $result = AnyEvent::Groonga::Result->new( data => $data );
$cv->send($result);
undef $timer;
},
);
}
sub _post_to_http_server {
my $self = shift;
my $command = shift;
my $args_ref = shift;
my $url = $self->_generate_groonga_url( $command, $args_ref );
my $cv = AnyEvent->condvar;
$self->_set_timeout( $cv, $args_ref->{timeout} ) if $args_ref->{timeout};
http_get(
$url,
sub {
my $json = shift;
my $result;
try {
my $data = JSON->new->utf8->decode($json);
$result = AnyEvent::Groonga::Result->new(
posted_command => $command,
data => $data
);
}
catch {
$result = $_;
};
$cv->send($result);
}
);
return $cv;
}
sub _post_to_gqtp_server {
my $self = shift;
my $command = shift;
my $args_ref = shift;
my $groonga_command
= $self->_generate_groonga_command( $command, $args_ref );
my $cv = AnyEvent->condvar;
$self->_set_timeout( $cv, $args_ref->{timeout} ) if $args_ref->{timeout};
my $cmd_cv = run_cmd $groonga_command,
'>' => \my $stdout,
'2>' => \my $stderr;
$cmd_cv->cb(
sub {
my $json = $stdout;
my $result;
try {
my $data = JSON->new->utf8->decode($json);
$result = AnyEvent::Groonga::Result->new(
posted_command => $command,
data => $data
);
}
catch {
$result = $_;
};
$cv->send($result);
}
);
return $cv;
}
sub _post_to_local_db {
my $self = shift;
my $command = shift;
my $args_ref = shift;
# just a proxy!
return $self->_post_to_gqtp_server( $command, $args_ref );
}
sub _generate_groonga_url {
my $self = shift;
my $command = shift;
my $args_ref = shift;
my $uri = URI->new;
$uri->scheme("http");
$uri->host( $self->host );
$uri->port( $self->port );
$uri->path( "d/" . $command );
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 {
( run in 2.439 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )