Skip to content

Commit ea03ec7

Browse files
authored
Merge pull request #3483 from nextcloud/sharding-support
fix: hint table for columns where needed for sharded queries
2 parents 494273f + c897828 commit ea03ec7

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

lib/ACL/RuleManager.php

+21-20
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ public function getRulesForFilesByPath(IUser $user, int $storageId, array $fileP
8484
$rows = [];
8585
foreach (array_chunk($hashes, 1000) as $chunk) {
8686
$query = $this->connection->getQueryBuilder();
87-
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
87+
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'f.path'])
8888
->from('group_folders_acl', 'a')
8989
->innerJoin('a', 'filecache', 'f', $query->expr()->eq('f.fileid', 'a.fileid'))
90-
->where($query->expr()->in('path_hash', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY)))
91-
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
90+
->where($query->expr()->in('f.path_hash', $query->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY)))
91+
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
9292
->andWhere($query->expr()->orX(...array_map(fn (IUserMapping $userMapping): ICompositeExpression => $query->expr()->andX(
9393
$query->expr()->eq('mapping_type', $query->createNamedParameter($userMapping->getType())),
9494
$query->expr()->eq('mapping_id', $query->createNamedParameter($userMapping->getId()))
@@ -117,19 +117,20 @@ public function getRulesForFilesByParent(IUser $user, int $storageId, string $pa
117117
}
118118

119119
$query = $this->connection->getQueryBuilder();
120-
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
120+
$query->select(['f.fileid', 'a.mapping_type', 'a.mapping_id', 'a.mask', 'a.permissions', 'f.path'])
121121
->from('filecache', 'f')
122122
->leftJoin('f', 'group_folders_acl', 'a', $query->expr()->eq('f.fileid', 'a.fileid'))
123-
->andWhere($query->expr()->eq('parent', $query->createNamedParameter($parentId, IQueryBuilder::PARAM_INT)))
123+
->andWhere($query->expr()->eq('f.parent', $query->createNamedParameter($parentId, IQueryBuilder::PARAM_INT)))
124+
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
124125
->andWhere(
125126
$query->expr()->orX(
126127
$query->expr()->andX(
127-
$query->expr()->isNull('mapping_type'),
128-
$query->expr()->isNull('mapping_id')
128+
$query->expr()->isNull('a.mapping_type'),
129+
$query->expr()->isNull('a.mapping_id')
129130
),
130131
...array_map(fn (IUserMapping $userMapping): ICompositeExpression => $query->expr()->andX(
131-
$query->expr()->eq('mapping_type', $query->createNamedParameter($userMapping->getType())),
132-
$query->expr()->eq('mapping_id', $query->createNamedParameter($userMapping->getId()))
132+
$query->expr()->eq('a.mapping_type', $query->createNamedParameter($userMapping->getType())),
133+
$query->expr()->eq('a.mapping_id', $query->createNamedParameter($userMapping->getId()))
133134
), $userMappings)
134135
)
135136
);
@@ -170,11 +171,11 @@ private function getId(int $storageId, string $path): int {
170171
public function getAllRulesForPaths(int $storageId, array $filePaths): array {
171172
$hashes = array_map(fn (string $path): string => md5(trim($path, '/')), $filePaths);
172173
$query = $this->connection->getQueryBuilder();
173-
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
174+
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'f.path'])
174175
->from('group_folders_acl', 'a')
175176
->innerJoin('a', 'filecache', 'f', $query->expr()->eq('f.fileid', 'a.fileid'))
176-
->where($query->expr()->in('path_hash', $query->createNamedParameter($hashes, IQueryBuilder::PARAM_STR_ARRAY)))
177-
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
177+
->where($query->expr()->in('f.path_hash', $query->createNamedParameter($hashes, IQueryBuilder::PARAM_STR_ARRAY)))
178+
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
178179

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

@@ -203,14 +204,14 @@ private function rulesByPath(array $rows, array $result = []): array {
203204
*/
204205
public function getAllRulesForPrefix(int $storageId, string $prefix): array {
205206
$query = $this->connection->getQueryBuilder();
206-
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
207+
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'f.path'])
207208
->from('group_folders_acl', 'a')
208209
->innerJoin('a', 'filecache', 'f', $query->expr()->eq('f.fileid', 'a.fileid'))
209210
->where($query->expr()->orX(
210-
$query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($prefix) . '/%')),
211-
$query->expr()->eq('path_hash', $query->createNamedParameter(md5($prefix)))
211+
$query->expr()->like('f.path', $query->createNamedParameter($this->connection->escapeLikeParameter($prefix) . '/%')),
212+
$query->expr()->eq('f.path_hash', $query->createNamedParameter(md5($prefix)))
212213
))
213-
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
214+
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
214215

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

@@ -224,14 +225,14 @@ public function getRulesForPrefix(IUser $user, int $storageId, string $prefix):
224225
$userMappings = $this->userMappingManager->getMappingsForUser($user);
225226

226227
$query = $this->connection->getQueryBuilder();
227-
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'path'])
228+
$query->select(['f.fileid', 'mapping_type', 'mapping_id', 'mask', 'a.permissions', 'f.path'])
228229
->from('group_folders_acl', 'a')
229230
->innerJoin('a', 'filecache', 'f', $query->expr()->eq('f.fileid', 'a.fileid'))
230231
->where($query->expr()->orX(
231-
$query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($prefix) . '/%')),
232-
$query->expr()->eq('path_hash', $query->createNamedParameter(md5($prefix)))
232+
$query->expr()->like('f.path', $query->createNamedParameter($this->connection->escapeLikeParameter($prefix) . '/%')),
233+
$query->expr()->eq('f.path_hash', $query->createNamedParameter(md5($prefix)))
233234
))
234-
->andWhere($query->expr()->eq('storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
235+
->andWhere($query->expr()->eq('f.storage', $query->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)))
235236
->andWhere($query->expr()->orX(...array_map(fn (IUserMapping $userMapping): ICompositeExpression => $query->expr()->andX(
236237
$query->expr()->eq('mapping_type', $query->createNamedParameter($userMapping->getType())),
237238
$query->expr()->eq('mapping_id', $query->createNamedParameter($userMapping->getId()))

lib/Folder/FolderManager.php

+29-28
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ private function joinQueryWithFileCache(IQueryBuilder $query, int $rootStorageId
123123
$query->leftJoin('f', 'filecache', 'c', $query->expr()->andX(
124124
// concat with empty string to work around missing cast to string
125125
$query->expr()->eq('c.name', $query->func()->concat('f.folder_id', $query->expr()->literal(''))),
126-
$query->expr()->eq('c.parent', $query->createNamedParameter($this->getGroupFolderRootId($rootStorageId)))
126+
$query->expr()->eq('c.parent', $query->createNamedParameter($this->getGroupFolderRootId($rootStorageId))),
127+
$query->expr()->eq('c.storage', $query->createNamedParameter($rootStorageId)),
127128
));
128129
}
129130

@@ -136,7 +137,7 @@ public function getAllFoldersWithSize(int $rootStorageId): array {
136137

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

139-
$query->select('folder_id', 'mount_point', 'quota', 'size', 'acl')
140+
$query->select('folder_id', 'mount_point', 'quota', 'c.size', 'acl')
140141
->from('group_folders', 'f');
141142
$this->joinQueryWithFileCache($query, $rootStorageId);
142143

@@ -172,7 +173,7 @@ public function getAllFoldersForUserWithSize(int $rootStorageId, IUser $user): a
172173

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

175-
$query->select('f.folder_id', 'mount_point', 'quota', 'size', 'acl')
176+
$query->select('f.folder_id', 'mount_point', 'quota', 'c.size', 'acl')
176177
->from('group_folders', 'f')
177178
->innerJoin(
178179
'f',
@@ -285,7 +286,7 @@ public function getFolder(int $id, int $rootStorageId = 0): ?array {
285286

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

288-
$query->select('folder_id', 'mount_point', 'quota', 'size', 'acl')
289+
$query->select('folder_id', 'mount_point', 'quota', 'c.size', 'acl')
289290
->from('group_folders', 'f')
290291
->where($query->expr()->eq('folder_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
291292
$this->joinQueryWithFileCache($query, $rootStorageId);
@@ -496,18 +497,18 @@ public function getFoldersForGroup(string $groupId, int $rootStorageId = 0): arr
496497
'mount_point',
497498
'quota',
498499
'acl',
499-
'fileid',
500-
'storage',
501-
'path',
502-
'name',
503-
'mimetype',
504-
'mimepart',
505-
'size',
506-
'mtime',
507-
'storage_mtime',
508-
'etag',
509-
'encrypted',
510-
'parent'
500+
'c.fileid',
501+
'c.storage',
502+
'c.path',
503+
'c.name',
504+
'c.mimetype',
505+
'c.mimepart',
506+
'c.size',
507+
'c.mtime',
508+
'c.storage_mtime',
509+
'c.etag',
510+
'c.encrypted',
511+
'c.parent'
511512
)
512513
->selectAlias('a.permissions', 'group_permissions')
513514
->selectAlias('c.permissions', 'permissions')
@@ -546,18 +547,18 @@ public function getFoldersForGroups(array $groupIds, int $rootStorageId = 0): ar
546547
'mount_point',
547548
'quota',
548549
'acl',
549-
'fileid',
550-
'storage',
551-
'path',
552-
'name',
553-
'mimetype',
554-
'mimepart',
555-
'size',
556-
'mtime',
557-
'storage_mtime',
558-
'etag',
559-
'encrypted',
560-
'parent'
550+
'c.fileid',
551+
'c.storage',
552+
'c.path',
553+
'c.name',
554+
'c.mimetype',
555+
'c.mimepart',
556+
'c.size',
557+
'c.mtime',
558+
'c.storage_mtime',
559+
'c.etag',
560+
'c.encrypted',
561+
'c.parent'
561562
)
562563
->selectAlias('a.permissions', 'group_permissions')
563564
->selectAlias('c.permissions', 'permissions')

0 commit comments

Comments
 (0)