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 | qCDebug(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 | qCDebug(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)); | ||
Show All 11 Lines | |||||
73 | } | 76 | } | ||
74 | 77 | | |||
75 | QVector<quint64> IdTreeDB::get(quint64 docId) | 78 | QVector<quint64> IdTreeDB::get(quint64 docId) | ||
76 | { | 79 | { | ||
77 | MDB_val key; | 80 | MDB_val key; | ||
78 | key.mv_size = sizeof(quint64); | 81 | key.mv_size = sizeof(quint64); | ||
79 | key.mv_data = static_cast<void*>(&docId); | 82 | key.mv_data = static_cast<void*>(&docId); | ||
80 | 83 | | |||
81 | MDB_val val; | 84 | MDB_val val{0, nullptr}; | ||
82 | int rc = mdb_get(m_txn, m_dbi, &key, &val); | 85 | int rc = mdb_get(m_txn, m_dbi, &key, &val); | ||
83 | if (rc == MDB_NOTFOUND) { | 86 | if (rc) { | ||
87 | qCDebug(ENGINE) << "IdTreeDB::get" << docId << mdb_strerror(rc); | ||||
84 | return QVector<quint64>(); | 88 | return QVector<quint64>(); | ||
85 | } | 89 | } | ||
86 | Q_ASSERT_X(rc == 0, "IdTreeeDB::get", mdb_strerror(rc)); | | |||
87 | 90 | | |||
88 | // FIXME: This still makes a copy of the data. Perhaps we can avoid that? | 91 | // FIXME: This still makes a copy of the data. Perhaps we can avoid that? | ||
89 | QVector<quint64> list(val.mv_size / sizeof(quint64)); | 92 | QVector<quint64> list(val.mv_size / sizeof(quint64)); | ||
90 | memcpy(list.data(), val.mv_data, val.mv_size); | 93 | memcpy(list.data(), val.mv_data, val.mv_size); | ||
91 | 94 | | |||
92 | return list; | 95 | return list; | ||
93 | } | 96 | } | ||
94 | 97 | | |||
▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Line(s) | 165 | { | |||
164 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 167 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
165 | 168 | | |||
166 | MDB_val key = {0, nullptr}; | 169 | MDB_val key = {0, nullptr}; | ||
167 | MDB_val val; | 170 | MDB_val val; | ||
168 | 171 | | |||
169 | QMap<quint64, QVector<quint64>> map; | 172 | QMap<quint64, QVector<quint64>> map; | ||
170 | while (1) { | 173 | while (1) { | ||
171 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | 174 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | ||
172 | if (rc == MDB_NOTFOUND) { | 175 | if (rc) { | ||
173 | break; | 176 | break; | ||
174 | } | 177 | } | ||
175 | Q_ASSERT_X(rc == 0, "PostingDB::toTestMap", mdb_strerror(rc)); | 178 | Q_ASSERT_X(rc == 0, "PostingDB::toTestMap", mdb_strerror(rc)); | ||
176 | 179 | | |||
177 | const quint64 id = *(static_cast<quint64*>(key.mv_data)); | 180 | const quint64 id = *(static_cast<quint64*>(key.mv_data)); | ||
178 | 181 | | |||
179 | QVector<quint64> list(val.mv_size / sizeof(quint64)); | 182 | QVector<quint64> list(val.mv_size / sizeof(quint64)); | ||
180 | memcpy(list.data(), val.mv_data, val.mv_size); | 183 | memcpy(list.data(), val.mv_data, val.mv_size); | ||
181 | 184 | | |||
182 | map.insert(id, list); | 185 | map.insert(id, list); | ||
183 | } | 186 | } | ||
184 | 187 | | |||
185 | mdb_cursor_close(cursor); | 188 | mdb_cursor_close(cursor); | ||
186 | return map; | 189 | return map; | ||
187 | } | 190 | } |