Merge pull request #55954 from nextcloud/carl/fix-preview2

fix: Fetching previews on Oracle
This commit is contained in:
Ferdinand Thiessen 2025-10-27 13:00:25 +01:00 committed by GitHub
commit d0e25af1af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 63 additions and 34 deletions

View file

@ -205,13 +205,20 @@ class PartitionedQueryBuilder extends ShardedQueryBuilder {
}
public function leftJoin($fromAlias, $join, $alias, $condition = null): self {
return $this->join($fromAlias, (string)$join, $alias, $condition, PartitionQuery::JOIN_MODE_LEFT);
return $this->join($fromAlias, $join, $alias, $condition, PartitionQuery::JOIN_MODE_LEFT);
}
public function join($fromAlias, $join, $alias, $condition = null, $joinMode = PartitionQuery::JOIN_MODE_INNER): self {
$partition = $this->getPartition($join);
$fromPartition = $this->getPartition($fromAlias);
if ($join instanceof IQueryFunction) {
$partition = null;
$fromPartition = null;
} else {
$partition = $this->getPartition($join);
$fromPartition = $this->getPartition($fromAlias);
}
if ($partition && $partition !== $this->mainPartition) {
/** @var string $join */
// join from the main db to a partition
$joinCondition = JoinCondition::parse($condition, $join, $alias, $fromAlias);
@ -238,6 +245,7 @@ class PartitionedQueryBuilder extends ShardedQueryBuilder {
parent::andWhere(...$joinCondition->fromConditions);
return $this;
} elseif ($fromPartition && $fromPartition !== $partition) {
/** @var string $join */
// join from partition, to the main db
$joinCondition = JoinCondition::parse($condition, $join, $alias, $fromAlias);

View file

@ -236,12 +236,16 @@ class ShardedQueryBuilder extends ExtendedQueryBuilder {
}
public function innerJoin($fromAlias, $join, $alias, $condition = null) {
$this->checkJoin($join);
if (is_string($join)) {
$this->checkJoin($join);
}
return parent::innerJoin($fromAlias, $join, $alias, $condition);
}
public function leftJoin($fromAlias, $join, $alias, $condition = null) {
$this->checkJoin($join);
if (is_string($join)) {
$this->checkJoin($join);
}
return parent::leftJoin($fromAlias, $join, $alias, $condition);
}

View file

@ -34,36 +34,53 @@ class PreviewService {
* @return \Generator<array{storageId: int, fileIds: int[]}>
*/
public function getAvailableFileIds(): \Generator {
$maxQb = $this->connection->getQueryBuilder();
$maxQb->select($maxQb->func()->max('id'))
->from($this->previewMapper->getTableName())
->groupBy('file_id');
$lastId = null;
while (true) {
$maxQb = $this->connection->getQueryBuilder();
$maxQb->selectAlias($maxQb->func()->max('id'), 'max_id')
->from($this->previewMapper->getTableName())
->groupBy('file_id')
->orderBy('max_id', 'ASC');
$qb = $this->connection->getQueryBuilder();
$qb->select('file_id', 'storage_id')
->from($this->previewMapper->getTableName())
->where($qb->expr()->in('id', $qb->createFunction($maxQb->getSQL())));
$qb = $this->connection->getQueryBuilder();
$result = $qb->executeQuery();
$lastStorageId = -1;
/** @var int[] $fileIds */
$fileIds = [];
// Previews next to each others in the database are likely in the same storage, so group them
while ($row = $result->fetch()) {
if ($lastStorageId !== $row['storage_id']) {
if ($lastStorageId !== -1) {
yield ['storageId' => $lastStorageId, 'fileIds' => $fileIds];
$fileIds = [];
}
$lastStorageId = (int)$row['storage_id'];
if ($lastId !== null) {
$maxQb->andWhere($maxQb->expr()->gt('id', $qb->createNamedParameter($lastId)));
}
$fileIds[] = (int)$row['file_id'];
}
if (count($fileIds) > 0) {
yield ['storageId' => $lastStorageId, 'fileIds' => $fileIds];
$qb->select('id', 'file_id', 'storage_id')
->from($this->previewMapper->getTableName(), 'p1')
->innerJoin('p1', $qb->createFunction('(' . $maxQb->getSQL() . ')'), 'p2', $qb->expr()->eq('p1.id', 'p2.max_id'))
->setMaxResults(1000);
$result = $qb->executeQuery();
$lastStorageId = -1;
/** @var int[] $fileIds */
$fileIds = [];
$found = false;
// Previews next to each others in the database are likely in the same storage, so group them
while ($row = $result->fetch()) {
$found = true;
if ($lastStorageId !== (int)$row['storage_id']) {
if ($lastStorageId !== -1) {
yield ['storageId' => $lastStorageId, 'fileIds' => $fileIds];
$fileIds = [];
}
$lastStorageId = (int)$row['storage_id'];
}
$fileIds[] = (int)$row['file_id'];
$lastId = $row['id'];
}
if (count($fileIds) > 0) {
yield ['storageId' => $lastStorageId, 'fileIds' => $fileIds];
}
if (!$found) {
break;
}
}
}

View file

@ -524,7 +524,7 @@ interface IQueryBuilder {
* </code>
*
* @param string $fromAlias The alias that points to a from clause.
* @param string $join The table name to join.
* @param string|IQueryFunction $join The table name to join.
* @param string $alias The alias of the join table.
* @param string|ICompositeExpression|null $condition The condition for the join.
*
@ -549,7 +549,7 @@ interface IQueryBuilder {
* </code>
*
* @param string $fromAlias The alias that points to a from clause.
* @param string $join The table name to join.
* @param string|IQueryFunction $join The table name to join.
* @param string $alias The alias of the join table.
* @param string|ICompositeExpression|null $condition The condition for the join.
*
@ -600,7 +600,7 @@ interface IQueryBuilder {
* </code>
*
* @param string $fromAlias The alias that points to a from clause.
* @param string $join The table name to join.
* @param string|IQueryFunction $join The table name to join.
* @param string $alias The alias of the join table.
* @param string|ICompositeExpression|null $condition The condition for the join.
*