|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gielfeldt\TransactionalPHP; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Indexer |
|
7
|
|
|
* |
|
8
|
|
|
* @package Gielfeldt\TransactionalPHP |
|
9
|
|
|
*/ |
|
10
|
|
|
class Indexer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Operations indexed by key and operation index. |
|
14
|
|
|
* |
|
15
|
|
|
* @var Operation[][] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $index = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Operations indexed by this indexer. |
|
21
|
|
|
* |
|
22
|
|
|
* @var Operation[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $operations = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The connection used by this indexer. |
|
28
|
|
|
* |
|
29
|
|
|
* @var Connection |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $connection; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Indexer constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Connection $connection |
|
37
|
|
|
*/ |
|
38
|
6 |
|
public function __construct(Connection $connection) |
|
39
|
|
|
{ |
|
40
|
6 |
|
$this->connection = $connection; |
|
41
|
6 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get connection. |
|
45
|
|
|
* |
|
46
|
|
|
* @return Connection |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function getConnection() |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $this->connection; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Index operation. |
|
55
|
|
|
* |
|
56
|
|
|
* @param Operation $operation |
|
57
|
|
|
* The operation to index. |
|
58
|
|
|
* @param string $key |
|
59
|
|
|
* (optional) The key to index under. |
|
60
|
|
|
* |
|
61
|
|
|
* @return Operation |
|
62
|
|
|
* The operation indexed. |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function index(Operation $operation, $key = null) |
|
65
|
|
|
{ |
|
66
|
5 |
|
$this->operations[$operation->idx($this->connection)] = $operation; |
|
67
|
5 |
|
if (isset($key)) { |
|
68
|
5 |
|
$this->index[$key][$operation->idx($this->connection)] = $operation; |
|
69
|
5 |
|
} |
|
70
|
5 |
|
$indexer = $this; |
|
71
|
|
|
$operation->onCommit(function ($operation) use ($key, $indexer) { |
|
72
|
1 |
|
$indexer->deIndex($operation, $key); |
|
73
|
5 |
|
}); |
|
74
|
5 |
|
$operation->onRollback(function ($operation) use ($key, $indexer) { |
|
75
|
1 |
|
$indexer->deIndex($operation, $key); |
|
76
|
5 |
|
}); |
|
77
|
5 |
|
return $operation; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* De-index operation. |
|
82
|
|
|
* |
|
83
|
|
|
* @param Operation $operation |
|
84
|
|
|
* The operation to index. |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
* (optional) The key to index under. |
|
87
|
|
|
* |
|
88
|
|
|
* @return Operation |
|
89
|
|
|
* The operation de-indexed. |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function deIndex(Operation $operation, $key = null) |
|
92
|
|
|
{ |
|
93
|
2 |
|
unset($this->operations[$operation->idx($this->connection)]); |
|
94
|
2 |
|
if (isset($key)) { |
|
95
|
2 |
|
unset($this->index[$key][$operation->idx($this->connection)]); |
|
96
|
2 |
|
} |
|
97
|
2 |
|
return $operation; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Lookup operation. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $key |
|
104
|
|
|
* The key to look up. |
|
105
|
|
|
* |
|
106
|
|
|
* @return Operation[] |
|
107
|
|
|
* Operations keyed by operation index. |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function lookup($key) |
|
110
|
|
|
{ |
|
111
|
3 |
|
return isset($this->index[$key]) ? $this->index[$key] : []; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Lookup operation values. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $key |
|
118
|
|
|
* The key to look up. |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
* Values keyed by operation index. |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function lookupValues($key) |
|
124
|
|
|
{ |
|
125
|
1 |
|
$values = []; |
|
126
|
1 |
|
if (isset($this->index[$key])) { |
|
127
|
1 |
|
foreach ($this->index[$key] as $operation) { |
|
128
|
1 |
|
$values[$operation->idx($this->connection)] = $operation->getValue(); |
|
129
|
1 |
|
} |
|
130
|
1 |
|
} |
|
131
|
1 |
|
return $values; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get current indexed operations. |
|
136
|
|
|
* |
|
137
|
|
|
* @return Operation[] |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function getOperations() |
|
140
|
|
|
{ |
|
141
|
1 |
|
return $this->operations; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get current index. |
|
146
|
|
|
* |
|
147
|
|
|
* @return Operation[][] |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function getIndex() |
|
150
|
|
|
{ |
|
151
|
1 |
|
return $this->index; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|