File indexing completed on 2024-04-28 03:51:39

0001 /*
0002     This file is part of the KDE Baloo project.
0003     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "postingdb.h"
0009 #include "singledbtest.h"
0010 
0011 using namespace Baloo;
0012 
0013 class PostingDBTest : public SingleDBTest
0014 {
0015     Q_OBJECT
0016 private Q_SLOTS:
0017     void test() {
0018         PostingDB db(PostingDB::create(m_txn), m_txn);
0019 
0020         QByteArray word("fire");
0021         PostingList list = {1, 5, 6};
0022 
0023         db.put(word, list);
0024         QCOMPARE(db.get(word), list);
0025     }
0026 
0027     void testTermIter() {
0028         PostingDB db(PostingDB::create(m_txn), m_txn);
0029 
0030         db.put("abc", {1, 4, 5, 9, 11});
0031         db.put("fir", {1, 3, 5});
0032         db.put("fire", {1, 8, 9});
0033         db.put("fore", {2, 3, 5});
0034 
0035         std::unique_ptr<PostingIterator> it{db.iter("fir")};
0036         QVERIFY(it);
0037 
0038         QVector<quint64> result = {1, 3, 5};
0039         for (quint64 val : result) {
0040             QCOMPARE(it->next(), static_cast<quint64>(val));
0041             QCOMPARE(it->docId(), static_cast<quint64>(val));
0042         }
0043     }
0044 
0045     void testPrefixIter() {
0046         PostingDB db(PostingDB::create(m_txn), m_txn);
0047 
0048         db.put("abc", {1, 4, 5, 9, 11});
0049         db.put("fir", {1, 3, 5});
0050         db.put("fire", {1, 8, 9});
0051         db.put("fore", {2, 3, 5});
0052 
0053         std::unique_ptr<PostingIterator> it{db.prefixIter("fi")};
0054         QVERIFY(it);
0055 
0056         QVector<quint64> result = {1, 3, 5, 8, 9};
0057         for (quint64 val : result) {
0058             QCOMPARE(it->next(), static_cast<quint64>(val));
0059             QCOMPARE(it->docId(), static_cast<quint64>(val));
0060         }
0061     }
0062 
0063     void testRegExpIter() {
0064         PostingDB db(PostingDB::create(m_txn), m_txn);
0065 
0066         db.put("abc", {1, 4, 5, 9, 11});
0067         db.put("fir", {1, 3, 5, 7});
0068         db.put("fire", {1, 8});
0069         db.put("fore", {2, 3, 5});
0070         db.put("zib", {4, 5, 6});
0071 
0072         std::unique_ptr<PostingIterator> it{db.regexpIter(QRegularExpression(QStringLiteral(".re")), QByteArray("f"))};
0073         QVERIFY(it);
0074 
0075         QVector<quint64> result = {1, 2, 3, 5, 8};
0076         for (quint64 val : result) {
0077             QCOMPARE(it->next(), static_cast<quint64>(val));
0078             QCOMPARE(it->docId(), static_cast<quint64>(val));
0079         }
0080 
0081         // Non existing
0082         it.reset(db.regexpIter(QRegularExpression(QStringLiteral("dub")), QByteArray("f")));
0083         QVERIFY(it == nullptr);
0084     }
0085 
0086     void testCompIter() {
0087         PostingDB db(PostingDB::create(m_txn), m_txn);
0088 
0089         db.put("abc", {1, 4, 5, 9, 11});
0090         db.put("R1", {1, 3, 5, 7});
0091         db.put("R2", {1, 8});
0092         db.put("R3", {2, 3, 5});
0093         db.put("R10", {10, 12});
0094         // X20- is the internal encoding for KFileMetaData::Property::LineCount
0095         db.put("X20-90", {1, 2});
0096         db.put("X20-1000", {10, 11});
0097 
0098         std::unique_ptr<PostingIterator> it{db.compIter("R", 2, PostingDB::GreaterEqual)};
0099         QVERIFY(it);
0100 
0101         QVector<quint64> result = {1, 2, 3, 5, 8, 10, 12};
0102         for (quint64 val : std::as_const(result)) {
0103             QCOMPARE(it->next(), static_cast<quint64>(val));
0104             QCOMPARE(it->docId(), static_cast<quint64>(val));
0105         }
0106 
0107         it.reset(db.compIter("R", 2, PostingDB::LessEqual));
0108         QVERIFY(it);
0109         result = {1, 3, 5, 7, 8};
0110         for (quint64 val : std::as_const(result)) {
0111             QCOMPARE(it->next(), static_cast<quint64>(val));
0112             QCOMPARE(it->docId(), static_cast<quint64>(val));
0113         }
0114 
0115         it.reset(db.compIter("R", 10, PostingDB::GreaterEqual));
0116         QVERIFY(it);
0117         result = {10, 12};
0118         for (quint64 val : std::as_const(result)) {
0119             QCOMPARE(it->next(), static_cast<quint64>(val));
0120             QCOMPARE(it->docId(), static_cast<quint64>(val));
0121         }
0122 
0123         it.reset(db.compIter("X20-", 80, PostingDB::GreaterEqual));
0124         QVERIFY(it);
0125         result = {1, 2, 10, 11};
0126         for (quint64 val : std::as_const(result)) {
0127             QCOMPARE(it->next(), static_cast<quint64>(val));
0128             QCOMPARE(it->docId(), static_cast<quint64>(val));
0129         }
0130 
0131         it.reset(db.compIter("X20-", 100, PostingDB::GreaterEqual));
0132         QVERIFY(it);
0133         result = {10, 11};
0134         for (quint64 val : std::as_const(result)) {
0135             QCOMPARE(it->next(), static_cast<quint64>(val));
0136             QCOMPARE(it->docId(), static_cast<quint64>(val));
0137         }
0138     }
0139 
0140     void testFetchTermsStartingWith() {
0141         PostingDB db(PostingDB::create(m_txn), m_txn);
0142 
0143         db.put("abc", {1, 4, 5, 9, 11});
0144         db.put("fir", {1, 3, 5, 7});
0145         db.put("fire", {1, 8});
0146         db.put("fore", {2, 3, 5});
0147         db.put("zib", {4, 5, 6});
0148 
0149         QVector<QByteArray> list = {"fir", "fire", "fore"};
0150         QCOMPARE(db.fetchTermsStartingWith("f"), list);
0151     }
0152 };
0153 
0154 QTEST_MAIN(PostingDBTest)
0155 
0156 #include "postingdbtest.moc"