From f3fbf4a1dcf648bc1e81a1f1c72cbee1f675550d Mon Sep 17 00:00:00 2001 From: Albert S Date: Thu, 14 Apr 2022 14:57:30 +0200 Subject: [PATCH] shared: Begin DirScanWorker --- shared/dirscanworker.cpp | 50 ++++++++++++++++++++++++++++++++++++++++ shared/dirscanworker.h | 31 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 shared/dirscanworker.cpp create mode 100644 shared/dirscanworker.h diff --git a/shared/dirscanworker.cpp b/shared/dirscanworker.cpp new file mode 100644 index 0000000..16bb316 --- /dev/null +++ b/shared/dirscanworker.cpp @@ -0,0 +1,50 @@ +#include +#include "dirscanworker.h" +#include "logger.h" +DirScanWorker::DirScanWorker(ConcurrentQueue &queue, ConcurrentQueue &resultQueue, + QStringList ignorePattern, unsigned int progressReportThreshold, + std::atomic &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(); +} diff --git a/shared/dirscanworker.h b/shared/dirscanworker.h new file mode 100644 index 0000000..213ec7d --- /dev/null +++ b/shared/dirscanworker.h @@ -0,0 +1,31 @@ +#ifndef DIRSCANWORKER_H +#define DIRSCANWORKER_H +#include +#include +#include +#include "concurrentqueue.h" +class DirScanWorker : public QObject, public QRunnable +{ + Q_OBJECT + protected: + unsigned int progressReportThreshold = 1000; + ConcurrentQueue *queue = nullptr; + ConcurrentQueue *resultQueue = nullptr; + + QStringList ignorePattern; + QVector results; + + std::atomic *stopToken; + + public: + DirScanWorker(ConcurrentQueue &queue, ConcurrentQueue &resultQueue, QStringList ignorePattern, + unsigned int progressReportThreshold, std::atomic &stopToken); + + void run() override; + + signals: + void progress(unsigned int); + void finished(); +}; + +#endif // DIRSCANWORKER_H