File indexing completed on 2024-12-22 04:53:02

0001 /*
0002  * This file is part of the KDE Akonadi Search Project
0003  * SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  *
0007  */
0008 
0009 #include <QCommandLineOption>
0010 #include <QCommandLineParser>
0011 #include <QCoreApplication>
0012 #include <QDebug>
0013 #include <QDir>
0014 #include <QFileInfo>
0015 #include <QTemporaryDir>
0016 #include <QUuid>
0017 
0018 #include "xapiandatabase.h"
0019 #include "xapiandocument.h"
0020 
0021 int main(int argc, char **argv)
0022 {
0023     QCoreApplication app(argc, argv);
0024 
0025     QCommandLineParser parser;
0026     parser.addPositionalArgument(QStringLiteral("num"), QStringLiteral("The number of terms. Each term is of length 10"));
0027     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("p") << QStringLiteral("position"), QStringLiteral("Add positional information")));
0028     parser.addHelpOption();
0029     parser.process(app);
0030 
0031     const QStringList args = parser.positionalArguments();
0032     if (args.size() != 1) {
0033         parser.showHelp(1);
0034     }
0035 
0036     QTemporaryDir tempDir;
0037     tempDir.setAutoRemove(false);
0038 
0039     Akonadi::Search::XapianDatabase db(tempDir.path(), true);
0040 
0041     qDebug() << tempDir.path();
0042     qDebug() << "Creating the document";
0043 
0044     Akonadi::Search::XapianDocument doc;
0045     const int size = args.first().toInt();
0046 
0047     for (int i = 0; i < size; i++) {
0048         const QByteArray term = QUuid::createUuid().toByteArray().mid(1, 10);
0049 
0050         if (parser.isSet(QStringLiteral("p"))) {
0051             const std::string stdString(term.constData(), term.length());
0052             doc.doc().add_posting(stdString, i);
0053         } else {
0054             doc.addTerm(QString::fromUtf8(term));
0055         }
0056     }
0057 
0058     db.replaceDocument(1, doc);
0059     db.commit();
0060 
0061     int dbSize = 0;
0062     const QDir dbDir(tempDir.path());
0063     const auto entryInfoList = dbDir.entryInfoList(QDir::Files);
0064     for (const QFileInfo &file : entryInfoList) {
0065         qDebug() << file.fileName() << file.size() / 1024 << "kb";
0066         dbSize += file.size();
0067     }
0068     qDebug() << "Database Size:" << dbSize / 1024 << "kb";
0069 
0070     return app.exec();
0071 }