Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UnorderedQueue class. #71

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
build:
runs-on: ubuntu-18.04
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
Expand Down
6 changes: 3 additions & 3 deletions src/AbstractQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ final public function get(array $query, array $options = []) : array
$query
);

$options += self::DEFAULT_GET_OPTIONS;
$options += static::DEFAULT_GET_OPTIONS;
$update = ['$set' => ['earliestGet' => $this->calculateEarliestGet($options['runningResetDuration'])]];
$end = $this->calculateEndTime($options['waitDurationInMillis']);
$sleepTime = $this->calculateSleepTime($options['pollDurationInMillis']);
Expand Down Expand Up @@ -305,12 +305,12 @@ private function calculateEarliestGet(int $runningResetDuration) : UTCDateTime
{
$resetTimestamp = time() + $runningResetDuration;
//ints overflow to floats, max at PHP_INT_MAX
return new UTCDateTime(min(max(0, $resetTimestamp * 1000), self::MONGO_INT32_MAX));
return new UTCDateTime(min(max(0, $resetTimestamp * 1000), static::MONGO_INT32_MAX));
}

private function tryFindOneAndUpdate(array $query, array $update, ArrayObject $messages) : bool
{
$document = $this->collection->findOneAndUpdate($query, $update, self::FIND_ONE_AND_UPDATE_OPTIONS);
$document = $this->collection->findOneAndUpdate($query, $update, static::FIND_ONE_AND_UPDATE_OPTIONS);
if ($document === null) {
return false;
}
Expand Down
48 changes: 48 additions & 0 deletions src/UnorderedQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Defines the TraderInteractive\Mongo\UnorderedQueue class.
*/

namespace TraderInteractive\Mongo;

use MongoDB\Client;
use MongoDB\Operation\FindOneAndUpdate;

/**
* Abstraction of mongo db collection as unordered queue.
*/
final class UnorderedQueue extends AbstractQueue implements QueueInterface
{
/**
* Override from AbstractQueue class to remove sort options.
*
* @var array
*/
const FIND_ONE_AND_UPDATE_OPTIONS = [
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
];

/**
* Construct queue.
*
* @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
* @param string $db the mongo db name
* @param string $collection the collection name to use for the queue
*
* @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
*/
public function __construct($collectionOrUrl, string $db = null, string $collection = null)
{
if ($collectionOrUrl instanceof \MongoDB\Collection) {
$this->collection = $collectionOrUrl;
return;
}

if (!is_string($collectionOrUrl)) {
throw new \InvalidArgumentException('$collectionOrUrl was not a string');
}

$this->collection = (new Client($collectionOrUrl))->selectDatabase($db)->selectCollection($collection);
}
}
Loading