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