Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Oct 27, 2023
1 parent 683ef89 commit ecd4bd1
Show file tree
Hide file tree
Showing 23 changed files with 175 additions and 158 deletions.
5 changes: 5 additions & 0 deletions apps/files_trashbin/lib/Trash/TrashItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OCA\Files_Trashbin\Trash;

use OCP\Files\FileInfo;
use OCP\FilesMetadata\Model\IFilesMetadata;
use OCP\IUser;

class TrashItem implements ITrashItem {
Expand Down Expand Up @@ -191,6 +192,10 @@ public function getParentId(): int {
return $this->fileInfo->getParentId();
}

/**
* @inheritDoc
* @return IFilesMetadata[]
*/
public function getMetadata(): array {
return $this->fileInfo->getMetadata();
}
Expand Down
66 changes: 40 additions & 26 deletions core/Command/FilesMetadata/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@

namespace OC\Core\Command\FilesMetadata;

use OC\User\NoUserException;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
use OCP\FilesMetadata\IFilesMetadataManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -43,52 +46,63 @@ public function __construct(
parent::__construct();
}

protected function configure() {
protected function configure(): void {
$this->setName('metadata:get')
->setDescription('get stored metadata about a file, by its id')
->addArgument(
'fileId',
InputArgument::REQUIRED,
'id of the file document'
'fileId',
InputArgument::REQUIRED,
'id of the file document'
)
->addArgument(
'userId',
InputArgument::OPTIONAL,
'file owner'
'userId',
InputArgument::OPTIONAL,
'file owner'
)
->addOption(
'as-array',
'',
InputOption::VALUE_NONE,
'display metadata as a simple key=>value array'
'as-array',
'',
InputOption::VALUE_NONE,
'display metadata as a simple key=>value array'
)
->addOption(
'refresh',
'',
InputOption::VALUE_NONE,
'refresh metadata'
)
'refresh',
'',
InputOption::VALUE_NONE,
'refresh metadata'
)
->addOption(
'reset',
'',
InputOption::VALUE_NONE,
'refresh metadata from scratch'
'reset',
'',
InputOption::VALUE_NONE,
'refresh metadata from scratch'
);
}

/**
* @throws NotPermittedException
* @throws FilesMetadataNotFoundException
* @throws NoUserException
* @throws NotFoundException
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$fileId = (int)$input->getArgument('fileId');

if ($input->getOption('reset')) {
$this->filesMetadataManager->deleteMetadata($fileId);
if (!$input->getOption('refresh')) {
return 0;
}
}

if ($input->getOption('refresh')) {
$node = $this->rootFolder->getUserFolder($input->getArgument('userId'))->getById($fileId);
$file = $node[0];
if (null === $file) {
if (count($node) === 0) {
throw new NotFoundException();
}

$metadata = $this->filesMetadataManager->refreshMetadata(
$file,
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND,
$input->getOption('reset')
$node[0],
IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
);
} else {
$metadata = $this->filesMetadataManager->getMetadata($fileId);
Expand Down
17 changes: 10 additions & 7 deletions core/Migrations/Version28000Date20231004103301.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Maxence Lange <maxence@artificial-owl.com>
* @copyright 2023 Maxence Lange <maxence@artificial-owl.com>
*
* @author Maxence Lange <maxence@artificial-owl.com>
*
Expand Down Expand Up @@ -33,10 +32,10 @@
use OCP\Migration\SimpleMigrationStep;

class Version28000Date20231004103301 extends SimpleMigrationStep {

public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$updated = false;

if (!$schema->hasTable('files_metadata')) {
$table = $schema->createTable('files_metadata');
Expand All @@ -53,6 +52,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt

$table->setPrimaryKey(['id']);
$table->addUniqueIndex(['file_id'], 'files_meta_fileid');
$updated = true;
}

if (!$schema->hasTable('files_metadata_index')) {
Expand All @@ -65,14 +65,17 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
]);
$table->addColumn('file_id', Types::BIGINT, ['notnull' => false, 'length' => 15]);
$table->addColumn('meta_key', Types::STRING, ['notnull' => false, 'length' => 31]);
$table->addColumn('meta_value', Types::STRING, ['notnull' => false, 'length' => 63]);
$table->addColumn('meta_value_string', Types::STRING, ['notnull' => false, 'length' => 63]);
$table->addColumn('meta_value_int', Types::BIGINT, ['notnull' => false, 'length' => 11]);
// $table->addColumn('meta_value_float', Types::FLOAT, ['notnull' => false, 'length' => 11,5]);

$table->setPrimaryKey(['id']);
$table->addIndex(['file_id', 'meta_key', 'meta_value'], 'f_meta_index');
$table->addIndex(['file_id', 'meta_key', 'meta_value_string'], 'f_meta_index');
$table->addIndex(['file_id', 'meta_key', 'meta_value_int'], 'f_meta_index_i');
// $table->addIndex(['file_id', 'meta_key', 'meta_value_float'], 'f_meta_index_f');
$updated = true;
}

if (!$updated) {
return null;
}

return $schema;
Expand Down
3 changes: 0 additions & 3 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ public function get($file) {
$data = $result->fetch();
$result->closeCursor();

// @Louis: use asArray()
// $data['metadata'] = $metadataQuery?->extractMetadata($data)?->asArray() ?? [];

//merge partial data
if (!$data && is_string($file) && isset($this->partial[$file])) {
return $this->partial[$file];
Expand Down
6 changes: 5 additions & 1 deletion lib/private/Files/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
*/
namespace OC\Files;

use OC\FilesMetadata\Model\FilesMetadata;
use OCA\Files_Sharing\ISharedStorage;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\IHomeStorage;
use OCP\Files\Mount\IMountPoint;
use OCP\FilesMetadata\Model\IFilesMetadata;
use OCP\IUser;

class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
Expand Down Expand Up @@ -418,6 +418,10 @@ public function getParentId(): int {
return $this->data['parent'] ?? -1;
}

/**
* @inheritDoc
* @return IFilesMetadata[]
*/
public function getMetadata(): array {
return $this->data['metadata'] ?? [];
}
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Files/Node/LazyFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotPermittedException;
use OCP\FilesMetadata\Model\IFilesMetadata;

/**
* Class LazyFolder
Expand Down Expand Up @@ -575,6 +576,10 @@ public function getParentId(): int {
return $this->__call(__FUNCTION__, func_get_args());
}

/**
* @inheritDoc
* @return IFilesMetadata[]
*/
public function getMetadata(): array {
return $this->data['metadata'] ?? $this->__call(__FUNCTION__, func_get_args());
}
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Files/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\FilesMetadata\Model\IFilesMetadata;
use OCP\Lock\LockedException;
use OCP\PreConditionNotMetException;

Expand Down Expand Up @@ -491,6 +492,10 @@ public function getParentId(): int {
return $this->fileInfo->getParentId();
}

/**
* @inheritDoc
* @return IFilesMetadata[]
*/
public function getMetadata(): array {
return $this->fileInfo->getMetadata();
}
Expand Down
17 changes: 7 additions & 10 deletions lib/private/FilesMetadata/FilesMetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public function __construct(
*
* @param Node $node related node
* @param int $process type of process
* @param bool $fromScratch reset known metadata first
*
* @return IFilesMetadata
* @throws FilesMetadataException if metadata are invalid
Expand All @@ -85,13 +84,8 @@ public function __construct(
*/
public function refreshMetadata(
Node $node,
int $process = self::PROCESS_LIVE,
bool $fromScratch = false,
int $process = self::PROCESS_LIVE
): IFilesMetadata {
if ($fromScratch) {
$this->deleteMetadata($node->getId());
}

try {
$metadata = $this->metadataRequestService->getMetadataFromFileId($node->getId());
} catch (FilesMetadataNotFoundException) {
Expand Down Expand Up @@ -134,7 +128,7 @@ public function getMetadata(int $fileId): IFilesMetadata {
}

/**
* @param IFilesMetadata $filesMetadata
* @param IFilesMetadata $filesMetadata metadata
*
* @inheritDoc
* @throws FilesMetadataException if metadata seems malformed
Expand Down Expand Up @@ -169,7 +163,11 @@ public function saveMetadata(IFilesMetadata $filesMetadata): void {
}

foreach ($filesMetadata->getIndexes() as $index) {
$this->indexRequestService->updateIndex($filesMetadata, $index);
try {
$this->indexRequestService->updateIndex($filesMetadata, $index);
} catch (DBException $e) {
$this->logger->warning('issue while updateIndex', ['exception' => $e]);
}
}
}

Expand All @@ -193,7 +191,6 @@ public function deleteMetadata(int $fileId): void {
}
}


/**
* @param IQueryBuilder $qb
* @param string $fileTableAlias alias of the table that contains data about files
Expand Down
2 changes: 2 additions & 0 deletions lib/private/FilesMetadata/Listener/MetadataDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

/**
* Handle file deletion event and remove stored metadata related to the deleted file
*
* @template-implements IEventListener<NodeDeletedEvent>
*/
class MetadataDelete implements IEventListener {
public function __construct(
Expand Down
2 changes: 2 additions & 0 deletions lib/private/FilesMetadata/Listener/MetadataUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* Handle file creation/modification events and initiate a new event related to the created/edited file.
* The generated new event is broadcast in order to obtain file related metadata from other apps.
* metadata will be stored in database.
*
* @template-implements IEventListener<NodeCreatedEvent|NodeWrittenEvent>
*/
class MetadataUpdate implements IEventListener {
public function __construct(
Expand Down
1 change: 0 additions & 1 deletion lib/private/FilesMetadata/Model/FilesMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
* @since 28.0.0
*/
class FilesMetadata implements IFilesMetadata {

/** @var array<string, MetadataValueWrapper> */
private array $metadata = [];
private bool $updated = false;
Expand Down
25 changes: 5 additions & 20 deletions lib/private/FilesMetadata/Model/MetadataQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ public function retrieveMetadata(): void {
*/
public function extractMetadata(array $row): IFilesMetadata {
$fileId = (array_key_exists($this->fileIdField, $row)) ? $row[$this->fileIdField] : 0;
$metadata = new FilesMetadata($fileId);
$metadata = new FilesMetadata((int)$fileId);
try {
$metadata->importFromDatabase($row, $this->alias . '_');
} catch (FilesMetadataNotFoundException $e) {
} catch (FilesMetadataNotFoundException) {
// can be ignored as files' metadata are optional and might not exist in database
}

return $metadata;
Expand Down Expand Up @@ -107,22 +108,6 @@ public function joinIndex(string $metadataKey = '', bool $enforce = false): void
}
}

/**
* @param string $metadataKey metadata key
*
* @inheritDoc
* @since 28.0.0
*/
public function enforceMetadataKey(string $metadataKey): void {
$expr = $this->queryBuilder->expr();
$this->queryBuilder->andWhere(
$expr->eq(
$this->getMetadataKeyField(),
$this->queryBuilder->createNamedParameter($metadataKey)
)
);
}

/**
* @param string $value metadata value
* @inheritDoc
Expand All @@ -132,7 +117,7 @@ public function enforceMetadataValue(string $value): void {
$expr = $this->queryBuilder->expr();
$this->queryBuilder->andWhere(
$expr->eq(
$this->getMetadataKeyField(),
$this->getMetadataValueField(),
$this->queryBuilder->createNamedParameter($value)
)
);
Expand Down Expand Up @@ -168,7 +153,7 @@ public function getMetadataKeyField(): string {
* @since 28.0.0
*/
public function getMetadataValueField(): string {
return $this->aliasIndex . '.meta_value';
return $this->aliasIndex . '.meta_value_string';
}

/**
Expand Down
Loading

0 comments on commit ecd4bd1

Please sign in to comment.