1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Client; |
4
|
|
|
|
5
|
|
|
use Tarantool\Client\Connection\Connection; |
6
|
|
|
use Tarantool\Client\Exception\Exception; |
7
|
|
|
use Tarantool\Client\Packer\Packer; |
8
|
|
|
use Tarantool\Client\Request\AuthenticateRequest; |
9
|
|
|
use Tarantool\Client\Request\CallRequest; |
10
|
|
|
use Tarantool\Client\Request\EvaluateRequest; |
11
|
|
|
use Tarantool\Client\Request\PingRequest; |
12
|
|
|
use Tarantool\Client\Request\Request; |
13
|
|
|
use Tarantool\Client\Schema\Index; |
14
|
|
|
use Tarantool\Client\Schema\Space; |
15
|
|
|
|
16
|
|
|
class Client |
17
|
|
|
{ |
18
|
|
|
private $connection; |
19
|
|
|
private $packer; |
20
|
|
|
private $salt; |
21
|
|
|
private $username; |
22
|
|
|
private $password; |
23
|
|
|
private $spaces = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Connection $connection |
27
|
|
|
* @param Packer $packer |
28
|
|
|
*/ |
29
|
50 |
|
public function __construct(Connection $connection, Packer $packer) |
30
|
|
|
{ |
31
|
50 |
|
$this->connection = $connection; |
32
|
50 |
|
$this->packer = $packer; |
33
|
50 |
|
} |
34
|
|
|
|
35
|
3 |
|
public function getConnection() |
36
|
|
|
{ |
37
|
3 |
|
return $this->connection; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function getPacker() |
41
|
|
|
{ |
42
|
1 |
|
return $this->packer; |
43
|
|
|
} |
44
|
|
|
|
45
|
50 |
|
public function connect() |
46
|
|
|
{ |
47
|
50 |
|
$this->salt = $this->connection->open(); |
48
|
|
|
|
49
|
29 |
|
if ($this->username) { |
50
|
2 |
|
$this->authenticate($this->username, $this->password); |
51
|
|
|
} |
52
|
29 |
|
} |
53
|
|
|
|
54
|
47 |
|
public function disconnect() |
55
|
|
|
{ |
56
|
47 |
|
$this->connection->close(); |
57
|
47 |
|
$this->salt = null; |
58
|
47 |
|
} |
59
|
|
|
|
60
|
14 |
|
public function isDisconnected() |
61
|
|
|
{ |
62
|
14 |
|
return $this->connection->isClosed() || !$this->salt; |
63
|
|
|
} |
64
|
|
|
|
65
|
14 |
|
public function authenticate($username, $password = null) |
66
|
|
|
{ |
67
|
14 |
|
if ($this->isDisconnected()) { |
68
|
12 |
|
$this->salt = $this->connection->open(); |
69
|
|
|
} |
70
|
|
|
|
71
|
14 |
|
$request = new AuthenticateRequest($this->salt, $username, $password); |
72
|
14 |
|
$response = $this->sendRequest($request); |
73
|
|
|
|
74
|
10 |
|
$this->username = $username; |
75
|
10 |
|
$this->password = $password; |
76
|
|
|
|
77
|
10 |
|
$this->flushSpaces(); |
78
|
|
|
|
79
|
10 |
|
return $response; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
public function ping() |
83
|
|
|
{ |
84
|
6 |
|
$request = new PingRequest(); |
85
|
|
|
|
86
|
6 |
|
return $this->sendRequest($request); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string|int $space |
91
|
|
|
* |
92
|
|
|
* @return Space |
93
|
|
|
*/ |
94
|
63 |
|
public function getSpace($space) |
95
|
|
|
{ |
96
|
63 |
|
if (isset($this->spaces[$space])) { |
97
|
57 |
|
return $this->spaces[$space]; |
98
|
|
|
} |
99
|
|
|
|
100
|
16 |
|
if (!is_string($space)) { |
101
|
10 |
|
return $this->spaces[$space] = new Space($this, $space); |
102
|
|
|
} |
103
|
|
|
|
104
|
14 |
|
$spaceId = $this->getSpaceIdByName($space); |
105
|
|
|
|
106
|
12 |
|
return $this->spaces[$space] = $this->spaces[$spaceId] = new Space($this, $spaceId); |
107
|
|
|
} |
108
|
|
|
|
109
|
8 |
|
public function call($funcName, array $args = []) |
110
|
|
|
{ |
111
|
8 |
|
$request = new CallRequest($funcName, $args); |
112
|
|
|
|
113
|
8 |
|
return $this->sendRequest($request); |
114
|
|
|
} |
115
|
|
|
|
116
|
26 |
|
public function evaluate($expr, array $args = []) |
117
|
|
|
{ |
118
|
26 |
|
$request = new EvaluateRequest($expr, $args); |
119
|
|
|
|
120
|
26 |
|
return $this->sendRequest($request); |
121
|
|
|
} |
122
|
|
|
|
123
|
12 |
|
public function flushSpaces() |
124
|
|
|
{ |
125
|
12 |
|
$this->spaces = []; |
126
|
12 |
|
} |
127
|
|
|
|
128
|
110 |
|
public function sendRequest(Request $request) |
129
|
|
|
{ |
130
|
110 |
|
if ($this->connection->isClosed()) { |
131
|
26 |
|
$this->connect(); |
132
|
|
|
} |
133
|
|
|
|
134
|
108 |
|
$data = $this->packer->pack($request); |
135
|
108 |
|
$data = $this->connection->send($data); |
136
|
|
|
|
137
|
108 |
|
return $this->packer->unpack($data); |
138
|
|
|
} |
139
|
|
|
|
140
|
14 |
|
private function getSpaceIdByName($spaceName) |
141
|
|
|
{ |
142
|
14 |
|
$schema = $this->getSpace(Space::VSPACE); |
143
|
14 |
|
$response = $schema->select([$spaceName], Index::SPACE_NAME); |
144
|
14 |
|
$data = $response->getData(); |
145
|
|
|
|
146
|
14 |
|
if (empty($data)) { |
147
|
3 |
|
throw new Exception("Space '$spaceName' does not exist"); |
148
|
|
|
} |
149
|
|
|
|
150
|
12 |
|
return $data[0][0]; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|