Introduce FileSaverOptions to consolidate common parameters

This commit is contained in:
Albert S. 2023-01-08 17:37:28 +01:00
parent efca45b88a
commit 4b1522b82a
11 changed files with 77 additions and 53 deletions

View File

@ -71,9 +71,16 @@ int CommandAdd::handle(QStringList arguments)
} }
} }
FileSaverOptions fileSaverOptions;
fileSaverOptions.keepGoing = keepGoing;
fileSaverOptions.fillPathsOnlyWithContent = fillContent;
fileSaverOptions.pathsOnly = pathsOnly;
fileSaverOptions.verbose = false;
indexer = new Indexer(*this->dbService); indexer = new Indexer(*this->dbService);
indexer->setFileSaverOptions(fileSaverOptions);
indexer->setTargetPaths(files.toVector()); indexer->setTargetPaths(files.toVector());
indexer->setKeepGoing(keepGoing);
connect(indexer, &Indexer::pathsCountChanged, this, connect(indexer, &Indexer::pathsCountChanged, this,
[](int pathsCount) { Logger::info() << "Found paths: " << pathsCount << Qt::endl; }); [](int pathsCount) { Logger::info() << "Found paths: " << pathsCount << Qt::endl; });

View File

@ -40,8 +40,12 @@ int CommandUpdate::handle(QStringList arguments)
bool hasErrors = false; bool hasErrors = false;
IndexSyncer *syncer = new IndexSyncer(*this->dbService); IndexSyncer *syncer = new IndexSyncer(*this->dbService);
syncer->setKeepGoing(keepGoing);
syncer->setVerbose(verbose); FileSaverOptions fileOptions;
fileOptions.keepGoing = keepGoing;
fileOptions.verbose = verbose;
syncer->setFileSaverOptions(fileOptions);
syncer->setPattern(pattern); syncer->setPattern(pattern);
syncer->setDryRun(dryRun); syncer->setDryRun(dryRun);
syncer->setRemoveDeletedFromIndex(deleteMissing); syncer->setRemoveDeletedFromIndex(deleteMissing);

View File

@ -264,8 +264,11 @@ void MainWindow::startIndexSync()
progressDialog.setValue(0); progressDialog.setValue(0);
progressDialog.open(); progressDialog.open();
indexSyncer->setKeepGoing(true); FileSaverOptions options;
indexSyncer->setVerbose(false); options.keepGoing = true;
options.verbose = false;
indexSyncer->setFileSaverOptions(options);
indexSyncer->setDryRun(false); indexSyncer->setDryRun(false);
indexSyncer->setRemoveDeletedFromIndex(true); indexSyncer->setRemoveDeletedFromIndex(true);

View File

