|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Defines the TraderInteractive\Mongo\Queue class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace TraderInteractive\Mongo; |
|
7
|
|
|
|
|
8
|
|
|
use MongoDB\BSON\UTCDateTime; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Abstraction of mongo db collection as priority queue. |
|
12
|
|
|
* |
|
13
|
|
|
* Tied priorities are ordered by time. So you may use a single priority for normal queuing (default args exist for |
|
14
|
|
|
* this purpose). Using a random priority achieves random get() |
|
15
|
|
|
*/ |
|
16
|
|
|
final class Queue implements QueueInterface |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Maximum millisecond value to use for UTCDateTime creation. |
|
20
|
|
|
* |
|
21
|
|
|
* @var integer |
|
22
|
|
|
*/ |
|
23
|
|
|
const MONGO_INT32_MAX = PHP_INT_MAX; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* mongo collection to use for queue. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \MongoDB\Collection |
|
29
|
|
|
*/ |
|
30
|
|
|
private $collection; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Construct queue. |
|
34
|
|
|
* |
|
35
|
|
|
* @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url. |
|
36
|
|
|
* @param string $db the mongo db name |
|
37
|
|
|
* @param string $collection the collection name to use for the queue |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($collectionOrUrl, string $db = null, string $collection = null) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($collectionOrUrl instanceof \MongoDB\Collection) { |
|
44
|
|
|
$this->collection = $collectionOrUrl; |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (!is_string($collectionOrUrl)) { |
|
49
|
|
|
throw new \InvalidArgumentException('$collectionOrUrl was not a string'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$mongo = new \MongoDB\Client( |
|
53
|
|
|
$collectionOrUrl, |
|
54
|
|
|
[], |
|
55
|
|
|
['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']] |
|
56
|
|
|
); |
|
57
|
|
|
$mongoDb = $mongo->selectDatabase($db); |
|
58
|
|
|
$this->collection = $mongoDb->selectCollection($collection); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|