Skip to content

Commit 3f86f4e

Browse files
ArtificialOwljoshtrichards
authored andcommitted
feat(metadata): migrate to lazy appconfig
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent f707984 commit 3f86f4e

7 files changed

Lines changed: 55 additions & 54 deletions

File tree

lib/private/Files/Cache/Cache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function get($file) {
171171
} elseif (!$data) {
172172
return $data;
173173
} else {
174-
$data['metadata'] = $metadataQuery?->extractMetadata($data)->asArray() ?? [];
174+
$data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
175175
return self::cacheEntryFromData($data, $this->mimetypeLoader);
176176
}
177177
}
@@ -243,7 +243,7 @@ public function getFolderContentsById($fileId) {
243243
$result->closeCursor();
244244

245245
return array_map(function (array $data) use ($metadataQuery) {
246-
$data['metadata'] = $metadataQuery?->extractMetadata($data)->asArray() ?? [];
246+
$data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
247247
return self::cacheEntryFromData($data, $this->mimetypeLoader);
248248
}, $files);
249249
}

lib/private/Files/Cache/CacheQueryBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ public function whereParentInParameter(string $parameter) {
138138
/**
139139
* join metadata to current query builder and returns an helper
140140
*
141-
* @return IMetadataQuery|null NULL if no metadata have never been generated
141+
* @return IMetadataQuery
142142
*/
143-
public function selectMetadata(): ?IMetadataQuery {
143+
public function selectMetadata(): IMetadataQuery {
144144
$metadataQuery = $this->filesMetadataManager->getMetadataQuery($this, $this->alias, 'fileid');
145-
$metadataQuery?->retrieveMetadata();
145+
$metadataQuery->retrieveMetadata();
146146
return $metadataQuery;
147147
}
148148
}

lib/private/Files/Cache/QuerySearchHelper.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,7 @@ public function searchInCaches(ISearchQuery $searchQuery, array $caches): array
195195
$files = $result->fetchAll();
196196

197197
$rawEntries = array_map(function (array $data) use ($metadataQuery) {
198-
// migrate to null safe ...
199-
if ($metadataQuery === null) {
200-
$data['metadata'] = [];
201-
} else {
202-
$data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
203-
}
198+
$data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
204199
return Cache::cacheEntryFromData($data, $this->mimetypeLoader);
205200
}, $files);
206201

lib/private/FilesMetadata/FilesMetadataManager.php

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151
use OCP\FilesMetadata\IMetadataQuery;
5252
use OCP\FilesMetadata\Model\IFilesMetadata;
5353
use OCP\FilesMetadata\Model\IMetadataValueWrapper;
54-
use OCP\IConfig;
55-
use OCP\IDBConnection;
54+
use OCP\IAppConfig;
5655
use Psr\Log\LoggerInterface;
5756

5857
/**
@@ -69,7 +68,7 @@ class FilesMetadataManager implements IFilesMetadataManager {
6968
public function __construct(
7069
private IEventDispatcher $eventDispatcher,
7170
private IJobList $jobList,
72-
private IConfig $config,
71+
private IAppConfig $appConfig,
7372
private LoggerInterface $logger,
7473
private MetadataRequestService $metadataRequestService,
7574
private IndexRequestService $indexRequestService,
@@ -206,7 +205,7 @@ public function saveMetadata(IFilesMetadata $filesMetadata): void {
206205
// update metadata types list
207206
$current = $this->getKnownMetadata();
208207
$current->import($filesMetadata->jsonSerialize(true));
209-
$this->config->setAppValue('core', self::CONFIG_KEY, json_encode($current));
208+
$this->appConfig->setValueArray('core', self::CONFIG_KEY, $current->jsonSerialize(), lazy: true);
210209
}
211210

212211
/**
@@ -235,20 +234,16 @@ public function deleteMetadata(int $fileId): void {
235234
* @param string $fileIdField alias of the field that contains file ids
236235
*
237236
* @inheritDoc
238-
* @return IMetadataQuery|null
237+
* @return IMetadataQuery
239238
* @see IMetadataQuery
240239
* @since 28.0.0
241240
*/
242241
public function getMetadataQuery(
243242
IQueryBuilder $qb,
244243
string $fileTableAlias,
245244
string $fileIdField
246-
): ?IMetadataQuery {
247-
if (!$this->metadataInitiated()) {
248-
return null;
249-
}
250-
251-
return new MetadataQuery($qb, $this->getKnownMetadata(), $fileTableAlias, $fileIdField);
245+
): IMetadataQuery {
246+
return new MetadataQuery($qb, $this, $fileTableAlias, $fileIdField);
252247
}
253248

254249
/**
@@ -263,8 +258,7 @@ public function getKnownMetadata(): IFilesMetadata {
263258
$this->all = new FilesMetadata();
264259

265260
try {
266-
$data = json_decode($this->config->getAppValue('core', self::CONFIG_KEY, '[]'), true, 127, JSON_THROW_ON_ERROR);
267-
$this->all->import($data);
261+
$this->all->import($this->appConfig->getValueArray('core', self::CONFIG_KEY, lazy: true));
268262
} catch (JsonException) {
269263
$this->logger->warning('issue while reading stored list of metadata. Advised to run ./occ files:scan --all --generate-metadata');
270264
}
@@ -310,7 +304,7 @@ public function initMetadata(
310304
}
311305

312306
$current->import([$key => ['type' => $type, 'indexed' => $indexed, 'editPermission' => $editPermission]]);
313-
$this->config->setAppValue('core', self::CONFIG_KEY, json_encode($current));
307+
$this->appConfig->setValueArray('core', self::CONFIG_KEY, $current->jsonSerialize(), lazy: true);
314308
$this->all = $current;
315309
}
316310

@@ -323,26 +317,4 @@ public static function loadListeners(IEventDispatcher $eventDispatcher): void {
323317
$eventDispatcher->addServiceListener(NodeWrittenEvent::class, MetadataUpdate::class);
324318
$eventDispatcher->addServiceListener(CacheEntryRemovedEvent::class, MetadataDelete::class);
325319
}
326-
327-
/**
328-
* Will confirm that tables were created and store an app value to cache the result.
329-
* Can be removed in 29 as this is to avoid strange situation when Nextcloud files were
330-
* replaced but the upgrade was not triggered yet.
331-
*
332-
* @return bool
333-
*/
334-
private function metadataInitiated(): bool {
335-
if ($this->config->getAppValue('core', self::MIGRATION_DONE, '0') === '1') {
336-
return true;
337-
}
338-
339-
$dbConnection = \OCP\Server::get(IDBConnection::class);
340-
if ($dbConnection->tableExists(MetadataRequestService::TABLE_METADATA)) {
341-
$this->config->setAppValue('core', self::MIGRATION_DONE, '1');
342-
343-
return true;
344-
}
345-
346-
return false;
347-
}
348320
}

