Skip to content

Commit 89315eb

Browse files
committed
perf: Replace getById call with getFirstNodeById
Avoid looking at all the mounts Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 08e1d9b commit 89315eb

File tree

12 files changed

+39
-41
lines changed

12 files changed

+39
-41
lines changed

apps/comments/lib/Activity/Listener.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use OCP\Comments\CommentsEvent;
1313
use OCP\Files\Config\IMountProviderCollection;
1414
use OCP\Files\IRootFolder;
15-
use OCP\Files\Node;
1615
use OCP\IUser;
1716
use OCP\IUserSession;
1817
use OCP\Share\IShareHelper;
@@ -47,10 +46,8 @@ public function commentEvent(CommentsEvent $event): void {
4746
foreach ($mounts as $mount) {
4847
$owner = $mount->getUser()->getUID();
4948
$ownerFolder = $this->rootFolder->getUserFolder($owner);
50-
$nodes = $ownerFolder->getById((int)$event->getComment()->getObjectId());
51-
if (!empty($nodes)) {
52-
/** @var Node $node */
53-
$node = array_shift($nodes);
49+
$node = $ownerFolder->getFirstNodeById((int)$event->getComment()->getObjectId());
50+
if ($node !== null) {
5451
$al = $this->shareHelper->getPathsForAccessList($node);
5552
$users += $al['users'];
5653
}

apps/comments/lib/Controller/NotificationsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ public function view(string $id): RedirectResponse|NotFoundResponse {
7070
return new NotFoundResponse();
7171
}
7272
$userFolder = $this->rootFolder->getUserFolder($currentUser->getUID());
73-
$files = $userFolder->getById((int)$comment->getObjectId());
73+
$file = $userFolder->getFirstNodeById((int)$comment->getObjectId());
7474

7575
$this->markProcessed($comment, $currentUser);
7676

77-
if (empty($files)) {
77+
if ($file === null) {
7878
return new NotFoundResponse();
7979
}
8080

apps/comments/lib/Listener/CommentsEntityEventListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function handle(Event $event): void {
3232
}
3333

3434
$event->addEntityCollection('files', function ($name): bool {
35-
$nodes = $this->rootFolder->getUserFolder($this->userId)->getById((int)$name);
36-
return !empty($nodes);
35+
$node = $this->rootFolder->getUserFolder($this->userId)->getFirstNodeById((int)$name);
36+
return $node !== null;
3737
});
3838
}
3939
}

apps/comments/lib/Notification/Notifier.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,10 @@ public function prepare(INotification $notification, string $languageCode): INot
8484
throw new UnknownNotificationException('Unsupported comment object');
8585
}
8686
$userFolder = $this->rootFolder->getUserFolder($notification->getUser());
87-
$nodes = $userFolder->getById((int)$parameters[1]);
88-
if (empty($nodes)) {
87+
$node = $userFolder->getFirstNodeById((int)$parameters[1]);
88+
if ($node === null) {
8989
throw new AlreadyProcessedException();
9090
}
91-
$node = $nodes[0];
9291

9392
$path = rtrim($node->getPath(), '/');
9493
if (str_starts_with($path, '/' . $notification->getUser() . '/files/')) {

apps/comments/lib/Search/CommentsSearchProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
116116
* @throws NotFoundException
117117
*/
118118
protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
119-
$nodes = $userFolder->getById((int)$comment->getObjectId());
120-
if (empty($nodes)) {
119+
$node = $userFolder->getFirstNodeById((int)$comment->getObjectId());
120+
if ($node === null) {
121121
throw new NotFoundException('File not found');
122122
}
123123

124-
return array_shift($nodes);
124+
return $node;
125125
}
126126
}

apps/comments/tests/Unit/Activity/ListenerTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ public function testCommentEvent(): void {
9292
->willReturn($userMountCache);
9393

9494
$node = $this->createMock(Node::class);
95-
$nodes = [ $node ];
9695

9796
$ownerFolder = $this->createMock(Folder::class);
9897
$ownerFolder->expects($this->any())
99-
->method('getById')
100-
->willReturn($nodes);
98+
->method('getFirstNodeById')
99+
->willReturn($node);
101100

102101
$this->rootFolder->expects($this->any())
103102
->method('getUserFolder')

apps/comments/tests/Unit/Controller/NotificationsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public function testViewSuccess(): void {
107107
->willReturn($folder);
108108

109109
$folder->expects($this->once())
110-
->method('getById')
111-
->willReturn([$file]);
110+
->method('getFirstNodeById')
111+
->willReturn($file);
112112

113113
$this->session->expects($this->once())
114114
->method('getUser')
@@ -183,8 +183,8 @@ public function testViewNoFile(): void {
183183
->willReturn($folder);
184184

185185
$folder->expects($this->once())
186-
->method('getById')
187-
->willReturn([]);
186+
->method('getFirstNodeById')
187+
->willReturn(null);
188188

189189
$user = $this->createMock(IUser::class);
190190

apps/comments/tests/Unit/Notification/NotifierTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ public function testPrepareSuccess(): void {
8686
->with('you')
8787
->willReturn($userFolder);
8888
$userFolder->expects($this->once())
89-
->method('getById')
89+
->method('getFirstNodeById')
9090
->with('678')
91-
->willReturn([$node]);
91+
->willReturn($node);
9292

9393
$this->notification->expects($this->exactly(2))
9494
->method('getUser')
@@ -202,9 +202,9 @@ public function testPrepareSuccessDeletedUser(): void {
202202
->with('you')
203203
->willReturn($userFolder);
204204
$userFolder->expects($this->once())
205-
->method('getById')
205+
->method('getFirstNodeById')
206206
->with('678')
207-
->willReturn([$node]);
207+
->willReturn($node);
208208

209209
$this->notification->expects($this->exactly(2))
210210
->method('getUser')
@@ -301,7 +301,7 @@ public function testPrepareDifferentApp(): void {
301301

302302
$this->folder
303303
->expects($this->never())
304-
->method('getById');
304+
->method('getFirstNodeById');
305305

306306
$this->notification
307307
->expects($this->once())
@@ -338,7 +338,7 @@ public function testPrepareNotFound(): void {
338338

339339
$this->folder
340340
->expects($this->never())
341-
->method('getById');
341+
->method('getFirstNodeById');
342342

343343
$this->notification
344344
->expects($this->once())
@@ -378,7 +378,7 @@ public function testPrepareDifferentSubject(): void {
378378

379379
$this->folder
380380
->expects($this->never())
381-
->method('getById');
381+
->method('getFirstNodeById');
382382

383383
$this->notification
384384
->expects($this->once())
@@ -435,7 +435,7 @@ public function testPrepareNotFiles(): void {
435435

436436
$this->folder
437437
->expects($this->never())
438-
->method('getById');
438+
->method('getFirstNodeById');
439439

440440
$this->notification
441441
->expects($this->once())
@@ -497,9 +497,9 @@ public function testPrepareUnresolvableFileID(): void {
497497
->with('you')
498498
->willReturn($userFolder);
499499
$userFolder->expects($this->once())
500-
->method('getById')
500+
->method('getFirstNodeById')
501501
->with('678')
502-
->willReturn([]);
502+
->willReturn(null);
503503

504504
$this->notification->expects($this->once())
505505
->method('getUser')

apps/dav/lib/DAV/ViewOnlyPlugin.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ public function checkViewOnly(RequestInterface $request): bool {
6969
// The version source file is relative to the owner storage.
7070
// But we need the node from the current user perspective.
7171
if ($node->getOwner()->getUID() !== $currentUserId) {
72-
$nodes = $this->userFolder->getById($node->getId());
73-
$node = array_pop($nodes);
74-
if (!$node) {
72+
$node = $this->userFolder->getFirstNodeById($node->getId());
73+
if ($node === null) {
7574
throw new NotFoundException('Version file not accessible by current user');
7675
}
7776
}

apps/dav/tests/unit/DAV/ViewOnlyPluginTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanD
122122
->method('getUID')
123123
->willReturn('bob');
124124
$this->userFolder->expects($this->once())
125-
->method('getById')
126-
->willReturn([$nodeInfo]);
125+
->method('getFirstNodeById')
126+
->willReturn($nodeInfo);
127127
$this->userFolder->expects($this->once())
128128
->method('getOwner')
129129
->willReturn($owner);

0 commit comments

Comments
 (0)