1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Client\Schema; |
4
|
|
|
|
5
|
|
|
use Tarantool\Client\Client; |
6
|
|
|
use Tarantool\Client\Exception\Exception; |
7
|
|
|
use Tarantool\Client\Request\DeleteRequest; |
8
|
|
|
use Tarantool\Client\Request\InsertRequest; |
9
|
|
|
use Tarantool\Client\Request\ReplaceRequest; |
10
|
|
|
use Tarantool\Client\Request\SelectRequest; |
11
|
|
|
use Tarantool\Client\Request\UpdateRequest; |
12
|
|
|
|
13
|
|
|
class Space |
14
|
|
|
{ |
15
|
|
|
const VSPACE = 281; |
16
|
|
|
const VINDEX = 289; |
17
|
|
|
|
18
|
|
|
private $client; |
19
|
|
|
private $id; |
20
|
|
|
private $indexes = []; |
21
|
|
|
|
22
|
8 |
|
public function __construct(Client $client, $id) |
23
|
|
|
{ |
24
|
8 |
|
$this->client = $client; |
25
|
8 |
|
$this->id = $id; |
26
|
8 |
|
} |
27
|
|
|
|
28
|
1 |
|
public function getId() |
29
|
|
|
{ |
30
|
1 |
|
return $this->id; |
31
|
|
|
} |
32
|
|
|
|
33
|
7 |
|
public function select(array $key = null, $index = null, $limit = null, $offset = null, $iteratorType = null) |
34
|
|
|
{ |
35
|
7 |
|
$key = null === $key ? [] : $key; |
36
|
7 |
|
$offset = null === $offset ? 0 : $offset; |
37
|
7 |
|
$limit = null === $limit ? PHP_INT_MAX & 0xffffffff : $limit; |
38
|
7 |
|
$iteratorType = null === $iteratorType ? 0 : $iteratorType; |
39
|
7 |
|
$index = $this->normalizeIndex($index); |
40
|
|
|
|
41
|
7 |
|
$request = new SelectRequest($this->id, $index, $key, $offset, $limit, $iteratorType); |
42
|
|
|
|
43
|
7 |
|
return $this->client->sendRequest($request); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function insert(array $values) |
47
|
|
|
{ |
48
|
1 |
|
$request = new InsertRequest($this->id, $values); |
49
|
|
|
|
50
|
1 |
|
return $this->client->sendRequest($request); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function replace(array $values) |
54
|
|
|
{ |
55
|
1 |
|
$request = new ReplaceRequest($this->id, $values); |
56
|
|
|
|
57
|
1 |
|
return $this->client->sendRequest($request); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function update($key, array $operations, $index = null) |
61
|
|
|
{ |
62
|
1 |
|
$index = $this->normalizeIndex($index); |
63
|
1 |
|
$request = new UpdateRequest($this->id, $index, $key, $operations); |
64
|
|
|
|
65
|
|
|
return $this->client->sendRequest($request); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function delete(array $key, $index = null) |
69
|
|
|
{ |
70
|
1 |
|
$index = $this->normalizeIndex($index); |
71
|
1 |
|
$request = new DeleteRequest($this->id, $index, $key); |
72
|
|
|
|
73
|
1 |
|
return $this->client->sendRequest($request); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function flushIndexes() |
77
|
|
|
{ |
78
|
|
|
$this->indexes = []; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getIndexIdByName($indexName) |
82
|
|
|
{ |
83
|
|
|
if (isset($this->indexes[$indexName])) { |
84
|
|
|
return $this->indexes[$indexName]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$schema = $this->client->getSpace(Space::VINDEX); |
88
|
|
|
$response = $schema->select([$this->id, $indexName], Index::INDEX_NAME); |
89
|
|
|
$data = $response->getData(); |
90
|
|
|
|
91
|
|
|
if (empty($data)) { |
92
|
|
|
throw new Exception("No index '$indexName' is defined in space #{$this->id}"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->indexes[$indexName] = $response->getData()[0][1]; |
96
|
|
|
} |
97
|
|
|
|
98
|
9 |
|
private function normalizeIndex($index) |
99
|
|
|
{ |
100
|
9 |
|
if (null === $index) { |
101
|
8 |
|
return 0; |
102
|
|
|
} |
103
|
|
|
|
104
|
7 |
|
if (is_int($index)) { |
105
|
7 |
|
return $index; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->getIndexIdByName($index); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|