Skip to content

Commit

Permalink
refactor(index): better query
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <radialapps@gmail.com>
  • Loading branch information
pulsejet committed May 1, 2024
1 parent 64c8ce0 commit a9b3dac
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions lib/Service/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Memories\Util;
use OCP\App\IAppManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\DB\QueryBuilder\IQueryFunction;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
Expand Down Expand Up @@ -172,24 +173,28 @@ public function indexFolder(Folder $folder): void
;

// Filter out files that are already indexed
$addFilter = static function (
string $table,
string $alias,
bool $orphan = true,
) use (&$query): void {
$query->leftJoin('f', $table, $alias, $query->expr()->andX(
$query->expr()->eq('f.fileid', "{$alias}.fileid"),
$query->expr()->eq('f.mtime', "{$alias}.mtime"),
$orphan
? $query->expr()->eq("{$alias}.orphan", $query->expr()->literal(0))
: $query->expr()->literal(1),
));

$query->andWhere($query->expr()->isNull("{$alias}.fileid"));
$getFilter = function (string $table, bool $notOrpaned) use (&$query): IQueryFunction {
// Make subquery to check if file exists in table
$clause = $this->db->getQueryBuilder();
$clause->select($clause->expr()->literal(1))
->from($table, 'a')
->andWhere($clause->expr()->eq('f.fileid', 'a.fileid'))
->andWhere($clause->expr()->eq('f.mtime', 'a.mtime'))
;

// Filter only non-orphaned files
if ($notOrpaned) {
$clause->andWhere($clause->expr()->eq('a.orphan', $clause->expr()->literal(0)));
}

// Add the clause to the main query
return SQL::notExists($query, $clause);
};
$addFilter('memories', 'm');
$addFilter('memories_livephoto', 'lp');
$addFilter('memories_failures', 'fail', false);

// Filter out files that are already indexed or failed
$query->andWhere($getFilter('memories', true));
$query->andWhere($getFilter('memories_livephoto', true));
$query->andWhere($getFilter('memories_failures', false));

// Get file IDs to actually index
$fileIds = Util::transaction(static fn (): array => $query->executeQuery()->fetchAll(\PDO::FETCH_COLUMN));
Expand Down

0 comments on commit a9b3dac

Please sign in to comment.