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\Exception\RequestFailed; |
17
|
|
|
use Tarantool\Client\Handler\Handler; |
18
|
|
|
use Tarantool\Client\IProto; |
19
|
|
|
use Tarantool\Client\Request\Delete; |
20
|
|
|
use Tarantool\Client\Request\Insert; |
21
|
|
|
use Tarantool\Client\Request\Replace; |
22
|
|
|
use Tarantool\Client\Request\Select; |
23
|
16 |
|
use Tarantool\Client\Request\Update; |
24
|
|
|
use Tarantool\Client\Request\Upsert; |
25
|
16 |
|
|
26
|
16 |
|
final class Space |
27
|
16 |
|
{ |
28
|
|
|
public const VSPACE_ID = 281; |
29
|
1 |
|
public const VINDEX_ID = 289; |
30
|
|
|
|
31
|
1 |
|
private $handler; |
32
|
|
|
private $id; |
33
|
|
|
private $indexes = []; |
34
|
34 |
|
|
35
|
|
|
public function __construct(Handler $handler, int $id) |
36
|
34 |
|
{ |
37
|
34 |
|
$this->handler = $handler; |
38
|
34 |
|
$this->id = $id; |
39
|
34 |
|
} |
40
|
34 |
|
|
41
|
|
|
public function getId() : int |
42
|
34 |
|
{ |
43
|
|
|
return $this->id; |
44
|
34 |
|
} |
45
|
|
|
|
46
|
|
|
public function select(Criteria $criteria) : array |
47
|
16 |
|
{ |
48
|
|
|
$index = $criteria->getIndex(); |
49
|
16 |
|
|
50
|
|
|
if (\is_string($index)) { |
|
|
|
|
51
|
16 |
|
$index = $this->getIndexIdByName($index); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$request = new Select( |
55
|
|
|
$this->id, |
56
|
2 |
|
$index, |
57
|
|
|
$criteria->getKey(), |
58
|
2 |
|
$criteria->getOffset(), |
59
|
|
|
$criteria->getLimit(), |
60
|
|
|
$criteria->getIteratorType() |
61
|
13 |
|
); |
62
|
|
|
|
63
|
13 |
|
return $this->handler->handle($request)->getBodyField(IProto::DATA); |
64
|
13 |
|
} |
65
|
|
|
|
66
|
13 |
|
public function insert(array $tuple) : array |
67
|
|
|
{ |
68
|
|
|
$request = new Insert($this->id, $tuple); |
69
|
1 |
|
|
70
|
|
|
return $this->handler->handle($request)->getBodyField(IProto::DATA); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
1 |
|
public function replace(array $tuple) : array |
74
|
|
|
{ |
75
|
|
|
$request = new Replace($this->id, $tuple); |
76
|
5 |
|
|
77
|
|
|
return $this->handler->handle($request)->getBodyField(IProto::DATA); |
78
|
5 |
|
} |
79
|
5 |
|
|
80
|
|
|
public function update(array $key, Operations $operations, $index = 0) : array |
81
|
5 |
|
{ |
82
|
|
|
if (\is_string($index)) { |
83
|
|
|
$index = $this->getIndexIdByName($index); |
84
|
2 |
|
} |
85
|
|
|
|
86
|
2 |
|
$request = new Update($this->id, $index, $key, $operations->toArray()); |
87
|
2 |
|
|
88
|
|
|
return $this->handler->handle($request)->getBodyField(IProto::DATA); |
89
|
16 |
|
} |
90
|
|
|
|
91
|
16 |
|
public function upsert(array $tuple, Operations $operations) : void |
92
|
12 |
|
{ |
93
|
|
|
$request = new Upsert($this->id, $tuple, $operations->toArray()); |
94
|
|
|
|
95
|
5 |
|
$this->handler->handle($request); |
96
|
5 |
|
} |
97
|
5 |
|
|
98
|
|
|
public function delete(array $key, $index = 0) : array |
99
|
5 |
|
{ |
100
|
1 |
|
if (\is_string($index)) { |
101
|
|
|
$index = $this->getIndexIdByName($index); |
102
|
|
|
} |
103
|
4 |
|
|
104
|
|
|
$request = new Delete($this->id, $index, $key); |
105
|
|
|
|
106
|
49 |
|
return $this->handler->handle($request)->getBodyField(IProto::DATA); |
107
|
|
|
} |
108
|
49 |
|
|
109
|
26 |
|
public function flushIndexes() : void |
110
|
|
|
{ |
111
|
|
|
$this->indexes = []; |
112
|
32 |
|
} |
113
|
21 |
|
|
114
|
|
|
private function getIndexIdByName(string $indexName) : int |
115
|
|
|
{ |
116
|
16 |
|
if (isset($this->indexes[$indexName])) { |
117
|
|
|
return $this->indexes[$indexName]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$schema = new self($this->handler, self::VINDEX_ID); |
121
|
|
|
$data = $schema->select(Criteria::key([$this->id, $indexName])->andIndex(Index::INDEX_NAME)); |
122
|
|
|
|
123
|
|
|
if ([] === $data) { |
124
|
|
|
throw RequestFailed::unknownIndex($indexName, $this->id); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->indexes[$indexName] = $data[0][1]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|