Skip to content

Commit 1d9354a

Browse files
committed
feat: add interface for lower level filecache acess without having to do direct db queries
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 46906b7 commit 1d9354a

4 files changed

Lines changed: 219 additions & 0 deletions

File tree

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@
319319
'OCP\\Files\\Cache\\ICache' => $baseDir . '/lib/public/Files/Cache/ICache.php',
320320
'OCP\\Files\\Cache\\ICacheEntry' => $baseDir . '/lib/public/Files/Cache/ICacheEntry.php',
321321
'OCP\\Files\\Cache\\ICacheEvent' => $baseDir . '/lib/public/Files/Cache/ICacheEvent.php',
322+
'OCP\\Files\\Cache\\IFileAccess' => $baseDir . '/lib/public/Files/Cache/IFileAccess.php',
322323
'OCP\\Files\\Cache\\IPropagator' => $baseDir . '/lib/public/Files/Cache/IPropagator.php',
323324
'OCP\\Files\\Cache\\IScanner' => $baseDir . '/lib/public/Files/Cache/IScanner.php',
324325
'OCP\\Files\\Cache\\IUpdater' => $baseDir . '/lib/public/Files/Cache/IUpdater.php',
@@ -1364,6 +1365,7 @@
13641365
'OC\\Files\\Cache\\CacheEntry' => $baseDir . '/lib/private/Files/Cache/CacheEntry.php',
13651366
'OC\\Files\\Cache\\CacheQueryBuilder' => $baseDir . '/lib/private/Files/Cache/CacheQueryBuilder.php',
13661367
'OC\\Files\\Cache\\FailedCache' => $baseDir . '/lib/private/Files/Cache/FailedCache.php',
1368+
'OC\\Files\\Cache\\FileAccess' => $baseDir . '/lib/private/Files/Cache/FileAccess.php',
13671369
'OC\\Files\\Cache\\HomeCache' => $baseDir . '/lib/private/Files/Cache/HomeCache.php',
13681370
'OC\\Files\\Cache\\HomePropagator' => $baseDir . '/lib/private/Files/Cache/HomePropagator.php',
13691371
'OC\\Files\\Cache\\LocalRootScanner' => $baseDir . '/lib/private/Files/Cache/LocalRootScanner.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
352352
'OCP\\Files\\Cache\\ICache' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICache.php',
353353
'OCP\\Files\\Cache\\ICacheEntry' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICacheEntry.php',
354354
'OCP\\Files\\Cache\\ICacheEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICacheEvent.php',
355+
'OCP\\Files\\Cache\\IFileAccess' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IFileAccess.php',
355356
'OCP\\Files\\Cache\\IPropagator' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IPropagator.php',
356357
'OCP\\Files\\Cache\\IScanner' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IScanner.php',
357358
'OCP\\Files\\Cache\\IUpdater' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IUpdater.php',
@@ -1397,6 +1398,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
13971398
'OC\\Files\\Cache\\CacheEntry' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/CacheEntry.php',
13981399
'OC\\Files\\Cache\\CacheQueryBuilder' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/CacheQueryBuilder.php',
13991400
'OC\\Files\\Cache\\FailedCache' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/FailedCache.php',
1401+
'OC\\Files\\Cache\\FileAccess' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/FileAccess.php',
14001402
'OC\\Files\\Cache\\HomeCache' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/HomeCache.php',
14011403
'OC\\Files\\Cache\\HomePropagator' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/HomePropagator.php',
14021404
'OC\\Files\\Cache\\LocalRootScanner' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/LocalRootScanner.php',
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2024 Robin Appelman <robin@icewind.nl>
7+
*
8+
* @author Robin Appelman <robin@icewind.nl>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
namespace OC\Files\Cache;
27+
28+
use OC\FilesMetadata\FilesMetadataManager;
29+
use OC\SystemConfig;
30+
use OCP\DB\QueryBuilder\IQueryBuilder;
31+
use OCP\Files\Cache\IFileAccess;
32+
use OCP\Files\IMimeTypeLoader;
33+
use OCP\IDBConnection;
34+
use Psr\Log\LoggerInterface;
35+
36+
/**
37+
* Low level access to the file cache
38+
*/
39+
class FileAccess implements IFileAccess {
40+
public function __construct(
41+
private IDBConnection $connection,
42+
private SystemConfig $systemConfig,
43+
private LoggerInterface $logger,
44+
private FilesMetadataManager $metadataManager,
45+
private IMimeTypeLoader $mimeTypeLoader,
46+
) {
47+
}
48+
49+
private function getQuery(): CacheQueryBuilder {
50+
return new CacheQueryBuilder(
51+
$this->connection,
52+
$this->systemConfig,
53+
$this->logger,
54+
$this->metadataManager,
55+
);
56+
}
57+
58+
public function getByFileIdInStorage(int $fileId, int $storageId): ?CacheEntry {
59+
$items = $this->getByFileIdsInStorage([$fileId], $storageId);
60+
return $items[0] ?? null;
61+
}
62+
63+
public function getByPathInStorage(string $path, int $storageId): ?CacheEntry {
64+
$query = $this->getQuery()->selectFileCache();
65+
$query->andWhere($query->expr()->eq('filecache.path_hash', $query->createNamedParameter(md5($path))));
66+
$query->andWhere($query->expr()->eq('filecache.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
67+
68+
$row = $query->executeQuery()->fetch();
69+
return $row ? Cache::cacheEntryFromData($row, $this->mimeTypeLoader) : null;
70+
}
71+
72+
public function getByFileId(int $fileId): ?CacheEntry {
73+
$items = $this->getByFileIds([$fileId]);
74+
return $items[0] ?? null;
75+
}
76+
77+
/**
78+
* @param array[] $rows
79+
* @return array<int, CacheEntry>
80+
*/
81+
private function rowsToEntries(array $rows): array {
82+
$result = [];
83+
foreach ($rows as $row) {
84+
$entry = Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
85+
$result[$entry->getId()] = $entry;
86+
}
87+
return $result;
88+
}
89+
90+
/**
91+
* @param int[] $fileIds
92+
* @return array<int, CacheEntry>
93+
*/
94+
public function getByFileIds(array $fileIds): array {
95+
$query = $this->getQuery()->selectFileCache();
96+
$query->andWhere($query->expr()->in('filecache.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
97+
98+
$rows = $query->executeQuery()->fetchAll();
99+
return $this->rowsToEntries($rows);
100+
}
101+
102+
/**
103+
* @param int[] $fileIds
104+
* @param int $storageId
105+
* @return array<int, CacheEntry>
106+
*/
107+
public function getByFileIdsInStorage(array $fileIds, int $storageId): array {
108+
$fileIds = array_values($fileIds);
109+
$query = $this->getQuery()->selectFileCache();
110+
$query->andWhere($query->expr()->in('filecache.fileid', $query->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY)));
111+
$query->andWhere($query->expr()->eq('filecache.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
112+
113+
$rows = $query->executeQuery()->fetchAll();
114+
return $this->rowsToEntries($rows);
115+
}
116+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2024 Robin Appelman <robin@icewind.nl>
7+
*
8+
* @author Robin Appelman <robin@icewind.nl>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
namespace OCP\Files\Cache;
27+
28+
/**
29+
* Low level access to the file cache.
30+
*
31+
* This is intended for use cases where data from the filecache needs to be loaded, but the full filesystem apis are
32+
* insufficient or too inefficient for the use-case.
33+
*
34+
* @since 29.0.0
35+
*/
36+
interface IFileAccess {
37+
/**
38+
* Get a filecache data by file id from a specific storage.
39+
*
40+
* This is preferred over `getByFileId` when the storage id is known as it
41+
* can be more efficient in some setups.
42+
*
43+
* @param int $fileId
44+
* @param int $storageId
45+
* @return ICacheEntry|null
46+
*
47+
* @since 29.0.0
48+
*/
49+
public function getByFileIdInStorage(int $fileId, int $storageId): ?ICacheEntry;
50+
51+
/**
52+
* Get a filecache data by path and storage id.
53+
*
54+
* @param string $path
55+
* @param int $storageId
56+
* @return ICacheEntry|null
57+
*
58+
* @since 29.0.0
59+
*/
60+
public function getByPathInStorage(string $path, int $storageId): ?ICacheEntry;
61+
62+
/**
63+
* Get a filecache data by file id.
64+
*
65+
* If the storage id is known then `getByFileIdInStorage` is preferred as it can be more efficient in some setups.
66+
*
67+
* @param int $fileId
68+
* @return ICacheEntry|null
69+
*
70+
* @since 29.0.0
71+
*/
72+
public function getByFileId(int $fileId): ?ICacheEntry;
73+
74+
/**
75+
* Get filecache data by file ids.
76+
*
77+
* If the storage id is known then `getByFileIdsInStorage` is preferred as it can be more efficient in some setups.
78+
*
79+
* @param int[] $fileIds
80+
* @return array<int, ICacheEntry>
81+
*
82+
* @since 29.0.0
83+
*/
84+
public function getByFileIds(array $fileIds): array;
85+
86+
/**
87+
* Get filecache data by file ids from a specific storage.
88+
*
89+
* This is prefered over `getByFileIds` when the storage id is known as it
90+
* can be more efficient in some setups.
91+
*
92+
* @param int[] $fileIds
93+
* @param int $storageId
94+
* @return array<int, ICacheEntry>
95+
*
96+
* @since 29.0.0
97+
*/
98+
public function getByFileIdsInStorage(array $fileIds, int $storageId): array;
99+
}

0 commit comments

Comments
 (0)