From c8978288d7e4b936ddb4d1f3d24c0606bc91e631 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 12 Dec 2024 17:44:54 +0100 Subject: [PATCH] fix: hint table for columns where needed for sharded queries Signed-off-by: Robin Appelman --- lib/ACL/RuleManager.php | 41 +++++++++++++------------- lib/Folder/FolderManager.php | 57 ++++++++++++++++++------------------ 2 files changed, 50 insertions(+), 48 deletions(-) diff --git a/lib/ACL/RuleManager.php b/lib/ACL/RuleManager.php index 81b5905c9..491464860 100644 --- a/lib/ACL/RuleManager.php +++ b/lib/ACL/RuleManager.php @@ -84,11 +84,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(fn (IUserMapping $userMapping): ICompositeExpression => $query->expr()->andX( $query->expr()->eq('mapping_type', $query->createNamedParameter($userMapping->getType())), $query->expr()->eq('mapping_id', $query->createNamedParameter($userMapping->getId())) @@ -117,19 +117,20 @@ 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(fn (IUserMapping $userMapping): ICompositeExpression => $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) ) ); @@ -170,11 +171,11 @@ private function getId(int $storageId, string $path): int { public function getAllRulesForPaths(int $storageId, array $filePaths): array { $hashes = array_map(fn (string $path): string => 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(); @@ -203,14 +204,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(); @@ -224,14 +225,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(fn (IUserMapping $userMapping): ICompositeExpression => $query->expr()->andX( $query->expr()->eq('mapping_type', $query->createNamedParameter($userMapping->getType())), $query->expr()->eq('mapping_id', $query->createNamedParameter($userMapping->getId())) diff --git a/lib/Folder/FolderManager.php b/lib/Folder/FolderManager.php index 94a1e872b..971aaaea3 100644 --- a/lib/Folder/FolderManager.php +++ b/lib/Folder/FolderManager.php @@ -123,7 +123,8 @@ private function joinQueryWithFileCache(IQueryBuilder $query, int $rootStorageId $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.parent', $query->createNamedParameter($this->getGroupFolderRootId($rootStorageId))), + $query->expr()->eq('c.storage', $query->createNamedParameter($rootStorageId)), )); } @@ -136,7 +137,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); @@ -172,7 +173,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', @@ -285,7 +286,7 @@ public function getFolder(int $id, int $rootStorageId = 0): ?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') ->where($query->expr()->eq('folder_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); $this->joinQueryWithFileCache($query, $rootStorageId); @@ -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') @@ -546,18 +547,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')