Skip to content

Commit

Permalink
add class for (future) shared access to the filecache
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Feb 19, 2024
1 parent 9bddf9e commit 367ba8b
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 74 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,7 @@
'OC\\Files\\Cache\\CacheDependencies' => $baseDir . '/lib/private/Files/Cache/CacheDependencies.php',
'OC\\Files\\Cache\\CacheEntry' => $baseDir . '/lib/private/Files/Cache/CacheEntry.php',
'OC\\Files\\Cache\\CacheQueryBuilder' => $baseDir . '/lib/private/Files/Cache/CacheQueryBuilder.php',
'OC\\Files\\Cache\\Database' => $baseDir . '/lib/private/Files/Cache/Database.php',
'OC\\Files\\Cache\\FailedCache' => $baseDir . '/lib/private/Files/Cache/FailedCache.php',
'OC\\Files\\Cache\\HomeCache' => $baseDir . '/lib/private/Files/Cache/HomeCache.php',
'OC\\Files\\Cache\\HomePropagator' => $baseDir . '/lib/private/Files/Cache/HomePropagator.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Files\\Cache\\CacheDependencies' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/CacheDependencies.php',
'OC\\Files\\Cache\\CacheEntry' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/CacheEntry.php',
'OC\\Files\\Cache\\CacheQueryBuilder' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/CacheQueryBuilder.php',
'OC\\Files\\Cache\\Database' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/Database.php',
'OC\\Files\\Cache\\FailedCache' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/FailedCache.php',
'OC\\Files\\Cache\\HomeCache' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/HomeCache.php',
'OC\\Files\\Cache\\HomePropagator' => __DIR__ . '/../../..' . '/lib/private/Files/Cache/HomePropagator.php',
Expand Down
61 changes: 18 additions & 43 deletions lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
use OCP\Files\Search\ISearchQuery;
use OCP\Files\Storage\IStorage;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IDBConnection;
use OCP\Server;
use OCP\Util;
use Psr\Log\LoggerInterface;

