-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b255fe6
commit 7b77db6
Showing
10 changed files
with
88 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace AnourValar\LaravelAtom\Strategies; | ||
|
||
use Illuminate\Database\Connection; | ||
|
||
class OptimisticAdvisoryStrategy implements StrategyInterface | ||
{ | ||
/** | ||
* @throws \AnourValar\LaravelAtom\Exceptions\OptimisticException | ||
* | ||
* {@inheritDoc} | ||
* @see \AnourValar\LaravelAtom\Strategies\StrategyInterface::lock() | ||
*/ | ||
public function lock(string $sha1, Connection $connection): void | ||
{ | ||
if (! $connection->transactionLevel()) { | ||
throw new \LogicException('Lock can be applied only inside transaction'); | ||
} | ||
|
||
$id1 = crc32($sha1) - 2147483648; | ||
$id2 = crc32(strrev($sha1)) - 2147483648; | ||
|
||
if (! $connection->select('SELECT pg_try_advisory_xact_lock(:id1, :id2)', ['id1' => $id1, 'id2' => $id2])[0]->pg_try_advisory_xact_lock) { | ||
throw new \AnourValar\LaravelAtom\Exceptions\OptimisticException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace AnourValar\LaravelAtom\Strategies; | ||
|
||
use Illuminate\Database\Connection; | ||
|
||
class PessimisticAdvisoryStrategy implements StrategyInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* @see \AnourValar\LaravelAtom\Strategies\StrategyInterface::lock() | ||
*/ | ||
public function lock(string $sha1, Connection $connection): void | ||
{ | ||
if (! $connection->transactionLevel()) { | ||
throw new \LogicException('Lock can be applied only inside transaction'); | ||
} | ||
|
||
$id1 = crc32($sha1) - 2147483648; | ||
$id2 = crc32(strrev($sha1)) - 2147483648; | ||
|
||
$connection->select('SELECT pg_advisory_xact_lock(:id1, :id2)', ['id1' => $id1, 'id2' => $id2]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters