File: CoroClient.pm

package info (click to toggle)
libdr-tarantool-perl 0.15-1%2Bdeb70u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 308 kB
  • sloc: perl: 2,901; makefile: 21
file content (124 lines) | stat: -rw-r--r-- 2,535 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use utf8;
use strict;
use warnings;

package DR::Tarantool::CoroClient;
use base 'DR::Tarantool::AsyncClient';
use Coro;
use Carp;
use AnyEvent;

=head1 NAME

DR::Tarantool::CoroClient - async coro driver for
L<tarantool|http://tarantool.org>

=head1 SYNOPSIS

    use DR::Tarantool::CoroClient;
    use Coro;
    my $client = DR::Tarantool::CoroClient->connect(
        port    => $port,
        spaces  => $spaces;
    );

    my @res;
    for (1 .. 100) {
        async {
            push @res => $client->select(space_name => $_);
        }
    }
    cede while @res < 100;


=head1 METHODS

=head2 connect

Connects to tarantool.

=head3 Arguments

The same as L<DR::Tarantool::AsyncClient/connect> exclude callback.

Returns a connector or croaks error.

=cut

sub connect {
    my ($class, %opts) = @_;
    my $cv = condvar AnyEvent;

    my $cb = Coro::rouse_cb;
    $class->SUPER::connect(%opts, $cb);

    my ($self) = Coro::rouse_wait;
    croak $self unless ref $self;
    $self;
}

=head2 ping

The same as L<DR::Tarantool::AsyncClient/ping> exclude callback.

Returns B<TRUE> or B<FALSE> if an error.

=head2 insert

The same as L<DR::Tarantool::AsyncClient/insert> exclude callback.

Returns tuples that were extracted from database or undef.
Croaks error if an error was happened.

=head2 select

The same as L<DR::Tarantool::AsyncClient/select> exclude callback.

Returns tuples that were extracted from database or undef.
Croaks error if an error was happened.

=head2 update

The same as L<DR::Tarantool::AsyncClient/update> exclude callback.

Returns tuples that were extracted from database or undef.
Croaks error if an error was happened.

=head2 delete

The same as L<DR::Tarantool::AsyncClient/delete> exclude callback.

Returns tuples that were extracted from database or undef.
Croaks error if an error was happened.

=head2 call_lua

The same as L<DR::Tarantool::AsyncClient/call_lua> exclude callback.

Returns tuples that were extracted from database or undef.
Croaks error if an error was happened.

=cut


for my $method (qw(ping insert select update delete call_lua)) {
    no strict 'refs';
    *{ __PACKAGE__ . "::$method" } = sub {
        my ($self, @args) = @_;

        my $cb = Coro::rouse_cb;
        my $m = "SUPER::$method";
        $self->$m(@args, $cb);

        my @res = Coro::rouse_wait;

        if ($res[0] ~~ 'ok') {
            return 1 if $method eq 'ping';
            return $res[1];
        }
        return 0 if $method eq 'ping';
        croak  "$res[1]: $res[2]";
    };
}

1;