From abc126548b73fd107346e1495c2e1152778ba051 Mon Sep 17 00:00:00 2001 From: Albert S Date: Fri, 3 Jun 2022 10:06:03 +0200 Subject: [PATCH] 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 --- shared/indexsyncer.cpp | 118 +++++++++++++++++++++++++++++++++++++++++ shared/indexsyncer.h | 36 +++++++++++++ shared/shared.pro | 2 + 3 files changed, 156 insertions(+) create mode 100644 shared/indexsyncer.cpp create mode 100644 shared/indexsyncer.h diff --git a/shared/indexsyncer.cpp b/shared/indexsyncer.cpp new file mode 100644 index 0000000..0c7a8d5 --- /dev/null +++ b/shared/indexsyncer.cpp @@ -0,0 +1,118 @@ +#include +#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 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 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(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); +} diff --git a/shared/indexsyncer.h b/shared/indexsyncer.h new file mode 100644 index 0000000..59e8e00 --- /dev/null +++ b/shared/indexsyncer.h @@ -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 diff --git a/shared/shared.pro b/shared/shared.pro index 432269c..b2499f4 100644 --- a/shared/shared.pro +++ b/shared/shared.pro @@ -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 \