Completed
Push — master ( f6db52...9cdeb8 )
by Dmitry
14:08
created

Schema::initOverride()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace Tarantool\Mapper\Plugin\Temporal;
4
5
use Tarantool\Mapper\Entity;
6
use Tarantool\Mapper\Mapper;
7
use InvalidArgumentException;
8
9
class Schema
10
{
11
    private $mapper;
12
13
    public function __construct(Mapper $mapper)
14
    {
15
        $this->mapper = $mapper;
16
    }
17
18
    public function init($name)
19
    {
20
        if (!$name) {
21
            throw new InvalidArgumentException("Nothing to initialize");
22
        }
23
24
        $method = 'init'.ucfirst($name);
25
        if (!method_exists($this, $method)) {
26
            throw new InvalidArgumentException("No initializer for $name");
27
        }
28
29
        $this->$method();
30
    }
31
32
    private function initLink()
33
    {
34
        $this->mapper->getSchema()->once(__CLASS__.'@link', function (Mapper $mapper) {
35
            $mapper->getSchema()
36
                ->createSpace('_temporal_link', [
37
                    'id'        => 'unsigned',
38
                    'parent'    => 'unsigned',
39
                    'entity'    => 'unsigned',
40
                    'entityId'  => 'unsigned',
41
                    'begin'     => 'unsigned',
42
                    'end'       => 'unsigned',
43
                    'timestamp' => 'unsigned',
44
                    'actor'     => 'unsigned',
45
                    'data'      => '*',
46
                ])
47
                ->addIndex(['id'])
48
                ->addIndex(['entity', 'entityId', 'parent', 'begin', 'timestamp', 'actor'])
49
                ->addIndex([
50
                    'fields' => 'parent',
51
                    'unique' => false,
52
                ]);
53
54
            $mapper->getSchema()
55
                ->createSpace('_temporal_link_aggregate', [
56
                    'entity' => 'unsigned',
57
                    'id'     => 'unsigned',
58
                    'begin'  => 'unsigned',
59
                    'end'    => 'unsigned',
60
                    'data'   => '*',
61
                ])
62
                ->addIndex(['entity', 'id', 'begin']);
63
        });
64
65
        $this->mapper->getSchema()->once(__CLASS__.'@link-idle', function (Mapper $mapper) {
66
            $mapper->getSchema()->getSpace('_temporal_link')->addProperty('idle', 'unsigned');
67
        });
68
    }
69
70
    private function initOverride()
71
    {
72
        $this->mapper->getSchema()->once(__CLASS__.'@states', function (Mapper $mapper) {
73
            $mapper->getSchema()
74
                ->createSpace('_temporal_override', [
75
                    'entity'     => 'unsigned',
76
                    'id'         => 'unsigned',
77
                    'begin'      => 'unsigned',
78
                    'end'        => 'unsigned',
79
                    'timestamp'  => 'unsigned',
80
                    'actor'      => 'unsigned',
81
                    'data'       => '*',
82
                ])
83
                ->addIndex(['entity', 'id', 'begin', 'timestamp', 'actor']);
84
85
            $mapper->getSchema()
86
                ->createSpace('_temporal_override_aggregate', [
87
                    'entity'     => 'unsigned',
88
                    'id'         => 'unsigned',
89
                    'begin'      => 'unsigned',
90
                    'end'        => 'unsigned',
91
                    'data'       => '*',
92
                ])
93
                ->addIndex(['entity', 'id', 'begin']);
94
        });
95
96
        $this->mapper->getSchema()->once(__CLASS__.'@override-idle', function (Mapper $mapper) {
97
            $mapper->getSchema()->getSpace('_temporal_override')->addProperty('idle', 'unsigned');
98
        });
99
    }
100
101
    private function initReference()
102
    {
103
        $this->mapper->getSchema()->once(__CLASS__.'@reference', function (Mapper $mapper) {
104
            $mapper->getSchema()
105
                ->createSpace('_temporal_reference', [
106
                    'idle'       => 'unsigned',
107
                    'entity'     => 'unsigned',
108
                    'id'         => 'unsigned',
109
                    'begin'      => 'unsigned',
110
                    'end'        => 'unsigned',
111
                    'target'     => 'unsigned',
112
                    'targetId'   => 'unsigned',
113
                    'timestamp'  => 'unsigned',
114
                    'actor'      => 'unsigned',
115
                ])
116
                ->addIndex(['entity', 'id', 'target', 'targetId', 'begin', 'timestamp', 'actor']);
117
118
            $mapper->getSchema()
119
                ->createSpace('_temporal_reference_state', [
120
                    'entity'     => 'unsigned',
121
                    'id'         => 'unsigned',
122
                    'target'     => 'unsigned',
123
                    'begin'      => 'unsigned',
124
                    'end'        => 'unsigned',
125
                    'targetId'   => 'unsigned',
126
                ])
127
                ->addIndex(['entity', 'id', 'target', 'begin'])
128
                ->addIndex(['target', 'targetId', 'entity', 'begin', 'id'])
129
                ;
130
131
            $mapper->getSchema()
132
                ->createSpace('_temporal_reference_aggregate', [
133
                    'entity'     => 'unsigned',
134
                    'id'         => 'unsigned',
135
                    'source'     => 'unsigned',
136
                    'begin'      => 'unsigned',
137
                    'end'        => 'unsigned',
138
                    'data'       => '*',
139
                ])
140
                ->addIndex(['entity', 'id', 'source', 'begin']);
141
        });
142
    }
143
}
144