perf: Replace getById call with getFirstNodeById

Avoid looking at all the mounts

Signed-off-by: Carl Schwan <carlschwan@kde.org>
This commit is contained in:
Carl Schwan 2026-02-05 12:01:14 +01:00 committed by backportbot[bot]
parent f86202cafe
commit f344420851
12 changed files with 41 additions and 41 deletions

View file

@ -12,7 +12,6 @@ use OCP\App\IAppManager;
use OCP\Comments\CommentsEvent;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IShareHelper;
@ -47,10 +46,8 @@ class Listener {
foreach ($mounts as $mount) {
$owner = $mount->getUser()->getUID();
$ownerFolder = $this->rootFolder->getUserFolder($owner);
$nodes = $ownerFolder->getById((int)$event->getComment()->getObjectId());
if (!empty($nodes)) {
/** @var Node $node */
$node = array_shift($nodes);
$node = $ownerFolder->getFirstNodeById((int)$event->getComment()->getObjectId());
if ($node !== null) {
$al = $this->shareHelper->getPathsForAccessList($node);
$users += $al['users'];
}

View file

@ -70,11 +70,11 @@ class NotificationsController extends Controller {
return new NotFoundResponse();
}
$userFolder = $this->rootFolder->getUserFolder($currentUser->getUID());
$files = $userFolder->getById((int)$comment->getObjectId());
$file = $userFolder->getFirstNodeById((int)$comment->getObjectId());
$this->markProcessed($comment, $currentUser);
if (empty($files)) {
if ($file === null) {
return new NotFoundResponse();
}

View file

@ -32,8 +32,8 @@ class CommentsEntityEventListener implements IEventListener {
}
$event->addEntityCollection('files', function ($name): bool {
$nodes = $this->rootFolder->getUserFolder($this->userId)->getById((int)$name);
return !empty($nodes);
$node = $this->rootFolder->getUserFolder($this->userId)->getFirstNodeById((int)$name);
return $node !== null;
});
}
}

View file

@ -84,11 +84,10 @@ class Notifier implements INotifier {
throw new UnknownNotificationException('Unsupported comment object');
}
$userFolder = $this->rootFolder->getUserFolder($notification->getUser());
$nodes = $userFolder->getById((int)$parameters[1]);
if (empty($nodes)) {
$node = $userFolder->getFirstNodeById((int)$parameters[1]);
if ($node === null) {
throw new AlreadyProcessedException();
}
$node = $nodes[0];
$path = rtrim($node->getPath(), '/');
if (str_starts_with($path, '/' . $notification->getUser() . '/files/')) {

View file

@ -116,11 +116,11 @@ class CommentsSearchProvider implements IProvider {
* @throws NotFoundException
*/
protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
$nodes = $userFolder->getById((int)$comment->getObjectId());
if (empty($nodes)) {
$node = $userFolder->getFirstNodeById((int)$comment->getObjectId());
if ($node === null) {
throw new NotFoundException('File not found');
}
return array_shift($nodes);
return $node;
}
}

View file

@ -92,12 +92,11 @@ class ListenerTest extends TestCase {
->willReturn($userMountCache);
$node = $this->createMock(Node::class);
$nodes = [ $node ];
$ownerFolder = $this->createMock(Folder::class);
$ownerFolder->expects($this->any())
->method('getById')
->willReturn($nodes);
->method('getFirstNodeById')
->willReturn($node);
$this->rootFolder->expects($this->any())
->method('getUserFolder')

View file

@ -107,8 +107,8 @@ class NotificationsTest extends TestCase {
->willReturn($folder);
$folder->expects($this->once())
->method('getById')
->willReturn([$file]);
->method('getFirstNodeById')
->willReturn($file);
$this->session->expects($this->once())
->method('getUser')
@ -183,8 +183,8 @@ class NotificationsTest extends TestCase {
->willReturn($folder);
$folder->expects($this->once())
->method('getById')
->willReturn([]);
->method('getFirstNodeById')
->willReturn(null);
$user = $this->createMock(IUser::class);

View file

@ -86,9 +86,9 @@ class NotifierTest extends TestCase {
->with('you')
->willReturn($userFolder);
$userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with('678')
->willReturn([$node]);
->willReturn($node);
$this->notification->expects($this->exactly(2))
->method('getUser')
@ -202,9 +202,9 @@ class NotifierTest extends TestCase {
->with('you')
->willReturn($userFolder);
$userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with('678')
->willReturn([$node]);
->willReturn($node);
$this->notification->expects($this->exactly(2))
->method('getUser')
@ -301,7 +301,7 @@ class NotifierTest extends TestCase {
$this->folder
->expects($this->never())
->method('getById');
->method('getFirstNodeById');
$this->notification
->expects($this->once())
@ -338,7 +338,7 @@ class NotifierTest extends TestCase {
$this->folder
->expects($this->never())
->method('getById');
->method('getFirstNodeById');
$this->notification
->expects($this->once())
@ -378,7 +378,7 @@ class NotifierTest extends TestCase {
$this->folder
->expects($this->never())
->method('getById');
->method('getFirstNodeById');
$this->notification
->expects($this->once())
@ -435,7 +435,7 @@ class NotifierTest extends TestCase {
$this->folder
->expects($this->never())
->method('getById');
->method('getFirstNodeById');
$this->notification
->expects($this->once())
@ -497,9 +497,9 @@ class NotifierTest extends TestCase {
->with('you')
->willReturn($userFolder);
$userFolder->expects($this->once())
->method('getById')
->method('getFirstNodeById')
->with('678')
->willReturn([]);
->willReturn(null);
$this->notification->expects($this->once())
->method('getUser')

View file

@ -69,9 +69,8 @@ class ViewOnlyPlugin extends ServerPlugin {
// The version source file is relative to the owner storage.
// But we need the node from the current user perspective.
if ($node->getOwner()->getUID() !== $currentUserId) {
$nodes = $this->userFolder->getById($node->getId());
$node = array_pop($nodes);
if (!$node) {
$node = $this->userFolder->getFirstNodeById($node->getId());
if ($node === null) {
throw new NotFoundException('Version file not accessible by current user');
}
}

View file

@ -97,6 +97,7 @@ class ViewOnlyPluginTest extends TestCase {
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'providesDataForCanGet')]
public function testCanGet(bool $isVersion, ?bool $attrEnabled, bool $expectCanDownloadFile, bool $allowViewWithoutDownload): void {
$nodeInfo = $this->createMock(File::class);
$nodeInfo->method('getId')->willReturn(42);
if ($isVersion) {
$davPath = 'versions/alice/versions/117/123456';
$version = $this->createMock(IVersion::class);
@ -122,8 +123,9 @@ class ViewOnlyPluginTest extends TestCase {
->method('getUID')
->willReturn('bob');
$this->userFolder->expects($this->once())
->method('getById')
->willReturn([$nodeInfo]);
->method('getFirstNodeById')
->with(42)
->willReturn($nodeInfo);
$this->userFolder->expects($this->once())
->method('getOwner')
->willReturn($owner);

View file

@ -20,6 +20,7 @@ use OCP\IUser;
class ResourceProvider implements IProvider {
public const RESOURCE_TYPE = 'file';
/** @var array<int, Node> $nodes */
protected array $nodes = [];
public function __construct(
@ -86,9 +87,9 @@ class ResourceProvider implements IProvider {
}
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$node = $userFolder->getById((int)$resource->getId());
$node = $userFolder->getFirstNodeById((int)$resource->getId());
if ($node) {
if ($node !== null) {
$this->nodes[(int)$resource->getId()] = $node;
return true;
}

View file

@ -164,8 +164,11 @@ class ShareByMailProvider extends DefaultShareProvider implements IShareProvider
if ($share->getShareOwner() !== $share->getSharedBy()) {
$ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
$fileId = $share->getNode()->getId();
$nodes = $ownerFolder->getById($fileId);
$ownerPath = $nodes[0]->getPath();
$node = $ownerFolder->getFirstNodeById($fileId);
if ($node === null) {
throw new \LogicException("Unable to find node $fileId asociated with the share");
}
$ownerPath = $node->getPath();
$this->publishActivity(
$type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_BY : Activity::SUBJECT_UNSHARED_EMAIL_BY,
[$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],