2016-09-02 09:41:59 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2026-03-10 06:36:46 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-09-02 09:41:59 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-09-02 09:41:59 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Files\Cache;
|
|
|
|
|
|
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the mapping between the string and numeric storage ids
|
|
|
|
|
*
|
|
|
|
|
* Each storage has 2 different ids
|
|
|
|
|
* a string id which is generated by the storage backend and reflects the configuration of the storage (e.g. 'smb://user@host/share')
|
|
|
|
|
* and a numeric storage id which is referenced in the file cache
|
|
|
|
|
*
|
2022-07-27 08:51:42 -04:00
|
|
|
* A mapping between the two storage ids is stored in the database and accessible through this class
|
2016-09-02 09:41:59 -04:00
|
|
|
*
|
|
|
|
|
* @package OC\Files\Cache
|
|
|
|
|
*/
|
|
|
|
|
class StorageGlobal {
|
2026-03-10 06:36:46 -04:00
|
|
|
/** @var array<string, array{id: string, numeric_id: int, available: bool, last_checked: int}> */
|
|
|
|
|
private array $cache = [];
|
|
|
|
|
|
|
|
|
|
/** @var array<int, array{id: string, numeric_id: int, available: bool, last_checked: int}> */
|
|
|
|
|
private array $numericIdCache = [];
|
2016-09-02 09:41:59 -04:00
|
|
|
|
2024-10-16 04:41:21 -04:00
|
|
|
public function __construct(
|
2026-03-10 06:36:46 -04:00
|
|
|
private readonly IDBConnection $connection,
|
2024-10-16 04:41:21 -04:00
|
|
|
) {
|
2016-09-02 09:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string[] $storageIds
|
|
|
|
|
*/
|
2026-03-10 06:36:46 -04:00
|
|
|
public function loadForStorageIds(array $storageIds): void {
|
2016-09-02 09:41:59 -04:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
|
|
|
|
|
->from('storages')
|
|
|
|
|
->where($builder->expr()->in('id', $builder->createNamedParameter(array_values($storageIds), IQueryBuilder::PARAM_STR_ARRAY)));
|
|
|
|
|
|
2024-10-16 04:41:21 -04:00
|
|
|
$result = $query->executeQuery();
|
2026-03-10 06:36:46 -04:00
|
|
|
while (($row = $result->fetch()) !== false) {
|
|
|
|
|
$normalizedRow = [
|
|
|
|
|
'id' => (string)$row['id'],
|
|
|
|
|
'numeric_id' => (int)$row['numeric_id'],
|
|
|
|
|
'available' => (bool)$row['available'],
|
|
|
|
|
'last_checked' => (int)$row['last_checked'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->cache[$normalizedRow['id']] = $normalizedRow;
|
2016-09-02 09:41:59 -04:00
|
|
|
}
|
2026-03-10 06:36:46 -04:00
|
|
|
|
2020-11-05 04:50:53 -05:00
|
|
|
$result->closeCursor();
|
2016-09-02 09:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-10 06:36:46 -04:00
|
|
|
* @return array{id: string, numeric_id: int, available: bool, last_checked: int}|null
|
2016-09-02 09:41:59 -04:00
|
|
|
*/
|
2022-03-31 08:10:20 -04:00
|
|
|
public function getStorageInfo(string $storageId): ?array {
|
2016-09-02 09:41:59 -04:00
|
|
|
if (!isset($this->cache[$storageId])) {
|
2019-09-24 13:22:36 -04:00
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
|
|
|
|
|
->from('storages')
|
|
|
|
|
->where($builder->expr()->eq('id', $builder->createNamedParameter($storageId)));
|
|
|
|
|
|
2024-10-16 04:41:21 -04:00
|
|
|
$result = $query->executeQuery();
|
2020-11-05 04:50:53 -05:00
|
|
|
$row = $result->fetch();
|
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
2026-03-10 06:36:46 -04:00
|
|
|
if ($row !== false) {
|
|
|
|
|
$normalizedRow = [
|
|
|
|
|
'id' => (string)$row['id'],
|
|
|
|
|
'numeric_id' => (int)$row['numeric_id'],
|
|
|
|
|
'available' => (bool)$row['available'],
|
|
|
|
|
'last_checked' => (int)$row['last_checked'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->cache[$storageId] = $normalizedRow;
|
|
|
|
|
$this->numericIdCache[$normalizedRow['numeric_id']] = $normalizedRow;
|
2019-09-24 13:22:36 -04:00
|
|
|
}
|
2016-09-02 09:41:59 -04:00
|
|
|
}
|
2026-03-10 06:36:46 -04:00
|
|
|
|
2022-03-31 08:10:20 -04:00
|
|
|
return $this->cache[$storageId] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-10 06:36:46 -04:00
|
|
|
* @return array{id: string, numeric_id: int, available: bool, last_checked: int}|null
|
2022-03-31 08:10:20 -04:00
|
|
|
*/
|
|
|
|
|
public function getStorageInfoByNumericId(int $numericId): ?array {
|
|
|
|
|
if (!isset($this->numericIdCache[$numericId])) {
|
|
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
|
|
|
$query = $builder->select(['id', 'numeric_id', 'available', 'last_checked'])
|
|
|
|
|
->from('storages')
|
|
|
|
|
->where($builder->expr()->eq('numeric_id', $builder->createNamedParameter($numericId)));
|
|
|
|
|
|
2024-10-16 04:41:21 -04:00
|
|
|
$result = $query->executeQuery();
|
2022-03-31 08:10:20 -04:00
|
|
|
$row = $result->fetch();
|
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
2026-03-10 06:36:46 -04:00
|
|
|
if ($row !== false) {
|
|
|
|
|
$normalizedRow = [
|
|
|
|
|
'id' => (string)$row['id'],
|
|
|
|
|
'numeric_id' => (int)$row['numeric_id'],
|
|
|
|
|
'available' => (bool)$row['available'],
|
|
|
|
|
'last_checked' => (int)$row['last_checked'],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$this->numericIdCache[$numericId] = $normalizedRow;
|
|
|
|
|
$this->cache[$normalizedRow['id']] = $normalizedRow;
|
2022-03-31 08:10:20 -04:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-10 06:36:46 -04:00
|
|
|
|
2022-03-31 08:10:20 -04:00
|
|
|
return $this->numericIdCache[$numericId] ?? null;
|
2016-09-02 09:41:59 -04:00
|
|
|
}
|
|
|
|
|
|
2026-03-10 06:36:46 -04:00
|
|
|
public function clearCache(): void {
|
2016-09-02 09:41:59 -04:00
|
|
|
$this->cache = [];
|
|
|
|
|
}
|
|
|
|
|
}
|