|
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
|
|
|
use LogicException; |
|
10
|
|
|
|
|
11
|
|
|
class Schema implements Contracts\Schema |
|
12
|
|
|
{ |
|
13
|
|
|
protected $client; |
|
14
|
|
|
protected $spaceSpace; |
|
15
|
|
|
protected $indexSpace; |
|
16
|
|
|
|
|
17
|
4 |
|
public function __construct(Client $client) |
|
18
|
|
|
{ |
|
19
|
4 |
|
$this->client = $client; |
|
20
|
4 |
|
$this->spaceSpace = new Space($client, Space::VSPACE); |
|
21
|
4 |
|
$this->indexSpace = new Space($client, Space::VINDEX); |
|
22
|
4 |
|
} |
|
23
|
|
|
|
|
24
|
4 |
|
public function getSpaceId($space) |
|
25
|
|
|
{ |
|
26
|
4 |
|
$response = $this->spaceSpace->select([$space], Index::SPACE_NAME); |
|
27
|
4 |
|
$data = $response->getData(); |
|
28
|
4 |
|
if (!empty($data)) { |
|
29
|
4 |
|
return $data[0][0]; |
|
30
|
|
|
} |
|
31
|
4 |
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
public function hasSpace($space) |
|
34
|
|
|
{ |
|
35
|
4 |
|
return $this->getSpaceId($space) != null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
4 |
|
public function createSpace($space) |
|
39
|
|
|
{ |
|
40
|
4 |
|
$this->client->evaluate("box.schema.space.create('$space')"); |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getIndexId($space, $index) |
|
44
|
|
|
{ |
|
45
|
|
|
$response = $this->indexSpace->select([$this->getSpaceId($space), $index], Index::INDEX_NAME); |
|
46
|
|
|
$data = $response->getData(); |
|
47
|
|
|
|
|
48
|
|
|
if (!empty($data)) { |
|
49
|
|
|
return $data[0][0]; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
public function hasIndex($space, $index) |
|
54
|
|
|
{ |
|
55
|
4 |
|
$spaceId = $this->getSpaceId($space); |
|
56
|
4 |
|
$response = $this->indexSpace->select([$spaceId, $index], Index::INDEX_NAME); |
|
57
|
4 |
|
return !empty($response->getData()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
4 |
|
public function createIndex($space, $index, array $arguments) |
|
61
|
|
|
{ |
|
62
|
4 |
|
$config = []; |
|
63
|
4 |
|
foreach ($arguments as $k => $v) { |
|
64
|
4 |
|
if (is_array($v)) { |
|
65
|
|
|
// convert to lua array |
|
66
|
4 |
|
$v = str_replace(['[', ']'], ['{', '}'], json_encode($v)); |
|
67
|
4 |
|
} |
|
68
|
4 |
|
if (is_bool($v)) { |
|
69
|
4 |
|
$v = $v ? 'true' : 'false'; |
|
70
|
4 |
|
} |
|
71
|
4 |
|
$config[] = $k.' = '.$v; |
|
72
|
4 |
|
} |
|
73
|
4 |
|
$config = '{'.implode(', ', $config).'}'; |
|
74
|
4 |
|
$this->client->evaluate("box.space.$space:create_index('$index', $config)"); |
|
75
|
4 |
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|