File indexing completed on 2024-04-21 07:38:25

0001 /*
0002     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "firstrunindexer.h"
0008 #include "basicindexingjob.h"
0009 #include "fileindexerconfig.h"
0010 #include "filtereddiriterator.h"
0011 
0012 #include "baloodebug.h"
0013 #include "database.h"
0014 #include "transaction.h"
0015 
0016 #include <QMimeDatabase>
0017 
0018 using namespace Baloo;
0019 
0020 FirstRunIndexer::FirstRunIndexer(Database* db, FileIndexerConfig* config, const QStringList& folders)
0021     : m_db(db)
0022     , m_config(config)
0023     , m_folders(folders)
0024 {
0025     Q_ASSERT(m_db);
0026     Q_ASSERT(m_config);
0027     Q_ASSERT(!m_folders.isEmpty());
0028 }
0029 
0030 void FirstRunIndexer::run()
0031 {
0032     QMimeDatabase mimeDb;
0033     BasicIndexingJob::IndexingLevel level = m_config->onlyBasicIndexing() ? BasicIndexingJob::NoLevel
0034         : BasicIndexingJob::MarkForContentIndexing;
0035 
0036     for (const QString& folder : std::as_const(m_folders)) {
0037         Transaction tr(m_db, Transaction::ReadWrite);
0038         int transactionDocumentCount = 0;
0039 
0040         FilteredDirIterator it(m_config, folder);
0041         while (!it.next().isEmpty()) {
0042             QString mimetype;
0043             if (it.fileInfo().isDir()) {
0044                 mimetype = QStringLiteral("inode/directory");
0045             } else {
0046                 mimetype = mimeDb.mimeTypeForFile(it.filePath(), QMimeDatabase::MatchExtension).name();
0047             }
0048 
0049             BasicIndexingJob job(it.filePath(), mimetype, level);
0050             if (!job.index()) {
0051                 continue;
0052             }
0053 
0054             // Even though this is the first run, because 2 hard links will resolve to the same id,
0055             // we land up crashing (due to the asserts in addDocument).
0056             // Hence we are checking before.
0057             // FIXME: Silently ignore hard links!
0058             //
0059             if (tr.hasDocument(job.document().id())) {
0060                 continue;
0061             }
0062             tr.addDocument(job.document());
0063 
0064             transactionDocumentCount++;
0065             if (transactionDocumentCount > 20000) {
0066                 qCDebug(BALOO) << "Commit";
0067                 tr.commit();
0068                 tr.reset(Transaction::ReadWrite);
0069                 transactionDocumentCount = 0;
0070             }
0071         }
0072 
0073         // FIXME: This would consume too much memory. We should make some more commits
0074         //        based on how much memory we consume
0075         tr.commit();
0076     }
0077 
0078     Q_EMIT done();
0079 }
0080 
0081 #include "moc_firstrunindexer.cpp"