Skip to content

Commit

Permalink
Merge pull request #3487 from nextcloud/backport/3483/stable30
Browse files Browse the repository at this point in the history
[stable30] fix: hint table for columns where needed for sharded queries
  • Loading branch information
icewind1991 authored Dec 13, 2024
2 parents 208c87c + c34dc32 commit a450f8d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
41 changes: 21 additions & 20 deletions lib/ACL/RuleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public function getRulesForFilesByPath(IUser $user, int $storageId, array $fileP
$rows = [];
foreach (array_chunk($hashes, 1000) as $chunk) {
$query = $this->connection->getQueryBuilder();
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'f.path'])
->from('group_folders_acl', 'a')
->innerJoin('a', 'filecache', 'f', $query->expr()->eq('f.fileid', 'a.fileid'))
->where($query->expr()->in('path_hash', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY)))
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->where($query->expr()->in('f.path_hash', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY)))
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->orX(...array_map(function (IUserMapping $userMapping) use ($query) {
return $query->expr()->andX(
$query->expr()->eq('mapping_type', $query->createNamedParameter($userMapping->getType())),
Expand Down Expand Up @@ -129,20 +129,21 @@ public function getRulesForFilesByParent(IUser $user, int $storageId, string $pa
}

$query = $this->connection->getQueryBuilder();
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
$query->select(['f.fileid', 'a.mapping_type', 'a.mapping_id', 'a.mask', 'a.permissions', 'f.path'])
->from('filecache', 'f')
->leftJoin('f', 'group_folders_acl', 'a', $query->expr()->eq('f.fileid', 'a.fileid'))
->andWhere($query->expr()->eq('parent', $query->createNamedParameter($parentId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('f.parent', $query->createNamedParameter($parentId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->andWhere(
$query->expr()->orX(
$query->expr()->andX(
$query->expr()->isNull('mapping_type'),
$query->expr()->isNull('mapping_id')
$query->expr()->isNull('a.mapping_type'),
$query->expr()->isNull('a.mapping_id')
),
...array_map(function (IUserMapping $userMapping) use ($query) {
return $query->expr()->andX(
$query->expr()->eq('mapping_type', $query->createNamedParameter($userMapping->getType())),
$query->expr()->eq('mapping_id', $query->createNamedParameter($userMapping->getId()))
$query->expr()->eq('a.mapping_type', $query->createNamedParameter($userMapping->getType())),
$query->expr()->eq('a.mapping_id', $query->createNamedParameter($userMapping->getId()))
);
}, $userMappings)
)
Expand Down Expand Up @@ -185,11 +186,11 @@ public function getAllRulesForPaths(int $storageId, array $filePaths): array {
return md5(trim($path, '/'));
}, $filePaths);
$query = $this->connection->getQueryBuilder();
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'f.path'])
->from('group_folders_acl', 'a')
->innerJoin('a', 'filecache', 'f', $query->expr()->eq('f.fileid', 'a.fileid'))
->where($query->expr()->in('path_hash', $query->createNamedParameter($hashes, IQueryBuilder::PARAM_STR_ARRAY)))
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
->where($query->expr()->in('f.path_hash', $query->createNamedParameter($hashes, IQueryBuilder::PARAM_STR_ARRAY)))
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));

$rows = $query->executeQuery()->fetchAll();

Expand Down Expand Up @@ -219,14 +220,14 @@ private function rulesByPath(array $rows, array $result = []): array {
*/
public function getAllRulesForPrefix(int $storageId, string $prefix): array {
$query = $this->connection->getQueryBuilder();
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'f.path'])
->from('group_folders_acl', 'a')
->innerJoin('a', 'filecache', 'f', $query->expr()->eq('f.fileid', 'a.fileid'))
->where($query->expr()->orX(
$query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($prefix) . '/%')),
$query->expr()->eq('path_hash', $query->createNamedParameter(md5($prefix)))
$query->expr()->like('f.path', $query->createNamedParameter($this->connection->escapeLikeParameter($prefix) . '/%')),
$query->expr()->eq('f.path_hash', $query->createNamedParameter(md5($prefix)))
))
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));

$rows = $query->executeQuery()->fetchAll();

Expand All @@ -243,14 +244,14 @@ public function getRulesForPrefix(IUser $user, int $storageId, string $prefix):
$userMappings = $this->userMappingManager->getMappingsForUser($user);

$query = $this->connection->getQueryBuilder();
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'f.path'])
->from('group_folders_acl', 'a')
->innerJoin('a', 'filecache', 'f', $query->expr()->eq('f.fileid', 'a.fileid'))
->where($query->expr()->orX(
$query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($prefix) . '/%')),
$query->expr()->eq('path_hash', $query->createNamedParameter(md5($prefix)))
$query->expr()->like('f.path', $query->createNamedParameter($this->connection->escapeLikeParameter($prefix) . '/%')),
$query->expr()->eq('f.path_hash', $query->createNamedParameter(md5($prefix)))
))
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->orX(...array_map(function (IUserMapping $userMapping) use ($query) {
return $query->expr()->andX(
$query->expr()->eq('mapping_type', $query->createNamedParameter($userMapping->getType())),
Expand Down
59 changes: 30 additions & 29 deletions lib/Folder/FolderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ private function getGroupFolderRootId(int $rootStorageId): int {
private function joinQueryWithFileCache(IQueryBuilder $query, int $rootStorageId): void {
$query->leftJoin('f', 'filecache', 'c', $query->expr()->andX(
// concat with empty string to work around missing cast to string
$query->expr()->eq('c.name', $query->func()->concat('f.folder_id', $query->expr()->literal(""))),
$query->expr()->eq('c.parent', $query->createNamedParameter($this->getGroupFolderRootId($rootStorageId)))
$query->expr()->eq('c.name', $query->func()->concat('f.folder_id', $query->expr()->literal(''))),
$query->expr()->eq('c.parent', $query->createNamedParameter($this->getGroupFolderRootId($rootStorageId))),
$query->expr()->eq('c.storage', $query->createNamedParameter($rootStorageId)),
));
}

Expand All @@ -137,7 +138,7 @@ public function getAllFoldersWithSize(int $rootStorageId): array {

$query = $this->connection->getQueryBuilder();

$query->select('folder_id', 'mount_point', 'quota', 'size', 'acl')
$query->select('folder_id', 'mount_point', 'quota', 'c.size', 'acl')
->from('group_folders', 'f');
$this->joinQueryWithFileCache($query, $rootStorageId);

Expand Down Expand Up @@ -173,7 +174,7 @@ public function getAllFoldersForUserWithSize(int $rootStorageId, IUser $user): a

$query = $this->connection->getQueryBuilder();

$query->select('f.folder_id', 'mount_point', 'quota', 'size', 'acl')
$query->select('f.folder_id', 'mount_point', 'quota', 'c.size', 'acl')
->from('group_folders', 'f')
->innerJoin(
'f',
Expand Down Expand Up @@ -283,7 +284,7 @@ public function getFolder(int $id, int $rootStorageId = 0) {

$query = $this->connection->getQueryBuilder();

$query->select('folder_id', 'mount_point', 'quota', 'size', 'acl')
$query->select('folder_id', 'mount_point', 'quota', 'c.size', 'acl')
->from('group_folders', 'f')
->where($query->expr()->eq('folder_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$this->joinQueryWithFileCache($query, $rootStorageId);
Expand Down Expand Up @@ -496,18 +497,18 @@ public function getFoldersForGroup(string $groupId, int $rootStorageId = 0): arr
'mount_point',
'quota',
'acl',
'fileid',
'storage',
'path',
'name',
'mimetype',
'mimepart',
'size',
'mtime',
'storage_mtime',
'etag',
'encrypted',
'parent'
'c.fileid',
'c.storage',
'c.path',
'c.name',
'c.mimetype',
'c.mimepart',
'c.size',
'c.mtime',
'c.storage_mtime',
'c.etag',
'c.encrypted',
'c.parent'
)
->selectAlias('a.permissions', 'group_permissions')
->selectAlias('c.permissions', 'permissions')
Expand Down Expand Up @@ -547,18 +548,18 @@ public function getFoldersForGroups(array $groupIds, int $rootStorageId = 0): ar
'mount_point',
'quota',
'acl',
'fileid',
'storage',
'path',
'name',
'mimetype',
'mimepart',
'size',
'mtime',
'storage_mtime',
'etag',
'encrypted',
'parent'
'c.fileid',
'c.storage',
'c.path',
'c.name',
'c.mimetype',
'c.mimepart',
'c.size',
'c.mtime',
'c.storage_mtime',
'c.etag',
'c.encrypted',
'c.parent'
)
->selectAlias('a.permissions', 'group_permissions')
->selectAlias('c.permissions', 'permissions')
Expand Down

0 comments on commit a450f8d

Please sign in to comment.