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->setFileSaverOptions(fileSaverOptions);
indexer->setTargetPaths(files.toVector());
indexer->setKeepGoing(keepGoing);
connect(indexer, &Indexer::pathsCountChanged, this,
[](int pathsCount) { Logger::info() << "Found paths: " << pathsCount << Qt::endl; });

View File

@ -40,8 +40,12 @@ int CommandUpdate::handle(QStringList arguments)
bool hasErrors = false;
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->setDryRun(dryRun);
syncer->setRemoveDeletedFromIndex(deleteMissing);

View File

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

View File

@ -38,18 +38,17 @@ SaveFileResult FileSaver::updateFile(QString path)
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,
bool keepGoing, bool verbose)
int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFileResult(QString path)> saverFunc)
{
std::atomic<bool> terminate{false};
std::atomic<int> processedCount{0};
@ -60,7 +59,7 @@ int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFile
{
return;
}
if(verbose)
if(this->fileSaverOptions.verbose)
{
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)
{
Logger::error() << "Failed to process " << path << Qt::endl;
if(!keepGoing)
if(!this->fileSaverOptions.keepGoing)
{
terminate = true;
}
@ -76,7 +75,7 @@ int FileSaver::processFiles(const QVector<QString> paths, std::function<SaveFile
else
{
++processedCount;
if(verbose)
if(this->fileSaverOptions.verbose)
{
if(result == SKIPPED)
{

View File

@ -2,6 +2,7 @@
#define FILESAVER_H
#include <QSqlDatabase>
#include <QFileInfo>
#include "filesaveroptions.h"
#include "pagedata.h"
#include "filedata.h"
#include "sqlitedbservice.h"
@ -11,16 +12,21 @@ class FileSaver
private:
SqliteDbService *dbService;
QStringList excludedPaths = Common::excludedPaths();
FileSaverOptions fileSaverOptions;
public:
FileSaver(SqliteDbService &dbService);
SaveFileResult addFile(QString path);
SaveFileResult updateFile(QString path);
SaveFileResult saveFile(const QFileInfo &fileInfo);
int processFiles(const QVector<QString> paths, std::function<SaveFileResult(QString path)> saverFunc,
bool keepGoing, bool verbose);
int addFiles(const QVector<QString> paths, bool keepGoing, bool verbose);
int updateFiles(const QVector<QString> paths, bool keepGoing, bool verbose);
int processFiles(const QVector<QString> paths, std::function<SaveFileResult(QString path)> saverFunc);
int addFiles(const QVector<QString> paths);
int updateFiles(const QVector<QString> paths);
void setFileSaverOptions(FileSaverOptions options)
{
this->fileSaverOptions = options;
}
};
#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;
}
void Indexer::setVerbose(bool verbose)
{
this->verbose = verbose;
}
void Indexer::setKeepGoing(bool keepGoing)
{
this->keepGoing = keepGoing;
}
void Indexer::requestCancellation()
{
this->dirScanner->cancel();
@ -123,7 +113,7 @@ void Indexer::processFileScanResult(FileScanResult result)
if(isErrorSaveFileResult(result.second))
{
this->currentIndexResult.results.append(result);
if(!keepGoing)
if(!this->fileSaverOptions.keepGoing)
{
this->requestCancellation();
emit finished();
@ -132,7 +122,7 @@ void Indexer::processFileScanResult(FileScanResult result)
}
else
{
if(verbose)
if(this->fileSaverOptions.verbose)
{
this->currentIndexResult.results.append(result);
}
@ -175,3 +165,8 @@ void Indexer::processFinishedWorker()
emit finished();
}
}
void Indexer::setFileSaverOptions(FileSaverOptions options)
{
this->fileSaverOptions = options;
}

View File

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

View File

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

View File

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

View File

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