shared: Begin DirScanWorker
This commit is contained in:
szülő
56414ee5e2
commit
f3fbf4a1dc
50
shared/dirscanworker.cpp
Normal file
50
shared/dirscanworker.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <QThread>
|
||||
#include "dirscanworker.h"
|
||||
#include "logger.h"
|
||||
DirScanWorker::DirScanWorker(ConcurrentQueue<QString> &queue, ConcurrentQueue<QString> &resultQueue,
|
||||
QStringList ignorePattern, unsigned int progressReportThreshold,
|
||||
std::atomic<bool> &stopToken)
|
||||
{
|
||||
this->queue = &queue;
|
||||
this->resultQueue = &resultQueue;
|
||||
this->ignorePattern = ignorePattern;
|
||||
this->progressReportThreshold = progressReportThreshold;
|
||||
this->stopToken = &stopToken;
|
||||
setAutoDelete(false);
|
||||
}
|
||||
|
||||
void DirScanWorker::run()
|
||||
{
|
||||
unsigned int currentProgress = 0;
|
||||
QString path;
|
||||
/* TODO: if we have e. g. only one path, then only one thread will scan this path.
|
||||
*
|
||||
* Thus, we must resubmit to the queue directories so other threads can help
|
||||
the current one (requires a new logic for threads in ParallelDirScanner). Alterantively,
|
||||
start new DirScanWorkers ourselves here... */
|
||||
while(queue->dequeue(path))
|
||||
{
|
||||
QDirIterator iterator(path, ignorePattern, QDir::Files, QDirIterator::Subdirectories);
|
||||
while(iterator.hasNext())
|
||||
{
|
||||
this->results.append(iterator.next());
|
||||
++currentProgress;
|
||||
if(currentProgress == progressReportThreshold)
|
||||
{
|
||||
if(this->stopToken->load(std::memory_order_relaxed))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
this->resultQueue->enqueue(this->results);
|
||||
emit progress(results.length());
|
||||
currentProgress = 0;
|
||||
this->results.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
this->resultQueue->enqueue(this->results);
|
||||
emit progress(results.length());
|
||||
this->results.clear();
|
||||
emit finished();
|
||||
}
|
31
shared/dirscanworker.h
Normal file
31
shared/dirscanworker.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef DIRSCANWORKER_H
|
||||
#define DIRSCANWORKER_H
|
||||
#include <QObject>
|
||||
#include <QRunnable>
|
||||
#include <QDirIterator>
|
||||
#include "concurrentqueue.h"
|
||||
class DirScanWorker : public QObject, public QRunnable
|
||||
{
|
||||
Q_OBJECT
|
||||
protected:
|
||||
unsigned int progressReportThreshold = 1000;
|
||||
ConcurrentQueue<QString> *queue = nullptr;
|
||||
ConcurrentQueue<QString> *resultQueue = nullptr;
|
||||
|
||||
QStringList ignorePattern;
|
||||
QVector<QString> results;
|
||||
|
||||
std::atomic<bool> *stopToken;
|
||||
|
||||
public:
|
||||
DirScanWorker(ConcurrentQueue<QString> &queue, ConcurrentQueue<QString> &resultQueue, QStringList ignorePattern,
|
||||
unsigned int progressReportThreshold, std::atomic<bool> &stopToken);
|
||||
|
||||
void run() override;
|
||||
|
||||
signals:
|
||||
void progress(unsigned int);
|
||||
void finished();
|
||||
};
|
||||
|
||||
#endif // DIRSCANWORKER_H
|
Betöltés…
Reference in New Issue
Block a user