Baloo

documentdatadb.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "documentdatadb.h"
8#include "enginedebug.h"
9
10using namespace Baloo;
11
12DocumentDataDB::DocumentDataDB(MDB_dbi dbi, MDB_txn* txn)
13 : m_txn(txn)
14 , m_dbi(dbi)
15{
16 Q_ASSERT(txn != nullptr);
17 Q_ASSERT(dbi != 0);
18}
19
20DocumentDataDB::~DocumentDataDB()
21{
22}
23
24MDB_dbi DocumentDataDB::create(MDB_txn* txn)
25{
26 MDB_dbi dbi = 0;
27 int rc = mdb_dbi_open(txn, "documentdatadb", MDB_CREATE | MDB_INTEGERKEY, &dbi);
28 if (rc) {
29 qCWarning(ENGINE) << "DocumentDataDB::create" << mdb_strerror(rc);
30 return 0;
31 }
32
33 return dbi;
34}
35
36MDB_dbi DocumentDataDB::open(MDB_txn* txn)
37{
38 MDB_dbi dbi = 0;
39 int rc = mdb_dbi_open(txn, "documentdatadb", MDB_INTEGERKEY, &dbi);
40 if (rc) {
41 qCWarning(ENGINE) << "DocumentDataDB::open" << mdb_strerror(rc);
42 return 0;
43 }
44
45 return dbi;
46}
47
48void DocumentDataDB::put(quint64 docId, const QByteArray& url)
49{
50 Q_ASSERT(docId > 0);
51 Q_ASSERT(!url.isEmpty());
52
53 MDB_val key;
54 key.mv_size = sizeof(quint64);
55 key.mv_data = static_cast<void*>(&docId);
56
57 MDB_val val;
58 val.mv_size = url.size();
59 val.mv_data = static_cast<void*>(const_cast<char*>(url.constData()));
60
61 int rc = mdb_put(m_txn, m_dbi, &key, &val, 0);
62 if (rc) {
63 qCWarning(ENGINE) << "DocumentDataDB::put" << mdb_strerror(rc);
64 }
65}
66
67QByteArray DocumentDataDB::get(quint64 docId)
68{
69 Q_ASSERT(docId > 0);
70
71 MDB_val key;
72 key.mv_size = sizeof(quint64);
73 key.mv_data = static_cast<void*>(&docId);
74
75 MDB_val val{0, nullptr};
76 int rc = mdb_get(m_txn, m_dbi, &key, &val);
77 if (rc) {
78 if (rc != MDB_NOTFOUND) {
79 qCDebug(ENGINE) << "DocumentDataDB::get" << docId << mdb_strerror(rc);
80 }
81 return QByteArray();
82 }
83
84 return QByteArray(static_cast<char*>(val.mv_data), val.mv_size);
85}
86
87void DocumentDataDB::del(quint64 docId)
88{
89 Q_ASSERT(docId > 0);
90
91 MDB_val key;
92 key.mv_size = sizeof(quint64);
93 key.mv_data = static_cast<void*>(&docId);
94
95 int rc = mdb_del(m_txn, m_dbi, &key, nullptr);
96 if (rc != 0 && rc != MDB_NOTFOUND) {
97 qCDebug(ENGINE) << "DocumentDataDB::del" << docId << mdb_strerror(rc);
98 }
99}
100
101bool DocumentDataDB::contains(quint64 docId)
102{
103 Q_ASSERT(docId > 0);
104
105 MDB_val key;
106 key.mv_size = sizeof(quint64);
107 key.mv_data = static_cast<void*>(&docId);
108
109 MDB_val val{0, nullptr};
110 int rc = mdb_get(m_txn, m_dbi, &key, &val);
111 if (rc) {
112 if (rc != MDB_NOTFOUND) {
113 qCDebug(ENGINE) << "DocumentDataDB::contains" << docId << mdb_strerror(rc);
114 }
115 return false;
116 }
117
118 return true;
119}
120
121QMap<quint64, QByteArray> DocumentDataDB::toTestMap() const
122{
123 MDB_cursor* cursor;
124 mdb_cursor_open(m_txn, m_dbi, &cursor);
125
126 MDB_val key = {0, nullptr};
127 MDB_val val;
128
130 while (1) {
131 int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT);
132 if (rc) {
133 qCDebug(ENGINE) << "DocumentDataDB::toTestMap" << mdb_strerror(rc);
134 break;
135 }
136
137 const quint64 id = *(static_cast<quint64*>(key.mv_data));
138 const QByteArray ba(static_cast<char*>(val.mv_data), val.mv_size);
139 map.insert(id, ba);
140 }
141
142 mdb_cursor_close(cursor);
143 return map;
144}
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
const char * constData() const const
bool isEmpty() const const
qsizetype size() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.