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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2015-06-01
* Description : DB Jobs thread for listing and scanning
*
* SPDX-FileCopyrightText: 2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "dbjobsthread.h"
// Local includes
#include "coredbaccess.h"
#include "duplicatesprogressobserver.h"
#include "digikam_debug.h"
namespace Digikam
{
DBJobsThread::DBJobsThread(QObject* const parent)
: ActionThreadBase(parent)
{
setObjectName(QLatin1String("DBJobsThread"));
}
bool DBJobsThread::hasErrors()
{
return !m_errorsList.isEmpty();
}
QList<QString>& DBJobsThread::errorsList()
{
return m_errorsList;
}
void DBJobsThread::connectFinishAndErrorSignals(DBJob* const j)
{
connect(j, SIGNAL(signalDone()),
this, SIGNAL(finished()));
connect(j, SIGNAL(error(QString)),
this, SLOT(error(QString)));
}
void DBJobsThread::error(const QString& errString)
{
m_errorsList.append(errString);
}
// -------------------------------------------------
AlbumsDBJobsThread::AlbumsDBJobsThread(QObject* const parent)
: DBJobsThread(parent)
{
}
void AlbumsDBJobsThread::albumsListing(const AlbumsDBJobInfo& info)
{
AlbumsJob* const j = new AlbumsJob(info);
connectFinishAndErrorSignals(j);
if (info.isFoldersJob())
{
connect(j, SIGNAL(foldersData(QHash<int,int>)),
this, SIGNAL(foldersData(QHash<int,int>)));
}
else
{
connect(j, SIGNAL(data(QList<ItemListerRecord>)),
this, SIGNAL(data(QList<ItemListerRecord>)));
}
ActionJobCollection collection;
collection.insert(j, 0);
appendJobs(collection);
}
// -------------------------------------------------
TagsDBJobsThread::TagsDBJobsThread(QObject* const parent)
: DBJobsThread(parent)
{
}
void TagsDBJobsThread::tagsListing(const TagsDBJobInfo& info)
{
TagsJob* const j = new TagsJob(info);
connectFinishAndErrorSignals(j);
if (info.isFoldersJob())
{
connect(j, SIGNAL(foldersData(QHash<int,int>)),
this, SIGNAL(foldersData(QHash<int,int>)));
}
else if (info.isFaceFoldersJob())
{
connect(j, SIGNAL(faceFoldersData(QMap<QString,QHash<int,int> >)), // krazy:exclude=normalize
this, SIGNAL(faceFoldersData(QMap<QString,QHash<int,int> >))); // krazy:exclude=normalize
}
else
{
connect(j, SIGNAL(data(QList<ItemListerRecord>)),
this, SIGNAL(data(QList<ItemListerRecord>)));
}
ActionJobCollection collection;
collection.insert(j, 0);
appendJobs(collection);
}
// -------------------------------------------------
DatesDBJobsThread::DatesDBJobsThread(QObject* const parent)
: DBJobsThread(parent)
{
}
void DatesDBJobsThread::datesListing(const DatesDBJobInfo& info)
{
DatesJob* const j = new DatesJob(info);
connectFinishAndErrorSignals(j);
if (info.isFoldersJob())
{
connect(j, SIGNAL(foldersData(QHash<QDateTime,int>)),
this, SIGNAL(foldersData(QHash<QDateTime,int>)));
}
else
{
connect(j, SIGNAL(data(QList<ItemListerRecord>)),
this, SIGNAL(data(QList<ItemListerRecord>)));
}
ActionJobCollection collection;
collection.insert(j, 0);
appendJobs(collection);
}
// -------------------------------------------------
GPSDBJobsThread::GPSDBJobsThread(QObject* const parent)
: DBJobsThread(parent)
{
}
void GPSDBJobsThread::GPSListing(const GPSDBJobInfo& info)
{
GPSJob* const j = new GPSJob(info);
connectFinishAndErrorSignals(j);
if (info.isDirectQuery())
{
connect(j, SIGNAL(directQueryData(QList<QVariant>)),
this, SIGNAL(directQueryData(QList<QVariant>)));
}
else
{
connect(j, SIGNAL(data(QList<ItemListerRecord>)),
this, SIGNAL(data(QList<ItemListerRecord>)));
}
ActionJobCollection collection;
collection.insert(j, 0);
appendJobs(collection);
}
// -------------------------------------------------
SearchesDBJobsThread::SearchesDBJobsThread(QObject* const parent)
: DBJobsThread(parent)
{
}
void SearchesDBJobsThread::searchesListing(const SearchesDBJobInfo& info)
{
ActionJobCollection collection;
if (info.isDuplicatesJob())
{
m_results.clear();
m_haarIface.reset(new HaarIface(info.imageIds()));
m_isAlbumUpdate = info.isAlbumUpdate();
m_processedImages = 0;
m_totalImages2Scan = info.imageIds().count();
const int threadsCount = (m_totalImages2Scan < 200) ? 1 : qMax(1, maximumNumberOfThreads());
const int images2ScanPerThread = m_totalImages2Scan / threadsCount;
QSet<qlonglong>::const_iterator begin = info.imageIds().constBegin();
QSet<qlonglong>::const_iterator end = info.imageIds().constBegin();
// Split job on multiple threads
for (int i = 0 ; i < threadsCount ; ++i)
{
// The last thread should read until the end of the list.
if (i == threadsCount - 1)
{
end = info.imageIds().constEnd();
}
else
{
// TODO: port to std::advance https://en.cppreference.com/w/cpp/iterator/advance
for (int j = 0 ; ((end != info.imageIds().constEnd()) && (j < images2ScanPerThread)) ; ++j, ++end);
}
SearchesJob* const job = new SearchesJob(info, begin, end, m_haarIface.data());<--- Variable 'job' can be declared as pointer to const
begin = end;
connect(job, &SearchesJob::signalDuplicatesResults,
this, &SearchesDBJobsThread::slotDuplicatesResults);
connect(job, &SearchesJob::signalImageProcessed,
this, &SearchesDBJobsThread::slotImageProcessed);
collection.insert(job, 0);
}
}
else
{
SearchesJob* const job = new SearchesJob(info);
connectFinishAndErrorSignals(job);
connect(job, SIGNAL(data(QList<ItemListerRecord>)),
this, SIGNAL(data(QList<ItemListerRecord>)));
collection.insert(job, 0);
}
appendJobs(collection);
}
void SearchesDBJobsThread::slotImageProcessed(const ItemInfo& inf, const QImage& img, int dup)
{
Q_EMIT signalProgress((++m_processedImages * 100) / m_totalImages2Scan, inf, img, dup);
}
void SearchesDBJobsThread::slotDuplicatesResults(const HaarIface::DuplicatesResultsMap& incoming)
{
auto searchResults = [&](qlonglong imageId) -> HaarIface::DuplicatesResultsMap::iterator
{
for (auto it = m_results.begin() ; it != m_results.end() ; ++it)
{
if ((imageId == it.key()) || (it->second.contains(imageId)))
{
return it;
}
}
return m_results.end();
};
const auto keys = incoming.keys();
for (const auto& referenceImage : keys)
{
if (searchResults(referenceImage) == m_results.end())
{
m_results.insert(referenceImage, incoming.value(referenceImage));
}
}
if (m_processedImages != m_totalImages2Scan)
{
return;
}
HaarIface::rebuildDuplicatesAlbums(m_results, m_isAlbumUpdate);
Q_EMIT finished();
}
} // namespace Digikam
#include "moc_dbjobsthread.cpp"
|