Client.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary Client/PBC.pm »
1 #
2 # This file is part of Riak-Client
3 #
4 # This software is copyright (c) 2014 by Damien Krotkine.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Riak::Client;
10 $Riak::Client::VERSION = '1.96';
11 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use Riak::Client::PBC;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use Type::Params qw(compile);
14 use Types::Standard -types;
15 use Errno qw(EINTR);
16 use Scalar::Util qw(blessed);
17 use JSON::XS;
18 use Carp;
19 $Carp::Internal{ (__PACKAGE__) }++;
20 use Module::Runtime qw(use_module);
21 require bytes;
22 use Moo;
23  
24 use IO::Socket::INET;
25 use IO::Socket::Timeout;
26  
27 use Scalar::Util qw(weaken);
28  
29 use constant {
30 # error
31 ERROR_RESPONSE_CODE => 0,
32 # ping
33 PING_REQUEST_CODE => 1,
34 PING_RESPONSE_CODE => 2,
35 # get, get_raw
36 GET_REQUEST_CODE => 9,
37 GET_RESPONSE_CODE => 10,
38 # put, put_raw
39 PUT_REQUEST_CODE => 11,
40 PUT_RESPONSE_CODE => 12,
41 # del
42 DEL_REQUEST_CODE => 13,
43 DEL_RESPONSE_CODE => 14,
44 # get_buckets
45 GET_BUCKETS_REQUEST_CODE => 15,
46 GET_BUCKETS_RESPONSE_CODE => 16,
47 # get_keys
48 GET_KEYS_REQUEST_CODE => 17,
49 GET_KEYS_RESPONSE_CODE => 18,
50 # get_bucket_props
51 GET_BUCKET_PROPS_REQUEST_CODE => 19,
52 GET_BUCKET_PROPS_RESPONSE_CODE => 20,
53 # set_bucket_props
54 SET_BUCKET_PROPS_REQUEST_CODE => 21,
55 SET_BUCKET_PROPS_RESPONSE_CODE => 22,
56 # map_reducd
57 MAP_REDUCE_REQUEST_CODE => 23,
58 MAP_REDUCE_RESPONSE_CODE => 24,
59 # query_index
60 QUERY_INDEX_REQUEST_CODE => 25,
61 QUERY_INDEX_RESPONSE_CODE => 26,
62 };
63  
64  
65 # ABSTRACT: Fast and lightweight Perl client for Riak
66  
67  
68 has host => ( is => 'ro', isa => Str, required => 1 );
69 has port => ( is => 'ro', isa => Int, required => 1 );
70 has r => ( is => 'ro', isa => Int, default => sub {2} );
71 has w => ( is => 'ro', isa => Int, default => sub {2} );
72 has dw => ( is => 'ro', isa => Int, default => sub {1} );
73 has connection_timeout => ( is => 'ro', isa => Num, default => sub {5} );
74 has read_timeout => ( is => 'ro', isa => Num, default => sub {5} );
75 has write_timeout => ( is => 'ro', isa => Num, default => sub {5} );
76 has no_delay => ( is => 'ro', isa => Bool, default => sub {0} );
77  
78  
79 has no_auto_connect => ( is => 'ro', isa => Bool, default => sub {0} );
80  
81 has _on_connect_cb => ( is => 'rw' );
82  
83 has _requests_lock => ( is => 'rw', default => sub { undef });
84  
85 has _socket => ( is => 'ro', lazy => 1, builder => 1 );
86 sub _build__socket {
87 my ($self) = @_;
88  
89 my $host = $self->host;
90 my $port = $self->port;
91  
92 my $socket = IO::Socket::INET->new(
93 PeerHost => $host,
94 PeerPort => $port,
95 Timeout => $self->connection_timeout,
96 );
97  
98 croak "Error ($!), can't connect to $host:$port"
99 unless defined $socket;
100  
101 if ($self->read_timeout || $self->write_timeout) {
102 # enable read and write timeouts on the socket
103 IO::Socket::Timeout->enable_timeouts_on($socket);
104 # setup the timeouts
105 $socket->read_timeout($self->read_timeout);
106 $socket->write_timeout($self->write_timeout);
107 }
108  
109 use Socket qw(IPPROTO_TCP TCP_NODELAY);
110 $self->no_delay
111 and $socket->setsockopt(IPPROTO_TCP, TCP_NODELAY, 1);
112  
113 return $socket;
114 }
115  
116 sub BUILD {
117 my ($self) = @_;
118 $self->no_auto_connect
119 and return;
120  
121 $self->connect();
122 }
123  
124  
125 sub connect {
126 state $check = compile(Any, Optional[CodeRef]);
127 my ( $self, $cb ) = $check->(@_);
128  
129 # that will perform connection
130 $self->_socket();
131 if ($cb) {
132 $cb->();
133 return;
134 } else {
135 return 1;
136 }
137  
138 }
139  
140 has _getkeys_accumulator => (is => 'rw', init_arg => undef);
141 has _mapreduce_accumulator => (is => 'rw', init_arg => undef);
142  
143  
144 sub ping {
145 state $check = compile(Any, Optional[CodeRef]);
146 my ( $self, $cb ) = $check->(@_);
147 $_[0]->_parse_response( {
148 request_code => PING_REQUEST_CODE,
149 expected_code => PING_RESPONSE_CODE,
150 operation_name => 'ping',
151 body_ref => \'',
152 cb => $cb,
153 } );
154 }
155  
156  
157 sub is_alive {
158 state $check = compile(Any, Optional[CodeRef]);
159 my ( $self, $cb ) = $check->(@_);
160 my $res = eval { $self->ping; 1 };
161 $cb and return $cb->($res);
162 return $res;
163 }
164  
165  
166 sub get {
167 state $check = compile(Any, Str, Str, Optional[CodeRef]);
168 my ( $self, $bucket, $key, $cb ) = $check->(@_);
169 $self->_fetch( $bucket, $key, 1, 0, $cb );
170 }
171  
172  
173 sub get_raw {
174 state $check = compile(Any, Str, Str, Optional[CodeRef]);
175 my ( $self, $bucket, $key, $cb ) = $check->(@_);
176 $self->_fetch( $bucket, $key, 0, 0, $cb );
177 }
178  
179  
180 #my $LinksStructure = declare as ArrayRef[Dict[bucket => Str, key => Str, tag => Str]];
181 #coerce $LinksStructure, from HashRef[] Num, q{ int($_) };
182  
183 sub put {
184 my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
185 state $check = compile(Any, Str, Str, Any, Optional[Str],
186 Optional[HashRef[Str]], # indexes
187 Optional[ArrayRef[Dict[bucket => Str, key => Str, tag => Str]]], # links
188 );
189 my ( $self, $bucket, $key, $value, $content_type, $indexes, $links ) = $check->(@_);
190  
191 ($content_type //= 'application/json')
192 eq 'application/json'
193 and $value = encode_json($value);
194  
195 $self->_store( $bucket, $key, $value, $content_type, $indexes, $links, $cb);
196 }
197  
198  
199  
200 sub put_raw {
201 my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
202 state $check = compile(Any, Str, Str, Any, Optional[Str],
203 Optional[HashRef[Str]], # indexes
204 Optional[ArrayRef[Dict[bucket => Str, key => Str, tag => Str]]], # links
205 );
206 my ( $self, $bucket, $key, $value, $content_type, $indexes, $links ) = $check->(@_);
207  
208 $content_type ||= 'plain/text';
209 $self->_store( $bucket, $key, $value, $content_type, $indexes, $links, $cb);
210 }
211  
212  
213 sub del {
214 state $check = compile(Any, Str, Str, Optional[CodeRef]);
215 my ( $self, $bucket, $key, $cb ) = $check->(@_);
216  
217 my $body = RpbDelReq->encode(
218 { key => $key,
219 bucket => $bucket,
220 rw => $self->dw
221 }
222 );
223  
224 $self->_parse_response( {
225 request_code => DEL_REQUEST_CODE,
226 expected_code => DEL_RESPONSE_CODE,
227 operation_name => 'del',
228 key => $key,
229 bucket => $bucket,
230 body_ref => \$body,
231 cb => $cb,
232 } );
233 }
234  
235  
236 sub get_keys {
237 state $check = compile(Any, Str, Optional[CodeRef]);
238 my ( $self, $bucket, $cb ) = $check->(@_);
239  
240 # reset accumulator
241 $self->_getkeys_accumulator([]);
242 my $body = RpbListKeysReq->encode( { bucket => $bucket } );
243 $self->_parse_response( {
244 request_code => GET_KEYS_REQUEST_CODE,
245 expected_code => GET_KEYS_RESPONSE_CODE,
246 operation_name => 'get_keys',
247 key => "*",
248 bucket => $bucket,
249 body_ref => \$body,
250 cb => $cb,
251 handle_response => \&_handle_get_keys_response,
252 lock_requests => 1,
253 } );
254 }
255  
256 sub _handle_get_keys_response {
257 my ( $self, $encoded_message, $args ) = @_;
258  
259 # TODO: support for 1.4 (which provides 'stream', 'return_terms', and 'stream')
260 my $obj = RpbListKeysResp->decode( $encoded_message );
261 my @keys = @{$obj->keys // []};
262  
263 # case 1 : no user callback
264 my $cb = $args->{cb};
265 if (! $cb ) {
266 # accumulate results
267 push @{$self->_getkeys_accumulator}, @keys;
268  
269 # if more to come, return by saying so
270 $obj->done
271 or return (undef, 1);
272  
273 # all results are there, return the whole
274 my $keys = $self->_getkeys_accumulator;
275 $self->_getkeys_accumulator([]);
276 return \$keys;
277 }
278  
279 # case 2 : we have a user callback
280 my $last_key;
281 my $obj_done = $obj->done
282 and $last_key = pop @keys;
283  
284 # no second arg = more to come
285 $cb->($_) foreach @keys;
286  
287 # if more to come, return by saying so
288 $obj->done
289 or return (undef, 1);
290  
291 # process last keys if any
292 defined $last_key and $cb->($last_key, 1);
293  
294 # means: nothing left to do, all results processed through callback
295 return;
296 }
297  
298  
299 sub exists {
300 state $check = compile(Any, Str, Str, Optional[CodeRef]);
301 my ( $self, $bucket, $key, $cb ) = $check->(@_);
302 $self->_fetch( $bucket, $key, 0, 1, $cb );
303 }
304  
305 sub _fetch {
306 my ( $self, $bucket, $key, $decode, $test_exist, $cb ) = @_;
307  
308 my $body = RpbGetReq->encode(
309 { r => $self->r,
310 key => $key,
311 bucket => $bucket,
312 head => $test_exist
313 }
314 );
315  
316 $self->_parse_response( {
317 request_code => GET_REQUEST_CODE,
318 expected_code => GET_RESPONSE_CODE,
319 operation_name => 'get',
320 key => $key,
321 bucket => $bucket,
322 body_ref => \$body,
323 decode => $decode,
324 handle_response => \&_handle_get_response,
325 test_exist => $test_exist,
326 cb => $cb,
327 cb_args => 1,
328 } );
329 }
330  
331 sub _handle_get_response {
332 my ( $self, $encoded_message, $args ) = @_;
333  
334 defined $encoded_message
335 or return _die_generic_error( "Undefined Message", 'get', $args );
336  
337 my $decoded_message = RpbGetResp->decode($encoded_message);
338 my $content = $decoded_message->content;
339  
340 # empty content
341 ref $content eq 'ARRAY'
342 or return \undef;
343  
344 # if we just need to test existence
345 $args->{test_exist}
346 and return \1;
347  
348 # TODO: handle metadata
349 my $value = $content->[0]->value;
350 my $content_type = $content->[0]->content_type;
351  
352 # if we need to decode
353 $args->{decode} && ($content_type // '') eq 'application/json'
354 and return \decode_json($value);
355  
356 # simply return the value
357 return \$value;
358 }
359  
360 sub _store {
361 my ( $self, $bucket, $key, $encoded_value, $content_type, $indexes, $links, $cb ) = @_;
362  
363 my $body = RpbPutReq->encode(
364 { key => $key,
365 bucket => $bucket,
366 content => {
367 value => $encoded_value,
368 content_type => $content_type,
369 ( $indexes ?
370 ( indexes => [
371 map {
372 { key => $_ , value => $indexes->{$_} }
373 } keys %$indexes
374 ])
375 : ()
376 ),
377 ( $links ? ( links => $links) : () ),
378 },
379 }
380 );
381  
382 $self->_parse_response( {
383 request_code => PUT_REQUEST_CODE,
384 expected_code => PUT_RESPONSE_CODE,
385 operation_name => 'put',
386 key => $key,
387 bucket => $bucket,
388 body_ref => \$body,
389 cb => $cb,
390 } );
391 }
392  
393  
394 sub query_index {
395 state $check = compile(Any, Str, Str, Str|ArrayRef, Optional[CodeRef]);
396 my ( $self, $bucket, $index, $value_to_match, $cb ) = $check->(@_);
397  
398 my $query_type_is_eq = 0; # eq
399 ref $value_to_match
400 and $query_type_is_eq = 1; # range
401 my $body = RpbIndexReq->encode(
402 { index => $index,
403 bucket => $bucket,
404 qtype => $query_type_is_eq,
405 $query_type_is_eq ?
406 ( range_min => $value_to_match->[0],
407 range_max => $value_to_match->[1] )
408 : (key => $value_to_match ),
409 }
410 );
411
412 $self->_parse_response( {
413 request_code => QUERY_INDEX_REQUEST_CODE,
414 expected_code => QUERY_INDEX_RESPONSE_CODE,
415 operation_name => 'query_index',
416 $query_type_is_eq ?
417 (key => '2i query on ' . join('...', @$value_to_match) )
418 : (key => $value_to_match ),
419 bucket => $bucket,
420 body_ref => \$body,
421 handle_response => \&_handle_query_index_response,
422 cb => $cb,
423 lock_requests => 1,
424 } );
425 }
426  
427 sub _handle_query_index_response {
428 my ( $self, $encoded_message, $args ) = @_;
429
430 my $obj = RpbIndexResp->decode( $encoded_message );
431 my @keys = @{$obj->keys // []};
432  
433 # case 1 : no user callback
434 my $cb = $args->{cb}
435 or return \\@keys;
436  
437 # case 2 : we have a user callback
438 $cb->($_) foreach @keys;
439  
440 # means: nothing left to do, all results processed through callback
441 return;
442  
443 }
444  
445  
446 sub get_buckets {
447 state $check = compile(Any, Optional[CodeRef]);
448 my ( $self, $cb ) = $check->(@_);
449  
450 $self->_parse_response( {
451 request_code => GET_BUCKETS_REQUEST_CODE,
452 expected_code => GET_BUCKETS_RESPONSE_CODE,
453 operation_name => 'get_buckets',
454 handle_response => \&_handle_get_buckets_response,
455 cb => $cb,
456 } );
457 }
458  
459 sub _handle_get_buckets_response {
460 my ( $self, $encoded_message, $args ) = @_;
461 my $obj = RpbListBucketsResp->decode( $encoded_message );
462 return \($obj->buckets // []);
463 }
464  
465  
466 sub get_bucket_props {
467 state $check = compile(Any, Str, Optional[CodeRef]);
468 my ( $self, $bucket, $cb ) = $check->(@_);
469  
470 my $body = RpbGetBucketReq->encode( { bucket => $bucket } );
471 $self->_parse_response( {
472 request_code => GET_BUCKET_PROPS_REQUEST_CODE,
473 expected_code => GET_BUCKET_PROPS_RESPONSE_CODE,
474 bucket => $bucket,
475 body_ref => \$body,
476 handle_response => \&_handle_get_bucket_props_response,
477 cb => $cb,
478 } );
479 }
480  
481 sub _handle_get_bucket_props_response {
482 my ( $self, $encoded_message, $args ) = @_;
483  
484 my $obj = RpbListBucketsResp->decode( $encoded_message );
485 my $props = RpbBucketProps->decode($obj->buckets->[0]);
486 return \{ %$props }; # unblessing variable
487 }
488  
489  
490 sub set_bucket_props {
491 state $check = compile( Any, Str,
492 Dict,
493 Optional[CodeRef] );
494 my ( $self, $bucket, $props, $cb ) = $check->(@_);
495 $props->{n_val} && $props->{n_val} < 0 and croak 'n_val should be possitive integer';
496  
497 my $body = RpbSetBucketReq->encode({ bucket => $bucket, props => $props });
498 $self->_parse_response( {
499 request_code => SET_BUCKET_PROPS_REQUEST_CODE,
500 expected_code => SET_BUCKET_PROPS_RESPONSE_CODE,
501 bucket => $bucket,
502 body_ref => \$body,
503 } );
504 }
505  
506  
507 sub map_reduce {
508 state $check = compile(Any, Any, Optional[CodeRef]);
509 my ( $self, $request, $cb) = $check->(@_);
510  
511 my @args;
512
513 push @args, ref($request) ? encode_json($request): $request;
514 push @args, 'application/json';
515 push @args, $cb if $cb;
516
517 map_reduce_raw($self, @args);
518  
519 }
520  
521  
522 sub map_reduce_raw {
523 state $check = compile(Any, Str, Str, Optional[CodeRef]);
524 my ( $self, $request, $content_type, $cb) = $check->(@_);
525
526 my $body = RpbMapRedReq->encode(
527 {
528 request => $request,
529 content_type => $content_type,
530 }
531 );
532  
533 # reset accumulator
534 $self->_mapreduce_accumulator([]);
535  
536 $self->_parse_response( {
537 request_code => MAP_REDUCE_REQUEST_CODE,
538 expected_code => MAP_REDUCE_RESPONSE_CODE,
539 operation => 'map_reduce',
540 body_ref => \$body,
541 cb => $cb,
542 decode => ($content_type eq 'application/json'),
543 handle_response => \&_handle_map_reduce_response,
544 lock_requests => 1,
545 } );
546 }
547  
548 sub _handle_map_reduce_response {
549 my ( $self, $encoded_message, $args ) = @_;
550 my $obj = RpbMapRedResp->decode( $encoded_message );
551  
552 # case 1 : no user callback
553 my $cb = $args->{cb};
554 if (! $cb ) {
555  
556 # all results were there, reset the accumulator and return the whole,
557 if ($obj->done) {
558 my $results = $self->_mapreduce_accumulator();
559 $self->_mapreduce_accumulator([]);
560 return \$results;
561 }
562  
563 # accumulate results
564 push @{$self->_mapreduce_accumulator},
565 { phase => $obj->phase, response => ($args->{decode}) ? decode_json($obj->response // '[]') : $obj->response };
566  
567 # more stuff to come, say so
568 return (undef, 1);
569  
570 }
571  
572 # case 2 : we have a user callback
573  
574 # means: nothing left to do, all results processed through callback
575 $obj->done
576 and return;
577  
578 $cb->($obj->response, $obj->phase, $obj->done);
579  
580 # more stuff to come, say so
581 return (undef, 1);
582  
583 }
584  
585 sub _parse_response {
586 my ( $self, $args ) = @_;
587  
588 my $socket = $self->_socket;
589 _send_bytes($socket, $args->{request_code}, $args->{body_ref} // \'');
590  
591 while (1) {
592 my $response;
593 # get and check response
594 my $raw_response_ref = _read_response($socket)
595 or return _die_generic_error( $! || "Socket Closed", $args);
596  
597 my ( $response_code, $response_body ) = unpack( 'c a*', $$raw_response_ref );
598  
599 # in case of error msg
600 if ($response_code == ERROR_RESPONSE_CODE) {
601 my $decoded_message = RpbErrorResp->decode($response_body);
602 my $errmsg = $decoded_message->errmsg;
603 my $errcode = $decoded_message->errcode;
604  
605 return _die_generic_error( "Riak Error (code: $errcode) '$errmsg'", $args);
606 }
607  
608  
609 # check if we have what we want
610 $response_code != $args->{expected_code}
611 and return _die_generic_error(
612 "Unexpected Response Code in (got: $response_code, expected: $args->{expected_code})",
613 $args );
614
615 # default value if we don't need to handle the response.
616 my ($ret, $more_to_come) = ( \1, undef);
617  
618 # handle the response.
619 if (my $handle_response = $args->{handle_response}) {
620 ($ret, $more_to_come) = $handle_response->( $self, $response_body, $args);
621 }
622  
623 # it's a multiple response request, loop again
624 $more_to_come
625 and next;
626  
627 # there is a result, process or return it
628 if ($ret) {
629 $args->{cb} and return $args->{cb}->($$ret);
630 return $$ret;
631 }
632  
633 # ret was undef, means we have processed everything in the callback
634 return;
635  
636 }
637 }
638  
639 sub _die_generic_error {
640 my ( $error, $args ) = @_;
641  
642 my ($operation_name, $bucket, $key) =
643 map { $args->{$_} // "<unknown $_>" } ( qw( operation_name bucket key) );
644  
645 my $extra = '';
646 defined $bucket && defined $key
647 and $extra = "(bucket: $bucket, key: $key) ";
648  
649 my $msg = "Error in '$operation_name' $extra: $error";
650 if ( my $cb = $args->{cb} ) {
651 $cb->((undef) x ($args->{cb_nb_args} // 0), $msg);
652 return;
653 } elsif (my $cv = $args->{cv}) {
654 $cv->croak($msg);
655 } else {
656 croak $msg;
657 }
658 return;
659 }
660  
661 sub _read_response {
662 my ($socket) = @_;
663 _read_bytes($socket, unpack( 'N', ${ _read_bytes($socket, 4) // return } ));
664 }
665  
666 sub _read_bytes {
667 my ( $socket, $length ) = @_;
668  
669 my $buffer;
670 my $offset = 0;
671 my $read = 0;
672  
673 while ($length > 0) {
674 $read = $socket->sysread( $buffer, $length, $offset );
675 if (! defined $read) {
676 $! == EINTR
677 and next;
678 return;
679 }
680  
681 $read > 0
682 or return;
683  
684 $offset += $read;
685 $length -= $read;
686 }
687  
688 return \$buffer;
689 }
690  
691  
692 sub _send_bytes {
693 my ( $socket, $request_code, $body_ref ) = @_;
694  
695 my $bytes = pack('N', my $length = (bytes::length($$body_ref) + 1)) . pack('c', $request_code) . $$body_ref;
696  
697 $length += 4;
698 my $offset = 0;
699 my $sent = 0;
700  
701 while ($length > 0) {
702 $sent = $socket->syswrite( $bytes, $length, $offset );
703 if (! defined $sent) {
704 $! == EINTR
705 and next;
706 return;
707 }
708  
709 $sent > 0
710 or return;
711  
712 $offset += $sent;
713 $length -= $sent;
714 }
715  
716 return $offset;
717 }
718  
719  
720  
721 1;
722  
723 __END__
↑ Summary Client/PBC.pm »