| 1 | # | ||
| 2 | # This file is part of AnyEvent-Riak | ||
| 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 AnyEvent::Riak; | ||
| 10 | $AnyEvent::Riak::VERSION = '0.93'; | ||
| 11 | # ABSTRACT: AnyEvent ProtocolBuffers Riak Client | ||
| 12 | |||
| 13 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 0 | ▶ | use AnyEvent::Riak::PBC; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | use Types::Standard -types; | ||
| 18 | use Carp; | ||
| 19 | require bytes; | ||
| 20 | use Moo; | ||
| 21 | |||
| 22 | use AnyEvent::Handle; | ||
| 23 | |||
| 24 | my $message_codes = { | ||
| 25 | RpbErrorResp => 0, | ||
| 26 | RpbPingReq => 1, | ||
| 27 | RpbPingResp => 2, | ||
| 28 | RpbGetClientIdReq => 3, | ||
| 29 | RpbGetClientIdResp => 4, | ||
| 30 | RpbSetClientIdReq => 5, | ||
| 31 | RpbSetClientIdResp => 6, | ||
| 32 | RpbGetServerInfoReq => 7, | ||
| 33 | RpbGetServerInfoResp => 8, | ||
| 34 | RpbGetReq => 9, | ||
| 35 | RpbGetResp => 10, | ||
| 36 | RpbPutReq => 11, | ||
| 37 | RpbPutResp => 12, | ||
| 38 | RpbDelReq => 13, | ||
| 39 | RpbDelResp => 14, | ||
| 40 | RpbListBucketsReq => 15, | ||
| 41 | RpbListBucketsResp => 16, | ||
| 42 | RpbListKeysReq => 17, | ||
| 43 | RpbListKeysResp => 18, | ||
| 44 | RpbGetBucketReq => 19, | ||
| 45 | RpbGetBucketResp => 20, | ||
| 46 | RpbSetBucketReq => 21, | ||
| 47 | RpbSetBucketResp => 22, | ||
| 48 | RpbMapRedReq => 23, | ||
| 49 | RpbMapRedResp => 24, | ||
| 50 | RpbIndexReq => 25, | ||
| 51 | RpbIndexResp => 26, | ||
| 52 | RpbSearchQueryReq => 27, | ||
| 53 | RbpSearchQueryResp => 28, | ||
| 54 | RpbResetBucketReq => 29, | ||
| 55 | RpbResetBucketResp => 30, | ||
| 56 | RpbGetBucketTypeReq => 31, | ||
| 57 | RpbSetBucketTypeResp => 32, | ||
| 58 | RpbCSBucketReq => 40, | ||
| 59 | RpbCSUpdateReq => 41, | ||
| 60 | RpbCounterUpdateReq => 50, | ||
| 61 | RpbCounterUpdateResp => 51, | ||
| 62 | RpbCounterGetReq => 52, | ||
| 63 | RpbCounterGetResp => 53, | ||
| 64 | RpbYokozunaIndexGetReq => 54, | ||
| 65 | RpbYokozunaIndexGetResp => 55, | ||
| 66 | RpbYokozunaIndexPutReq => 56, | ||
| 67 | RpbYokozunaIndexPutResp => 57, | ||
| 68 | RpbYokozunaSchemaGetReq => 58, | ||
| 69 | RpbYokozunaSchemaGetResp => 59, | ||
| 70 | RpbYokozunaSchemaPutReq => 60, | ||
| 71 | DtFetchReq => 80, | ||
| 72 | DtFetchResp => 81, | ||
| 73 | DtUpdateReq => 82, | ||
| 74 | DtUpdateResp => 83, | ||
| 75 | RpbAuthReq => 253, | ||
| 76 | RpbAuthResp => 254, | ||
| 77 | RpbStartTls => 255, | ||
| 78 | }; | ||
| 79 | |||
| 80 | |||
| 81 | has host => ( is => 'ro', isa => Str, default => sub { '127.0.0.1'} ); | ||
| 82 | has port => ( is => 'ro', isa => Int, default => sub { 8087 } ); | ||
| 83 | has on_connect => ( is => 'ro', isa => CodeRef, required => 1 ); | ||
| 84 | has on_connect_error => ( is => 'ro', isa => CodeRef, required => 1 ); | ||
| 85 | has connect_timeout => ( is => 'ro', isa => Num, default => sub {5} ); | ||
| 86 | has timeout => ( is => 'ro', isa => Num, default => sub {5} ); | ||
| 87 | has no_delay => ( is => 'ro', isa => Bool, default => sub {0} ); | ||
| 88 | |||
| 89 | has _handle => ( is => 'ro', lazy => 1, clearer => 1, builder => sub { | ||
| 90 | my ($self) = @_; | ||
| 91 | my ($host, $port) = ($self->host, $self->port); | ||
| 92 | |||
| 93 | my $on_connect = $self->on_connect; | ||
| 94 | my $on_connect_error = $self->on_connect_error; | ||
| 95 | my $c_timeout = $self->connect_timeout; | ||
| 96 | my $no_delay = $self->no_delay; | ||
| 97 | |||
| 98 | AnyEvent::Handle->new ( | ||
| 99 | connect => [$host, $port], | ||
| 100 | no_delay => $no_delay, | ||
| 101 | on_connect => $on_connect, | ||
| 102 | on_connect_error => $on_connect_error, | ||
| 103 | on_prepare => sub { $c_timeout }, | ||
| 104 | on_error => sub { my ($handle, $fatal, $message) = @_; | ||
| 105 | croak "Panic: no special on_error has been set yet! error: $message"; }, | ||
| 106 | ); | ||
| 107 | |||
| 108 | }); | ||
| 109 | |||
| 110 | |||
| 111 | sub BUILD { | ||
| 112 | my ($self) = @_; | ||
| 113 | $self->_handle(); | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub close { | ||
| 118 | my ($self, $callback) = @_; | ||
| 119 | defined $callback && ref($callback) eq 'CODE' | ||
| 120 | or croak "last parameter must be a CoderRef callback"; | ||
| 121 | $self->_handle->low_water_mark(0); | ||
| 122 | $self->_handle->on_drain( sub { shutdown($_[0]{fh}, 1); $callback->(); } ); | ||
| 123 | $self->_clear_handle(); | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | # we don't want DESTROY to be autoloaded | ||
| 128 | sub DESTROY { } | ||
| 129 | |||
| 130 | ### Deal with common, general case, Riak commands | ||
| 131 | our $AUTOLOAD; | ||
| 132 | |||
| 133 | sub AUTOLOAD { | ||
| 134 | my $command = $AUTOLOAD; | ||
| 135 | $command =~ s/.*://; | ||
| 136 | |||
| 137 | my ($request_name, $request_code, $response_name, $response_code) = _command_to_req($command); | ||
| 138 | my $method = sub { shift->_run_cmd($request_name, $request_code, $response_name, $response_code, @_) }; | ||
| 139 | |||
| 140 | # Save this method for future calls | ||
| 141 | no strict 'refs'; | ||
| 142 | *$AUTOLOAD = $method; | ||
| 143 | |||
| 144 | goto $method; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub _command_to_req { | ||
| 148 | my ($command) = @_; | ||
| 149 | my $request_name = 'Rpb' . ucfirst(_to_camel($command)) . 'Req'; | ||
| 150 | my $response_name = 'Rpb' . ucfirst(_to_camel($command)) . 'Resp'; | ||
| 151 | my $request_code = $message_codes->{$request_name}; | ||
| 152 | my $response_code = $message_codes->{$response_name}; | ||
| 153 | defined $request_code && defined $response_code | ||
| 154 | or croak "unknown method '$command'"; | ||
| 155 | return $request_name, $request_code, $response_name, $response_code; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _run_cmd { | ||
| 159 | my $callback = pop; | ||
| 160 | defined $callback && ref($callback) eq 'CODE' | ||
| 161 | or croak "last parameter must be a CoderRef callback"; | ||
| 162 | my ( $self, $request_name, $request_code, $response_name, $expected_response_code, $args ) = @_; | ||
| 163 | |||
| 164 | my $body = ''; | ||
| 165 | if (defined $args) { | ||
| 166 | eval { $body = "$request_name"->encode($args); 1 } | ||
| 167 | or return $callback->(undef, { error_code => -1, error_message => $@ }); | ||
| 168 | } | ||
| 169 | |||
| 170 | my $handle = $self->_handle; | ||
| 171 | $handle->on_error(sub { | ||
| 172 | my ($handle, $fatal, $message) = @_; | ||
| 173 | $fatal or $handle->destroy(); # force destroy even if non fatal | ||
| 174 | $callback->(undef, { error_code => $!, | ||
| 175 | error_message => $message }) }); | ||
| 176 | |||
| 177 | $handle->timeout_reset; | ||
| 178 | $handle->timeout($self->timeout); | ||
| 179 | $handle->on_timeout(sub { $callback->(undef, { error_code => -1, | ||
| 180 | error_message => 'timeout' }) }); | ||
| 181 | $handle->push_write( pack('N', bytes::length($body) + 1) | ||
| 182 | . pack('c', $request_code) . $body | ||
| 183 | ); | ||
| 184 | $handle->timeout_reset; | ||
| 185 | |||
| 186 | $handle->push_read( chunk => 4, sub { | ||
| 187 | my $len = unpack "N", $_[1]; | ||
| 188 | $_[0]->timeout_reset; | ||
| 189 | $_[0]->unshift_read( chunk => $len, sub { | ||
| 190 | $_[0]->timeout_reset; | ||
| 191 | $_[0]->timeout(0); | ||
| 192 | my ( $response_code, $response_body ) = unpack( 'c a*', $_[1] ); | ||
| 193 | |||
| 194 | if ($response_code == $message_codes->{RpbErrorResp}) { | ||
| 195 | my $decoded_message = RpbErrorResp->decode($response_body); | ||
| 196 | return $callback->(undef, { error_code => $decoded_message->errcode, | ||
| 197 | error_message => $decoded_message->errmsg }); | ||
| 198 | } | ||
| 199 | if ($response_code != $expected_response_code) { | ||
| 200 | return $callback->(undef, { | ||
| 201 | error_code => -2, | ||
| 202 | error_message => "wrong response (got: '$response_code', " | ||
| 203 | . "expected: '$expected_response_code')" }); | ||
| 204 | } | ||
| 205 | |||
| 206 | # my ($ret, $more_to_come) = ( 1, ); | ||
| 207 | |||
| 208 | my $result; | ||
| 209 | if ($response_name) { | ||
| 210 | $result = $response_name->decode($response_body); | ||
| 211 | ref($result) eq $response_name | ||
| 212 | or return $callback->(undef, { | ||
| 213 | error_code => -2, | ||
| 214 | error_message => "wrong response (got: '" . ref($result) . "', " | ||
| 215 | . "expected: '$response_name')" }); | ||
| 216 | } else { | ||
| 217 | $result = 1; | ||
| 218 | } | ||
| 219 | return $callback->($result); | ||
| 220 | }); | ||
| 221 | }); | ||
| 222 | $handle->timeout_reset; | ||
| 223 | return; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub _to_camel { | ||
| 227 | my ($str) = @_; | ||
| 228 | $str =~ s/_([a-z])/uc($1)/ge; | ||
| 229 | return $str; | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | # Now, some methods that are not generic | ||
| 235 | |||
| 236 | sub set_bucket { | ||
| 237 | my ($request_name, $request_code, $response_name, $response_code) = _command_to_req('set_bucket'); | ||
| 238 | shift->_run_cmd($request_name, $request_code, undef, $response_code, @_); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub reset_bucket { | ||
| 242 | my ($request_name, $request_code, $response_name, $response_code) = _command_to_req('reset_bucket'); | ||
| 243 | shift->_run_cmd($request_name, $request_code, undef, $response_code, @_); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub get_bucket_type { | ||
| 247 | my $request_name = 'RpbGetBucketTypeReq'; | ||
| 248 | my $response_name = 'RpbGetBucketResp'; | ||
| 249 | shift->_run_cmd($request_name, $message_codes->{$request_name}, | ||
| 250 | $response_name, $message_codes->{$response_name}, @_); | ||
| 251 | } | ||
| 252 | |||
| 253 | sub set_bucket_type { | ||
| 254 | croak "not implemented yet"; | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | sub get_client_id { | ||
| 259 | croak "deprecated since Riak 1.4"; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | sub set_client_id { | ||
| 264 | croak "deprecated since Riak 1.4"; | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | 1; | ||
| 269 | |||
| 270 | __END__ | ||