File indexing completed on 2024-05-05 03:52:20

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 /**
0009  * WARNING: make sure baloo_file is not running before running this test
0010  * otherwise we'll have two baloo_file_extractor processes running and that will
0011  * cause problems when registering the monitor.
0012  */
0013 
0014 #include "database.h"
0015 #include "firstrunindexer.h"
0016 #include "filecontentindexerprovider.h"
0017 #include "filecontentindexer.h"
0018 #include "fileindexerconfig.h"
0019 #include "timeestimator.h"
0020 #include "extractor_interface.h"
0021 
0022 #include <QCoreApplication>
0023 #include <QThreadPool>
0024 #include <QTemporaryDir>
0025 #include <QObject>
0026 #include <QDBusConnection>
0027 #include <QTextStream>
0028 #include <QString>
0029 #include <QDBusServiceWatcher>
0030 
0031 #include <iostream>
0032 
0033 namespace org {
0034     namespace kde {
0035         namespace baloo {
0036             typedef OrgKdeBalooExtractorInterface extractorInterface;
0037         }
0038     }
0039 }
0040 
0041 class Scheduler : public QObject
0042 {
0043     Q_OBJECT
0044 
0045 public:
0046     explicit Scheduler(QObject* parent = 0);
0047     void startIndexing();
0048 
0049 private Q_SLOTS:
0050     void startContentIndexer();
0051     void printTime();
0052     void finished();
0053     void registerMonitor(const QString& service);
0054 
0055 private:
0056     QTemporaryDir m_dir;
0057 
0058     Baloo::Database m_db;
0059     Baloo::FileContentIndexerProvider m_provider;
0060     Baloo::FileIndexerConfig* m_config;
0061     Baloo::FileContentIndexer* m_contentRunnable;
0062     org::kde::baloo::extractorInterface* m_extractorInterface;
0063 
0064     QThreadPool m_pool;
0065 
0066     int m_count;
0067     int m_batchSize;
0068 
0069     QTextStream m_out;
0070 };
0071 
0072 Scheduler::Scheduler(QObject* parent)
0073     : QObject(parent)
0074     , m_db(m_dir.path())
0075     , m_provider(&m_db)
0076     , m_contentRunnable(0)
0077     , m_count(0)
0078     , m_batchSize(40)
0079     , m_out(stdout)
0080 {
0081     m_db.open(Baloo::Database::CreateDatabase);
0082     m_pool.setMaxThreadCount(1);
0083 
0084     QString extractorService = QStringLiteral("org.kde.baloo.extractor");
0085     m_extractorInterface = new org::kde::baloo::extractorInterface(extractorService,
0086                                                             QStringLiteral("/extractor"),
0087                                                             QDBusConnection::sessionBus(),
0088                                                             this);
0089 
0090     connect(m_extractorInterface, &org::kde::baloo::extractorInterface::currentUrlChanged,
0091             this, &Scheduler::printTime);
0092 
0093     QDBusServiceWatcher* extractorWatcher = new QDBusServiceWatcher(extractorService,
0094                                                             QDBusConnection::sessionBus(),
0095                                                             QDBusServiceWatcher::WatchForRegistration,
0096                                                             this);
0097 
0098     connect(extractorWatcher, &QDBusServiceWatcher::serviceRegistered, this, &Scheduler::registerMonitor);
0099 
0100     // Set test path
0101     qputenv("BALOO_DB_PATH", m_dir.path().toUtf8());
0102 
0103     QStandardPaths::setTestModeEnabled(true);
0104     QString testConfigPath = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) +
0105                                                                             QStringLiteral("baloofilerc");
0106     // Remove config from previous runs
0107     QFile::remove(testConfigPath);
0108 
0109     m_config = new Baloo::FileIndexerConfig(this);
0110     m_config->setInitialRun(true);
0111 }
0112 
0113 void Scheduler::startIndexing()
0114 {
0115     auto firstRunnable = new Baloo::FirstRunIndexer(&m_db, m_config, m_config->includeFolders());
0116     connect(firstRunnable, &Baloo::FirstRunIndexer::done, this, &Scheduler::startContentIndexer);
0117     m_pool.start(firstRunnable);
0118 }
0119 
0120 void Scheduler::startContentIndexer()
0121 {
0122     m_contentRunnable = new Baloo::FileContentIndexer(&m_provider);
0123     connect(m_contentRunnable, &Baloo::FileContentIndexer::done, this, &Scheduler::finished);
0124     m_pool.start(m_contentRunnable);
0125 }
0126 
0127 void Scheduler::printTime()
0128 {
0129     Q_ASSERT(m_contentRunnable != 0);
0130 
0131     if (++m_count == 10 * m_batchSize) {
0132         Baloo::TimeEstimator estimator;
0133         estimator.setBatchTimings(m_contentRunnable->batchTimings());
0134         estimator.setFilesLeft(m_provider.size());
0135         // print Remaining time after every 10 batches
0136         m_out <<  "Remaining Time: " << estimator.calculateTimeLeft() << endl;
0137         m_count = 0;
0138     }
0139 }
0140 
0141 void Scheduler::finished()
0142 {
0143     m_out << "done!" << endl;
0144     QCoreApplication::exit();
0145 }
0146 
0147 void Scheduler::registerMonitor(const QString& service)
0148 {
0149     Q_UNUSED(service);
0150     m_extractorInterface->registerMonitor();
0151 }
0152 
0153 int main (int argc, char** argv)
0154 {
0155     QCoreApplication app(argc, argv);
0156 
0157     Scheduler sched;
0158     sched.startIndexing();
0159     app.exec();
0160 }
0161 
0162 #include "remainingtimetest.moc"