/usr/lib/perl5/DR/Tarantool/LLSyncClient.pm is in libdr-tarantool-perl 0.42-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | use utf8;
use strict;
use warnings;
package DR::Tarantool::LLSyncClient;
use Carp;
use IO::Socket::UNIX;
use IO::Socket::INET;
require DR::Tarantool;
my $LE = $] > 5.01 ? '<' : '';
$Carp::Internal{ (__PACKAGE__) }++;
sub connect {
my ($class, %opts) = @_;
my $host = $opts{host} || 'localhost';
my $port = $opts{port} or croak 'port is undefined';
my $reconnect_period = $opts{reconnect_period} || 0;
my $reconnect_always = $opts{reconnect_always} || 0;
my $raise_error = 1;
if (exists $opts{raise_error}) {
$raise_error = $opts{raise_error} ? 1 : 0;
}
my $self = bless {
host => $host,
port => $port,
raise_error => $raise_error,
reconnect_period => $reconnect_period,
id => 0,
} => ref ($class) || $class;
unless ($self->_connect()) {
unless ($reconnect_always) {
return undef unless $self->{raise_error};
croak "Can't connect to $self->{host}:$self->{port}: $@";
}
unless ($reconnect_period) {
return undef unless $self->{raise_error};
croak "Can't connect to $self->{host}:$self->{port}: $@";
}
}
return $self;
}
sub _connect {
my ($self) = @_;
if ($self->{host} eq 'unix/' or $self->{port} =~ /\D/) {
return $self->{fh} = IO::Socket::UNIX->new(Peer => $self->{port});
} else {
return $self->{fh} = IO::Socket::INET->new(
PeerHost => $self->{host},
PeerPort => $self->{port},
Proto => 'tcp',
);
}
}
sub _req_id {
my ($self) = @_;
return $self->{id}++ if $self->{id} < 0x7FFF_FFFE;
return $self->{id} = 0;
}
sub _request {
my ($self, $id, $pkt ) = @_;
until($self->{fh}) {
unless ($self->{reconnect_period}) {
$self->{last_error_string} = "Connection isn't established";
croak $self->{last_error_string} if $self->{raise_error};
return undef;
}
next if $self->_connect;
sleep $self->{reconnect_period};
}
my $len = length $pkt;
# send request
while($len > 0) {
no warnings; # closed socket
my $slen = syswrite $self->{fh}, $pkt;
goto SOCKET_ERROR unless defined $slen;
$len -= $slen;
substr $pkt, 0, $slen, '';
}
$pkt = '';
while(12 > length $pkt) {
no warnings; # closed socket
my $rl = sysread $self->{fh}, $pkt, 12 - length $pkt, length $pkt;
goto SOCKET_ERROR unless defined $rl;
}
my (undef, $blen) = unpack "L$LE L$LE", $pkt;
while(12 + $blen > length $pkt) {
no warnings; # closed socket
my $rl = sysread $self->{fh},
$pkt, 12 + $blen - length $pkt, length $pkt;
goto SOCKET_ERROR unless defined $rl;
}
my $res = DR::Tarantool::_pkt_parse_response( $pkt );
if ($res->{status} ne 'ok') {
$self->{last_error_string} = $res->{errstr};
$self->{last_code} = $res->{code};
# disconnect
delete $self->{fh} if $res->{status} =~ /^(fatal|buffer)$/;
croak $self->{last_error_string} if $self->{raise_error};
return undef;
}
$self->{last_error_string} = $res->{errstr} || '';
$self->{last_code} = $res->{code};
return $res;
SOCKET_ERROR:
delete $self->{fh};
$self->{last_error_string} = $!;
$self->{last_code} = undef;
croak $self->{last_error_string} if $self->{raise_error};
return undef;
}
sub ping :method {
my ($self) = @_;
unless ($self->{fh}) {
$self->_connect;
$self->{last_code} = -1;
$self->{last_error_string} = "Connection isn't established";
return 0 unless $self->{fh};
}
my $id = $self->_req_id;
my $pkt = DR::Tarantool::_pkt_ping( $id );
my $res = eval { $self->_request( $id, $pkt ); };
return 0 unless $res and $res->{status} eq 'ok';
return 1;
}
sub call_lua :method {
my $self = shift;
my $proc = shift;
my $tuple = shift;
$self->_check_tuple( $tuple );
my $flags = pop || 0;
my $id = $self->_req_id;
my $pkt = DR::Tarantool::_pkt_call_lua($id, $flags, $proc, $tuple);
return $self->_request( $id, $pkt );
}
sub select :method {
my $self = shift;
$self->_check_number( my $ns = shift );
$self->_check_number( my $idx = shift );
$self->_check_tuple_list( my $keys = shift );
$self->_check_number( my $limit = shift || 0x7FFFFFFF );
$self->_check_number( my $offset = shift || 0 );
my $id = $self->_req_id;
my $pkt =
DR::Tarantool::_pkt_select($id, $ns, $idx, $offset, $limit, $keys);
return $self->_request( $id, $pkt );
}
sub insert :method {
my $self = shift;
$self->_check_number( my $space = shift );
$self->_check_tuple( my $tuple = shift );
$self->_check_number( my $flags = pop || 0 );
croak "insert: tuple must be ARRAYREF" unless ref $tuple eq 'ARRAY';
$flags ||= 0;
my $id = $self->_req_id;
my $pkt = DR::Tarantool::_pkt_insert( $id, $space, $flags, $tuple );
return $self->_request( $id, $pkt );
}
sub update :method {
my $self = shift;
$self->_check_number( my $ns = shift );
$self->_check_tuple( my $key = shift );
$self->_check_operations( my $operations = shift );
$self->_check_number( my $flags = pop || 0 );
my $id = $self->_req_id;
my $pkt = DR::Tarantool::_pkt_update($id, $ns, $flags, $key, $operations);
return $self->_request( $id, $pkt );
}
sub delete :method {
my $self = shift;
my $ns = shift;
my $key = shift;
$self->_check_tuple( $key );
my $flags = pop || 0;
my $id = $self->_req_id;
my $pkt = DR::Tarantool::_pkt_delete($id, $ns, $flags, $key);
return $self->_request( $id, $pkt );
}
sub _check_tuple {
my ($self, $tuple) = @_;
croak 'Tuple must be ARRAYREF' unless 'ARRAY' eq ref $tuple;
}
sub _check_tuple_list {
my ($self, $list) = @_;
croak 'Tuplelist must be ARRAYREF of ARRAYREF' unless 'ARRAY' eq ref $list;
croak 'Tuplelist is empty' unless @$list;
$self->_check_tuple($_) for @$list;
}
sub _check_number {
my ($self, $number) = @_;
croak "argument must be number"
unless defined $number and $number =~ /^\d+$/;
}
sub _check_operation {
my ($self, $op) = @_;
croak 'Operation must be ARRAYREF' unless 'ARRAY' eq ref $op;
croak 'Wrong update operation: too short arglist' unless @$op >= 2;
croak "Wrong operation: $op->[1]"
unless $op->[1] and
$op->[1] =~ /^(delete|set|insert|add|and|or|xor|substr)$/;
$self->_check_number($op->[0]);
}
sub _check_operations {
my ($self, $list) = @_;
croak 'Operations list must be ARRAYREF of ARRAYREF'
unless 'ARRAY' eq ref $list;
croak 'Operations list is empty' unless @$list;
$self->_check_operation( $_ ) for @$list;
}
sub last_error_string {
return $_[0]->{last_error_string};
}
sub last_code {
return $_[0]->{last_code};
}
sub raise_error {
return $_[0]->{raise_error};
}
1;
|