Skip to content

Commit

Permalink
Merge pull request #42008 from nextcloud/techdebt/noid/allow-to-load-…
Browse files Browse the repository at this point in the history
…multiple-metadata-with-1-query

fix(metadata): Allow to load metadata of multiple files at once
  • Loading branch information
nickvergessen authored Dec 4, 2023
2 parents 89aa395 + 2a0daf7 commit 3bff235
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/private/FilesMetadata/FilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ public function getMetadata(int $fileId, bool $generate = false): IFilesMetadata
}
}

/**
* returns metadata of multiple file ids
*
* @param int[] $fileIds file ids
*
* @return array File ID is the array key, files without metadata are not returned in the array
* @psalm-return array<int, IFilesMetadata>
* @since 28.0.0
*/
public function getMetadataForFiles(array $fileIds): array {
return $this->metadataRequestService->getMetadataFromFileIds($fileIds);
}

/**
* @param IFilesMetadata $filesMetadata metadata
*
Expand Down
34 changes: 34 additions & 0 deletions lib/private/FilesMetadata/Service/MetadataRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ public function getMetadataFromFileId(int $fileId): IFilesMetadata {
return $metadata;
}

/**
* returns metadata for multiple file ids
*
* If
*
* @param array $fileIds file ids
*
* @return array File ID is the array key, files without metadata are not returned in the array
* @psalm-return array<int, IFilesMetadata>
*/
public function getMetadataFromFileIds(array $fileIds): array {
$qb = $this->dbConnection->getQueryBuilder();
$qb->select('file_id', 'json', 'sync_token')->from(self::TABLE_METADATA);
$qb->where(
$qb->expr()->in('file_id', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY))
);

$list = [];
$result = $qb->executeQuery();
while ($data = $result->fetch()) {
$fileId = (int) $data['file_id'];
$metadata = new FilesMetadata($fileId);
try {
$metadata->importFromDatabase($data);
} catch (FilesMetadataNotFoundException) {
continue;
}
$list[$fileId] = $metadata;
}
$result->closeCursor();

return $list;
}

/**
* drop metadata related to a file id
*
Expand Down
14 changes: 13 additions & 1 deletion lib/public/FilesMetadata/IFilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function refreshMetadata(
): IFilesMetadata;

/**
* returns metadata from a file id
* returns metadata of a file id
*
* @param int $fileId file id
* @param boolean $generate Generate if metadata does not exist
Expand All @@ -82,6 +82,18 @@ public function refreshMetadata(
*/
public function getMetadata(int $fileId, bool $generate = false): IFilesMetadata;

/**
* returns metadata of multiple file ids
*
* @param int[] $fileIds file ids
*
* @return array File ID is the array key, files without metadata are not returned in the array
* @psalm-return array<int, IFilesMetadata>
* @throws FilesMetadataNotFoundException if not found
* @since 28.0.0
*/
public function getMetadataForFiles(array $fileIds): array;

/**
* save metadata to database and refresh indexes.
* metadata are saved if new data are available.
Expand Down

0 comments on commit 3bff235

Please sign in to comment.