CLI: Use new 'Indexer' to add Commands

Cette révision appartient à :
Albert S. 2022-04-14 15:03:19 +02:00
Parent c51fd3c555
révision be41fab5d5
6 fichiers modifiés avec 80 ajouts et 14 suppressions

Voir le fichier

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

Voir le fichier

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

Voir le fichier

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

Voir le fichier

@ -11,6 +11,28 @@
#include "commandadd.h" #include "commandadd.h"
#include "logger.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) int CommandAdd::handle(QStringList arguments)
{ {
QCommandLineParser parser; QCommandLineParser parser;
@ -51,15 +73,21 @@ int CommandAdd::handle(QStringList arguments)
} }
} }
FileSaver saver(*this->dbService); indexer = new Indexer(*this->dbService);
int numFilesCount = files.size(); indexer->setTargetPaths(files.toVector());
int processedFilesCount = saver.addFiles(files.toVector(), keepGoing, verbose);
if(processedFilesCount != numFilesCount) connect(indexer, &Indexer::pathsCountChanged, this,
{ [](int pathsCount) { Logger::info() << "Found paths: " << pathsCount << Qt::endl; });
Logger::error() << "Errors occured while trying to add files to the database. Processed " << processedFilesCount
<< "out of" << numFilesCount << "files" << Qt::endl; connect(indexer, &Indexer::indexProgress, this,
return 1; [](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; return 0;
} }

Voir le fichier

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

Voir le fichier

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