Skip to content

Commit 992c26f

Browse files
authored
Merge pull request #30531 from nextcloud/performance/optimize-filesystemtags-flow-groupfolder
Optimize FileSystemTags workflow for groupfolder
2 parents 89d109a + cbf9064 commit 992c26f

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

apps/workflowengine/lib/Check/FileSystemTags.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCP\SystemTag\TagNotFoundException;
3737
use OCP\WorkflowEngine\ICheck;
3838
use OCP\WorkflowEngine\IFileCheck;
39+
use OC\Files\Storage\Wrapper\Wrapper;
3940

4041
class FileSystemTags implements ICheck, IFileCheck {
4142
use TFileCheck;
@@ -132,13 +133,26 @@ protected function getSystemTags() {
132133
* @return int[]
133134
*/
134135
protected function getFileIds(ICache $cache, $path, $isExternalStorage) {
135-
// TODO: Fix caching inside group folders
136-
// Do not cache file ids inside group folders because multiple file ids might be mapped to
137-
// the same combination of cache id + path.
138136
/** @psalm-suppress InvalidArgument */
139-
$shouldCacheFileIds = !$this->storage->instanceOfStorage(\OCA\GroupFolders\Mount\GroupFolderStorage::class);
140-
$cacheId = $cache->getNumericStorageId();
141-
if ($shouldCacheFileIds && isset($this->fileIds[$cacheId][$path])) {
137+
if ($this->storage->instanceOfStorage(\OCA\GroupFolders\Mount\GroupFolderStorage::class)) {
138+
// Special implementation for groupfolder since all groupfolders share the same storage
139+
// id so add the group folder id in the cache key too.
140+
$groupFolderStorage = $this->storage;
141+
if ($this->storage instanceof Wrapper) {
142+
$groupFolderStorage = $this->storage->getInstanceOfStorage(\OCA\GroupFolders\Mount\GroupFolderStorage::class);
143+
}
144+
if ($groupFolderStorage === null) {
145+
throw new \LogicException('Should not happen: Storage is instance of GroupFolderStorage but no group folder storage found while unwrapping.');
146+
}
147+
/**
148+
* @psalm-suppress UndefinedDocblockClass
149+
* @psalm-suppress UndefinedInterfaceMethod
150+
*/
151+
$cacheId = $cache->getNumericStorageId() . '/' . $groupFolderStorage->getFolderId();
152+
} else {
153+
$cacheId = $cache->getNumericStorageId();
154+
}
155+
if (isset($this->fileIds[$cacheId][$path])) {
142156
return $this->fileIds[$cacheId][$path];
143157
}
144158

@@ -151,12 +165,10 @@ protected function getFileIds(ICache $cache, $path, $isExternalStorage) {
151165

152166
$fileId = $cache->getId($path);
153167
if ($fileId !== -1) {
154-
$parentIds[] = $cache->getId($path);
168+
$parentIds[] = $fileId;
155169
}
156170

157-
if ($shouldCacheFileIds) {
158-
$this->fileIds[$cacheId][$path] = $parentIds;
159-
}
171+
$this->fileIds[$cacheId][$path] = $parentIds;
160172

161173
return $parentIds;
162174
}

lib/private/Files/Storage/Wrapper/Wrapper.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ public function isLocal() {
486486
/**
487487
* Check if the storage is an instance of $class or is a wrapper for a storage that is an instance of $class
488488
*
489-
* @param string $class
489+
* @param class-string<IStorage> $class
490490
* @return bool
491491
*/
492492
public function instanceOfStorage($class) {
@@ -497,6 +497,25 @@ public function instanceOfStorage($class) {
497497
return is_a($this, $class) or $this->getWrapperStorage()->instanceOfStorage($class);
498498
}
499499

500+
/**
501+
* @psalm-template T of IStorage
502+
* @psalm-param class-string<T> $class
503+
* @psalm-return T|null
504+
*/
505+
public function getInstanceOfStorage(string $class) {
506+
$storage = $this;
507+
while ($storage instanceof Wrapper) {
508+
if ($storage instanceof $class) {
509+
break;
510+
}
511+
$storage = $storage->getWrapperStorage();
512+
}
513+
if (!($storage instanceof $class)) {
514+
return null;
515+
}
516+
return $storage;
517+
}
518+
500519
/**
501520
* Pass any methods custom to specific storage implementations to the wrapped storage
502521
*

0 commit comments

Comments
 (0)