Skip to content

Commit 9ccc798

Browse files
committed
fix(FileAccess): Make getByAncestorInStorage sharding ready
Signed-off-by: Marcel Klehr <[email protected]>
1 parent 8e5f436 commit 9ccc798

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

lib/private/Files/Cache/FileAccess.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,20 @@ public function getByAncestorInStorage(int $storageId, int $folderId, int $fileI
114114

115115
$path = $root['path'] === '' ? '' : $root['path'] . '/';
116116

117-
$qb->selectDistinct('*')
117+
$qb->selectDistinct('f.*')
118118
->from('filecache', 'f')
119119
->where($qb->expr()->like('f.path', $qb->createNamedParameter($this->connection->escapeLikeParameter($path) . '%')))
120120
->andWhere($qb->expr()->eq('f.storage', $qb->createNamedParameter($storageId)))
121-
->andWhere($qb->expr()->gt('f.fileid', $qb->createNamedParameter($fileIdCursor, IQueryBuilder::PARAM_INT)));
121+
->andWhere($qb->expr()->gt('f.fileid', $qb->createNamedParameter($fileIdCursor, IQueryBuilder::PARAM_INT)))
122+
->hintShardKey('storage', $storageId);
122123

123-
if (!$endToEndEncrypted) {
124+
if (!$endToEndEncrypted && $this->connection->getShardDefinition('filecache') === null) {
124125
// End to end encrypted files are descendants of a folder with encrypted=1
125-
// Use a subquery to check the `encrypted` status of the parent folder
126-
$subQuery = $this->getQuery()->select('p.encrypted')
127-
->from('filecache', 'p')
128-
->andWhere($qb->expr()->eq('p.fileid', 'f.parent'))
129-
->getSQL();
126+
// We can only do this inner join if the filecache table is not sharded
127+
$qb->innerJoin('f', 'filecache', 'f2', $qb->expr()->eq('f2.fileid', 'f.parent'));
130128

131129
$qb->andWhere(
132-
$qb->expr()->eq($qb->createFunction(sprintf('(%s)', $subQuery)), $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
130+
$qb->expr()->eq('f2.encrypted', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
133131
);
134132
}
135133

@@ -152,6 +150,21 @@ public function getByAncestorInStorage(int $storageId, int $folderId, int $fileI
152150
/** @var array */
153151
$row = $files->fetch()
154152
) {
153+
if (!$endToEndEncrypted && $this->connection->getShardDefinition('filecache') !== null) {
154+
// End to end encrypted files are descendants of a folder with encrypted=1
155+
// If the filecache table is sharded we need to check individually if the parent is encrypted
156+
$parentQuery = $this->getQuery();
157+
$parentQuery->select('encrypted')->from('filecache');
158+
$parentQuery->whereFileId($row['parent']);
159+
$parentQuery->hintShardKey('storage', $storageId);
160+
$parentQuery->setMaxResults(1);
161+
$result = $parentQuery->executeQuery();
162+
$encrypted = $result->fetchOne();
163+
$result->closeCursor();
164+
if ($encrypted === 1) {
165+
continue;
166+
}
167+
}
155168
yield Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
156169
}
157170

0 commit comments

Comments
 (0)