AnyEvent-Groonga

 view release on metacpan or  search on metacpan

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

                    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;
    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(



( run in 0.890 second using v1.01-cache-2.11-cpan-39bf76dae61 )