@ -38,18 +38,17 @@ SaveFileResult FileSaver::updateFile(QString path)
return saveFile(info); return saveFile(info);
} }
int FileSaver::addFiles(const QVector<QString> paths, bool keepGoing, bool verbose) int FileSaver::addFiles(const QVector<QString> paths)
{ {
return processFiles(paths, std::bind(&FileSaver::addFile, this, std::placeholders::_1), keepGoing, verbose); return processFiles(paths, std::bind(&FileSaver::addFile, this, std::placeholders::_1));
} }
int FileSaver::updateFiles(const QVector<QString> paths, bool keepGoing, bool verbose) int FileSaver::updateFiles(const QVector<QString> paths)
{ {
return processFiles(paths, std::bind(&FileSaver::updateFile, this, std::placeholders::_1), keepGoing, verbose); return processFiles(paths, std::bind(&FileSaver::updateFile, this, std::placeholders::_1));
} }
int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFileResult(QString path)> saverFunc, int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFileResult(QString path)> saverFunc)
bool keepGoing, bool verbose)
{ {
std::atomic<bool> terminate{false}; std::atomic<bool> terminate{false};
std::atomic<int> processedCount{0}; std::atomic<int> processedCount{0};
@ -60,7 +59,7 @@ int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFile
{ {
return; return;
} }
if(verbose) if(this->fileSaverOptions.verbose)
{ {
Logger::info() << "Processing " << path << Qt::endl; Logger::info() << "Processing " << path << Qt::endl;
} }
@ -68,7 +67,7 @@ int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFile
if(result == DBFAIL || result == PROCESSFAIL) if(result == DBFAIL || result == PROCESSFAIL)
{ {
Logger::error() << "Failed to process " << path << Qt::endl; Logger::error() << "Failed to process " << path << Qt::endl;
if(!keepGoing) if(!this->fileSaverOptions.keepGoing)
{ {
terminate = true; terminate = true;
} }
@ -76,7 +75,7 @@ int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFile
else else
{ {
++processedCount; ++processedCount;
if(verbose) if(this->fileSaverOptions.verbose)
{ {
if(result == SKIPPED) if(result == SKIPPED)
{ {

View File

@ -2,6 +2,7 @@
#define FILESAVER_H #define FILESAVER_H
#include <QSqlDatabase> #include <QSqlDatabase>
#include <QFileInfo> #include <QFileInfo>
#include "filesaveroptions.h"
#include "pagedata.h" #include "pagedata.h"
#include "filedata.h" #include "filedata.h"
#include "sqlitedbservice.h" #include "sqlitedbservice.h"
@ -11,16 +12,21 @@ class FileSaver
private: private:
SqliteDbService *dbService; SqliteDbService *dbService;
QStringList excludedPaths = Common::excludedPaths(); QStringList excludedPaths = Common::excludedPaths();
FileSaverOptions fileSaverOptions;
public: public:
FileSaver(SqliteDbService &dbService); FileSaver(SqliteDbService &dbService);
SaveFileResult addFile(QString path); SaveFileResult addFile(QString path);
SaveFileResult updateFile(QString path); SaveFileResult updateFile(QString path);
SaveFileResult saveFile(const QFileInfo &fileInfo); SaveFileResult saveFile(const QFileInfo &fileInfo);
int processFiles(const QVector<QString> paths, std::function<SaveFileResult(QString path)> saverFunc, int processFiles(const QVector<QString> paths, std::function<SaveFileResult(QString path)> saverFunc);
bool keepGoing, bool verbose); int addFiles(const QVector<QString> paths);
int addFiles(const QVector<QString> paths, bool keepGoing, bool verbose); int updateFiles(const QVector<QString> paths);
int updateFiles(const QVector<QString> paths, bool keepGoing, bool verbose);
void setFileSaverOptions(FileSaverOptions options)
{
this->fileSaverOptions = options;
}
}; };
#endif // FILESAVER_H #endif // FILESAVER_H

14
shared/filesaveroptions.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef FILESAVEROPTIONS_H
#define FILESAVEROPTIONS_H
class FileSaverOptions
{
public:
bool verbose = false;
bool keepGoing = false;
bool pathsOnly = false;
/* Whether those previously explicitly without content should be filled */
bool fillPathsOnlyWithContent = false;
};
#endif // FILESAVEROPTIONS_H

View File

@ -73,16 +73,6 @@ void Indexer::setTargetPaths(QVector<QString> pathsToScan)
this->pathsToScan = pathsToScan; this->pathsToScan = pathsToScan;
} }
void Indexer::setVerbose(bool verbose)
{
this->verbose = verbose;
}
void Indexer::setKeepGoing(bool keepGoing)
{
this->keepGoing = keepGoing;
}
void Indexer::requestCancellation() void Indexer::requestCancellation()
{ {
this->dirScanner->cancel(); this->dirScanner->cancel();
@ -123,7 +113,7 @@ void Indexer::processFileScanResult(FileScanResult result)
if(isErrorSaveFileResult(result.second)) if(isErrorSaveFileResult(result.second))
{ {
this->currentIndexResult.results.append(result); this->currentIndexResult.results.append(result);
if(!keepGoing) if(!this->fileSaverOptions.keepGoing)
{ {
this->requestCancellation(); this->requestCancellation();
emit finished(); emit finished();
@ -132,7 +122,7 @@ void Indexer::processFileScanResult(FileScanResult result)
} }
else else
{ {
if(verbose) if(this->fileSaverOptions.verbose)
{ {
this->currentIndexResult.results.append(result); this->currentIndexResult.results.append(result);
} }
@ -175,3 +165,8 @@ void Indexer::processFinishedWorker()
emit finished(); emit finished();
} }
} }
void Indexer::setFileSaverOptions(FileSaverOptions options)
{
this->fileSaverOptions = options;
}

View File

@ -52,8 +52,7 @@ class Indexer : public QObject
{ {
Q_OBJECT Q_OBJECT
protected: protected:
bool verbose = false; FileSaverOptions fileSaverOptions;
bool keepGoing = true;
SqliteDbService *db; SqliteDbService *db;
int progressReportThreshold = 50; int progressReportThreshold = 50;
@ -80,8 +79,8 @@ class Indexer : public QObject
void beginIndexing(); void beginIndexing();
void setIgnorePattern(QStringList ignorePattern); void setIgnorePattern(QStringList ignorePattern);
void setTargetPaths(QVector<QString> pathsToScan); void setTargetPaths(QVector<QString> pathsToScan);
void setVerbose(bool verbose);
void setKeepGoing(bool keepGoing); void setFileSaverOptions(FileSaverOptions options);
void requestCancellation(); void requestCancellation();

View File

@ -7,21 +7,16 @@ IndexSyncer::IndexSyncer(SqliteDbService &dbService)
this->dbService = &dbService; this->dbService = &dbService;
} }
void IndexSyncer::setFileSaverOptions(FileSaverOptions options)
{
fileSaverOptions = options;
}
void IndexSyncer::setDryRun(bool dryRun) void IndexSyncer::setDryRun(bool dryRun)
{ {
this->dryRun = 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) void IndexSyncer::setRemoveDeletedFromIndex(bool removeDeletedFromIndex)
{ {
this->removeDeletedFromIndex = removeDeletedFromIndex; this->removeDeletedFromIndex = removeDeletedFromIndex;
@ -35,7 +30,7 @@ void IndexSyncer::setPattern(QString pattern)
void IndexSyncer::sync() void IndexSyncer::sync()
{ {
this->stopToken.store(false, std::memory_order_relaxed); this->stopToken.store(false, std::memory_order_relaxed);
FileSaver saver(*this->dbService);
QVector<FileData> files; QVector<FileData> files;
int offset = 0; int offset = 0;
int limit = 10000; int limit = 10000;
@ -87,7 +82,7 @@ void IndexSyncer::sync()
if(!this->dbService->deleteFile(fileData.absPath)) if(!this->dbService->deleteFile(fileData.absPath))
{ {
emit error("Error: Failed to delete " + fileData.absPath + " from the index"); emit error("Error: Failed to delete " + fileData.absPath + " from the index");
if(!this->keepGoing) if(!this->fileSaverOptions.keepGoing)
{ {
emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount); emit finished(totalUpdatesFilesCount, totalDeletedFilesCount, totalErroredFilesCount);
return; return;
@ -104,13 +99,15 @@ void IndexSyncer::sync()
} }
} }
unsigned int updatedFilesCount = saver.updateFiles(filePathsToUpdate, keepGoing, verbose); FileSaver saver(*this->dbService);
saver.setFileSaverOptions(this->fileSaverOptions);
unsigned int updatedFilesCount = saver.updateFiles(filePathsToUpdate);
unsigned int shouldHaveUpdatedCount = static_cast<unsigned int>(filePathsToUpdate.size()); unsigned int shouldHaveUpdatedCount = static_cast<unsigned int>(filePathsToUpdate.size());
if(updatedFilesCount != shouldHaveUpdatedCount) if(updatedFilesCount != shouldHaveUpdatedCount)
{ {
totalErroredFilesCount += (shouldHaveUpdatedCount - updatedFilesCount); totalErroredFilesCount += (shouldHaveUpdatedCount - updatedFilesCount);
if(!keepGoing) if(!this->fileSaverOptions.keepGoing)
{ {
QString errorMsg = QString("Failed to update all files selected for updating in this batch. Updated") + QString errorMsg = QString("Failed to update all files selected for updating in this batch. Updated") +
updatedFilesCount + "out of" + shouldHaveUpdatedCount + "selected for updating"; updatedFilesCount + "out of" + shouldHaveUpdatedCount + "selected for updating";

View File

@ -1,16 +1,15 @@
#ifndef INDEXSYNCER_H #ifndef INDEXSYNCER_H
#define INDEXSYNCER_H #define INDEXSYNCER_H
#include "sqlitedbservice.h" #include "sqlitedbservice.h"
#include "filesaveroptions.h"
class IndexSyncer : public QObject class IndexSyncer : public QObject
{ {
Q_OBJECT Q_OBJECT
private: private:
SqliteDbService *dbService = nullptr; SqliteDbService *dbService = nullptr;
bool keepGoing = true; FileSaverOptions fileSaverOptions;
bool removeDeletedFromIndex = true; bool removeDeletedFromIndex = true;
bool dryRun = false; bool dryRun = false;
bool verbose = false;
QString pattern; QString pattern;
std::atomic<bool> stopToken{false}; std::atomic<bool> stopToken{false};
@ -18,12 +17,12 @@ class IndexSyncer : public QObject
public: public:
IndexSyncer(SqliteDbService &dbService); IndexSyncer(SqliteDbService &dbService);
void setFileSaverOptions(FileSaverOptions options);
public slots: public slots:
void sync(); void sync();
void cancel(); void cancel();
void setDryRun(bool dryRun); void setDryRun(bool dryRun);
void setVerbose(bool verbose);
void setKeepGoing(bool keepGoing);
void setRemoveDeletedFromIndex(bool removeDeletedFromIndex); void setRemoveDeletedFromIndex(bool removeDeletedFromIndex);
void setPattern(QString pattern); void setPattern(QString pattern);

View File

@ -74,6 +74,7 @@ HEADERS += sqlitesearch.h \
encodingdetector.h \ encodingdetector.h \
filedata.h \ filedata.h \
filesaver.h \ filesaver.h \
filesaveroptions.h \
filescanworker.h \ filescanworker.h \
indexer.h \ indexer.h \
indexsyncer.h \ indexsyncer.h \