shared: Introduce IndexSyncer, containing logic of cli/CommandUpdate

IndexSyncer contains most of the logic of cli/CommandUpdate, so
it can be reused in the GUI where we need it too
This commit is contained in:
Albert S. 2022-06-03 10:06:03 +02:00
bovenliggende 86d629c957
commit abc126548b
3 gewijzigde bestanden met toevoegingen van 156 en 0 verwijderingen

118
shared/indexsyncer.cpp Normal file
Bestand weergeven

@ -0,0 +1,118 @@
#include <QDateTime>
#include "filesaver.h"
#include "indexsyncer.h"
IndexSyncer::IndexSyncer(SqliteDbService &dbService)
{
this->dbService = &dbService;
}
void IndexSyncer::setDryRun(bool dryRun)
{
this->dryRun = dryRun;
}
void IndexSyncer::setVerbose(bool verbose)
{
this->verbose = verbose;
}
void IndexSyncer::setKeepGoing(bool keepGoing)
{
this->keepGoing = keepGoing;
}
void IndexSyncer::setRemoveDeletedFromIndex(bool removeDeletedFromIndex)
{
this->removeDeletedFromIndex = removeDeletedFromIndex;
}
void IndexSyncer::setPattern(QString pattern)
{
this->pattern = pattern;
}
void IndexSyncer::sync()
{
FileSaver saver(*this->dbService);
QVector<FileData> files;
int offset = 0;
int limit = 1000;
unsigned int processedRows = dbService->getFiles(files, pattern, offset, limit);
unsigned int totalUpdatesFilesCount = 0;
unsigned int totalDeletedFilesCount = 0;
unsigned int totalErroredFilesCount = 0;
while(processedRows > 0)
{
QVector<QString> filePathsToUpdate;
for(FileData &fileData : files)
{
QFileInfo fileInfo(fileData.absPath);
if(fileInfo.exists())
{
if(fileInfo.isFile())
{
if(fileInfo.lastModified().toSecsSinceEpoch() != fileData.mtime)
{
if(!dryRun)
{
filePathsToUpdate.append(fileData.absPath);
}
else
{
emit updatedDryRun(fileData.absPath);
}
}
}
}
else
{
if(this->removeDeletedFromIndex)
{
if(!dryRun)
{
if(!this->dbService->deleteFile(fileData.absPath))
{
emit error("Error: Failed to delete " + fileData.absPath + " from the index");
if(!this->keepGoing)
{
emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount);
return;
}
}
emit removed(fileData.absPath);
++totalDeletedFilesCount;
}
else
{
emit removedDryRun(fileData.absPath);
}
}
}
}
unsigned int updatedFilesCount = saver.updateFiles(filePathsToUpdate, keepGoing, verbose);
unsigned int shouldHaveUpdatedCount = static_cast<unsigned int>(filePathsToUpdate.size());
if(updatedFilesCount != shouldHaveUpdatedCount)
{
totalErroredFilesCount += (shouldHaveUpdatedCount - updatedFilesCount);
if(!keepGoing)
{
QString errorMsg = QString("Failed to update all files selected for updating in this batch. Updated") +
updatedFilesCount + "out of" + shouldHaveUpdatedCount + "selected for updating";
emit error(errorMsg);
emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount);
}
}
offset += limit;
files.clear();
processedRows = this->dbService->getFiles(files, pattern, offset, limit);
totalUpdatesFilesCount += updatedFilesCount;
}
emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount);
}

36
shared/indexsyncer.h Normal file
Bestand weergeven

@ -0,0 +1,36 @@
#ifndef INDEXSYNCER_H
#define INDEXSYNCER_H
#include "sqlitedbservice.h"
class IndexSyncer : public QObject
{
Q_OBJECT
private:
SqliteDbService *dbService = nullptr;
bool keepGoing = true;
bool removeDeletedFromIndex = true;
bool dryRun = false;
bool verbose = false;
QString pattern;
public:
IndexSyncer(SqliteDbService &dbService);
public slots:
void sync();
void setDryRun(bool dryRun);
void setVerbose(bool verbose);
void setKeepGoing(bool keepGoing);
void setRemoveDeletedFromIndex(bool removeDeletedFromIndex);
void setPattern(QString pattern);
signals:
void error(QString error);
void removed(QString path);
void removedDryRun(QString path);
void updatedDryRun(QString path);
void updated(QString path);
void finished(unsigned int totalUpdated, unsigned int totalDeleted, unsigned int totalErrored);
};
#endif // INDEXSYNCER_H

Bestand weergeven

@ -42,6 +42,7 @@ SOURCES += sqlitesearch.cpp \
filesaver.cpp \
filescanworker.cpp \
indexer.cpp \
indexsyncer.cpp \
logger.cpp \
looqsgeneralexception.cpp \
common.cpp \
@ -71,6 +72,7 @@ HEADERS += sqlitesearch.h \
filesaver.h \
filescanworker.h \
indexer.h \
indexsyncer.h \
logger.h \
looqsgeneralexception.h \
looqsquery.h \