1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Schema; |
4
|
|
|
|
5
|
|
|
use Tarantool\Client; |
6
|
|
|
use Tarantool\Mapper\Contracts; |
7
|
|
|
use Tarantool\Schema\Space; |
8
|
|
|
use Tarantool\Schema\Index; |
9
|
|
|
|
10
|
|
|
class Schema implements Contracts\Schema |
11
|
|
|
{ |
12
|
|
|
protected $client; |
13
|
|
|
protected $spaceSpace; |
14
|
|
|
protected $indexSpace; |
15
|
|
|
protected $spaceId = []; |
16
|
|
|
|
17
|
59 |
|
public function __construct(Client $client) |
18
|
|
|
{ |
19
|
59 |
|
$this->client = $client; |
20
|
|
|
|
21
|
59 |
|
$this->spaceSpace = $client->getSpace('_vspace'); |
22
|
59 |
|
$this->indexSpace = $client->getSpace('_vindex'); |
23
|
59 |
|
$spaces = $this->spaceSpace->select([])->getData(); |
24
|
59 |
|
foreach ($spaces as $row) { |
|
|
|
|
25
|
59 |
|
list($id, $sys, $name) = $row; |
|
|
|
|
26
|
59 |
|
$this->spaceId[$name] = $id; |
27
|
|
|
} |
28
|
59 |
|
} |
29
|
|
|
|
30
|
59 |
View Code Duplication |
public function getSpaceId($space) |
|
|
|
|
31
|
|
|
{ |
32
|
59 |
|
if (!array_key_exists($space, $this->spaceId)) { |
33
|
59 |
|
$response = $this->spaceSpace->select([$space], Index::SPACE_NAME); |
34
|
59 |
|
$data = $response->getData(); |
35
|
59 |
|
if (!empty($data)) { |
36
|
59 |
|
$this->spaceId[$space] = $data[0][0]; |
37
|
|
|
} |
38
|
|
|
} |
39
|
59 |
|
if (array_key_exists($space, $this->spaceId)) { |
40
|
59 |
|
return $this->spaceId[$space]; |
41
|
|
|
} |
42
|
59 |
|
} |
43
|
|
|
|
44
|
7 |
View Code Duplication |
public function getSpaceName($spaceId) |
|
|
|
|
45
|
|
|
{ |
46
|
7 |
|
if (!in_array($spaceId, $this->spaceId)) { |
47
|
|
|
$response = $this->spaceSpace->select([$spaceId], 0); |
48
|
|
|
$data = $response->getData(); |
49
|
|
|
if (!empty($data)) { |
50
|
|
|
$this->spaceId[$data[0][2]] = $spaceId; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
7 |
|
if (in_array($spaceId, $this->spaceId)) { |
55
|
7 |
|
return array_search($spaceId, $this->spaceId); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
59 |
|
public function hasSpace($space) |
60
|
|
|
{ |
61
|
59 |
|
return $this->getSpaceId($space) !== null; |
62
|
|
|
} |
63
|
|
|
|
64
|
59 |
|
public function createSpace($space) |
65
|
|
|
{ |
66
|
59 |
|
$this->client->evaluate("box.schema.space.create('$space')"); |
67
|
59 |
|
} |
68
|
|
|
|
69
|
2 |
|
public function dropSpace($space) |
70
|
|
|
{ |
71
|
2 |
|
$this->client->evaluate('box.schema.space.drop('.$this->getSpaceId($space).')'); |
72
|
2 |
|
unset($this->spaceId[$space]); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
59 |
|
public function hasIndex($space, $index) |
76
|
|
|
{ |
77
|
59 |
|
$spaceId = $this->getSpaceId($space); |
78
|
59 |
|
$response = $this->indexSpace->select([$spaceId, $index], Index::INDEX_NAME); |
79
|
|
|
|
80
|
59 |
|
return !empty($response->getData()); |
81
|
|
|
} |
82
|
|
|
|
83
|
59 |
|
public function listIndexes($space) |
84
|
|
|
{ |
85
|
59 |
|
$result = []; |
86
|
59 |
|
$response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME); |
87
|
|
|
|
88
|
59 |
|
foreach ($response->getData() as $row) { |
|
|
|
|
89
|
59 |
|
$result[$row[2]] = []; |
90
|
59 |
|
foreach ($row[5] as $f) { |
91
|
59 |
|
$result[$row[2]][] = $f[0]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
59 |
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
59 |
|
public function createIndex($space, $index, array $arguments) |
99
|
|
|
{ |
100
|
59 |
|
$config = []; |
101
|
59 |
|
foreach ($arguments as $k => $v) { |
102
|
59 |
|
if (is_array($v)) { |
103
|
|
|
// convert to lua array |
104
|
59 |
|
$v = str_replace(['[', ']'], ['{', '}'], json_encode($v)); |
105
|
|
|
} |
106
|
59 |
|
if (is_bool($v)) { |
107
|
59 |
|
$v = $v ? 'true' : 'false'; |
108
|
|
|
} |
109
|
59 |
|
$config[] = $k.' = '.$v; |
110
|
|
|
} |
111
|
59 |
|
$config = '{'.implode(', ', $config).'}'; |
112
|
59 |
|
$this->client->evaluate("box.space.$space:create_index('$index', $config)"); |
113
|
|
|
|
114
|
59 |
|
$schema = $this->client->getSpace(Space::VINDEX); |
115
|
59 |
|
$response = $schema->select([$this->getSpaceId($space), $index], Index::INDEX_NAME); |
116
|
|
|
|
117
|
59 |
|
return $response->getData()[0][1]; |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
public function dropIndex($spaceId, $index) |
121
|
|
|
{ |
122
|
3 |
|
$space = $this->client->getSpace('_vindex'); |
123
|
3 |
|
$row = $space->select([$spaceId, $index])->getData(); |
124
|
3 |
|
$spaceName = $this->getSpaceName($spaceId); |
125
|
3 |
|
$indexName = $row[0][2]; |
126
|
3 |
|
$this->client->evaluate("box.space.$spaceName.index.$indexName:drop{}"); |
127
|
3 |
|
} |
128
|
|
|
} |
129
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.