looqs/shared/indexer.cpp

176 řádky
3.9 KiB
C++
Surový Normální zobrazení Historie

2022-04-14 14:58:27 +02:00
#include "indexer.h"
#include "logger.h"
#include "wildcardmatcher.h"
2022-04-14 14:58:27 +02:00
Indexer::Indexer(SqliteDbService &db)
{
dirScanner = QSharedPointer<ParallelDirScanner>(new ParallelDirScanner());
connect(dirScanner.data(), &ParallelDirScanner::scanComplete, this, &Indexer::dirScanFinished);
connect(dirScanner.data(), &ParallelDirScanner::progress, this, &Indexer::dirScanProgress);
this->db = &db;
}
void Indexer::beginIndexing()
{
this->runningWorkers = 0;
this->currentScanProcessedCount = 0;
this->currentIndexResult = IndexResult();
this->currentIndexResult.begin = QDateTime::currentDateTime();
QVector<QString> dirs;
WildcardMatcher wildcardMatcher;
QStringList ignoreList = this->ignorePattern;
for(QString &excludedPath : Common::excludedPaths())
{
QString pattern = excludedPath;
if(!pattern.endsWith("/"))
{
pattern += "/";
}
pattern += "*";
ignoreList.append(excludedPath);
}
ignoreList.append(this->ignorePattern);
wildcardMatcher.setPatterns(ignoreList);
2022-04-14 14:58:27 +02:00
for(QString &path : this->pathsToScan)
{
if(wildcardMatcher.match(path))
{
continue;
}
2022-04-14 14:58:27 +02:00
QFileInfo info{path};
if(info.isDir())
{
dirs.append(path);
}
else
{
this->filePathTargetsQueue.enqueue(path);
}
}
if(!dirs.empty())
{
this->dirScanner->setPaths(dirs);
this->dirScanner->setIgnorePatterns(ignoreList);
this->dirScanner->scan();
}
2022-04-14 14:58:27 +02:00
2022-04-15 21:06:19 +02:00
this->workerCancellationToken.store(false, std::memory_order_seq_cst);
2022-04-14 14:58:27 +02:00
launchWorker(this->filePathTargetsQueue, this->filePathTargetsQueue.remaining());
}
void Indexer::setIgnorePattern(QStringList ignorePattern)
{
this->ignorePattern = ignorePattern;
}
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;
}
2022-04-15 21:06:19 +02:00
void Indexer::requestCancellation()
{
this->dirScanner->cancel();
this->workerCancellationToken.store(true, std::memory_order_release);
}
2022-04-14 14:58:27 +02:00
IndexResult Indexer::getResult()
{
return this->currentIndexResult;
}
void Indexer::dirScanFinished()
{
Logger::info() << "Dir scan finished" << Qt::endl;
2022-04-15 21:06:19 +02:00
if(!isRunning())
{
emit finished();
}
2022-04-14 14:58:27 +02:00
}
void Indexer::launchWorker(ConcurrentQueue<QString> &queue, int batchsize)
{
2022-04-15 21:06:19 +02:00
FileScanWorker *runnable = new FileScanWorker(*this->db, queue, batchsize, this->workerCancellationToken);
2022-04-14 14:58:27 +02:00
connect(runnable, &FileScanWorker::result, this, &Indexer::processFileScanResult);
connect(runnable, &FileScanWorker::finished, this, &Indexer::processFinishedWorker);
++this->runningWorkers;
QThreadPool::globalInstance()->start(runnable);
}
void Indexer::dirScanProgress(int current, int total)
{
launchWorker(this->dirScanner->getResults(), current);
emit pathsCountChanged(total);
}
void Indexer::processFileScanResult(FileScanResult result)
{
if(result.second != OK || result.second != OK_WASEMPTY || result.second != SKIPPED)
2022-04-14 14:58:27 +02:00
{
this->currentIndexResult.results.append(result);
if(!keepGoing)
{
this->requestCancellation();
emit finished();
return;
}
2022-04-14 14:58:27 +02:00
}
else
{
if(verbose)
2022-04-14 14:58:27 +02:00
{
this->currentIndexResult.results.append(result);
}
}
/* TODO: OK_WASEMPTY might need a special list */
if(result.second == OK || result.second == OK_WASEMPTY)
2022-04-14 14:58:27 +02:00
{
++this->currentIndexResult.addedPaths;
}
else if(result.second == SKIPPED)
{
++this->currentIndexResult.skippedPaths;
}
else
{
++this->currentIndexResult.erroredPaths;
}
if(currentScanProcessedCount++ == progressReportThreshold)
{
emit indexProgress(this->currentIndexResult.total(), this->currentIndexResult.addedPaths,
this->currentIndexResult.skippedPaths, this->currentIndexResult.erroredPaths,
this->dirScanner->pathCount());
currentScanProcessedCount = 0;
}
}
2022-04-15 21:06:19 +02:00
bool Indexer::isRunning()
{
return this->runningWorkers > 0 || this->dirScanner->isRunning();
}
2022-04-14 14:58:27 +02:00
void Indexer::processFinishedWorker()
{
--this->runningWorkers;
2022-04-15 21:06:19 +02:00
if(!isRunning())
2022-04-14 14:58:27 +02:00
{
emit finished();
}
}