Skip to content

Commit 1282816

Browse files
committed
feat(cache): add iterate method to list files from cache
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 7f71b46 commit 1282816

File tree

10 files changed

+70
-5
lines changed

10 files changed

+70
-5
lines changed

apps/files_sharing/lib/External/Cache.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,13 @@ public function getFolderContentsById($fileId) {
4848
}
4949
return $results;
5050
}
51+
52+
#[Override]
53+
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
54+
$displayId = $this->cloudId->getDisplayId();
55+
foreach (parent::iterateFolderContentsById($fileI, $includeMetadata, $sortByName) as $entry) {
56+
$entry['displayname_owner'] = $displayId;
57+
yield $entry;
58+
}
59+
}
5160
}

apps/files_trashbin/lib/Helper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public static function getTrashFiles($dir, $user, $sortAttribute = '', $sortDesc
4141
$internalPath = $mount->getInternalPath($absoluteDir);
4242

4343
$extraData = Trashbin::getExtraData($user);
44-
$dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir)));
44+
$dirId = $storage->getCache()->getId($mount->getInternalPath($view->getAbsolutePath($dir)));
45+
$dirContent = $storage->getCache()->iterateFolderContentsById($dirId);
4546
foreach ($dirContent as $entry) {
4647
$entryName = $entry->getName();
4748
$name = $entryName;

lib/private/Files/Cache/Cache.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCP\FilesMetadata\IFilesMetadataManager;
3636
use OCP\IDBConnection;
3737
use OCP\Util;
38+
use Override;
3839
use Psr\Log\LoggerInterface;
3940

4041
/**
@@ -238,6 +239,33 @@ public function getFolderContentsById($fileId) {
238239
return [];
239240
}
240241

242+
#[Override]
243+
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
244+
if ($fileId < 1) {
245+
return [];
246+
}
247+
248+
$query = $this->getQueryBuilder()
249+
->selectFileCache()
250+
->whereParent($fileId)
251+
->whereStorageId($this->getNumericStorageId());
252+
if ($includeMetadata === true) {
253+
$metadataQuery = $query->selectMetadata();
254+
}
255+
if ($sortByName) {
256+
$query->orderBy('name', 'ASC');
257+
}
258+
259+
$result = $query->executeQuery();
260+
foreach ($result->iterateAssociative() as $row) {
261+
if ($includeMetadata === true) {
262+
$row['metadata'] = $metadataQuery->extractMetadata($row)->asArray();
263+
}
264+
yield self::cacheEntryFromData($row, $this->mimetypeLoader);
265+
}
266+
$result->closeCursor();
267+
}
268+
241269
/**
242270
* insert or update meta data for a file or folder
243271
*

lib/private/Files/Cache/FailedCache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public function getFolderContentsById($fileId) {
5959
return [];
6060
}
6161

62+
#[Override]
63+
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
64+
return [];
65+
}
66+
6267
public function put($file, array $data) {
6368
}
6469

lib/private/Files/Cache/Watcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function needsUpdate($path, $cachedData) {
141141
* @param string $path
142142
*/
143143
public function cleanFolder($path) {
144-
$cachedContent = $this->cache->getFolderContents($path);
144+
$cachedContent = $this->cache->iterateFolderContentsById($this->cache->getId($path));
145145
foreach ($cachedContent as $entry) {
146146
if (!$this->storage->file_exists($entry['path'])) {
147147
$this->cache->remove($entry['path']);

lib/private/Files/Cache/Wrapper/CacheWrapper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ public function getFolderContentsById($fileId) {
110110
return array_map([$this, 'formatCacheEntry'], $results);
111111
}
112112

113+
#[Override]
114+
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
115+
foreach ($this->getCache()->iterateFolderContentsById($fileId, $includeMetadata, $sortByName) as $entry) {
116+
yield $this->formatCacheEntry($entry);
117+
}
118+
}
119+
113120
/**
114121
* insert or update meta data for a file or folder
115122
*

lib/private/Files/ObjectStore/ObjectStoreStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function rmdir(string $path): bool {
164164
}
165165

166166
private function rmObjects(ICacheEntry $entry): bool {
167-
$children = $this->getCache()->getFolderContentsById($entry->getId());
167+
$children = $this->getCache()->iterateFolderContentsById($entry->getId());
168168
foreach ($children as $child) {
169169
if ($child->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
170170
if (!$this->rmObjects($child)) {
@@ -263,7 +263,7 @@ public function opendir(string $path) {
263263

264264
try {
265265
$files = [];
266-
$folderContents = $this->getCache()->getFolderContents($path);
266+
$folderContents = $this->getCache()->iterateFolderContentsById($this->getCache()->getId($path), false, true);
267267
foreach ($folderContents as $file) {
268268
$files[] = $file['name'];
269269
}

lib/private/Files/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
15071507
}
15081508

15091509
$folderId = $data->getId();
1510-
$contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter
1510+
$contents = $cache->iterateFolderContentsById($folderId, false, true); // TODO Mimetype filter
15111511

15121512
$sharingDisabled = \OCP\Util::isSharingDisabledForUser();
15131513
$permissionsMask = ~\OCP\Constants::PERMISSION_SHARE;

lib/private/Lockdown/Filesystem/NullCache.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function getFolderContentsById($fileId) {
4949
return [];
5050
}
5151

52+
#[Override]
53+
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
54+
return [];
55+
}
56+
5257
public function put($file, array $data) {
5358
throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
5459
}

lib/public/Files/Cache/ICache.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public function getFolderContents($folder);
8282
*/
8383
public function getFolderContentsById($fileId);
8484

85+
/**
86+
* Get an iterator on files stored in folder
87+
*
88+
* Only returns files one level deep, no recursion
89+
*
90+
* @return iterable<IcacheEntry>
91+
* @since 34.0.0
92+
*/
93+
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable;
94+
8595
/**
8696
* store meta data for a file or folder
8797
* This will automatically call either insert or update depending on if the file exists

0 commit comments

Comments
 (0)