Skip to content

Commit 35f41e7

Browse files
Merge pull request #53730 from nextcloud/backport/52981/stable31
[stable31] perf(dav): Preload dav search with tags/favorites
2 parents daf3084 + a9b4a6b commit 35f41e7

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

apps/dav/lib/Connector/Sabre/TagsPlugin.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function initialize(\Sabre\DAV\Server $server) {
9494
$this->server = $server;
9595
$this->server->on('propFind', [$this, 'handleGetProperties']);
9696
$this->server->on('propPatch', [$this, 'handleUpdateProperties']);
97+
$this->server->on('preloadProperties', [$this, 'handlePreloadProperties']);
9798
}
9899

99100
/**
@@ -149,6 +150,24 @@ private function getTags($fileId) {
149150
return null;
150151
}
151152

153+
/**
154+
* Prefetches tags for a list of file IDs and caches the results
155+
*
156+
* @param array $fileIds List of file IDs to prefetch tags for
157+
* @return void
158+
*/
159+
private function prefetchTagsForFileIds(array $fileIds) {
160+
$tags = $this->getTagger()->getTagsForObjects($fileIds);
161+
if ($tags === false) {
162+
// the tags API returns false on error...
163+
$tags = [];
164+
}
165+
166+
foreach ($fileIds as $fileId) {
167+
$this->cachedTags[$fileId] = $tags[$fileId] ?? [];
168+
}
169+
}
170+
152171
/**
153172
* Updates the tags of the given file id
154173
*
@@ -199,22 +218,11 @@ public function handleGetProperties(
199218
)) {
200219
// note: pre-fetching only supported for depth <= 1
201220
$folderContent = $node->getChildren();
202-
$fileIds[] = (int)$node->getId();
221+
$fileIds = [(int)$node->getId()];
203222
foreach ($folderContent as $info) {
204223
$fileIds[] = (int)$info->getId();
205224
}
206-
$tags = $this->getTagger()->getTagsForObjects($fileIds);
207-
if ($tags === false) {
208-
// the tags API returns false on error...
209-
$tags = [];
210-
}
211-
212-
$this->cachedTags = $this->cachedTags + $tags;
213-
$emptyFileIds = array_diff($fileIds, array_keys($tags));
214-
// also cache the ones that were not found
215-
foreach ($emptyFileIds as $fileId) {
216-
$this->cachedTags[$fileId] = [];
217-
}
225+
$this->prefetchTagsForFileIds($fileIds);
218226
}
219227

220228
$isFav = null;
@@ -270,4 +278,14 @@ public function handleUpdateProperties($path, PropPatch $propPatch) {
270278
return 200;
271279
});
272280
}
281+
282+
public function handlePreloadProperties(array $nodes, array $requestProperties): void {
283+
if (
284+
!in_array(self::FAVORITE_PROPERTYNAME, $requestProperties, true) &&
285+
!in_array(self::TAGS_PROPERTYNAME, $requestProperties, true)
286+
) {
287+
return;
288+
}
289+
$this->prefetchTagsForFileIds(array_map(fn ($node) => $node->getId(), $nodes));
290+
}
273291
}

apps/dav/lib/Files/FileSearchBackend.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\DAV\Connector\Sabre\Directory;
1616
use OCA\DAV\Connector\Sabre\File;
1717
use OCA\DAV\Connector\Sabre\FilesPlugin;
18+
use OCA\DAV\Connector\Sabre\Server;
1819
use OCA\DAV\Connector\Sabre\TagsPlugin;
1920
use OCP\Files\Cache\ICacheEntry;
2021
use OCP\Files\Folder;
@@ -44,6 +45,7 @@ class FileSearchBackend implements ISearchBackend {
4445
public const OPERATOR_LIMIT = 100;
4546

4647
public function __construct(
48+
private Server $server,
4749
private CachingTree $tree,
4850
private IUser $user,
4951
private IRootFolder $rootFolder,
@@ -133,6 +135,7 @@ private function getPropertyDefinitionsForMetadata(): array {
133135
* @param string[] $requestProperties
134136
*/
135137
public function preloadPropertyFor(array $nodes, array $requestProperties): void {
138+
$this->server->emit('preloadProperties', [$nodes, $requestProperties]);
136139
}
137140

138141
private function getFolderForPath(?string $path = null): Folder {

apps/dav/lib/Server.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ public function __construct(
344344
\OC::$server->getAppManager()
345345
));
346346
$lazySearchBackend->setBackend(new FileSearchBackend(
347+
$this->server,
347348
$this->server->tree,
348349
$user,
349350
\OC::$server->getRootFolder(),

apps/dav/tests/unit/Files/FileSearchBackendTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\DAV\Connector\Sabre\File;
1414
use OCA\DAV\Connector\Sabre\FilesPlugin;
1515
use OCA\DAV\Connector\Sabre\ObjectTree;
16+
use OCA\DAV\Connector\Sabre\Server;
1617
use OCA\DAV\Files\FileSearchBackend;
1718
use OCP\Files\FileInfo;
1819
use OCP\Files\Folder;
@@ -32,6 +33,7 @@
3233
class FileSearchBackendTest extends TestCase {
3334
/** @var ObjectTree|\PHPUnit\Framework\MockObject\MockObject */
3435
private $tree;
36+
private Server&\PHPUnit\Framework\MockObject\MockObject $server;
3537

3638
/** @var IUser */
3739
private $user;
@@ -70,6 +72,8 @@ protected function setUp(): void {
7072
->disableOriginalConstructor()
7173
->getMock();
7274

75+
$this->server = $this->createMock(Server::class);
76+
7377
$this->view = $this->createMock(View::class);
7478

7579
$this->view->expects($this->any())
@@ -100,7 +104,7 @@ protected function setUp(): void {
100104

101105
$filesMetadataManager = $this->createMock(IFilesMetadataManager::class);
102106

103-
$this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager);
107+
$this->search = new FileSearchBackend($this->server, $this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager);
104108
}
105109

106110
public function testSearchFilename(): void {
@@ -424,4 +428,17 @@ public function testSearchOperatorLimit(): void {
424428
$this->expectException(\InvalidArgumentException::class);
425429
$this->search->search($query);
426430
}
431+
432+
public function testPreloadPropertyFor(): void {
433+
$node1 = $this->createMock(File::class);
434+
$node2 = $this->createMock(Directory::class);
435+
$nodes = [$node1, $node2];
436+
$requestProperties = ['{DAV:}getcontenttype', '{DAV:}getlastmodified'];
437+
438+
$this->server->expects($this->once())
439+
->method('emit')
440+
->with('preloadProperties', [$nodes, $requestProperties]);
441+
442+
$this->search->preloadPropertyFor($nodes, $requestProperties);
443+
}
427444
}

0 commit comments

Comments
 (0)