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