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
|
22 |
|
public function __construct(Client $client) |
18
|
|
|
{ |
19
|
22 |
|
$this->client = $client; |
20
|
|
|
|
21
|
22 |
|
$this->spaceSpace = $client->getSpace('_vspace'); |
22
|
22 |
|
$this->indexSpace = $client->getSpace('_vindex'); |
23
|
22 |
|
} |
24
|
|
|
|
25
|
22 |
View Code Duplication |
public function getSpaceId($space) |
|
|
|
|
26
|
|
|
{ |
27
|
22 |
|
if (!array_key_exists($space, $this->spaceId)) { |
28
|
22 |
|
$response = $this->spaceSpace->select([$space], Index::SPACE_NAME); |
29
|
22 |
|
$data = $response->getData(); |
30
|
22 |
|
if (!empty($data)) { |
31
|
22 |
|
$this->spaceId[$space] = $data[0][0]; |
32
|
22 |
|
} |
33
|
22 |
|
} |
34
|
22 |
|
if (array_key_exists($space, $this->spaceId)) { |
35
|
22 |
|
return $this->spaceId[$space]; |
36
|
|
|
} |
37
|
22 |
|
} |
38
|
|
|
|
39
|
1 |
View Code Duplication |
public function getSpaceName($spaceId) |
|
|
|
|
40
|
|
|
{ |
41
|
1 |
|
if (!in_array($spaceId, $this->spaceId)) { |
42
|
|
|
$response = $this->spaceSpace->select([$spaceId], 0); |
43
|
|
|
$data = $response->getData(); |
44
|
|
|
if (!empty($data)) { |
45
|
|
|
$this->spaceId[$data[0][2]] = $spaceId; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
if (in_array($spaceId, $this->spaceId)) { |
50
|
1 |
|
return array_search($spaceId, $this->spaceId); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
22 |
|
public function hasSpace($space) |
55
|
|
|
{ |
56
|
22 |
|
return $this->getSpaceId($space) !== null; |
57
|
|
|
} |
58
|
|
|
|
59
|
22 |
|
public function createSpace($space) |
60
|
|
|
{ |
61
|
22 |
|
$this->client->evaluate("box.schema.space.create('$space')"); |
62
|
22 |
|
} |
63
|
|
|
|
64
|
22 |
|
public function hasIndex($space, $index) |
65
|
|
|
{ |
66
|
22 |
|
$spaceId = $this->getSpaceId($space); |
67
|
22 |
|
$response = $this->indexSpace->select([$spaceId, $index], Index::INDEX_NAME); |
68
|
|
|
|
69
|
22 |
|
return !empty($response->getData()); |
70
|
|
|
} |
71
|
|
|
|
72
|
22 |
|
public function listIndexes($space) |
73
|
|
|
{ |
74
|
22 |
|
$result = []; |
75
|
22 |
|
$response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME); |
76
|
|
|
|
77
|
22 |
|
foreach ($response->getData() as $row) { |
|
|
|
|
78
|
22 |
|
$result[$row[2]] = []; |
79
|
22 |
|
foreach ($row[5] as $f) { |
80
|
22 |
|
$result[$row[2]][] = $f[0]; |
81
|
22 |
|
} |
82
|
22 |
|
} |
83
|
|
|
|
84
|
22 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
22 |
|
public function createIndex($space, $index, array $arguments) |
88
|
|
|
{ |
89
|
22 |
|
$config = []; |
90
|
22 |
|
foreach ($arguments as $k => $v) { |
91
|
22 |
|
if (is_array($v)) { |
92
|
|
|
// convert to lua array |
93
|
22 |
|
$v = str_replace(['[', ']'], ['{', '}'], json_encode($v)); |
94
|
22 |
|
} |
95
|
22 |
|
if (is_bool($v)) { |
96
|
22 |
|
$v = $v ? 'true' : 'false'; |
97
|
22 |
|
} |
98
|
22 |
|
$config[] = $k.' = '.$v; |
99
|
22 |
|
} |
100
|
22 |
|
$config = '{'.implode(', ', $config).'}'; |
101
|
22 |
|
$this->client->evaluate("box.space.$space:create_index('$index', $config)"); |
102
|
22 |
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.