lib/private/FilesMetadata/MetadataQuery.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
use OCP\DB\QueryBuilder\IQueryBuilder;
3232
use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
3333
use OCP\FilesMetadata\Exceptions\FilesMetadataTypeException;
34+
use OCP\FilesMetadata\IFilesMetadataManager;
3435
use OCP\FilesMetadata\IMetadataQuery;
3536
use OCP\FilesMetadata\Model\IFilesMetadata;
3637
use OCP\FilesMetadata\Model\IMetadataValueWrapper;
38+
use Psr\Log\LoggerInterface;
3739

3840
/**
3941
* @inheritDoc
@@ -43,12 +45,23 @@ class MetadataQuery implements IMetadataQuery {
4345
private array $knownJoinedIndex = [];
4446
public function __construct(
4547
private IQueryBuilder $queryBuilder,
46-
private IFilesMetadata $knownMetadata,
48+
private IFilesMetadata|IFilesMetadataManager $manager,
4749
private string $fileTableAlias = 'fc',
4850
private string $fileIdField = 'fileid',
4951
private string $alias = 'meta',
5052
private string $aliasIndexPrefix = 'meta_index'
5153
) {
54+
if ($manager instanceof IFilesMetadata) {
55+
/**
56+
* Since 29, because knownMetadata is stored in lazy appconfig, it seems smarter
57+
* to not call getKnownMetadata() at the load of this class as it is only needed
58+
* in {@see getMetadataValueField}.
59+
*
60+
* FIXME: remove support for IFilesMetadata
61+
*/
62+
$logger = \OCP\Server::get(LoggerInterface::class);
63+
$logger->debug('It is deprecated to use IFilesMetadata as second parameter when calling MetadataQuery::__construct()');
64+
}
5265
}
5366

