Skip to content

Commit 7b4052b

Browse files
authored
Merge pull request #2388 from nextcloud/backport/2360/stable33
[stable33] perf: Replace getById call with getFirstNodeById
2 parents 442ca79 + abd98de commit 7b4052b

File tree

3 files changed

+35
-50
lines changed

3 files changed

+35
-50
lines changed

lib/ViewInfoCache.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
57
* SPDX-License-Identifier: AGPL-3.0-only
@@ -13,24 +15,14 @@
1315
use OCP\Files\NotFoundException;
1416

1517
class ViewInfoCache {
16-
/** @var array */
17-
protected $cachePath;
18-
/** @var array */
19-
protected $cacheId;
20-
18+
protected array $cacheId = [];
2119

2220
public function __construct(
2321
protected IRootFolder $rootFolder,
2422
) {
2523
}
2624

27-
/**
28-
* @param string $user
29-
* @param int $fileId
30-
* @param string $path
31-
* @return array
32-
*/
33-
public function getInfoById($user, $fileId, $path) {
25+
public function getInfoById(string $user, int $fileId, string $path): array {
3426
if (isset($this->cacheId[$user][$fileId])) {
3527
$cache = $this->cacheId[$user][$fileId];
3628
if ($cache['path'] === null) {
@@ -43,12 +35,9 @@ public function getInfoById($user, $fileId, $path) {
4335
}
4436

4537
/**
46-
* @param string $user
47-
* @param int $fileId
48-
* @param string $filePath
49-
* @return array
38+
* @return array{path: string, exists: bool, is_dir: bool, view: string, node?: Node}
5039
*/
51-
protected function findInfoById($user, $fileId, $filePath) {
40+
protected function findInfoById(string $user, int $fileId, string $filePath): array {
5241
$cache = [
5342
'path' => $filePath,
5443
'exists' => false,
@@ -59,39 +48,36 @@ protected function findInfoById($user, $fileId, $filePath) {
5948
$notFound = false;
6049
try {
6150
$userFolder = $this->rootFolder->getUserFolder($user);
62-
$entries = $userFolder->getById($fileId);
63-
if (empty($entries)) {
51+
$entry = $userFolder->getFirstNodeById($fileId);
52+
if ($entry === null) {
6453
throw new NotFoundException('No entries returned');
6554
}
66-
/** @var Node $entry */
67-
$entry = array_shift($entries);
6855

6956
$cache['path'] = $userFolder->getRelativePath($entry->getPath());
7057
$cache['is_dir'] = $entry instanceof Folder;
7158
$cache['exists'] = true;
7259
$cache['node'] = $entry;
73-
} catch (NotFoundException $e) {
60+
} catch (NotFoundException) {
7461
// The file was not found in the normal view,
7562
// maybe it is in the trashbin?
7663
try {
77-
/** @var Folder $userTrashBin */
7864
$userTrashBin = $this->rootFolder->get('/' . $user . '/files_trashbin');
79-
$entries = $userTrashBin->getById($fileId);
80-
if (empty($entries)) {
65+
if (!$userTrashBin instanceof Folder) {
66+
throw new NotFoundException('No trash bin found for user: ' . $user);
67+
}
68+
$entry = $userTrashBin->getFirstNodeById($fileId);
69+
if ($entry === null) {
8170
throw new NotFoundException('No entries returned');
8271
}
8372

84-
/** @var Node $entry */
85-
$entry = array_shift($entries);
86-
8773
$cache = [
8874
'path' => $userTrashBin->getRelativePath($entry->getPath()),
8975
'exists' => true,
9076
'is_dir' => $entry instanceof Folder,
9177
'view' => 'trashbin',
9278
'node' => $entry,
9379
];
94-
} catch (NotFoundException $e) {
80+
} catch (NotFoundException) {
9581
$notFound = true;
9682
}
9783
}

tests/ViewInfoCacheTest.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use OCP\Files\File;
2828
use OCP\Files\Folder;
2929
use OCP\Files\IRootFolder;
30-
use OCP\Files\NotFoundException;
3130
use PHPUnit\Framework\Attributes\DataProvider;
3231
use PHPUnit\Framework\MockObject\MockObject;
3332

@@ -57,7 +56,7 @@ public function getCache(array $methods = []): ViewInfoCache|MockObject {
5756
public static function dataGetInfoById(): array {
5857
return [
5958
[
60-
'user', 23, 'path', [], true, 'findInfoById',
59+
'user', 23, 'path', [], true, ['findInfoById'],
6160
],
6261
[
6362
'user',
@@ -69,7 +68,7 @@ public static function dataGetInfoById(): array {
6968
]
7069
],
7170
true,
72-
'findInfoById',
71+
['findInfoById'],
7372
],
7473
[
7574
'user',
@@ -81,7 +80,7 @@ public static function dataGetInfoById(): array {
8180
]
8281
],
8382
true,
84-
'findInfoById',
83+
['findInfoById'],
8584
],
8685
[
8786
'user',
@@ -111,15 +110,15 @@ public static function dataGetInfoById(): array {
111110
}
112111

113112
#[DataProvider('dataGetInfoById')]
114-
public function testGetInfoById(string $user, int $id, string|array $path, array $cache, bool $callsFind, string|array $expected): void {
113+
public function testGetInfoById(string $user, int $id, string|array $path, array $cache, bool $callsFind, array $expected): void {
115114
$infoCache = $this->getCache([
116115
'findInfoById',
117116
]);
118117

119118
if ($callsFind) {
120119
$infoCache->expects($this->once())
121120
->method('findInfoById')
122-
->willReturn('findInfoById');
121+
->willReturn(['findInfoById']);
123122
} else {
124123
$infoCache->expects($this->never())
125124
->method('findInfoById');
@@ -239,9 +238,9 @@ public function testFindInfoById(string $user, int $fileId, string $filename, ?s
239238
->willReturn($userFolder);
240239
if ($path === null) {
241240
$userFolder->expects($this->once())
242-
->method('getById')
241+
->method('getFirstNodeById')
243242
->with($fileId)
244-
->willThrowException(new NotFoundException());
243+
->willReturn(null);
245244

246245
$userTrashBin = $this->createMock(Folder::class);
247246
$this->rootFolder->expects($this->once())
@@ -250,9 +249,9 @@ public function testFindInfoById(string $user, int $fileId, string $filename, ?s
250249
->willReturn($userTrashBin);
251250
if ($pathTrash === null) {
252251
$userTrashBin->expects($this->once())
253-
->method('getById')
252+
->method('getFirstNodeById')
254253
->with($fileId)
255-
->willThrowException(new NotFoundException());
254+
->willReturn(null);
256255
} else {
257256
$node = $this->createMock($isDir ? Folder::class : File::class);
258257
$node
@@ -264,9 +263,9 @@ public function testFindInfoById(string $user, int $fileId, string $filename, ?s
264263
->willReturn($pathTrash);
265264

266265
$userTrashBin->expects($this->once())
267-
->method('getById')
266+
->method('getFirstNodeById')
268267
->with($fileId)
269-
->willReturn([2 => $node]);
268+
->willReturn($node);
270269
$expected['node'] = $node;
271270
$expectedCache[$user][$fileId]['node'] = $node;
272271
}
@@ -281,9 +280,9 @@ public function testFindInfoById(string $user, int $fileId, string $filename, ?s
281280
->willReturn($path);
282281

283282
$userFolder->expects($this->once())
284-
->method('getById')
283+
->method('getFirstNodeById')
285284
->with($fileId)
286-
->willReturn([3 => $node]);
285+
->willReturn($node);
287286
$expected['node'] = $node;
288287
$expectedCache[$user][$fileId]['node'] = $node;
289288
}

vendor-bin/psalm/composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)