-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonfileshandler.cpp
80 lines (75 loc) · 3.58 KB
/
jsonfileshandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Copyright (C) 2020 brian DOT l DOT miller DOT ttu AT gmail DOT com
* This file is part of QCovidTracker.
*
* This program comes with ABSOLUTELY NO WARRANTY
* QCovidTracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QCovidTracker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QCovidTracker. If not, see <https://www.gnu.org/licenses/.
*/
#include "jsonfileshandler.h"
#include <QDir>
#include <QStringList>
#include <QDebug>
#include <QThreadPool>
#include "handlejsonfilesrunnable.h"
JsonFilesHandler::JsonFilesHandler()
{
}
void JsonFilesHandler::readJsonFilesDir(QString dir) {
QDir directory(dir);
QStringList jsonFiles = directory.entryList(QStringList() << "*.json", QDir::Files);
emit updateNumberOfFiles(jsonFiles.size());
QThreadPool *threadpool = new QThreadPool();
int maxThreads = threadpool->maxThreadCount();
int filesPerThread = jsonFiles.size()/maxThreads;
int lastThreadCount = jsonFiles.size() - maxThreads*filesPerThread;
int threadNumber{0};
/* There might be an optimum number of files here in which the overhead in creating
* threads outweighs just using one
* For example splitting 80 files into 4 threads might be slower than just using
* one thread; don't know if this can be done in general, or if there's a heuristic
* argument
*/
if (jsonFiles.size() != 0 && jsonFiles.size() <= maxThreads) {
HandleJsonFilesRunnable *jsonFileRunnable = new HandleJsonFilesRunnable();
jsonFileRunnable->setDbName(QString::number(threadNumber));
jsonFileRunnable->setJsonDirectory(dir);
jsonFileRunnable->setBeginningAndEnd(0, jsonFiles.size());
jsonFileRunnable->setJsonFileList(jsonFiles);
connect(jsonFileRunnable, SIGNAL(sendStatusMessage(QString)), this, SLOT(jsonReceiveStatusMessage(QString)));
threadpool->waitForDone();
//TODO emit signal here
}
else if (jsonFiles.size() != 0) {
for (threadNumber = 0; threadNumber < maxThreads; threadNumber++) {
HandleJsonFilesRunnable *jsonFileRunnable = new HandleJsonFilesRunnable();
jsonFileRunnable->setDbName(QString::number(threadNumber));
jsonFileRunnable->setJsonDirectory(dir);
if (threadNumber == maxThreads -1 ) {
jsonFileRunnable->setBeginningAndEnd(threadNumber*filesPerThread, threadNumber*filesPerThread + filesPerThread + lastThreadCount);
}
else {
jsonFileRunnable->setBeginningAndEnd(threadNumber*filesPerThread, threadNumber*filesPerThread + filesPerThread);
}
jsonFileRunnable->setJsonFileList(jsonFiles);
connect(jsonFileRunnable, SIGNAL(sendStatusMessage(QString)), this, SLOT(jsonReceiveStatusMessage(QString)));
threadpool->start(jsonFileRunnable);
}
//We're in a thread outside of the main event loop, waiting here won't slow the GUI
threadpool->waitForDone();
emit threadsAreDone();
}
}
void JsonFilesHandler::jsonReceiveStatusMessage(QString message) {
emit jsonSendStatusMessage(message);
}