Skip to content

Commit 43e45ae

Browse files
committed
perf(dav): Preload dav search with tags/favorites
Signed-off-by: Julius Knorr <[email protected]>
1 parent 080473c commit 43e45ae

File tree

4 files changed

+52
-14
lines changed

4 files changed

+52
-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
@@ -352,6 +352,7 @@ public function __construct(
352352
\OCP\Server::get(IAppManager::class)
353353
));
354354
$lazySearchBackend->setBackend(new FileSearchBackend(
355+
$this->server,
355356
$this->server->tree,
356357
$user,
357358
\OCP\Server::get(IRootFolder::class),

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\DAV\Connector\Sabre\File;
1616
use OCA\DAV\Connector\Sabre\FilesPlugin;
1717
use OCA\DAV\Connector\Sabre\ObjectTree;
18+
use OCA\DAV\Connector\Sabre\Server;
1819
use OCA\DAV\Files\FileSearchBackend;
1920
use OCP\Files\FileInfo;
2021
use OCP\Files\Folder;
@@ -36,6 +37,7 @@
3637

3738
class FileSearchBackendTest extends TestCase {
3839
private ObjectTree&MockObject $tree;
40+
private Server&MockObject $server;
3941
private IUser&MockObject $user;
4042
private IRootFolder&MockObject $rootFolder;
4143
private IManager&MockObject $shareManager;
@@ -53,6 +55,7 @@ protected function setUp(): void {
5355
->willReturn('test');
5456

5557
$this->tree = $this->createMock(ObjectTree::class);
58+
$this->server = $this->createMock(Server::class);
5659
$this->view = $this->createMock(View::class);
5760
$this->rootFolder = $this->createMock(IRootFolder::class);
5861
$this->shareManager = $this->createMock(IManager::class);
@@ -78,7 +81,7 @@ protected function setUp(): void {
7881

7982
$filesMetadataManager = $this->createMock(IFilesMetadataManager::class);
8083

81-
$this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager);
84+
$this->search = new FileSearchBackend($this->server, $this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager);
8285
}
8386

8487
public function testSearchFilename(): void {
@@ -402,4 +405,17 @@ public function testSearchOperatorLimit(): void {
402405
$this->expectException(\InvalidArgumentException::class);
403406
$this->search->search($query);
404407
}
408+
409+
public function testPreloadPropertyFor(): void {
410+
$node1 = $this->createMock(File::class);
411+
$node2 = $this->createMock(Directory::class);
412+
$nodes = [$node1, $node2];
413+
$requestProperties = ['{DAV:}getcontenttype', '{DAV:}getlastmodified'];
414+
415+
$this->server->expects($this->once())
416+
->method('emit')
417+
->with('preloadProperties', [$nodes, $requestProperties]);
418+
419+
$this->search->preloadPropertyFor($nodes, $requestProperties);
420+
}
405421
}

0 commit comments

Comments
 (0)