From edd37d349b974baf4feeafe300ff79cad86f9492 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Thu, 29 Jan 2026 15:42:28 +0100 Subject: [PATCH] perf(file-cache): Add mimetype filter on getFolderContents Signed-off-by: Carl Schwan --- .../tests/Crypto/EncryptAllTest.php | 4 ++-- apps/files/lib/Controller/ApiController.php | 5 ++++- autotest.sh | 2 +- lib/private/Files/Cache/Cache.php | 19 +++++-------------- lib/private/Files/Cache/FailedCache.php | 2 +- .../Files/Cache/Wrapper/CacheWrapper.php | 4 ++-- lib/private/Files/Filesystem.php | 4 ++-- lib/private/Lockdown/Filesystem/NullCache.php | 2 +- lib/public/Files/Cache/ICache.php | 18 ++++++++++-------- 9 files changed, 28 insertions(+), 32 deletions(-) diff --git a/apps/encryption/tests/Crypto/EncryptAllTest.php b/apps/encryption/tests/Crypto/EncryptAllTest.php index d3dcffae677..91d3559c70a 100644 --- a/apps/encryption/tests/Crypto/EncryptAllTest.php +++ b/apps/encryption/tests/Crypto/EncryptAllTest.php @@ -331,7 +331,7 @@ class EncryptAllTest extends TestCase { ->willReturnMap([ [ '/user1/files', - '', + null, null, [ $this->createFileInfoMock(FileInfo::TYPE_FOLDER, 'foo'), @@ -340,7 +340,7 @@ class EncryptAllTest extends TestCase { ], [ '/user1/files/foo', - '', + null, null, [ $this->createFileInfoMock(FileInfo::TYPE_FILE, 'subfile'), diff --git a/apps/files/lib/Controller/ApiController.php b/apps/files/lib/Controller/ApiController.php index 790b0264594..735c3a17923 100644 --- a/apps/files/lib/Controller/ApiController.php +++ b/apps/files/lib/Controller/ApiController.php @@ -247,9 +247,11 @@ class ApiController extends Controller { /** * @param \OCP\Files\Node[] $nodes + * @param ?non-empty-string $mimeTypeFilter limit returned content to this mimetype or mimepart * @param int $depth The depth to traverse into the contents of each node + * @return FilesFolderTree */ - private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0, string $mimeTypeFilter = ''): array { + private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0, ?string $mimeTypeFilter = null): array { if ($currentDepth >= $depth) { return []; } @@ -272,6 +274,7 @@ class ApiController extends Controller { } $children[] = $entry; } + /** @var FilesFolderTree $children */ return $children; } diff --git a/autotest.sh b/autotest.sh index ec8631a7272..bac67a768be 100755 --- a/autotest.sh +++ b/autotest.sh @@ -309,7 +309,7 @@ function execute_tests { if [ ! -z "$USEDOCKER" ] ; then echo "Fire up the postgres docker" DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_DB="$DATABASENAME" -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres) - DATABASEHOST=$(docker inspect --format="{{ range .NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}" "$DOCKER_CONTAINER_ID") + DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID") echo "Waiting for Postgres initialisation ..." diff --git a/lib/private/Files/Cache/Cache.php b/lib/private/Files/Cache/Cache.php index 25045d14769..9bd6e584974 100644 --- a/lib/private/Files/Cache/Cache.php +++ b/lib/private/Files/Cache/Cache.php @@ -37,6 +37,7 @@ use OCP\FilesMetadata\IFilesMetadataManager; use OCP\IDBConnection; use OCP\Server; use OCP\Util; +use Override; use Psr\Log\LoggerInterface; /** @@ -201,23 +202,13 @@ class Cache implements ICache { return new CacheEntry($normalized); } - /** - * get the metadata of all files stored in $folder - * - * @param string $folder - * @return ICacheEntry[] - */ - public function getFolderContents($folder) { + #[Override] + public function getFolderContents(string $folder, ?string $mimeTypeFilter = null) { $fileId = $this->getId($folder); - return $this->getFolderContentsById($fileId); + return $this->getFolderContentsById($fileId, $mimeTypeFilter); } - /** - * get the metadata of all files stored in $folder - * - * @param int $fileId the file id of the folder - * @return ICacheEntry[] - */ + #[Override] public function getFolderContentsById(int $fileId, ?string $mimeTypeFilter = null) { if ($fileId > -1) { $query = $this->getQueryBuilder(); diff --git a/lib/private/Files/Cache/FailedCache.php b/lib/private/Files/Cache/FailedCache.php index acfc0a947d5..82d2dbbd8cf 100644 --- a/lib/private/Files/Cache/FailedCache.php +++ b/lib/private/Files/Cache/FailedCache.php @@ -45,7 +45,7 @@ class FailedCache implements ICache { } } - public function getFolderContents($folder): array { + public function getFolderContents(string $folder, ?string $mimeTypeFilter = null): array { return []; } diff --git a/lib/private/Files/Cache/Wrapper/CacheWrapper.php b/lib/private/Files/Cache/Wrapper/CacheWrapper.php index 1a75a13a80f..06e94ae1463 100644 --- a/lib/private/Files/Cache/Wrapper/CacheWrapper.php +++ b/lib/private/Files/Cache/Wrapper/CacheWrapper.php @@ -89,11 +89,11 @@ class CacheWrapper extends Cache { * @param string $folder * @return ICacheEntry[] */ - public function getFolderContents($folder) { + public function getFolderContents(string $folder, ?string $mimeTypeFilter = null): array { // can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this // and not the wrapped cache $fileId = $this->getId($folder); - return $this->getFolderContentsById($fileId); + return $this->getFolderContentsById($fileId, $mimeTypeFilter); } /** diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php index d883e7fbe86..fcefbac5e24 100644 --- a/lib/private/Files/Filesystem.php +++ b/lib/private/Files/Filesystem.php @@ -666,10 +666,10 @@ class Filesystem { * Get the content of a directory. * * @param string $directory path under datadirectory - * @param string $mimeTypeFilter limit returned content to this mimetype or mimepart + * @param ?non-empty-string $mimeTypeFilter limit returned content to this mimetype or mimepart * @return FileInfo[] */ - public static function getDirectoryContent($directory, string $mimeTypeFilter = '') { + public static function getDirectoryContent($directory, ?string $mimeTypeFilter = null): array { return self::$defaultInstance->getDirectoryContent($directory, $mimeTypeFilter); } diff --git a/lib/private/Lockdown/Filesystem/NullCache.php b/lib/private/Lockdown/Filesystem/NullCache.php index d17ff49c3d3..b722d719d6a 100644 --- a/lib/private/Lockdown/Filesystem/NullCache.php +++ b/lib/private/Lockdown/Filesystem/NullCache.php @@ -44,7 +44,7 @@ class NullCache implements ICache { ]); } - public function getFolderContents($folder): array { + public function getFolderContents(string $folder, ?string $mimeTypeFilter = null): array { return []; } diff --git a/lib/public/Files/Cache/ICache.php b/lib/public/Files/Cache/ICache.php index 00bed53b44f..2f324c85f85 100644 --- a/lib/public/Files/Cache/ICache.php +++ b/lib/public/Files/Cache/ICache.php @@ -52,7 +52,7 @@ interface ICache { public function getNumericStorageId(); /** - * get the stored metadata of a file or folder + * Get the stored metadata of a file or folder. * * @param string | int $file either the path of a file or folder or the file id for a file or folder * @return ICacheEntry|false the cache entry or false if the file is not found in the cache @@ -61,20 +61,21 @@ interface ICache { public function get($file); /** - * get the metadata of all files stored in $folder + * Get the metadata of all files stored in $folder. * - * Only returns files one level deep, no recursion + * @note This only returns files one level deep with no recursion. * * @param string $folder + * @param ?non-empty-string $mimeTypeFilter The mimetype or mimepart for which the content should be filtered * @return ICacheEntry[] * @since 9.0.0 */ - public function getFolderContents($folder); + public function getFolderContents(string $folder, ?string $mimeTypeFilter = null); /** - * get the metadata of all files stored in $folder + * Get the metadata of all files stored in $folder. * - * Only returns files one level deep, no recursion + * @note This only returns files one level deep with no recursion. * * @param int $fileId the file id of the folder * @param ?non-empty-string $mimeTypeFilter The mimetype or mimepart for which the content should be filtered @@ -85,8 +86,9 @@ interface ICache { public function getFolderContentsById(int $fileId, ?string $mimeTypeFilter = null); /** - * store meta data for a file or folder - * This will automatically call either insert or update depending on if the file exists + * Store meta data for a file or folder. + * + * This will automatically call either insert or update depending on if the file exists. * * @param string $file * @param array $data