Changeset View
Changeset View
Standalone View
Standalone View
src/engine/idtreedb.cpp
Show All 13 Lines | |||||
14 | * | 14 | * | ||
15 | * You should have received a copy of the GNU Lesser General Public | 15 | * You should have received a copy of the GNU Lesser General Public | ||
16 | * License along with this library; if not, write to the Free Software | 16 | * License along with this library; if not, write to the Free Software | ||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | * | 18 | * | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "idtreedb.h" | 21 | #include "idtreedb.h" | ||
22 | #include "enginedebug.h" | ||||
22 | #include "postingiterator.h" | 23 | #include "postingiterator.h" | ||
23 | 24 | | |||
24 | #include <QDebug> | | |||
25 | #include <algorithm> | 25 | #include <algorithm> | ||
26 | 26 | | |||
27 | using namespace Baloo; | 27 | using namespace Baloo; | ||
28 | 28 | | |||
29 | IdTreeDB::IdTreeDB(MDB_dbi dbi, MDB_txn* txn) | 29 | IdTreeDB::IdTreeDB(MDB_dbi dbi, MDB_txn* txn) | ||
30 | : m_txn(txn) | 30 | : m_txn(txn) | ||
31 | , m_dbi(dbi) | 31 | , m_dbi(dbi) | ||
32 | { | 32 | { | ||
33 | Q_ASSERT(txn != nullptr); | 33 | Q_ASSERT(txn != nullptr); | ||
34 | Q_ASSERT(dbi != 0); | 34 | Q_ASSERT(dbi != 0); | ||
35 | } | 35 | } | ||
36 | 36 | | |||
37 | MDB_dbi IdTreeDB::create(MDB_txn* txn) | 37 | MDB_dbi IdTreeDB::create(MDB_txn* txn) | ||
38 | { | 38 | { | ||
39 | MDB_dbi dbi; | 39 | MDB_dbi dbi = 0; | ||
40 | int rc = mdb_dbi_open(txn, "idtree", MDB_CREATE | MDB_INTEGERKEY, &dbi); | 40 | int rc = mdb_dbi_open(txn, "idtree", MDB_CREATE | MDB_INTEGERKEY, &dbi); | ||
41 | Q_ASSERT_X(rc == 0, "IdTreeDB::create", mdb_strerror(rc)); | 41 | if (rc) { | ||
42 | qCWarning(ENGINE) << "IdTreeDB::create" << mdb_strerror(rc); | ||||
43 | return 0; | ||||
44 | } | ||||
42 | 45 | | |||
43 | return dbi; | 46 | return dbi; | ||
44 | } | 47 | } | ||
45 | 48 | | |||
46 | MDB_dbi IdTreeDB::open(MDB_txn* txn) | 49 | MDB_dbi IdTreeDB::open(MDB_txn* txn) | ||
47 | { | 50 | { | ||
48 | MDB_dbi dbi; | 51 | MDB_dbi dbi = 0; | ||
49 | int rc = mdb_dbi_open(txn, "idtree", MDB_INTEGERKEY, &dbi); | 52 | int rc = mdb_dbi_open(txn, "idtree", MDB_INTEGERKEY, &dbi); | ||
50 | if (rc == MDB_NOTFOUND) { | 53 | if (rc) { | ||
54 | qCWarning(ENGINE) << "IdTreeDB::open" << mdb_strerror(rc); | ||||
51 | return 0; | 55 | return 0; | ||
52 | } | 56 | } | ||
53 | Q_ASSERT_X(rc == 0, "IdTreeDB::open", mdb_strerror(rc)); | | |||
54 | 57 | | |||
55 | return dbi; | 58 | return dbi; | ||
56 | } | 59 | } | ||
57 | 60 | | |||
58 | void IdTreeDB::put(quint64 docId, const QVector<quint64> subDocIds) | 61 | void IdTreeDB::put(quint64 docId, const QVector<quint64> subDocIds) | ||
59 | { | 62 | { | ||
60 | Q_ASSERT(!subDocIds.isEmpty()); | 63 | Q_ASSERT(!subDocIds.isEmpty()); | ||
61 | Q_ASSERT(!subDocIds.contains(0)); | 64 | Q_ASSERT(!subDocIds.contains(0)); | ||
62 | 65 | | |||
63 | MDB_val key; | 66 | MDB_val key; | ||
64 | key.mv_size = sizeof(quint64); | 67 | key.mv_size = sizeof(quint64); | ||
65 | key.mv_data = static_cast<void*>(&docId); | 68 | key.mv_data = static_cast<void*>(&docId); | ||
66 | 69 | | |||
67 | MDB_val val; | 70 | MDB_val val; | ||
68 | val.mv_size = subDocIds.size() * sizeof(quint64); | 71 | val.mv_size = subDocIds.size() * sizeof(quint64); | ||
69 | val.mv_data = static_cast<void*>(const_cast<quint64*>(subDocIds.constData())); | 72 | val.mv_data = static_cast<void*>(const_cast<quint64*>(subDocIds.constData())); | ||
70 | 73 | | |||
71 | int rc = mdb_put(m_txn, m_dbi, &key, &val, 0); | 74 | int rc = mdb_put(m_txn, m_dbi, &key, &val, 0); | ||
72 | Q_ASSERT_X(rc == 0, "IdTreeDB::put", mdb_strerror(rc)); | 75 | if (rc) { | ||
76 | qCWarning(ENGINE) << "IdTreeDB::put" << mdb_strerror(rc); | ||||
77 | } | ||||
73 | } | 78 | } | ||
74 | 79 | | |||
75 | QVector<quint64> IdTreeDB::get(quint64 docId) | 80 | QVector<quint64> IdTreeDB::get(quint64 docId) | ||
76 | { | 81 | { | ||
77 | MDB_val key; | 82 | MDB_val key; | ||
78 | key.mv_size = sizeof(quint64); | 83 | key.mv_size = sizeof(quint64); | ||
79 | key.mv_data = static_cast<void*>(&docId); | 84 | key.mv_data = static_cast<void*>(&docId); | ||
80 | 85 | | |||
81 | MDB_val val; | 86 | MDB_val val{0, nullptr}; | ||
82 | int rc = mdb_get(m_txn, m_dbi, &key, &val); | 87 | int rc = mdb_get(m_txn, m_dbi, &key, &val); | ||
83 | if (rc == MDB_NOTFOUND) { | 88 | if (rc) { | ||
89 | if (rc != MDB_NOTFOUND) { | ||||
90 | qCDebug(ENGINE) << "IdTreeDB::get" << docId << mdb_strerror(rc); | ||||
91 | } | ||||
84 | return QVector<quint64>(); | 92 | return QVector<quint64>(); | ||
85 | } | 93 | } | ||
86 | Q_ASSERT_X(rc == 0, "IdTreeeDB::get", mdb_strerror(rc)); | | |||
87 | 94 | | |||
88 | // FIXME: This still makes a copy of the data. Perhaps we can avoid that? | 95 | // FIXME: This still makes a copy of the data. Perhaps we can avoid that? | ||
89 | QVector<quint64> list(val.mv_size / sizeof(quint64)); | 96 | QVector<quint64> list(val.mv_size / sizeof(quint64)); | ||
90 | memcpy(list.data(), val.mv_data, val.mv_size); | 97 | memcpy(list.data(), val.mv_data, val.mv_size); | ||
91 | 98 | | |||
92 | return list; | 99 | return list; | ||
93 | } | 100 | } | ||
94 | 101 | | |||
95 | void IdTreeDB::del(quint64 docId) | 102 | void IdTreeDB::del(quint64 docId) | ||
96 | { | 103 | { | ||
97 | MDB_val key; | 104 | MDB_val key; | ||
98 | key.mv_size = sizeof(quint64); | 105 | key.mv_size = sizeof(quint64); | ||
99 | key.mv_data = static_cast<void*>(&docId); | 106 | key.mv_data = static_cast<void*>(&docId); | ||
100 | 107 | | |||
101 | int rc = mdb_del(m_txn, m_dbi, &key, nullptr); | 108 | int rc = mdb_del(m_txn, m_dbi, &key, nullptr); | ||
102 | Q_ASSERT_X(rc == 0, "IdTreeDB::del", mdb_strerror(rc)); | 109 | if (rc != 0 && rc != MDB_NOTFOUND) { | ||
110 | qCDebug(ENGINE) << "IdTreeDB::del" << mdb_strerror(rc); | ||||
111 | } | ||||
103 | } | 112 | } | ||
104 | 113 | | |||
105 | // | 114 | // | ||
106 | // Iter | 115 | // Iter | ||
107 | // | 116 | // | ||
108 | class IdTreePostingIterator : public PostingIterator { | 117 | class IdTreePostingIterator : public PostingIterator { | ||
109 | public: | 118 | public: | ||
110 | IdTreePostingIterator(const IdTreeDB& db, const QVector<quint64> list) | 119 | IdTreePostingIterator(const IdTreeDB& db, const QVector<quint64> list) | ||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Line(s) | 171 | { | |||
164 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 173 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
165 | 174 | | |||
166 | MDB_val key = {0, nullptr}; | 175 | MDB_val key = {0, nullptr}; | ||
167 | MDB_val val; | 176 | MDB_val val; | ||
168 | 177 | | |||
169 | QMap<quint64, QVector<quint64>> map; | 178 | QMap<quint64, QVector<quint64>> map; | ||
170 | while (1) { | 179 | while (1) { | ||
171 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | 180 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | ||
172 | if (rc == MDB_NOTFOUND) { | 181 | if (rc) { | ||
182 | qCDebug(ENGINE) << "PostingDB::toTestMap" << mdb_strerror(rc); | ||||
173 | break; | 183 | break; | ||
174 | } | 184 | } | ||
175 | Q_ASSERT_X(rc == 0, "PostingDB::toTestMap", mdb_strerror(rc)); | | |||
176 | 185 | | |||
177 | const quint64 id = *(static_cast<quint64*>(key.mv_data)); | 186 | const quint64 id = *(static_cast<quint64*>(key.mv_data)); | ||
178 | 187 | | |||
179 | QVector<quint64> list(val.mv_size / sizeof(quint64)); | 188 | QVector<quint64> list(val.mv_size / sizeof(quint64)); | ||
180 | memcpy(list.data(), val.mv_data, val.mv_size); | 189 | memcpy(list.data(), val.mv_data, val.mv_size); | ||
181 | 190 | | |||
182 | map.insert(id, list); | 191 | map.insert(id, list); | ||
183 | } | 192 | } | ||
184 | 193 | | |||
185 | mdb_cursor_close(cursor); | 194 | mdb_cursor_close(cursor); | ||
186 | return map; | 195 | return map; | ||
187 | } | 196 | } |