8#include "enginedebug.h"
10#include "orpostingiterator.h"
11#include "postingcodec.h"
15PostingDB::PostingDB(MDB_dbi dbi, MDB_txn* txn)
19 Q_ASSERT(txn !=
nullptr);
23PostingDB::~PostingDB()
27MDB_dbi PostingDB::create(MDB_txn* txn)
30 int rc = mdb_dbi_open(txn,
"postingdb", MDB_CREATE, &dbi);
32 qCWarning(ENGINE) <<
"PostingDB::create" << mdb_strerror(rc);
39MDB_dbi PostingDB::open(MDB_txn* txn)
42 int rc = mdb_dbi_open(txn,
"postingdb", 0, &dbi);
44 qCWarning(ENGINE) <<
"PostingDB::open" << mdb_strerror(rc);
57 key.mv_size = term.
size();
58 key.mv_data =
static_cast<void*
>(
const_cast<char*
>(term.
constData()));
63 val.mv_size = arr.
size();
64 val.mv_data =
static_cast<void*
>(arr.
data());
66 int rc = mdb_put(m_txn, m_dbi, &key, &val, 0);
68 qCWarning(ENGINE) <<
"PostingDB::put" << mdb_strerror(rc);
77 key.mv_size = term.
size();
78 key.mv_data =
static_cast<void*
>(
const_cast<char*
>(term.
constData()));
80 MDB_val val{0,
nullptr};
81 int rc = mdb_get(m_txn, m_dbi, &key, &val);
83 if (rc != MDB_NOTFOUND) {
84 qCDebug(ENGINE) <<
"PostingDB::get" << term << mdb_strerror(rc);
91 return PostingCodec::decode(arr);
99 key.mv_size = term.
size();
100 key.mv_data =
static_cast<void*
>(
const_cast<char*
>(term.
constData()));
102 int rc = mdb_del(m_txn, m_dbi, &key,
nullptr);
103 if (rc != 0 && rc != MDB_NOTFOUND) {
104 qCDebug(ENGINE) <<
"PostingDB::del" << term << mdb_strerror(rc);
111 key.mv_size = term.
size();
112 key.mv_data =
static_cast<void*
>(
const_cast<char*
>(term.
constData()));
115 int rc = mdb_cursor_open(m_txn, m_dbi, &cursor);
117 qCWarning(ENGINE) <<
"PostingDB::fetchTermsStartingWith" << mdb_strerror(rc);
122 rc = mdb_cursor_get(cursor, &key,
nullptr, MDB_SET_RANGE);
124 const QByteArray arr(
static_cast<char*
>(key.mv_data), key.mv_size);
129 rc = mdb_cursor_get(cursor, &key,
nullptr, MDB_NEXT);
131 if (rc != MDB_NOTFOUND) {
132 qCDebug(ENGINE) <<
"PostingDB::fetchTermsStartingWith" << mdb_strerror(rc);
135 mdb_cursor_close(cursor);
141 DBPostingIterator(
void* data, uint size);
142 quint64 docId()
const override;
143 quint64
next()
override;
153 key.mv_size = term.
size();
154 key.mv_data =
static_cast<void*
>(
const_cast<char*
>(term.
constData()));
157 int rc = mdb_get(m_txn, m_dbi, &key, &val);
159 qCDebug(ENGINE) <<
"PostingDB::iter" << term << mdb_strerror(rc);
163 return new DBPostingIterator(val.mv_data, val.mv_size);
169DBPostingIterator::DBPostingIterator(
void* data, uint size)
170 : m_vec(PostingCodec().decode(
QByteArray(static_cast<char*>(data), size)))
175quint64 DBPostingIterator::docId()
const
177 if (m_pos < 0 || m_pos >= m_vec.size()) {
184quint64 DBPostingIterator::next()
186 if (m_pos >= m_vec.size() - 1) {
187 m_pos = m_vec.size();
195template <
typename Val
idator>
198 Q_ASSERT(!prefix.isEmpty());
201 key.mv_size = prefix.size();
202 key.mv_data =
static_cast<void*
>(
const_cast<char*
>(prefix.constData()));
205 int rc = mdb_cursor_open(m_txn, m_dbi, &cursor);
208 qCWarning(ENGINE) <<
"PostingDB::regexpIter" << mdb_strerror(rc);
215 rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE);
217 const QByteArray arr(
static_cast<char*
>(key.mv_data), key.mv_size);
222 termIterators <<
new DBPostingIterator(val.mv_data, val.mv_size);
224 rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT);
227 if (rc != 0 && rc != MDB_NOTFOUND) {
228 qCWarning(ENGINE) <<
"PostingDB::regexpIter" << mdb_strerror(rc);
231 mdb_cursor_close(cursor);
235 return new OrPostingIterator(termIterators);
244 return iter(prefix, validate);
249 int prefixLen = prefix.length();
250 auto validate = [®exp, prefixLen] (
const QByteArray& arr) {
255 return iter(prefix, validate);
260 int prefixLen = prefix.length();
261 auto validate = [prefixLen, comVal, com] (
const QByteArray& arr) {
264 return ok && ((com == LessEqual && val <= comVal) || (com == GreaterEqual && val >= comVal));
266 return iter(prefix, validate);
271 int prefixLen = prefix.length();
272 auto validate = [prefixLen, comVal, com] (
const QByteArray& arr) {
275 return ok && ((com == LessEqual && val <= comVal) ||
276 (com == GreaterEqual && val >= comVal));
278 return iter(prefix, validate);
283 int prefixLen = prefix.length();
284 auto validate = [prefixLen, comVal, com] (
const QByteArray& arr) {
286 return ((com == LessEqual && val <= comVal) ||
287 (com == GreaterEqual && val >= comVal));
289 return iter(prefix, validate);
295 mdb_cursor_open(m_txn, m_dbi, &cursor);
297 MDB_val key = {0,
nullptr};
302 int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT);
303 if (rc == MDB_NOTFOUND) {
307 qCDebug(ENGINE) <<
"PostingDB::toTestMap" << mdb_strerror(rc);
311 const QByteArray ba(
static_cast<char*
>(key.mv_data), key.mv_size);
312 const PostingList plist = PostingCodec::decode(
QByteArray(
static_cast<char*
>(val.mv_data), val.mv_size));
313 map.insert(ba, plist);
316 mdb_cursor_close(cursor);
A PostingIterator is an abstract base class which can be used to iterate over all the "postings" or "...
Implements storage for docIds without any associated data Instantiated for:
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & next()
const char * constData() const const
QByteArray fromRawData(const char *data, qsizetype size)
bool isEmpty() const const
qsizetype length() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
qsizetype size() const const
bool startsWith(QByteArrayView bv) const const
double toDouble(bool *ok) const const
qlonglong toLongLong(bool *ok, int base) const const
bool isEmpty() const const
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, MatchType matchType, MatchOptions matchOptions) const const
bool hasMatch() const const
QString fromUtf8(QByteArrayView str)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)