Skip to content

Commit

Permalink
Merge pull request #1630 from nextcloud/fix/sharding
Browse files Browse the repository at this point in the history
fix: hint table for columns for sharded queries
  • Loading branch information
juliusknorr authored Jan 15, 2025
2 parents f84f2fe + 7118fb3 commit 32c496a
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/Mount/CollectiveFolderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\Storage\Wrapper\PermissionsMask;
use OCA\Collectives\ACL\ACLStorageWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Folder;
use OCP\Files\InvalidPathException;
Expand All @@ -36,7 +37,7 @@ class CollectiveFolderManager {
private const SUFFIX = '.md';

private ?string $rootPath = null;
private ?int $rootFolderStorageId = null;
private ?int $rootFolderId = null;

public function __construct(
private IRootFolder $rootFolder,
Expand Down Expand Up @@ -149,19 +150,23 @@ private function getJailPath(int $folderId): string {
/**
* @throws NotFoundException
*/
private function getRootFolderStorageId(): int {
if ($this->rootFolderStorageId === null) {
private function getRootFolderId(): int {
if ($this->rootFolderId === null) {
$qb = $this->connection->getQueryBuilder();

$qb->select('fileid')
->from('filecache')
->where($qb->expr()->eq('storage', $qb->createNamedParameter($this->getRootFolder()->getStorage()->getCache()->getNumericStorageId())))
$qb->select('f.fileid')
->from('filecache', 'f')
->where($qb->expr()->eq('storage', $qb->createNamedParameter($this->getRootFolderStorageId())))
->andWhere($qb->expr()->eq('path_hash', $qb->createNamedParameter(md5($this->getRootPath()))));

$this->rootFolderStorageId = (int)$qb->execute()->fetchColumn();
$this->rootFolderId = (int)$qb->execute()->fetchColumn();
}

return $this->rootFolderStorageId;
return $this->rootFolderId;
}

private function getRootFolderStorageId(): int {
return $this->getRootFolder()->getStorage()->getCache()->getNumericStorageId();
}

/**
Expand Down Expand Up @@ -194,14 +199,13 @@ public function getLandingPagePath(string $path, string $lang): string {
public function getFolderFileCache(int $id, string $name): array {
$qb = $this->connection->getQueryBuilder();
$qb->select(
'co.id AS folder_id', 'fileid', 'storage', 'path', 'fc.name AS name',
'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'etag', 'encrypted', 'parent', 'fc.permissions AS permissions')
'co.id AS folder_id', 'fc.fileid', 'fc.storage', 'fc.path', 'fc.name AS name',
'fc.mimetype', 'fc.mimepart', 'fc.size', 'fc.mtime', 'fc.storage_mtime', 'fc.etag', 'fc.encrypted', 'fc.parent', 'fc.permissions AS permissions')
->from('collectives', 'co')
->leftJoin('co', 'filecache', 'fc', $qb->expr()->andX(
// concat with empty string to work around missing cast to string
$qb->expr()->eq('fc.name', $qb->func()->concat('co.id', $qb->expr()->literal(''))),
$qb->expr()->eq('parent', $qb->createNamedParameter($this->getRootFolderStorageId()))))
->where($qb->expr()->eq('co.id', $qb->createNamedParameter($id)));
->leftJoin('co', 'filecache', 'fc', $qb->expr()->eq('fc.name', $qb->expr()->castColumn('co.id', IQueryBuilder::PARAM_STR)))
->where($qb->expr()->eq('co.id', $qb->createNamedParameter($id)))
->andWhere($qb->expr()->eq('fc.parent', $qb->createNamedParameter($this->getRootFolderId())))
->andWhere($qb->expr()->eq('fc.storage', $qb->createNamedParameter($this->getRootFolderStorageId(), IQueryBuilder::PARAM_INT)));
$cache = $qb->execute()->fetch();
$cache['mount_point'] = $name;
return $cache;
Expand Down

0 comments on commit 32c496a

Please sign in to comment.