blib/lib/DR/Tarantool/RealSyncClient.pm

filter: Statementstmt 28.0% 30 / 107 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 40.9% 9 / 22 Podpod 0.0% 0 / 11 totaltotal 18.6% 39 / 209 CC 27 SCAR 37.1
CC
27
Cov
19%
connect
110.0
call_lua
72.0
select
30.0
CRAP 40.9
« blib/lib/DR/Tarantool/MsgPack/Proto.pm ↑ Summary blib/lib/DR/Tarantool/LLClient.pm »
1 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package DR::Tarantool::RealSyncClient;
6  
7  
8 =head1 NAME
9  
10 DR::Tarantool::RealSyncClient - a synchronous driver for L<Tarantool/Box|http://tarantool.org>
11  
12 =head1 SYNOPSIS
13  
14 my $client = DR::Tarantool::RealSyncClient->connect(
15 port => $tnt->primary_port,
16 spaces => $spaces
17 );
18  
19 if ($client->ping) { .. };
20  
21 my $t = $client->insert(
22 first_space => [ 1, 'val', 2, 'test' ], TNT_FLAG_RETURN
23 );
24  
25 $t = $client->call_lua('luafunc' => [ 0, 0, 1 ], 'space_name');
26  
27 $t = $client->select(space_name => $key);
28  
29 $t = $client->update(space_name => 2 => [ name => set => 'new' ]);
30  
31 $client->delete(space_name => $key);
32  
33  
34 =head1 DESCRIPTION
35  
36 The module is a clone of L<DR::Tarantool::SyncClient> but it doesn't
37 use L<AnyEvent> or L<Coro>.
38  
39 The module uses L<IO::Socket> sockets.
40  
41 =head1 COPYRIGHT AND LICENSE
42  
43 Copyright (C) 2011 Dmitry E. Oboukhov <unera@debian.org>
44 Copyright (C) 2011 Roman V. Nikolaev <rshadow@rambler.ru>
45  
46 This program is free software, you can redistribute it and/or
47 modify it under the terms of the Artistic License.
48  
49 =head1 VCS
50  
51 The project is placed git repo on github:
52 L<|https://github.com/dr-co/dr-tarantool/>.
53  
54 =cut
55  
56 1 ▶use DR::Tarantool::LLSyncClient;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use DR::Tarantool::Spaces;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use DR::Tarantool::Tuple;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 $Carp::Internal{ (__PACKAGE__) }++;
61 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 my $unpack = sub {
65 0 ▶ my ($self, $res, $s) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
66 0 ▶ return undef unless $res and $res->{status} eq 'ok';
Branch: 1 branch
BranchTrueFalseTotal
unless $res and $res->{"status"} eq "ok" 0 0 0
Condition0%0/3
$res and $res->{"status"} eq "ok"
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res->{"status"} eq "ok"
$res $res->{"status"} eq "ok"
Truth table0%0/3
A and B
A= $resB= $res->{"status"} eq "ok"
ABresult
0X0
100
111
67 0 ▶ return $s->tuple_class->unpack( $res->{tuples}, $s ) if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
68 0 return $res->{tuples};
69 1 };
70  
71 sub connect {
72 0 ▶ my ($class, %opts) = @_;
Subroutine connect: not called CC=10 SCAR=47.0
Pod undocumented
73  
74 0 ▶ my $host = $opts{host} || 'localhost';
Condition0%0/2
$opts{'host'} || 'localhost'
l!l
00
MC/DC0%0/1
$opts{'host'} || 'localhost'
$opts{'host'}
Truth table0%0/2
A
A= $opts{'host'}
Aresult
11
00
75 0 ▶ my $port = $opts{port} or croak "port isn't defined";
Branch: 1 branch
BranchTrueFalseTotal
my $port = $opts{"port"} or croak("port isn't defined") 0 0 0
76  
77 my $spaces = blessed($opts{spaces}) ?
78 0 ▶ $opts{spaces} : DR::Tarantool::Spaces->new($opts{spaces});
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($opts{'spaces'}) ? : 0 0 0
79 0 ▶ my $reconnect_period = $opts{reconnect_period} || 0;
Condition0%0/2
$opts{'reconnect_period'} || 0
l!l
00
MC/DC0%0/1
$opts{'reconnect_period'} || 0
$opts{'reconnect_period'}
Truth table0%0/2
A
A= $opts{'reconnect_period'}
Aresult
11
00
80 0 ▶ my $reconnect_always = $opts{reconnect_always} || 0;
Condition0%0/2
$opts{'reconnect_always'} || 0
l!l
00
MC/DC0%0/1
$opts{'reconnect_always'} || 0
$opts{'reconnect_always'}
Truth table0%0/2
A
A= $opts{'reconnect_always'}
Aresult
11
00
81  
82 my $client = DR::Tarantool::LLSyncClient->connect(
83 host => $host,
84 port => $port,
85 reconnect_period => $reconnect_period,
86 reconnect_always => $reconnect_always,
87 exists($opts{raise_error}) ?
88 0 ▶ ( raise_error => $opts{raise_error} ? 1: 0 )
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{'raise_error'} ? : 0 0 0
$opts{'raise_error'} ? : 0 0 0
89 : (),
90 );
91  
92  
93 0 ▶ return undef unless $client;
Branch: 1 branch
BranchTrueFalseTotal
unless $client 0 0 0
94 0 ▶ return bless { llc => $client, spaces => $spaces } => ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
95 }
96  
97 sub space {
98 0 ▶ my ($self, $name) = @_;
Subroutine space: not called CC=1 SCAR=6.9
Pod undocumented
99 0 return $self->{spaces}->space($name);
100 }
101  
102  
103 sub ping {
104 0 ▶ my ($self) = @_;
Subroutine ping: not called CC=1 SCAR=6.9
Pod undocumented
105 0 $self->{llc}->ping;
106 }
107  
108 sub insert {
109 0 ▶ my $self = shift;
Subroutine insert: not called CC=2 SCAR=17.9
Pod undocumented
110 0 my $space = shift;
111 0 $self->_llc->_check_tuple( my $tuple = shift );
112 0 ▶ my $flags = pop || 0;
Condition0%0/2
pop() || 0
l!l
00
MC/DC0%0/1
pop() || 0
pop()
Truth table0%0/2
A
A= pop()
Aresult
11
00
113  
114 0 my $s = $self->{spaces}->space($space);
115  
116 0 my $res =
117 $self->_llc->insert( $s->number, $s->pack_tuple( $tuple ), $flags );
118 0 return $unpack->($self, $res, $s);
119 }
120  
121 sub call_lua {
122 0 ▶ my $self = shift;
Subroutine call_lua: not called CC=8 SCAR=42.8
Pod undocumented
123 0 my $lua_name = shift;
124 0 my $args = shift;
125  
126 0 ▶ unshift @_ => 'space' if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
127 0 my %opts = @_;
128  
129 0 ▶ my $flags = $opts{flags} || 0;
Condition0%0/2
$opts{'flags'} || 0
l!l
00
MC/DC0%0/1
$opts{'flags'} || 0
$opts{'flags'}
Truth table0%0/2
A
A= $opts{'flags'}
Aresult
11
00
130 0 my $space_name = $opts{space};
131 0 my $fields = $opts{fields};
132  
133 0 my $s;
134 0 ▶ croak "You can't use 'fields' and 'space' at the same time"
Branch: 1 branch
BranchTrueFalseTotal
if $fields and $space_name 0 0 0
Condition0%0/3
$fields and $space_name
!ll&&!rl&&r
000
MC/DC0%0/2
$fields and $space_name
$fields $space_name
Truth table0%0/3
A and B
A= $fieldsB= $space_name
ABresult
0X0
100
111
135 if $fields and $space_name;
136  
137 0 ▶ if ($space_name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($space_name) { } 0 0 0
elsif ($fields) { } 0 0 0
138 0 $s = $self->space( $space_name );
139 } elsif ( $fields ) {
140 0 $s = DR::Tarantool::Space->new(
141 0 =>
142 {
143 name => 'temp_space',
144 fields => $fields,
145 indexes => {}
146 },
147 );
148 } else {
149 0 $s = DR::Tarantool::Space->new(
150 0 =>
151 {
152 name => 'temp_space',
153 fields => [],
154 indexes => {}
155 },
156 );
157 }
158  
159 0 ▶ if ($opts{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"args"}) 0 0 0
160 my $sa = DR::Tarantool::Space->new(
161 0 =>
162 {
163 name => 'temp_space_args',
164 fields => $opts{args},
165 0 indexes => {}
166 },
167 );
168 0 $args = $sa->pack_tuple( $args );
169 }
170  
171 0 my $res = $self->_llc->call_lua( $lua_name, $args, $flags );
172  
173 0 return $unpack->($self, $res, $s);
174 }
175  
176  
177 sub select {
178 0 ▶ my $self = shift;
Subroutine select: not called CC=5 SCAR=34.0
Pod undocumented
179 0 my $space = shift;
180 0 my $keys = shift;
181  
182 0 my ($index, $limit, $offset);
183  
184 0 ▶ if (@_ == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ == 3) { } 0 0 0
elsif (@_) { } 0 0 0
185 0 $index = shift;
186 } elsif (@_ == 3) {
187 0 ($index, $limit, $offset) = @_;
188 } elsif (@_) {
189 0 my %opts = @_;
190 0 $index = $opts{index};
191 0 $limit = $opts{limit};
192 0 $offset = $opts{offset};
193 }
194  
195 0 ▶ $index ||= 0;
Condition0%0/2
$index ||= 0
l!l
00
MC/DC0%0/1
$index ||= 0
$index
Truth table0%0/2
A
A= $index
Aresult
11
00
196  
197 0 my $s = $self->space($space);
198  
199 my $res = $self->_llc->select(
200 $s->number,
201 $s->_index( $index )->{no},
202 0 $s->pack_keys( $keys, $index ),
203 $limit,
204 $offset
205 );
206  
207 0 return $unpack->($self, $res, $s);
208 }
209  
210 sub update {
211 0 ▶ my $self = shift;
Subroutine update: not called CC=2 SCAR=17.9
Pod undocumented
212 0 my $space = shift;
213 0 my $key = shift;
214 0 my $op = shift;
215 0 ▶ my $flags = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
216  
217 0 my $s = $self->space($space);
218  
219 0 my $res = $self->_llc->update(
220 $s->number,
221 $s->pack_primary_key( $key ),
222 $s->pack_operations( $op ),
223 $flags,
224 );
225 0 return $unpack->($self, $res, $s);
226 }
227  
228 sub delete :method {
229 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Pod undocumented
230 0 my $space = shift;
231 0 my $key = shift;
232 0 ▶ my $flags = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
233  
234 0 my $s = $self->space($space);
235  
236 0 my $res = $self->_llc->delete(
237 $s->number,
238 $s->pack_primary_key( $key ),
239 $flags,
240 );
241 0 return $unpack->($self, $res, $s);
242 }
243  
244 0 ▶sub last_code { $_[0]->{llc}->last_code }
Subroutine last_code: not called CC=1 SCAR=6.9
Pod undocumented
245 0 ▶sub last_error_string { $_[0]->{llc}->last_error_string }
Subroutine last_error_string: not called CC=1 SCAR=6.9
Pod undocumented
246 0 ▶sub raise_error { $_[0]->raise_error };
Subroutine raise_error: not called CC=1 SCAR=6.9
Pod undocumented
247 0 ▶sub _llc { $_[0]{llc} }
Subroutine _llc: not called CC=1 SCAR=6.9
248  
249 1 1;
« blib/lib/DR/Tarantool/MsgPack/Proto.pm ↑ Summary blib/lib/DR/Tarantool/LLClient.pm »