|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Sharding\Query; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
|
6
|
|
|
use Bdf\Prime\Query\AbstractReadCommand; |
|
7
|
|
|
use Bdf\Prime\Query\Compiler\Preprocessor\DefaultPreprocessor; |
|
8
|
|
|
use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface; |
|
9
|
|
|
use Bdf\Prime\Query\Contract\Query\KeyValueQueryInterface; |
|
10
|
|
|
use Bdf\Prime\Query\Extension\CachableTrait; |
|
11
|
|
|
use Bdf\Prime\Query\Extension\ExecutableTrait; |
|
12
|
|
|
use Bdf\Prime\Sharding\Extension\ShardPicker; |
|
13
|
|
|
use Bdf\Prime\Sharding\ShardingConnection; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Handle simple key/value query on sharding connection |
|
17
|
|
|
* If the distribution key is found on the filters, the corresponding sharding query is used |
|
18
|
|
|
* In other case, all shards will be queried on |
|
|
|
|
|
|
19
|
|
|
* |
|
20
|
|
|
* @property ShardingConnection $connection |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
|
|
class ShardingKeyValueQuery extends AbstractReadCommand implements KeyValueQueryInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use CachableTrait; |
|
25
|
|
|
use ExecutableTrait; |
|
26
|
|
|
use ShardPicker; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var KeyValueQueryInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $queries = []; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* ShardingKeyValueQuery constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param ShardingConnection $connection |
|
38
|
|
|
* @param PreprocessorInterface $preprocessor |
|
39
|
|
|
*/ |
|
40
|
35 |
|
public function __construct(ShardingConnection $connection, PreprocessorInterface $preprocessor = null) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
35 |
|
parent::__construct($connection, $preprocessor ?: new DefaultPreprocessor()); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
35 |
|
$this->statements = [ |
|
45
|
|
|
'where' => [], |
|
46
|
|
|
'table' => null, |
|
47
|
|
|
'columns' => [], |
|
48
|
|
|
'aggregate' => null, |
|
49
|
|
|
'limit' => null, |
|
50
|
|
|
'values' => [ |
|
51
|
|
|
'data' => [], |
|
52
|
|
|
'types' => [] |
|
|
|
|
|
|
53
|
|
|
] |
|
|
|
|
|
|
54
|
|
|
]; |
|
55
|
35 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
|
|
|
|
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
35 |
|
public function on(ConnectionInterface $connection) |
|
61
|
|
|
{ |
|
62
|
35 |
|
$this->connection = $connection; |
|
|
|
|
|
|
63
|
35 |
|
$this->queries = []; |
|
64
|
|
|
|
|
65
|
35 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
|
|
|
|
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function project($columns = null) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->statements['columns'] = (array) $columns; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
|
|
|
|
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function select($columns = null) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->select($columns); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
|
|
|
|
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function addSelect($columns) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->statements['columns'] = array_merge($this->statements['columns'], $columns); |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
|
|
|
|
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
34 |
|
public function from($from, $alias = null) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
34 |
|
$this->statements['table'] = $from; |
|
102
|
|
|
|
|
103
|
34 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
|
|
|
|
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
27 |
|
public function where($field, $value = null) |
|
110
|
|
|
{ |
|
111
|
27 |
|
if (is_array($field)) { |
|
112
|
13 |
|
$this->statements['where'] = $field + $this->statements['where']; |
|
|
|
|
|
|
113
|
|
|
} else { |
|
114
|
14 |
|
$this->statements['where'][$field] = $value; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
27 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
|
|
|
|
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
*/ |
|
123
|
9 |
|
public function values(array $values = [], array $types = []) |
|
124
|
|
|
{ |
|
125
|
9 |
|
$this->statements['values'] = [ |
|
126
|
9 |
|
'data' => $values, |
|
127
|
9 |
|
'types' => $types, |
|
128
|
|
|
]; |
|
129
|
|
|
|
|
130
|
9 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
|
|
|
|
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
* |
|
136
|
|
|
* @internal Use internally for optimise "first" query. The offset parameter is not used |
|
137
|
|
|
*/ |
|
138
|
12 |
|
public function limit($limit, $offset = null) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
12 |
|
$this->statements['limit'] = $limit; |
|
141
|
|
|
|
|
142
|
12 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
|
|
|
|
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
7 |
|
public function count($column = null) |
|
149
|
|
|
{ |
|
150
|
7 |
|
return (int) array_sum($this->aggregate(__FUNCTION__, $column)); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
|
|
|
|
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
|
|
public function avg($column = null) |
|
157
|
|
|
{ |
|
158
|
|
|
$results = $this->aggregate(__FUNCTION__, $column); |
|
159
|
|
|
|
|
160
|
|
|
return (float) array_sum($results) / count($results); |
|
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
|
|
|
|
|
164
|
|
|
* {@inheritdoc} |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public function min($column = null) |
|
167
|
|
|
{ |
|
168
|
1 |
|
return (float) min($this->aggregate(__FUNCTION__, $column)); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
|
|
|
|
|
172
|
|
|
* {@inheritdoc} |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function max($column = null) |
|
175
|
|
|
{ |
|
176
|
1 |
|
return (float) max($this->aggregate(__FUNCTION__, $column)); |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
|
|
|
|
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
1 |
|
public function sum($column = null) |
|
183
|
|
|
{ |
|
184
|
1 |
|
return (float) array_sum($this->aggregate(__FUNCTION__, $column)); |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
|
|
|
|
|
188
|
|
|
* {@inheritdoc} |
|
189
|
|
|
* |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
7 |
|
public function aggregate($function, $column = null) |
|
193
|
|
|
{ |
|
194
|
7 |
|
$results = []; |
|
195
|
|
|
|
|
196
|
7 |
|
foreach ($this->selectQueries() as $query) { |
|
197
|
7 |
|
$results[] = $query->aggregate($function, $column); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
7 |
|
return $results; |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
|
|
|
|
|
204
|
|
|
* {@inheritdoc} |
|
205
|
|
|
* |
|
206
|
|
|
* @todo execute cached with closure |
|
|
|
|
|
|
207
|
|
|
*/ |
|
208
|
19 |
|
public function execute($columns = null) |
|
209
|
|
|
{ |
|
210
|
19 |
|
$results = []; |
|
211
|
19 |
|
$limit = $this->statements['limit']; |
|
212
|
|
|
|
|
213
|
19 |
|
foreach ($this->selectQueries() as $query) { |
|
214
|
19 |
|
$results = array_merge($results, $query->execute($columns)); |
|
215
|
|
|
|
|
216
|
19 |
|
if ($limit) { |
|
217
|
12 |
|
$count = count($results); |
|
218
|
|
|
|
|
219
|
12 |
|
if ($count == $limit) { |
|
220
|
9 |
|
return $results; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
4 |
|
if ($count > $limit) { |
|
224
|
11 |
|
return array_slice($results, 0, $limit); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
10 |
|
return $results; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
|
|
|
|
|
233
|
|
|
* {@inheritdoc} |
|
234
|
|
|
*/ |
|
235
|
12 |
|
public function update($values = null) |
|
236
|
|
|
{ |
|
237
|
12 |
|
$count = 0; |
|
238
|
|
|
|
|
239
|
12 |
|
foreach ($this->selectQueries() as $query) { |
|
240
|
12 |
|
$count += $query->update($values); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
12 |
|
if ($count > 0) { |
|
244
|
9 |
|
$this->clearCacheOnWrite(); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
12 |
|
return $count; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* {@inheritdoc} |
|
252
|
|
|
*/ |
|
253
|
5 |
|
public function delete() |
|
254
|
|
|
{ |
|
255
|
5 |
|
$count = 0; |
|
256
|
|
|
|
|
257
|
5 |
|
foreach ($this->selectQueries() as $query) { |
|
258
|
5 |
|
$count += $query->delete(); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
5 |
|
if ($count > 0) { |
|
262
|
4 |
|
$this->clearCacheOnWrite(); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
5 |
|
return $count; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Select the queriesvto use |
|
270
|
|
|
* |
|
271
|
|
|
* @return KeyValueQueryInterface[] |
|
272
|
|
|
* |
|
273
|
|
|
* @throws \Doctrine\DBAL\Sharding\ShardingException |
|
|
|
|
|
|
274
|
|
|
*/ |
|
275
|
34 |
|
private function selectQueries(): iterable |
|
|
|
|
|
|
276
|
|
|
{ |
|
277
|
34 |
|
foreach ($this->getShardIds() as $shardId) { |
|
278
|
34 |
|
yield $this->getQueryByShard($shardId); |
|
|
|
|
|
|
279
|
|
|
} |
|
280
|
30 |
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Get the targeted shard IDs |
|
284
|
|
|
* |
|
285
|
|
|
* @return string[] |
|
286
|
|
|
*/ |
|
287
|
34 |
|
private function getShardIds(): array |
|
|
|
|
|
|
288
|
|
|
{ |
|
289
|
34 |
|
if ($this->shardId !== null) { |
|
290
|
1 |
|
return [$this->shardId]; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
33 |
|
$distributionKey = $this->connection->getDistributionKey(); |
|
294
|
|
|
|
|
295
|
33 |
|
if (isset($this->statements['where'][$distributionKey])) { |
|
296
|
23 |
|
return [$this->connection->getShardChoser()->pick($this->statements['where'][$distributionKey], $this->connection)]; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
10 |
|
return $this->connection->getShardIds(); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Get and configure a query for the given shard |
|
304
|
|
|
* |
|
305
|
|
|
* @param string $shardId The shard id |
|
306
|
|
|
* |
|
307
|
|
|
* @return KeyValueQueryInterface |
|
308
|
|
|
* |
|
309
|
|
|
* @throws \Doctrine\DBAL\Sharding\ShardingException |
|
|
|
|
|
|
310
|
|
|
*/ |
|
311
|
34 |
|
private function getQueryByShard($shardId) |
|
|
|
|
|
|
312
|
|
|
{ |
|
313
|
|
|
/** @var KeyValueQueryInterface $query */ |
|
|
|
|
|
|
314
|
34 |
|
if (isset($this->queries[$shardId])) { |
|
315
|
9 |
|
$query = $this->queries[$shardId]; |
|
316
|
|
|
} else { |
|
317
|
34 |
|
$this->queries[$shardId] = $query = $this->connection->getShardConnection($shardId)->make(KeyValueQueryInterface::class, $this->preprocessor()); |
|
|
|
|
|
|
318
|
34 |
|
$query->setExtension($this->extension); |
|
|
|
|
|
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
34 |
|
$query->from($this->statements['table']); |
|
322
|
|
|
|
|
323
|
34 |
|
if (!empty($this->statements['limit'])) { |
|
324
|
12 |
|
$query->limit($this->statements['limit']); |
|
|
|
|
|
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
34 |
|
if (!empty($this->statements['columns'])) { |
|
328
|
1 |
|
$query->project($this->statements['columns']); |
|
|
|
|
|
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
34 |
|
if (!empty($this->statements['where'])) { |
|
332
|
27 |
|
$query->where($this->statements['where']); |
|
|
|
|
|
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
34 |
|
if (!empty($this->statements['values']['data'])) { |
|
336
|
9 |
|
$query->values($this->statements['values']['data'], $this->statements['values']['types']); |
|
|
|
|
|
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
34 |
|
return $query; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* {@inheritdoc} |
|
344
|
|
|
*/ |
|
345
|
2 |
|
protected function cacheNamespace() |
|
346
|
|
|
{ |
|
347
|
2 |
|
return $this->connection->getName().':'.$this->statements['table']; |
|
348
|
|
|
} |
|
349
|
|
|
} |
|
350
|
|
|
|