CLI: Use new 'Indexer' to add Commands

This commit is contained in:
Albert S. 2022-04-14 15:03:19 +02:00
parent c51fd3c555
commit be41fab5d5
6 changed files with 80 additions and 14 deletions

View File

@ -20,7 +20,8 @@ SOURCES += \
commanddelete.cpp \
commandupdate.cpp \
commandsearch.cpp \
commandlist.cpp
commandlist.cpp \
command.cpp
HEADERS += \
command.h \

View File

@ -3,3 +3,12 @@
#include <QDebug>
#include "command.h"
#include "looqsgeneralexception.h"
void Command::execute()
{
int result = handle(arguments);
if(autoFinish)
{
emit finishedCmd(result);
}
}

View File

@ -3,22 +3,37 @@
#include <QStringList>
#include <QThreadStorage>
#include <QVariant>
#include <QMutex>
#include <QWaitCondition>
#include "utils.h"
#include "sqlitedbservice.h"
class Command
class Command : public QObject
{
Q_OBJECT
signals:
void finishedCmd(int retval);
protected:
SqliteDbService *dbService;
QString dbConnectionString;
QStringList arguments;
bool autoFinish = true;
public:
Command(SqliteDbService &dbService)
{
this->dbService = &dbService;
}
void setArguments(QStringList arguments)
{
this->arguments = arguments;
}
virtual int handle(QStringList arguments) = 0;
virtual ~Command(){};
public slots:
void execute();
};
#endif // COMMAND_H

View File

@ -11,6 +11,28 @@
#include "commandadd.h"
#include "logger.h"
void CommandAdd::indexerFinished()
{
IndexResult result = indexer->getResult();
Logger::info() << "Total: " << result.total() << Qt::endl;
Logger::info() << "Added: " << result.addedPaths << Qt::endl;
Logger::info() << "Skipped: " << result.skippedPaths << Qt::endl;
auto failedPathsCount = result.erroredPaths;
Logger::info() << "Failed: " << failedPathsCount << Qt::endl;
if(failedPathsCount > 0)
{
Logger::info() << "Failed paths: " << Qt::endl;
for(QString paths : result.failedPaths())
{
Logger::info() << paths << Qt::endl;
}
}
/* TODO maybe not 0 if keepGoing not given */
emit finishedCmd(0);
}
int CommandAdd::handle(QStringList arguments)
{
QCommandLineParser parser;
@ -51,15 +73,21 @@ int CommandAdd::handle(QStringList arguments)
}
}
FileSaver saver(*this->dbService);
int numFilesCount = files.size();
int processedFilesCount = saver.addFiles(files.toVector(), keepGoing, verbose);
if(processedFilesCount != numFilesCount)
{
Logger::error() << "Errors occured while trying to add files to the database. Processed " << processedFilesCount
<< "out of" << numFilesCount << "files" << Qt::endl;
return 1;
}
indexer = new Indexer(*this->dbService);
indexer->setTargetPaths(files.toVector());
connect(indexer, &Indexer::pathsCountChanged, this,
[](int pathsCount) { Logger::info() << "Found paths: " << pathsCount << Qt::endl; });
connect(indexer, &Indexer::indexProgress, this,
[](int pathsCount, unsigned int added, unsigned int skipped, unsigned int failed, unsigned int totalCount)
{ Logger::info() << "Processed files: " << totalCount << Qt::endl; });
connect(indexer, &Indexer::finished, this, &CommandAdd::indexerFinished);
/* TODO: keepGoing, verbose */
this->autoFinish = false;
indexer->beginIndexing();
return 0;
}

View File

@ -3,15 +3,21 @@
#include <QMutex>
#include "command.h"
#include "filesaver.h"
#include "indexer.h"
class CommandAdd : public Command
{
private:
SaveFileResult addFile(QString path);
Indexer *indexer;
protected:
public:
using Command::Command;
int handle(QStringList arguments) override;
private slots:
void indexerFinished();
};
#endif // COMMANDADD_H

View File

@ -9,6 +9,8 @@
#include <QDebug>
#include <QSettings>
#include <functional>
#include <QTimer>
#include <exception>
#include "encodingdetector.h"
#include "pdfprocessor.h"
@ -23,6 +25,7 @@
#include "logger.h"
#include "sandboxedprocessor.h"
#include "../shared/common.h"
#include "../shared/filescanworker.h"
void printUsage(QString argv0)
{
@ -74,6 +77,7 @@ int main(int argc, char *argv[])
return 1;
}
qRegisterMetaType<PageData>();
qRegisterMetaType<FileScanResult>("FileScanResult");
QString connectionString = Common::databasePath();
DatabaseFactory dbFactory(connectionString);
@ -96,7 +100,9 @@ int main(int argc, char *argv[])
{
try
{
return cmd->handle(args);
QObject::connect(cmd, &Command::finishedCmd, [](int retval) { QCoreApplication::exit(retval); });
cmd->setArguments(args);
QTimer::singleShot(0, cmd, &Command::execute);
}
catch(const LooqsGeneralException &e)
{
@ -107,5 +113,6 @@ int main(int argc, char *argv[])
{
Logger::error() << "Unknown command " << commandName << Qt::endl;
}
return 1;
return app.exec();
}