5467
/**
@@ -158,7 +171,20 @@ public function getMetadataKeyField(string $metadataKey): string {
158171
* @since 28.0.0
159172
*/
160173
public function getMetadataValueField(string $metadataKey): string {
161-
return match ($this->knownMetadata->getType($metadataKey)) {
174+
if ($this->manager instanceof IFilesMetadataManager) {
175+
/**
176+
* Since 29, because knownMetadata is stored in lazy appconfig, it seems smarter
177+
* to not call getKnownMetadata() at the load of this class as it is only needed
178+
* in this method.
179+
*
180+
* FIXME: keep only this line and remove support for previous IFilesMetadata in constructor
181+
*/
182+
$knownMetadata = $this->manager->getKnownMetadata();
183+
} else {
184+
$knownMetadata = $this->manager;
185+
}
186+
187+
return match ($knownMetadata->getType($metadataKey)) {
162188
IMetadataValueWrapper::TYPE_STRING => $this->joinedTableAlias($metadataKey) . '.meta_value_string',
163189
IMetadataValueWrapper::TYPE_INT, IMetadataValueWrapper::TYPE_BOOL => $this->joinedTableAlias($metadataKey) . '.meta_value_int',
164190
default => throw new FilesMetadataTypeException('metadata is not set as indexed'),

lib/public/FilesMetadata/IFilesMetadataManager.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,35 @@ public function deleteMetadata(int $fileId): void;
122122
* @param string $fileTableAlias alias of the table that contains data about files
123123
* @param string $fileIdField alias of the field that contains file ids
124124
*
125-
* @return IMetadataQuery|null NULL if table are not set yet or never used
125+
* @return IMetadataQuery
126126
* @see IMetadataQuery
127127
* @since 28.0.0
128128
*/
129129
public function getMetadataQuery(
130130
IQueryBuilder $qb,
131131
string $fileTableAlias,
132132
string $fileIdField
133-
): ?IMetadataQuery;
133+
): IMetadataQuery;
134134

135135
/**
136136
* returns all type of metadata currently available.
137137
* The list is stored in a IFilesMetadata with null values but correct type.
138138
*
139+
* Note: this method loads lazy appconfig values.
140+
*
139141
* @return IFilesMetadata
140142
* @since 28.0.0
141143
*/
142144
public function getKnownMetadata(): IFilesMetadata;
143145

144146
/**
145-
* initiate a metadata key with its type.
147+
* Initiate a metadata key with its type.
148+
*
146149
* The call is mandatory before using the metadata property in a webdav request.
147-
* It is not needed to only use this method when the app is enabled: the method can be
148-
* called each time during the app loading as the metadata will only be initiated if not known
150+
* The call should be part of a migration/repair step and not be called on app's boot
151+
* process as it is using lazy-appconfig value
152+
*
153+
* Note: this method loads lazy appconfig values.
149154
*
150155
* @param string $key metadata key
151156
* @param string $type metadata type
@@ -164,6 +169,7 @@ public function getKnownMetadata(): IFilesMetadata;
164169
* @see IMetadataValueWrapper::EDIT_REQ_WRITE_PERMISSION
165170
* @see IMetadataValueWrapper::EDIT_REQ_READ_PERMISSION
166171
* @since 28.0.0
172+
* @since 29.0.0 uses lazy config value - do not use this method out of repair steps
167173
*/
168174
public function initMetadata(string $key, string $type, bool $indexed, int $editPermission): void;
169175
}

lib/public/FilesMetadata/IMetadataQuery.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public function getMetadataKeyField(string $metadataKey): string;
8383
/**
8484
* returns the name of the field for metadata string value to be used in query expressions
8585
*
86+
* Note: this method loads lazy appconfig values.
87+
*
8688
* @param string $metadataKey metadata key
8789
*
8890
* @return string table field

0 commit comments

Comments
 (0)