blib/lib/DR/Tarantool/CoroClient.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 78 CC 5 SCAR 25.5
CC
5
Cov
0%
connect
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 12.8
1 use utf8;
2 use strict;
3 use warnings;
4  
5 package DR::Tarantool::CoroClient;
6 use base 'DR::Tarantool::AsyncClient';
7 use Coro;
8 use Carp;
9 use AnyEvent;
10  
11 =head1 NAME
12  
13 DR::Tarantool::CoroClient - an asynchronous coro driver for
14 L<Tarantool|http://tarantool.org>
15  
16 =head1 SYNOPSIS
17  
18 use DR::Tarantool::CoroClient;
19 use Coro;
20 my $client = DR::Tarantool::CoroClient->connect(
21 port => $port,
22 spaces => $spaces;
23 );
24  
25 my @res;
26 for (1 .. 100) {
27 async {
28 push @res => $client->select(space_name => $_);
29 }
30 }
31 cede while @res < 100;
32  
33  
34 =head1 METHODS
35  
36 =head2 connect
37  
38 Connects to Tarantool.
39  
40 =head3 Arguments
41  
42 The same as L<DR::Tarantool::AsyncClient/connect>, excluding the callback.
43  
44 Returns a connection handle or croaks an error.
45  
46 =head3 Additional arguments
47  
48 =over
49  
50 =item raise_error
51  
52 If B<true> (default behaviour) the driver throws an exception for each
53 server error.
54  
55 =back
56  
57 =cut
58  
59 sub connect {
60 my ($class, %opts) = @_;
61  
62 my $raise_error = 1;
63 $raise_error = delete $opts{raise_error} if exists $opts{raise_error};
64  
65 my $cb = Coro::rouse_cb;
66 $class->SUPER::connect(%opts, $cb);
67  
68 my ($self) = Coro::rouse_wait;
69 unless (ref $self) {
70 croak $self if $raise_error;
71 $! = $self;
72 return undef;
73 }
74 $self->{raise_error} = $raise_error ? 1 : 0;
75 $self;
76 }
77  
78 =head2 ping
79  
80 The same as L<DR::Tarantool::AsyncClient/ping>, excluding the callback.
81  
82 Returns B<true> on success, B<false> on error.
83  
84 =head2 insert
85  
86 The same as L<DR::Tarantool::AsyncClient/insert>, excluding the callback.
87  
88 Returns the inserted tuple or undef.
89 Croaks an error if insert failed (B<raise_error> must be set).
90  
91 =head2 select
92  
93 The same as L<DR::Tarantool::AsyncClient/select>, excluding the callback.
94  
95 Returns tuple or tuples that match selection criteria, or undef
96 if no matching tuples were found.
97 Croaks an error if an error occurred (provided B<raise_error> is set).
98  
99 =head2 update
100  
101 The same as L<DR::Tarantool::AsyncClient/update>, excluding the callback.
102  
103 Returns the new value of the tuple.
104 Croaks an error if update failed (provided B<raise_error> is set).
105  
106 =head2 delete
107  
108 The same as L<DR::Tarantool::AsyncClient/delete>, excluding the callback.
109  
110 Returns the deleted tuple, or undef.
111 Croaks error if an error occurred (provided B<raise_error> is set).
112  
113 =head2 call_lua
114  
115 The same as L<DR::Tarantool::AsyncClient/call_lua>, excluding the callback.
116  
117 Returns a tuple or tuples returned by the called procedure.
118 Croaks an error if an error occurred (provided B<raise_error> is set).
119  
120 =cut
121  
122  
123 for my $method (qw(ping insert select update delete call_lua)) {
124 no strict 'refs';
125 *{ __PACKAGE__ . "::$method" } = sub {
126 my ($self, @args) = @_;
127  
128 my $cb = Coro::rouse_cb;
129 my $m = "SUPER::$method";
130 $self->$m(@args, $cb);
131  
132 my @res = Coro::rouse_wait;
133  
134 if ($res[0] eq 'ok') {
135 return 1 if $method eq 'ping';
136 return $res[1];
137 }
138 return 0 if $method eq 'ping';
139 return undef unless $self->{raise_error};
140 croak sprintf "%s: %s",
141 defined($res[1])? $res[1] : 'unknown',
142 $res[2]
143 ;
144 };
145 }
146  
147 1;