Skip to content

Commit e8ded6b

Browse files
committed
perf: Replace getById call with getFirstNodeById
And some other minor cleanup in ViewInfoCache Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 3b9de3a commit e8ded6b

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
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
}

0 commit comments

Comments
 (0)