-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
perf(preview): Split preview data to new table #54543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
18fbacd
perf(preview): Split preview data to new table
656e33e
perf(preview): Add support for multibucket storage
13c35c0
perf(preview): Migrate previews to the new optimized table
6008852
feat(preview): Support multibucket storage
CarlSchwan bba9667
perf(preview): Adapt BackgroundCleanupJob to new previews table
CarlSchwan b035766
perf(preview): Optimize migration and simplify DB layout
CarlSchwan 6f56dcf
fix(preview): Fix some tests
CarlSchwan 324b54b
refactor(preview): Cleanup the implementation of the new preview backend
CarlSchwan bfc7d5d
feat(preview): Implement scanning for previews
CarlSchwan 5802378
feat(preview): Store original file mimetype in preview table
CarlSchwan bd001c9
refactor: Use Override annotation in new preview code
CarlSchwan 66f50bd
refactor(preview): Use same mimetype ids as filecache
CarlSchwan bef3996
fix(preview): Make version column a string
CarlSchwan fed7a33
refactor(preview-object-store): Refactor object store backend
CarlSchwan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
perf(preview): Split preview data to new table
The new oc_previews table is optimized for storing previews and should decrease significantly the space taken by previews in the filecache table. This attend to reuse the IObjectStore abstraction over S3/Swift/Azure but currently only support one single bucket configuration. Signed-off-by: Carl Schwan <[email protected]>
- Loading branch information
commit 18fbacdd8d519e88e8cc53438a6209428f90085d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OC\Core\Migrations; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| class Version33000Date20250819110529 extends SimpleMigrationStep { | ||
|
|
||
| /** | ||
| * @param Closure(): ISchemaWrapper $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
| */ | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
|
|
||
| if (!$schema->hasTable('previews')) { | ||
| $table = $schema->createTable('previews'); | ||
| $table->addColumn('id', Types::BIGINT, ['autoincrement' => true, 'notnull' => true, 'length' => 20, 'unsigned' => true]); | ||
| $table->addColumn('file_id', Types::BIGINT, ['notnull' => true, 'length' => 20, 'unsigned' => true]); | ||
| $table->addColumn('width', Types::INTEGER, ['notnull' => true, 'unsigned' => true]); | ||
| $table->addColumn('height', Types::INTEGER, ['notnull' => true, 'unsigned' => true]); | ||
| $table->addColumn('mimetype', Types::INTEGER, ['notnull' => true]); | ||
| $table->addColumn('is_max', Types::BOOLEAN, ['notnull' => true, 'default' => false]); | ||
| $table->addColumn('crop', Types::BOOLEAN, ['notnull' => true, 'default' => false]); | ||
| $table->addColumn('etag', Types::STRING, ['notnull' => true, 'length' => 40]); | ||
| $table->addColumn('mtime', Types::INTEGER, ['notnull' => true, 'unsigned' => true]); | ||
| $table->addColumn('size', Types::INTEGER, ['notnull' => true, 'unsigned' => true]); | ||
| $table->addColumn('version', Types::BIGINT, ['notnull' => false, 'unsigned' => true]); | ||
Altahrim marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $table->setPrimaryKey(['id']); | ||
| $table->addUniqueIndex(['file_id', 'width', 'height', 'mimetype', 'crop', 'version'], 'previews_file_uniq_idx'); | ||
| } | ||
|
|
||
| return $schema; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-FileContributor: Carl Schwan | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Preview\Db; | ||
|
|
||
| use OCP\AppFramework\Db\Entity; | ||
| use OCP\DB\Types; | ||
| use OCP\IPreview; | ||
|
|
||
| /** | ||
| * @method \int getFileId() | ||
| * @method void setFileId(int $fileId) | ||
| * @method \int getWidth() | ||
| * @method void setWidth(int $width) | ||
| * @method \int getHeight() | ||
| * @method void setHeight(int $height) | ||
| * @method \int getMode() | ||
| * @method void setMode(int $mode) | ||
| * @method \bool getCrop() | ||
| * @method void setCrop(bool $crop) | ||
| * @method void setMimetype(int $mimetype) | ||
| * @method IPreview::MIMETYPE_* getMimetype() | ||
| * @method \int getMtime() | ||
| * @method void setMtime(int $mtime) | ||
| * @method \int getSize() | ||
| * @method void setSize(int $size) | ||
| * @method \bool getIsMax() | ||
| * @method void setIsMax(bool $max) | ||
| * @method \string getEtag() | ||
| * @method void setEtag(string $etag) | ||
| * @method ?\int getVersion() | ||
| * @method void setVersion(?int $version) | ||
| */ | ||
| class Preview extends Entity { | ||
| protected ?int $fileId = null; | ||
|
|
||
| protected ?int $width = null; | ||
|
|
||
| protected ?int $height = null; | ||
|
|
||
| protected ?int $mimetype = null; | ||
|
|
||
| protected ?int $mtime = null; | ||
|
|
||
| protected ?int $size = null; | ||
|
|
||
| protected ?bool $isMax = null; | ||
|
|
||
| protected ?bool $crop = null; | ||
|
|
||
| protected ?string $etag = null; | ||
| protected ?int $version = null; | ||
|
|
||
| public function __construct() { | ||
| $this->addType('fileId', Types::INTEGER); | ||
| $this->addType('width', Types::INTEGER); | ||
| $this->addType('height', Types::INTEGER); | ||
| $this->addType('mimetype', Types::INTEGER); | ||
| $this->addType('mtime', Types::INTEGER); | ||
| $this->addType('size', Types::INTEGER); | ||
| $this->addType('isMax', Types::BOOLEAN); | ||
| $this->addType('crop', Types::BOOLEAN); | ||
| $this->addType('etag', Types::STRING); | ||
| $this->addType('version', Types::INTEGER); | ||
| } | ||
|
|
||
| public function getName(): string { | ||
| $path = ($this->getVersion() ? $this->getVersion() . '-' : '') . $this->getWidth() . '-' . $this->getHeight(); | ||
| if ($this->getCrop()) { | ||
| $path .= '-crop'; | ||
| } | ||
| if ($this->getIsMax()) { | ||
| $path .= '-max'; | ||
| } | ||
|
|
||
| $ext = $this->getExtension(); | ||
| $path .= '.' . $ext; | ||
| return $path; | ||
| } | ||
|
|
||
| public function getMimetypeValue(): string { | ||
| return match ($this->mimetype) { | ||
| IPreview::MIMETYPE_JPEG => 'image/jpeg', | ||
| IPreview::MIMETYPE_PNG => 'image/png', | ||
| IPreview::MIMETYPE_WEBP => 'image/webp', | ||
| IPreview::MIMETYPE_GIF => 'image/gif', | ||
| }; | ||
| } | ||
|
|
||
| public function getExtension(): string { | ||
| return match ($this->mimetype) { | ||
| IPreview::MIMETYPE_JPEG => 'jpeg', | ||
| IPreview::MIMETYPE_PNG => 'png', | ||
| IPreview::MIMETYPE_WEBP => 'webp', | ||
| IPreview::MIMETYPE_GIF => 'gif', | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Preview\Db; | ||
|
|
||
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use OCP\AppFramework\Db\QBMapper; | ||
| use OCP\DB\Exception; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\IDBConnection; | ||
| use OCP\IPreview; | ||
|
|
||
| /** | ||
| * @template-extends QBMapper<Preview> | ||
| */ | ||
| class PreviewMapper extends QBMapper { | ||
|
|
||
| private const TABLE_NAME = 'previews'; | ||
|
|
||
| public function __construct(IDBConnection $db) { | ||
| parent::__construct($db, self::TABLE_NAME, Preview::class); | ||
| } | ||
|
|
||
| /** | ||
| * @param int[] $fileIds | ||
| * @return array<int, Preview[]> | ||
| * @throws Exception | ||
| */ | ||
| public function getAvailablePreviews(array $fileIds): array { | ||
| $selectQb = $this->db->getQueryBuilder(); | ||
| $selectQb->select('*') | ||
| ->from(self::TABLE_NAME) | ||
| ->where( | ||
| $selectQb->expr()->in('file_id', $selectQb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)), | ||
| ); | ||
| $previews = array_fill_keys($fileIds, []); | ||
| foreach ($this->yieldEntities($selectQb) as $preview) { | ||
| $previews[$preview->getFileId()][] = $preview; | ||
| } | ||
| return $previews; | ||
| } | ||
|
|
||
| public function getPreview(int $fileId, int $width, int $height, string $mode, int $mimetype = IPreview::MIMETYPE_JPEG): ?Preview { | ||
| $selectQb = $this->db->getQueryBuilder(); | ||
| $selectQb->select('*') | ||
| ->from(self::TABLE_NAME) | ||
| ->where( | ||
| $selectQb->expr()->eq('file_id', $selectQb->createNamedParameter($fileId)), | ||
| $selectQb->expr()->eq('width', $selectQb->createNamedParameter($width)), | ||
| $selectQb->expr()->eq('height', $selectQb->createNamedParameter($height)), | ||
| $selectQb->expr()->eq('mode', $selectQb->createNamedParameter($mode)), | ||
| $selectQb->expr()->eq('mimetype', $selectQb->createNamedParameter($mimetype)), | ||
| ); | ||
| try { | ||
| return $this->findEntity($selectQb); | ||
| } catch (DoesNotExistException) { | ||
| return null; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.