| 1 | /* |
| 2 | This file is part of the KDE Baloo project. |
| 3 | SPDX-FileCopyrightText: 2015 Vishesh Handa <me@vhanda.in> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifndef BALOO_POSTINGDB_H |
| 9 | #define BALOO_POSTINGDB_H |
| 10 | |
| 11 | #include "postingiterator.h" |
| 12 | |
| 13 | #include <QByteArray> |
| 14 | #include <QVector> |
| 15 | #include <QRegularExpression> |
| 16 | |
| 17 | #include <lmdb.h> |
| 18 | |
| 19 | namespace Baloo { |
| 20 | |
| 21 | typedef QVector<quint64> PostingList; |
| 22 | |
| 23 | /** |
| 24 | * The PostingDB is the main database that maps <term> -> <id1> <id2> <id2> ... |
| 25 | * This is used to lookup ids when searching for a <term>. |
| 26 | */ |
| 27 | class BALOO_ENGINE_EXPORT PostingDB |
| 28 | { |
| 29 | public: |
| 30 | PostingDB(MDB_dbi, MDB_txn* txn); |
| 31 | ~PostingDB(); |
| 32 | |
| 33 | static MDB_dbi create(MDB_txn* txn); |
| 34 | static MDB_dbi open(MDB_txn* txn); |
| 35 | |
| 36 | void put(const QByteArray& term, const PostingList& list); |
| 37 | PostingList get(const QByteArray& term); |
| 38 | void del(const QByteArray& term); |
| 39 | |
| 40 | PostingIterator* iter(const QByteArray& term); |
| 41 | PostingIterator* prefixIter(const QByteArray& term); |
| 42 | PostingIterator* regexpIter(const QRegularExpression& regexp, const QByteArray& prefix); |
| 43 | |
| 44 | enum Comparator { |
| 45 | LessEqual, |
| 46 | GreaterEqual, |
| 47 | }; |
| 48 | // For integral types only: |
| 49 | template<typename T> |
| 50 | typename std::enable_if<std::is_integral<T>::value, PostingIterator*>::type |
| 51 | compIter(const QByteArray& prefix, T val, Comparator com) { |
| 52 | qlonglong l = val; |
| 53 | return compIter(prefix, val: l, com); |
| 54 | } |
| 55 | PostingIterator* compIter(const QByteArray& prefix, qlonglong val, Comparator com); |
| 56 | PostingIterator* compIter(const QByteArray& prefix, double val, Comparator com); |
| 57 | PostingIterator* compIter(const QByteArray& prefix, const QByteArray& val, Comparator com); |
| 58 | |
| 59 | QVector<QByteArray> fetchTermsStartingWith(const QByteArray& term); |
| 60 | |
| 61 | QMap<QByteArray, PostingList> toTestMap() const; |
| 62 | private: |
| 63 | template <typename Validator> |
| 64 | PostingIterator* iter(const QByteArray& prefix, Validator validate); |
| 65 | |
| 66 | MDB_txn* m_txn; |
| 67 | MDB_dbi m_dbi; |
| 68 | }; |
| 69 | |
| 70 | } |
| 71 | |
| 72 | #endif // BALOO_POSTINGDB_H |
| 73 | |