1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Tarantool Client package. |
7
|
|
|
* |
8
|
|
|
* (c) Eugene Leonovich <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Schema; |
15
|
|
|
|
16
|
|
|
use Tarantool\Client\Client; |
17
|
|
|
use Tarantool\Client\Exception\Exception; |
18
|
|
|
use Tarantool\Client\Request\DeleteRequest; |
19
|
|
|
use Tarantool\Client\Request\InsertRequest; |
20
|
|
|
use Tarantool\Client\Request\ReplaceRequest; |
21
|
|
|
use Tarantool\Client\Request\SelectRequest; |
22
|
|
|
use Tarantool\Client\Request\UpdateRequest; |
23
|
|
|
use Tarantool\Client\Request\UpsertRequest; |
24
|
|
|
use Tarantool\Client\Response\BinaryResponse; |
25
|
|
|
|
26
|
|
|
final class Space |
27
|
|
|
{ |
28
|
|
|
public const VSPACE = 281; |
29
|
|
|
public const VINDEX = 289; |
30
|
|
|
|
31
|
|
|
private $client; |
32
|
|
|
private $id; |
33
|
|
|
private $indexes = []; |
34
|
|
|
|
35
|
62 |
|
public function __construct(Client $client, int $id) |
36
|
|
|
{ |
37
|
62 |
|
$this->client = $client; |
38
|
62 |
|
$this->id = $id; |
39
|
62 |
|
} |
40
|
|
|
|
41
|
1 |
|
public function getId() : int |
42
|
|
|
{ |
43
|
1 |
|
return $this->id; |
44
|
|
|
} |
45
|
|
|
|
46
|
61 |
|
public function select(array $key = [], $index = 0, int $limit = \PHP_INT_MAX &0xffffffff, int $offset = 0, int $iteratorType = IteratorTypes::EQ) : BinaryResponse |
47
|
|
|
{ |
48
|
61 |
|
if (\is_string($index)) { |
49
|
15 |
|
$index = $this->getIndexIdByName($index); |
50
|
|
|
} |
51
|
|
|
|
52
|
61 |
|
$request = new SelectRequest($this->id, $index, $key, $offset, $limit, $iteratorType); |
53
|
|
|
|
54
|
61 |
|
return BinaryResponse::createFromRaw($this->client->sendRequest($request)); |
55
|
|
|
} |
56
|
|
|
|
57
|
15 |
|
public function insert(array $values) : BinaryResponse |
58
|
|
|
{ |
59
|
15 |
|
$request = new InsertRequest($this->id, $values); |
60
|
|
|
|
61
|
15 |
|
return BinaryResponse::createFromRaw($this->client->sendRequest($request)); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function replace(array $values) : BinaryResponse |
65
|
|
|
{ |
66
|
1 |
|
$request = new ReplaceRequest($this->id, $values); |
67
|
|
|
|
68
|
1 |
|
return BinaryResponse::createFromRaw($this->client->sendRequest($request)); |
69
|
|
|
} |
70
|
|
|
|
71
|
12 |
|
public function update(array $key, array $operations, $index = 0) : BinaryResponse |
72
|
|
|
{ |
73
|
12 |
|
if (\is_string($index)) { |
74
|
|
|
$index = $this->getIndexIdByName($index); |
75
|
|
|
} |
76
|
|
|
|
77
|
12 |
|
$request = new UpdateRequest($this->id, $index, $key, $operations); |
78
|
|
|
|
79
|
12 |
|
return BinaryResponse::createFromRaw($this->client->sendRequest($request)); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function upsert(array $values, array $operations) : BinaryResponse |
83
|
|
|
{ |
84
|
1 |
|
$request = new UpsertRequest($this->id, $values, $operations); |
85
|
|
|
|
86
|
1 |
|
return BinaryResponse::createFromRaw($this->client->sendRequest($request)); |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
public function delete(array $key, $index = 0) : BinaryResponse |
90
|
|
|
{ |
91
|
4 |
|
if (\is_string($index)) { |
92
|
1 |
|
$index = $this->getIndexIdByName($index); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
$request = new DeleteRequest($this->id, $index, $key); |
96
|
|
|
|
97
|
4 |
|
return BinaryResponse::createFromRaw($this->client->sendRequest($request)); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public function flushIndexes() : void |
101
|
|
|
{ |
102
|
2 |
|
$this->indexes = []; |
103
|
2 |
|
} |
104
|
|
|
|
105
|
16 |
|
private function getIndexIdByName(string $indexName) : int |
106
|
|
|
{ |
107
|
16 |
|
if (isset($this->indexes[$indexName])) { |
108
|
1 |
|
return $this->indexes[$indexName]; |
109
|
|
|
} |
110
|
|
|
|
111
|
16 |
|
$schema = $this->client->getSpaceById(self::VINDEX); |
112
|
16 |
|
$response = $schema->select([$this->id, $indexName], Index::INDEX_NAME); |
113
|
16 |
|
$data = $response->getData(); |
114
|
|
|
|
115
|
16 |
|
if (empty($data)) { |
116
|
1 |
|
throw new Exception("No index '$indexName' is defined in space #{$this->id}"); |
117
|
|
|
} |
118
|
|
|
|
119
|
15 |
|
return $this->indexes[$indexName] = $response->getData()[0][1]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|