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 | |
10 | using namespace Baloo; |
11 | |
12 | DocumentDataDB::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 | |
20 | DocumentDataDB::~DocumentDataDB() |
21 | { |
22 | } |
23 | |
24 | MDB_dbi DocumentDataDB::create(MDB_txn* txn) |
25 | { |
26 | MDB_dbi dbi = 0; |
27 | int rc = mdb_dbi_open(txn, name: "documentdatadb" , MDB_CREATE | MDB_INTEGERKEY, dbi: &dbi); |
28 | if (rc) { |
29 | qCWarning(ENGINE) << "DocumentDataDB::create" << mdb_strerror(err: rc); |
30 | return 0; |
31 | } |
32 | |
33 | return dbi; |
34 | } |
35 | |
36 | MDB_dbi DocumentDataDB::open(MDB_txn* txn) |
37 | { |
38 | MDB_dbi dbi = 0; |
39 | int rc = mdb_dbi_open(txn, name: "documentdatadb" , MDB_INTEGERKEY, dbi: &dbi); |
40 | if (rc) { |
41 | qCWarning(ENGINE) << "DocumentDataDB::open" << mdb_strerror(err: rc); |
42 | return 0; |
43 | } |
44 | |
45 | return dbi; |
46 | } |
47 | |
48 | void 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(txn: m_txn, dbi: m_dbi, key: &key, data: &val, flags: 0); |
62 | if (rc) { |
63 | qCWarning(ENGINE) << "DocumentDataDB::put" << mdb_strerror(err: rc); |
64 | } |
65 | } |
66 | |
67 | QByteArray 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{.mv_size: 0, .mv_data: nullptr}; |
76 | int rc = mdb_get(txn: m_txn, dbi: m_dbi, key: &key, data: &val); |
77 | if (rc) { |
78 | if (rc != MDB_NOTFOUND) { |
79 | qCDebug(ENGINE) << "DocumentDataDB::get" << docId << mdb_strerror(err: rc); |
80 | } |
81 | return QByteArray(); |
82 | } |
83 | |
84 | return QByteArray(static_cast<char*>(val.mv_data), val.mv_size); |
85 | } |
86 | |
87 | void 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(txn: m_txn, dbi: m_dbi, key: &key, data: nullptr); |
96 | if (rc != 0 && rc != MDB_NOTFOUND) { |
97 | qCDebug(ENGINE) << "DocumentDataDB::del" << docId << mdb_strerror(err: rc); |
98 | } |
99 | } |
100 | |
101 | bool 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{.mv_size: 0, .mv_data: nullptr}; |
110 | int rc = mdb_get(txn: m_txn, dbi: m_dbi, key: &key, data: &val); |
111 | if (rc) { |
112 | if (rc != MDB_NOTFOUND) { |
113 | qCDebug(ENGINE) << "DocumentDataDB::contains" << docId << mdb_strerror(err: rc); |
114 | } |
115 | return false; |
116 | } |
117 | |
118 | return true; |
119 | } |
120 | |
121 | QMap<quint64, QByteArray> DocumentDataDB::toTestMap() const |
122 | { |
123 | MDB_cursor* cursor; |
124 | mdb_cursor_open(txn: m_txn, dbi: m_dbi, cursor: &cursor); |
125 | |
126 | MDB_val key = {.mv_size: 0, .mv_data: nullptr}; |
127 | MDB_val val; |
128 | |
129 | QMap<quint64, QByteArray> map; |
130 | while (1) { |
131 | int rc = mdb_cursor_get(cursor, key: &key, data: &val, op: MDB_NEXT); |
132 | if (rc) { |
133 | qCDebug(ENGINE) << "DocumentDataDB::toTestMap" << mdb_strerror(err: 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(key: id, value: ba); |
140 | } |
141 | |
142 | mdb_cursor_close(cursor); |
143 | return map; |
144 | } |
145 | |