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
|
25 |
|
public function __construct(Client $client) |
18
|
|
|
{ |
19
|
25 |
|
$this->client = $client; |
20
|
|
|
|
21
|
25 |
|
$this->spaceSpace = $client->getSpace('_vspace'); |
22
|
25 |
|
$this->indexSpace = $client->getSpace('_vindex'); |
23
|
25 |
|
$spaces = $this->spaceSpace->select([])->getData(); |
24
|
25 |
|
foreach ($spaces as $row) { |
25
|
25 |
|
list($id, $sys, $name) = $row; |
|
|
|
|
26
|
25 |
|
$this->spaceId[$name] = $id; |
27
|
|
|
} |
28
|
25 |
|
} |
29
|
|
|
|
30
|
25 |
View Code Duplication |
public function getSpaceId($space) |
|
|
|
|
31
|
|
|
{ |
32
|
25 |
|
if (!array_key_exists($space, $this->spaceId)) { |
33
|
25 |
|
$response = $this->spaceSpace->select([$space], Index::SPACE_NAME); |
34
|
25 |
|
$data = $response->getData(); |
35
|
25 |
|
if (!empty($data)) { |
36
|
25 |
|
$this->spaceId[$space] = $data[0][0]; |
37
|
|
|
} |
38
|
|
|
} |
39
|
25 |
|
if (array_key_exists($space, $this->spaceId)) { |
40
|
25 |
|
return $this->spaceId[$space]; |
41
|
|
|
} |
42
|
25 |
|
} |
43
|
|
|
|
44
|
2 |
View Code Duplication |
public function getSpaceName($spaceId) |
|
|
|
|
45
|
|
|
{ |
46
|
2 |
|
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
|
2 |
|
if (in_array($spaceId, $this->spaceId)) { |
55
|
2 |
|
return array_search($spaceId, $this->spaceId); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
25 |
|
public function hasSpace($space) |
60
|
|
|
{ |
61
|
25 |
|
return $this->getSpaceId($space) !== null; |
62
|
|
|
} |
63
|
|
|
|
64
|
25 |
|
public function createSpace($space) |
65
|
|
|
{ |
66
|
25 |
|
$this->client->evaluate("box.schema.space.create('$space')"); |
67
|
25 |
|
} |
68
|
|
|
|
69
|
25 |
|
public function hasIndex($space, $index) |
70
|
|
|
{ |
71
|
25 |
|
$spaceId = $this->getSpaceId($space); |
72
|
25 |
|
$response = $this->indexSpace->select([$spaceId, $index], Index::INDEX_NAME); |
73
|
|
|
|
74
|
25 |
|
return !empty($response->getData()); |
75
|
|
|
} |
76
|
|
|
|
77
|
25 |
|
public function listIndexes($space) |
78
|
|
|
{ |
79
|
25 |
|
$result = []; |
80
|
25 |
|
$response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME); |
81
|
|
|
|
82
|
25 |
|
foreach ($response->getData() as $row) { |
|
|
|
|
83
|
25 |
|
$result[$row[2]] = []; |
84
|
25 |
|
foreach ($row[5] as $f) { |
85
|
25 |
|
$result[$row[2]][] = $f[0]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
25 |
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
25 |
|
public function createIndex($space, $index, array $arguments) |
93
|
|
|
{ |
94
|
25 |
|
$config = []; |
95
|
25 |
|
foreach ($arguments as $k => $v) { |
96
|
25 |
|
if (is_array($v)) { |
97
|
|
|
// convert to lua array |
98
|
25 |
|
$v = str_replace(['[', ']'], ['{', '}'], json_encode($v)); |
99
|
|
|
} |
100
|
25 |
|
if (is_bool($v)) { |
101
|
25 |
|
$v = $v ? 'true' : 'false'; |
102
|
|
|
} |
103
|
25 |
|
$config[] = $k.' = '.$v; |
104
|
|
|
} |
105
|
25 |
|
$config = '{'.implode(', ', $config).'}'; |
106
|
25 |
|
$this->client->evaluate("box.space.$space:create_index('$index', $config)"); |
107
|
25 |
|
} |
108
|
|
|
} |
109
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.