Merge pull request #61438 from nextcloud/carl/usort-storagefactory

perf: Only sort wrappers when adding them
pull/61291/merge
Louis 2 months ago committed by GitHub
commit bf35fe8114
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 21
      lib/private/Files/Storage/StorageFactory.php

@ -18,6 +18,8 @@ use Psr\Log\LoggerInterface;
class StorageFactory implements IStorageFactory {
/** @var array<string, array{wrapper: callable(string $mountPoint, IStorage $storage): IStorage, priority: int}> $storageWrappers */
private array $storageWrappers = [];
/** @var bool $dirty Whether the list of storage wrappers is sorted */
private bool $dirty = true;
#[\Override]
public function addStorageWrapper(string $wrapperName, callable $callback, int $priority = 50, array $existingMounts = []): bool {
@ -31,6 +33,7 @@ class StorageFactory implements IStorageFactory {
}
$this->storageWrappers[$wrapperName] = ['wrapper' => $callback, 'priority' => $priority];
$this->dirty = true;
return true;
}
@ -56,16 +59,14 @@ class StorageFactory implements IStorageFactory {
}
public function wrap(IMountPoint $mountPoint, IStorage $storage): IStorage {
$wrappers = array_values($this->storageWrappers);
usort($wrappers, function ($a, $b) {
return $b['priority'] - $a['priority'];
});
/** @var callable[] $wrappers */
$wrappers = array_map(function ($wrapper) {
return $wrapper['wrapper'];
}, $wrappers);
foreach ($wrappers as $wrapper) {
$storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint);
if ($this->dirty) {
uasort($this->storageWrappers, static fn (array $a, array $b) => $b['priority'] - $a['priority']);
$this->dirty = false;
}
foreach ($this->storageWrappers as $wrapper) {
/** @var callable(string, IStorage, IMountPoint): IStorage $wrapperCallable */
$wrapperCallable = $wrapper['wrapper'];
$storage = $wrapperCallable($mountPoint->getMountPoint(), $storage, $mountPoint);
if (!($storage instanceof IStorage)) {
throw new \Exception('Invalid result from storage wrapper');
}

Loading…
Cancel
Save