Expand All @@ -87,7 +87,7 @@ class Cache implements ICache {
protected string $storageId;
protected Storage $storageCache;
protected IMimeTypeLoader$mimetypeLoader;
protected IDBConnection $connection;
protected Database $cacheDb;
protected SystemConfig $systemConfig;
protected LoggerInterface $logger;
protected QuerySearchHelper $querySearchHelper;
Expand All @@ -105,11 +105,11 @@ public function __construct(
$this->storageId = md5($this->storageId);
}
if (!$dependencies) {
$dependencies = \OC::$server->get(CacheDependencies::class);
$dependencies = Server::get(CacheDependencies::class);
}
$this->storageCache = new Storage($this->storage, true, $dependencies->getConnection());
$this->mimetypeLoader = $dependencies->getMimeTypeLoader();
$this->connection = $dependencies->getConnection();
$this->cacheDb = $dependencies->getCacheDb();
$this->systemConfig = $dependencies->getSystemConfig();
$this->logger = $dependencies->getLogger();
$this->querySearchHelper = $dependencies->getQuerySearchHelper();
Expand All @@ -118,12 +118,7 @@ public function __construct(
}

protected function getQueryBuilder() {
return new CacheQueryBuilder(
$this->connection,
$this->systemConfig,
$this->logger,
$this->metadataManager,
);
return $this->cacheDb->queryForStorageId($this->getNumericStorageId());
}

public function getStorageCache(): Storage {
Expand Down Expand Up @@ -304,7 +299,7 @@ public function insert($file, array $data) {
$values['storage'] = $storageId;

try {
$builder = $this->connection->getQueryBuilder();
$builder = $this->getQueryBuilder();
$builder->insert('filecache');

foreach ($values as $column => $value) {
Expand Down Expand Up @@ -332,9 +327,9 @@ public function insert($file, array $data) {
}
} catch (UniqueConstraintViolationException $e) {
// entry exists already
if ($this->connection->inTransaction()) {
$this->connection->commit();
$this->connection->beginTransaction();
if ($this->cacheDb->inTransaction($this->getNumericStorageId())) {
$this->cacheDb->commit($this->getNumericStorageId());
$this->cacheDb->beginTransaction($this->getNumericStorageId());
}
}

Expand Down Expand Up @@ -687,11 +682,11 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
throw new \Exception('Invalid target storage id: ' . $targetStorageId);
}

$this->connection->beginTransaction();
$this->cacheDb->beginTransaction($this->getNumericStorageId());
if ($sourceData['mimetype'] === 'httpd/unix-directory') {
//update all child entries
$sourceLength = mb_strlen($sourcePath);
$query = $this->connection->getQueryBuilder();
$query = $this->getQueryBuilder();

$fun = $query->func();
$newPathFunction = $fun->concat(
Expand All @@ -703,7 +698,7 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
->set('path_hash', $fun->md5($newPathFunction))
->set('path', $newPathFunction)
->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%')));
->andWhere($query->expr()->like('path', $query->createNamedParameter($query->escapeLikeParameter($sourcePath) . '/%')));

// when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
Expand All @@ -713,7 +708,7 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
try {
$query->execute();
} catch (\OC\DatabaseException $e) {
$this->connection->rollBack();
$this->cacheDb->rollBack($this->getNumericStorageId());
throw $e;
}
}
Expand All @@ -734,7 +729,7 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {

$query->execute();

$this->connection->commit();
$this->cacheDb->commit($this->getNumericStorageId());

if ($sourceCache->getNumericStorageId() !== $this->getNumericStorageId()) {
$this->eventDispatcher->dispatchTyped(new CacheEntryRemovedEvent($this->storage, $sourcePath, $sourceId, $sourceCache->getNumericStorageId()));
Expand All @@ -760,7 +755,7 @@ public function clear() {
->whereStorageId($this->getNumericStorageId());
$query->execute();

$query = $this->connection->getQueryBuilder();
$query = $this->getQueryBuilder();
$query->delete('storages')
->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
$query->execute();
Expand Down Expand Up @@ -834,8 +829,8 @@ public function searchByMime($mimetype) {
return $this->searchQuery(new SearchQuery($operator, 0, 0, [], null));
}

public function searchQuery(ISearchQuery $searchQuery) {
return current($this->querySearchHelper->searchInCaches($searchQuery, [$this]));
public function searchQuery(ISearchQuery $query) {
return current($this->querySearchHelper->searchInCaches($query, [$this]));
}

/**
Expand Down Expand Up @@ -1072,27 +1067,7 @@ public function getPathById($id) {
* @deprecated use getPathById() instead
*/
public static function getById($id) {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->select('path', 'storage')
->from('filecache')
->where($query->expr()->eq('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));

$result = $query->execute();
$row = $result->fetch();
$result->closeCursor();

if ($row) {
$numericId = $row['storage'];
$path = $row['path'];
} else {
return null;
}

if ($id = Storage::getStorageId($numericId)) {
return [$id, $path];
} else {
return null;
}
throw new \Exception("Cache::getById has been removed");
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Files/Cache/CacheDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(
private LoggerInterface $logger,
private IFilesMetadataManager $metadataManager,
private DisplayNameCache $displayNameCache,
private Database $cacheDb,
) {
}

Expand Down Expand Up @@ -54,4 +55,8 @@ public function getDisplayNameCache(): DisplayNameCache {
public function getMetadataManager(): IFilesMetadataManager {
return $this->metadataManager;
}

public function getCacheDb(): Database {
return $this->cacheDb;
}
}
12 changes: 6 additions & 6 deletions lib/private/Files/Cache/CacheQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function selectTagUsage(): self {
return $this;
}

public function selectFileCache(string $alias = null, bool $joinExtendedCache = true) {
public function selectFileCache(string $alias = null, bool $joinExtendedCache = true): self {
$name = $alias ?: 'filecache';
$this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", "$name.name", 'mimetype', 'mimepart', 'size', 'mtime',
'storage_mtime', 'encrypted', 'etag', "$name.permissions", 'checksum', 'unencrypted_size')
Expand All @@ -84,13 +84,13 @@ public function selectFileCache(string $alias = null, bool $joinExtendedCache =
return $this;
}

public function whereStorageId(int $storageId) {
public function whereStorageId(int $storageId): self {
$this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));

return $this;
}

public function whereFileId(int $fileId) {
public function whereFileId(int $fileId): self {
$alias = $this->alias;
if ($alias) {
$alias .= '.';
Expand All @@ -103,13 +103,13 @@ public function whereFileId(int $fileId) {
return $this;
}

public function wherePath(string $path) {
public function wherePath(string $path): self {
$this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));

return $this;
}

public function whereParent(int $parent) {
public function whereParent(int $parent): self {
$alias = $this->alias;
if ($alias) {
$alias .= '.';
Expand All @@ -122,7 +122,7 @@ public function whereParent(int $parent) {
return $this;
}

public function whereParentInParameter(string $parameter) {
public function whereParentInParameter(string $parameter): self {
$alias = $this->alias;
if ($alias) {
$alias .= '.';
Expand Down
Loading

0 comments on commit 367ba8b

Please sign in to comment.