From 796803897812159a746c3c1a96216cab7c97255e Mon Sep 17 00:00:00 2001 From: Max Kotliar Date: Tue, 12 Mar 2019 15:06:43 +0200 Subject: [PATCH] impl meta for lock --- src/PessimisticLock.php | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/PessimisticLock.php b/src/PessimisticLock.php index e234239..a228169 100644 --- a/src/PessimisticLock.php +++ b/src/PessimisticLock.php @@ -9,7 +9,7 @@ use MongoDB\Driver\Exception\DuplicateKeyException; use MongoDB\Driver\Exception\RuntimeException; -class PessimisticLock +class PessimisticLock implements StorageMetaInterface { /** * @var Collection @@ -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 []; } /**