index: add stale cleanup (close #610)

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/653/head
Varun Patil 2023-04-24 17:59:26 -07:00
parent 8735ac2159
commit 4b86dc96cc
4 changed files with 38 additions and 0 deletions

View File

@ -124,6 +124,9 @@ class Index extends Command
// Run the indexer
$this->runIndex();
// Clean up the index
$this->indexer->cleanupStale();
return 0;
} catch (\Exception $e) {
$this->output->writeln("<error>{$e->getMessage()}</error>");

View File

@ -61,6 +61,7 @@ class IndexJob extends TimedJob
try {
\OCA\Memories\Exif::ensureStaticExiftoolProc();
$this->indexAllUsers();
$this->service->cleanupStale();
$this->log('Indexing completed successfully', 'success');
} catch (Service\ProcessClosedException $e) {
$this->log('Indexing process stopped before completion. Will continue on next run', 'info');

View File

@ -213,6 +213,31 @@ class TimelineWrite
$this->connection->commit();
}
/**
* Clean up the table for entries not present in filecache.
*/
public function cleanupStale(): void
{
// Begin transaction
$this->connection->beginTransaction();
// Existence clause
$clause = 'SELECT 1 FROM *PREFIX*filecache AS f
WHERE f.fileid=m.fileid
AND f.path NOT LIKE "files_trashbin/%"';
// Delete all stale records
foreach (DELETE_TABLES as $table) {
// Query builder doesn't add the table to delete from,
// so we use need to use raw SQL here :/
$sql = "DELETE m FROM *PREFIX*{$table} m WHERE NOT EXISTS ({$clause})";
$this->connection->executeStatement($sql);
}
// Commit transaction
$this->connection->commit();
}
/**
* Clear the entire index. Does not need confirmation!
*

View File

@ -210,6 +210,15 @@ class Index
}
}
/**
* Cleanup all stale entries (passthrough to timeline write).
*/
public function cleanupStale(): void
{
$this->log('Cleaning up stale index entries'.PHP_EOL);
$this->timelineWrite->cleanupStale();
}
/**
* Get total number of files that are indexed.
*/