Skip to content

Commit

Permalink
impl meta for lock
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Mar 12, 2019
1 parent f011a97 commit 7968038
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/PessimisticLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use MongoDB\Driver\Exception\DuplicateKeyException;
use MongoDB\Driver\Exception\RuntimeException;

class PessimisticLock
class PessimisticLock implements StorageMetaInterface
{
/**
* @var Collection
Expand Down Expand Up @@ -148,18 +148,31 @@ public function createIndexes(int $lockExpireAfterSeconds = null): void
foreach ($this->collection->listIndexes() as $index) {
$existingIndexes[$index->getName()] = $index->getName();
}

if (empty($existingIndexes['id'])) {
$this->collection->createIndex(['id' => 1], ['unique' => true, 'name' => 'id']);

foreach ($this->getIndexes() as $index) {
if (empty($index->getOptions()['name'])) {
$this->collection->createIndex($index->getKey(), $index->getOptions());
}
}
}

if (empty($existingIndexes['timestamp'])) {
$this->collection->createIndex(['timestamp' => 1], ['expireAfterSeconds' => $lockExpireAfterSeconds, 'name' => 'timestamp']);
}
/**
* @return Index[]
*/
public function getIndexes(): array
{
$lockExpireAfterSeconds = $this->limit + 2;

return [
new Index(['id' => 1], ['unique' => true, 'name' => 'id']),
new Index(['timestamp' => 1], ['expireAfterSeconds' => $lockExpireAfterSeconds, 'name' => 'timestamp']),
new Index(['sessionId' => 1], ['unique' => false, 'name' => 'sessionId'])
];
}

if (empty($existingIndexes['sessionId'])) {
$this->collection->createIndex(['sessionId' => 1], ['unique' => false, 'name' => 'sessionId']);
}
public function getCreateCollectionOptions(): array
{
return [];
}

/**
Expand Down

0 comments on commit 7968038

Please sign in to comment.