blib/lib/DR/Tarantool/MsgPack/AsyncClient.pm untested

filter: Statementstmt 0.0% 0 / 174 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 248 CC 16 SCAR 23.1
CC
16
Cov
0%
_cb_default
30.0
connect
20.0
select
12.0
CRAP 10.1
1 use utf8;
2 use strict;
3 use warnings;
4  
5 package DR::Tarantool::MsgPack::AsyncClient;
6  
7 =head1 NAME
8  
9 DR::Tarantool::MsgPack::AsyncClient - async client for tarantool.
10  
11 =head1 SYNOPSIS
12  
13 use DR::Tarantool::MsgPack::AsyncClient;
14  
15 DR::Tarantool::MsgPack::AsyncClient->connect(
16 host => '127.0.0.1',
17 port => 12345,
18 spaces => $spaces,
19 sub {
20 my ($client) = @_;
21 }
22 );
23  
24 $client->insert('space_name', [1,2,3], sub { ... });
25  
26  
27 =head1 Class methods
28  
29 =head2 connect
30  
31 Connect to <Tarantool:http://tarantool.org>, returns (by callback) an
32 object which can be used to make requests.
33  
34 =head3 Arguments
35  
36 =over
37  
38 =item host & port & user & password
39  
40 Address and auth information of remote tarantool.
41  
42 =item space
43  
44 A hash with space description or a L<DR::Tarantool::Spaces> reference.
45  
46 =item reconnect_period
47  
48 An interval to wait before trying to reconnect after a fatal error
49 or unsuccessful connect. If the field is defined and is greater than
50 0, the driver tries to reconnect to the server after this interval.
51  
52 Important: the driver does not reconnect after the first
53 unsuccessful connection. It calls callback instead.
54  
55 =item reconnect_always
56  
57 Try to reconnect even after the first unsuccessful connection.
58  
59 =back
60  
61 =cut
62  
63  
64 use DR::Tarantool::MsgPack::LLClient;
65 use DR::Tarantool::Spaces;
66 use DR::Tarantool::Tuple;
67 use Carp;
68 $Carp::Internal{ (__PACKAGE__) }++;
69 use Scalar::Util ();
70 use Data::Dumper;
71  
72 sub connect {
73 my $class = shift;
74 my ($cb, %opts);
75 if ( @_ % 2 ) {
76 $cb = pop;
77 %opts = @_;
78 } else {
79 %opts = @_;
80 $cb = delete $opts{cb};
81 }
82  
83 $class->_llc->_check_cb( $cb );
84  
85 my $host = $opts{host} || 'localhost';
86 my $port = $opts{port} or croak "port isn't defined";
87  
88 my $user = delete $opts{user};
89 my $password = delete $opts{password};
90  
91 my $spaces = Scalar::Util::blessed($opts{spaces}) ?
92 $opts{spaces} : DR::Tarantool::Spaces->new($opts{spaces});
93 $spaces->family(2);
94  
95 my $reconnect_period = $opts{reconnect_period} || 0;
96 my $reconnect_always = $opts{reconnect_always} || 0;
97  
98 DR::Tarantool::MsgPack::LLClient->connect(
99 host => $host,
100 port => $port,
101 user => $user,
102 password => $password,
103 reconnect_period => $reconnect_period,
104 reconnect_always => $reconnect_always,
105 sub {
106 my ($client) = @_;
107 my $self;
108 if (ref $client) {
109 $self = bless {
110 llc => $client,
111 spaces => $spaces,
112 } => ref($class) || $class;
113 } else {
114 $self = $client;
115 }
116  
117 $cb->( $self );
118 }
119 );
120  
121 return;
122 }
123  
124 sub _llc { return $_[0]{llc} if ref $_[0]; 'DR::Tarantool::MsgPack::LLClient' }
125  
126  
127 sub _cb_default {
128 my ($res, $s, $cb) = @_;
129 if ($res->{status} ne 'ok') {
130 $cb->($res->{status} => $res->{CODE}, $res->{ERROR});
131 return;
132 }
133  
134 if ($s) {
135 $cb->(ok => $s->tuple_class->unpack( $res->{DATA}, $s ), $res->{CODE});
136 return;
137 }
138  
139 unless ('ARRAY' eq ref $res->{DATA}) {
140 $cb->(ok => $res->{DATA}, $res->{CODE});
141 return;
142 }
143  
144 unless (@{ $res->{DATA} }) {
145 $cb->(ok => undef, $res->{CODE});
146 return;
147 }
148 $cb->(ok => DR::Tarantool::Tuple->new($res->{DATA}), $res->{CODE});
149 return;
150 }
151  
152 =head1 Worker methods
153  
154 All methods accept callbacks which are invoked with the following
155 arguments:
156  
157 =over
158  
159 =item status
160  
161 On success, this field has value 'ok'. The value of this parameter
162 determines the contents of the rest of the callback arguments.
163  
164 =item a tuple or tuples or an error code
165  
166 On success, the second argument contains tuple(s) produced by the
167 request. On error, it contains the server error code.
168  
169 =item errorstr
170  
171 Error string in case of an error.
172  
173 sub {
174 if ($_[0] eq 'ok') {
175 my ($status, $tuples) = @_;
176 ...
177 } else {
178 my ($status, $code, $errstr) = @_;
179 ...
180 }
181 }
182  
183 =back
184  
185  
186 =head2 ping
187  
188 Ping the server.
189  
190 $client->ping(sub { ... });
191  
192 =head2 insert, replace
193  
194  
195 Insert/replace a tuple into a space.
196  
197 $client->insert('space', [ 1, 'Vasya', 20 ], sub { ... });
198 $client->replace('space', [ 2, 'Petya', 22 ], sub { ... });
199  
200  
201 =head2 call_lua
202  
203 Call Lua function.
204  
205 $client->call_lua(foo => ['arg1', 'arg2'], sub { });
206  
207  
208 =head2 select
209  
210 Select a tuple (or tuples) from a space by index.
211  
212 $client->select('space_name', 'index_name', [ 'key' ], %opts, sub { .. });
213  
214 Options can be:
215  
216 =over
217  
218 =item limit
219  
220 =item offset
221  
222 =item iterator
223  
224 An iterator for index. Can be:
225  
226 =over
227  
228 =item ALL
229  
230 Returns all tuples in space.
231  
232 =item EQ, GE, LE, GT, LT
233  
234 =back
235  
236 =back
237  
238  
239 =head2 delete
240  
241 Delete a tuple.
242  
243 $client->delete('space_name', [ 'key' ], sub { ... });
244  
245  
246 =head2 update
247  
248 Update a tuple.
249  
250 $client->update('space', [ 'key' ], \@ops, sub { ... });
251  
252 C<@ops> is array of operations to update.
253 Each operation is array of elements:
254  
255 =over
256  
257 =item code
258  
259 Code of operation: C<=>, C<+>, C<->, C<&>, C<|>, etc
260  
261 =item field
262  
263 Field number or name.
264  
265 =item arguments
266  
267 =back
268  
269 =cut
270  
271  
272  
273  
274 sub ping {
275 my $self = shift;
276 my $cb = pop;
277  
278 $self->_llc->_check_cb( $cb );
279 $self->_llc->ping(sub { _cb_default($_[0], undef, $cb) });
280 }
281  
282 sub insert {
283 my $self = shift;
284 my $cb = pop;
285 $self->_llc->_check_cb( $cb );
286 my $space = shift;
287 my $tuple = shift;
288 $self->_llc->_check_tuple( $tuple );
289  
290  
291 my $sno;
292 my $s;
293  
294 if (Scalar::Util::looks_like_number $space) {
295 $sno = $space;
296 } else {
297 $s = $self->{spaces}->space($space);
298 $sno = $s->number,
299 $tuple = $s->pack_tuple( $tuple );
300 }
301  
302 $self->_llc->insert(
303 $sno,
304 $tuple,
305 sub {
306 my ($res) = @_;
307 _cb_default($res, $s, $cb);
308 }
309 );
310 return;
311 }
312  
313 sub replace {
314 my $self = shift;
315 my $cb = pop;
316 $self->_llc->_check_cb( $cb );
317 my $space = shift;
318 my $tuple = shift;
319 $self->_llc->_check_tuple( $tuple );
320  
321  
322 my $sno;
323 my $s;
324  
325 if (Scalar::Util::looks_like_number $space) {
326 $sno = $space;
327 } else {
328 $s = $self->{spaces}->space($space);
329 $sno = $s->number,
330 $tuple = $s->pack_tuple( $tuple );
331 }
332  
333 $self->_llc->replace(
334 $sno,
335 $tuple,
336 sub {
337 my ($res) = @_;
338 _cb_default($res, $s, $cb);
339 }
340 );
341 return;
342 }
343  
344 sub delete :method {
345 my $self = shift;
346 my $cb = pop;
347 $self->_llc->_check_cb( $cb );
348
349 my $space = shift;
350 my $key = shift;
351  
352  
353 my $sno;
354 my $s;
355  
356 if (Scalar::Util::looks_like_number $space) {
357 $sno = $space;
358 } else {
359 $s = $self->{spaces}->space($space);
360 $sno = $s->number;
361 }
362  
363 $self->_llc->delete(
364 $sno,
365 $key,
366 sub {
367 my ($res) = @_;
368 _cb_default($res, $s, $cb);
369 }
370 );
371 return;
372 }
373  
374 sub select :method {
375 my $self = shift;
376 my $cb = pop;
377 $self->_llc->_check_cb( $cb );
378 my $space = shift;
379 my $index = shift;
380 my $key = shift;
381 my %opts = @_;
382  
383 my $sno;
384 my $ino;
385 my $s;
386 if (Scalar::Util::looks_like_number $space) {
387 $sno = $space;
388 croak 'If space is number, index must be number too'
389 unless Scalar::Util::looks_like_number $index;
390 $ino = $index;
391 } else {
392 $s = $self->{spaces}->space($space);
393 $sno = $s->number;
394 $ino = $s->_index( $index )->{no};
395 }
396 $self->_llc->select(
397 $sno,
398 $ino,
399 $key,
400 $opts{limit},
401 $opts{offset},
402 $opts{iterator},
403 sub {
404 my ($res) = @_;
405 _cb_default($res, $s, $cb);
406 }
407 );
408 }
409  
410 sub update :method {
411 my $self = shift;
412 my $cb = pop;
413 $self->_llc->_check_cb( $cb );
414 my $space = shift;
415 my $key = shift;
416 my $ops = shift;
417  
418 my $sno;
419 my $s;
420 if (Scalar::Util::looks_like_number $space) {
421 $sno = $space;
422 } else {
423 $s = $self->{spaces}->space($space);
424 $sno = $s->number;
425 $ops = $s->pack_operations($ops);
426 }
427 $self->_llc->update(
428 $sno,
429 $key,
430 $ops,
431 sub {
432 my ($res) = @_;
433 _cb_default($res, $s, $cb);
434 }
435 );
436 }
437  
438 sub call_lua {
439 my $self = shift;
440 my $cb = pop;
441 $self->_llc->_check_cb( $cb );
442  
443 my $proc = shift;
444 my $tuple = shift;
445  
446 $tuple = [ $tuple ] unless ref $tuple;
447 $self->_llc->_check_tuple( $tuple );
448  
449  
450 $self->_llc->call_lua(
451 $proc,
452 $tuple,
453 sub {
454 my ($res) = @_;
455 _cb_default($res, undef, $cb);
456 }
457 );
458 return;
459 }
460  
461  
462 sub last_code { $_[0]->_llc->last_code }
463  
464  
465 sub last_error_string { $_[0]->_llc->last_error_string }
466  
467 1;