-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathDataLoader.php
392 lines (339 loc) · 11.6 KB
/
DataLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/*
* This file is part of the DataLoaderPhp package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Overblog\DataLoader;
use Overblog\PromiseAdapter\PromiseAdapterInterface;
class DataLoader implements DataLoaderInterface
{
/**
* @var callable
*/
private $batchLoadFn;
/**
* @var Option
*/
private $options;
/**
* @var CacheMap
*/
private $promiseCache;
/**
* @var array
*/
private $queue = [];
/**
* @var self[]
*/
private static $instances = [];
/**
* @var PromiseAdapterInterface
*/
private $promiseAdapter;
public function __construct(callable $batchLoadFn, PromiseAdapterInterface $promiseFactory, ?Option $options = null)
{
$this->batchLoadFn = $batchLoadFn;
$this->promiseAdapter = $promiseFactory;
$this->options = $options ?: new Option();
$this->promiseCache = $this->options->getCacheMap();
self::$instances[] = $this;
}
/**
* {@inheritdoc}
*/
public function load($key)
{
$this->checkKey($key, __METHOD__);
// Determine options
$shouldBatch = $this->options->shouldBatch();
$shouldCache = $this->options->shouldCache();
$cacheKey = $this->getCacheKeyFromKey($key);
// If caching and there is a cache-hit, return cached Promise.
if ($shouldCache) {
$cachedPromise = $this->promiseCache->get($cacheKey);
if ($cachedPromise) {
return $cachedPromise;
}
}
// Otherwise, produce a new Promise for this value.
$promise = $this->getPromiseAdapter()->create(
$resolve,
$reject,
function () {
// Cancel/abort any running operations like network connections, streams etc.
throw new \RuntimeException('DataLoader destroyed before promise complete.');
}
);
$this->queue[] = [
'key' => $key,
'resolve' => $resolve,
'reject' => $reject,
'promise' => $promise,
];
// Determine if a dispatch of this queue should be scheduled.
// A single dispatch should be scheduled per queue at the time when the
// queue changes from "empty" to "full".
if (count($this->queue) === 1) {
if (!$shouldBatch) {
// Otherwise dispatch the (queue of one) immediately.
$this->dispatchQueue();
}
}
// If caching, cache this promise.
if ($shouldCache) {
$this->promiseCache->set($cacheKey, $promise);
}
return $promise;
}
/**
* {@inheritdoc}
*/
public function loadMany($keys)
{
if (!is_array($keys) && !$keys instanceof \Traversable) {
throw new \InvalidArgumentException(sprintf('The "%s" method must be called with Array<key> but got: %s.', __METHOD__, gettype($keys)));
}
return $this->getPromiseAdapter()->createAll(array_map(
function ($key) {
return $this->load($key);
},
$keys
));
}
/**
* {@inheritdoc}
*/
public function clear($key)
{
$this->checkKey($key, __METHOD__);
$cacheKey = $this->getCacheKeyFromKey($key);
$this->promiseCache->clear($cacheKey);
return $this;
}
/**
* {@inheritdoc}
*/
public function clearAll()
{
$this->promiseCache->clearAll();
return $this;
}
/**
* {@inheritdoc}
*/
public function prime($key, $value)
{
$this->checkKey($key, __METHOD__);
$cacheKey = $this->getCacheKeyFromKey($key);
// Only add the key if it does not already exist.
if (!$this->promiseCache->has($cacheKey)) {
// Cache a rejected promise if the value is an Error, in order to match
// the behavior of load(key).
$promise = $value instanceof \Exception ? $this->getPromiseAdapter()->createRejected($value) : $this->getPromiseAdapter()->createFulfilled($value);
$this->promiseCache->set($cacheKey, $promise);
}
return $this;
}
public function __destruct()
{
if ($this->needProcess()) {
foreach ($this->queue as $data) {
try {
$this->getPromiseAdapter()->cancel($data['promise']);
} catch (\Exception $e) {
// no need to do nothing if cancel failed
}
}
$this->await();
}
foreach (self::$instances as $i => $instance) {
if ($this !== $instance) {
continue;
}
unset(self::$instances[$i]);
}
}
protected function needProcess()
{
return count($this->queue) > 0;
}
protected function process()
{
if ($this->needProcess()) {
$this->getPromiseAdapter()->await();
$this->dispatchQueue();
}
}
protected function getPromiseAdapter()
{
return $this->promiseAdapter;
}
/**
* {@inheritdoc}
*/
public static function await($promise = null, $unwrap = true)
{
self::awaitInstances();
if (null === $promise) {
return null;
}
if (is_callable([$promise, 'then'])) {
$isPromiseCompleted = false;
$resolvedValue = null;
$rejectedReason = null;
$promise->then(
function ($value) use (&$isPromiseCompleted, &$resolvedValue) {
$isPromiseCompleted = true;
$resolvedValue = $value;
},
function ($reason) use (&$isPromiseCompleted, &$rejectedReason) {
$isPromiseCompleted = true;
$rejectedReason = $reason;
}
);
//Promise is completed?
if ($isPromiseCompleted) {
// rejected ?
if ($rejectedReason instanceof \Exception || (interface_exists('\Throwable') && $rejectedReason instanceof \Throwable)) {
if (!$unwrap) {
return $rejectedReason;
}
throw $rejectedReason;
}
return $resolvedValue;
}
}
if (empty(self::$instances)) {
throw new \RuntimeException('Found no active DataLoader instance.');
}
return self::$instances[0]->getPromiseAdapter()->await($promise, $unwrap);
}
private static function awaitInstances()
{
do {
$wait = false;
$dataLoaders = self::$instances;
foreach ($dataLoaders as $dataLoader) {
if (!$dataLoader || !$dataLoader->needProcess()) {
$wait |= false;
continue;
}
$wait = true;
$dataLoader->process();
}
} while ($wait);
}
/**
* @param $key
*
* @return mixed
*/
protected function getCacheKeyFromKey($key)
{
$cacheKeyFn = $this->options->getCacheKeyFn();
$cacheKey = $cacheKeyFn ? $cacheKeyFn($key) : $key;
return $cacheKey;
}
/**
* @param $key
* @param $method
*/
protected function checkKey($key, $method)
{
if (null === $key) {
throw new \InvalidArgumentException(
sprintf('The "%s" method must be called with a value, but got: %s.', $method, gettype($key))
);
}
}
/**
* Given the current state of a Loader instance, perform a batch load
* from its current queue.
*/
private function dispatchQueue()
{
// Take the current loader queue, replacing it with an empty queue.
$queue = $this->queue;
$this->queue = [];
$queueLength = count($queue);
// If a maxBatchSize was provided and the queue is longer, then segment the
// queue into multiple batches, otherwise treat the queue as a single batch.
$maxBatchSize = $this->options->getMaxBatchSize();
if ($maxBatchSize && $maxBatchSize > 0 && $maxBatchSize < $queueLength) {
for ($i = 0; $i < $queueLength / $maxBatchSize; $i++) {
$offset = $i * $maxBatchSize;
$length = ($i + 1) * $maxBatchSize - $offset;
$this->dispatchQueueBatch(array_slice($queue, $offset, $length));
}
} else {
$this->dispatchQueueBatch($queue);
}
}
private function dispatchQueueBatch(array $queue)
{
// Collect all keys to be loaded in this dispatch
$keys = array_column($queue, 'key');
// Call the provided batchLoadFn for this loader with the loader queue's keys.
$batchLoadFn = $this->batchLoadFn;
$batchPromise = $batchLoadFn($keys);
// Assert the expected response from batchLoadFn
if (!$batchPromise || !is_callable([$batchPromise, 'then'])) {
$this->failedDispatch($queue, new \RuntimeException(
'DataLoader must be constructed with a function which accepts ' .
'Array<key> and returns Promise<Array<value>>, but the function did ' .
sprintf('not return a Promise: %s.', gettype($batchPromise))
));
return;
}
// Await the resolution of the call to batchLoadFn.
$batchPromise->then(
function ($values) use ($keys, $queue) {
// Assert the expected resolution from batchLoadFn.
if (!is_array($values) && !$values instanceof \Traversable) {
throw new \RuntimeException(
'DataLoader must be constructed with a function which accepts ' .
'Array<key> and returns Promise<Array<value>>, but the function did ' .
sprintf('not return a Promise of an Array: %s.', gettype($values))
);
}
if (count($values) !== count($keys)) {
throw new \RuntimeException(
'DataLoader must be constructed with a function which accepts ' .
'Array<key> and returns Promise<Array<value>>, but the function did ' .
'not return a Promise of an Array of the same length as the Array of keys.'
);
}
// Step through the values, resolving or rejecting each Promise in the
// loaded queue.
foreach ($queue as $index => $data) {
$value = $values[$index];
if ($value instanceof \Exception) {
$data['reject']($value);
} else {
$data['resolve']($value);
}
};
}
)->then(null, function ($error) use ($queue) {
$this->failedDispatch($queue, $error);
});
}
/**
* Do not cache individual loads if the entire batch dispatch fails,
* but still reject each request so they do not hang.
* @param array $queue
* @param \Exception $error
*/
private function failedDispatch($queue, \Exception $error)
{
foreach ($queue as $index => $data) {
$this->clear($data['key']);
$data['reject']($error);
}
}
}