1/*
2 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "firstrunindexer.h"
8#include "basicindexingjob.h"
9#include "fileindexerconfig.h"
10#include "filtereddiriterator.h"
11
12#include "baloodebug.h"
13#include "database.h"
14#include "transaction.h"
15
16#include <QMimeDatabase>
17
18using namespace Baloo;
19
20FirstRunIndexer::FirstRunIndexer(Database* db, FileIndexerConfig* config, const QStringList& folders)
21 : m_db(db)
22 , m_config(config)
23 , m_folders(folders)
24{
25 Q_ASSERT(m_db);
26 Q_ASSERT(m_config);
27 Q_ASSERT(!m_folders.isEmpty());
28}
29
30void FirstRunIndexer::run()
31{
32 QMimeDatabase mimeDb;
33 BasicIndexingJob::IndexingLevel level = m_config->onlyBasicIndexing() ? BasicIndexingJob::NoLevel
34 : BasicIndexingJob::MarkForContentIndexing;
35
36 for (const QString& folder : std::as_const(t&: m_folders)) {
37 Transaction tr(m_db, Transaction::ReadWrite);
38 int transactionDocumentCount = 0;
39
40 FilteredDirIterator it(m_config, folder);
41 while (!it.next().isEmpty()) {
42 QString mimetype;
43 if (it.fileInfo().isDir()) {
44 mimetype = QStringLiteral("inode/directory");
45 } else {
46 mimetype = mimeDb.mimeTypeForFile(fileName: it.filePath(), mode: QMimeDatabase::MatchExtension).name();
47 }
48
49 BasicIndexingJob job(it.filePath(), mimetype, level);
50 if (!job.index()) {
51 continue;
52 }
53
54 // Even though this is the first run, because 2 hard links will resolve to the same id,
55 // we land up crashing (due to the asserts in addDocument).
56 // Hence we are checking before.
57 // FIXME: Silently ignore hard links!
58 //
59 if (tr.hasDocument(id: job.document().id())) {
60 continue;
61 }
62 tr.addDocument(doc: job.document());
63
64 transactionDocumentCount++;
65 if (transactionDocumentCount > 20000) {
66 qCDebug(BALOO) << "Commit";
67 tr.commit();
68 tr.reset(type: Transaction::ReadWrite);
69 transactionDocumentCount = 0;
70 }
71 }
72
73 // FIXME: This would consume too much memory. We should make some more commits
74 // based on how much memory we consume
75 tr.commit();
76 }
77
78 Q_EMIT done();
79}
80
81#include "moc_firstrunindexer.cpp"
82

source code of baloo/src/file/firstrunindexer.cpp