Skip to content

Commit

Permalink
Refactors lib/private/Lock.
Browse files Browse the repository at this point in the history
To improve code readability.

Signed-off-by: Faraz Samapoor <fsa@adlas.at>
  • Loading branch information
fsamapoor authored and Faraz Samapoor committed Jul 2, 2023
1 parent ac80328 commit 00c6671
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/private/Lock/AbstractLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ abstract class AbstractLockingProvider implements ILockingProvider {
/** how long until we clear stray locks in seconds */
protected int $ttl;

protected $acquiredLocks = [
protected array $acquiredLocks = [
'shared' => [],
'exclusive' => []
];
Expand Down
37 changes: 14 additions & 23 deletions lib/private/Lock/DBLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,14 @@
* Locking provider that stores the locks in the database
*/
class DBLockingProvider extends AbstractLockingProvider {
private IDBConnection $connection;
private ITimeFactory $timeFactory;
private array $sharedLocks = [];
private bool $cacheSharedLocks;

public function __construct(
IDBConnection $connection,
ITimeFactory $timeFactory,
int $ttl = 3600,
bool $cacheSharedLocks = true
private IDBConnection $connection,
private ITimeFactory $timeFactory,
protected int $ttl = 3600,
private bool $cacheSharedLocks = true
) {
$this->connection = $connection;
$this->timeFactory = $timeFactory;
$this->ttl = $ttl;
$this->cacheSharedLocks = $cacheSharedLocks;
}

/**
Expand Down Expand Up @@ -114,18 +107,16 @@ public function isLocked(string $path, int $type): bool {
->where($query->expr()->eq('key', $query->createNamedParameter($path)));
$result = $query->executeQuery();
$lockValue = (int)$result->fetchOne();
if ($type === self::LOCK_SHARED) {
if ($this->isLocallyLocked($path)) {
// if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db
return $lockValue > 1;
} else {
return $lockValue > 0;
}
} elseif ($type === self::LOCK_EXCLUSIVE) {
return $lockValue === -1;
} else {
return false;
}

return match ($type) {
// if we have a shared lock that we kept open locally, but it's
// released, we always have at least 1 shared lock in the db
self::LOCK_SHARED => $this->isLocallyLocked($path)
? $lockValue > 1
: $lockValue > 0,
self::LOCK_EXCLUSIVE => $lockValue === -1,
default => false,
};
}

/** @inheritDoc */
Expand Down
35 changes: 16 additions & 19 deletions lib/private/Lock/MemcacheLockingProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
use OCP\Lock\LockedException;

class MemcacheLockingProvider extends AbstractLockingProvider {
private IMemcache $memcache;

public function __construct(IMemcache $memcache, int $ttl = 3600) {
$this->memcache = $memcache;
$this->ttl = $ttl;
public function __construct(
private IMemcache $memcache,
protected int $ttl = 3600,
) {
}

private function setTTL(string $path): void {
Expand All @@ -47,13 +46,12 @@ private function setTTL(string $path): void {

public function isLocked(string $path, int $type): bool {
$lockValue = $this->memcache->get($path);
if ($type === self::LOCK_SHARED) {
return is_int($lockValue) && $lockValue > 0;
} elseif ($type === self::LOCK_EXCLUSIVE) {
return $lockValue === 'exclusive';
} else {
return false;
}

return match ($type) {
self::LOCK_SHARED => is_int($lockValue) && ($lockValue > 0),
self::LOCK_EXCLUSIVE => $lockValue === 'exclusive',
default => false,
};
}

public function acquireLock(string $path, int $type, ?string $readablePath = null): void {
Expand Down Expand Up @@ -115,12 +113,11 @@ public function changeLock(string $path, int $targetType): void {

private function getExistingLockForException(string $path): string {
$existing = $this->memcache->get($path);
if (!$existing) {
return 'none';
} elseif ($existing === 'exclusive') {
return $existing;
} else {
return $existing . ' shared locks';
}

return match (true) {
!$existing => 'none',
($existing === 'exclusive') => $existing,
default => $existing . ' shared locks',
};
}
}

0 comments on commit 00c6671

Please sign in to comment.