Changeset View
Changeset View
Standalone View
Standalone View
src/engine/mtimedb.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 "mtimedb.h" | 21 | #include "mtimedb.h" | ||
22 | #include "enginedebug.h" | ||||
22 | #include "vectorpostingiterator.h" | 23 | #include "vectorpostingiterator.h" | ||
23 | #include <algorithm> | 24 | #include <algorithm> | ||
24 | 25 | | |||
25 | using namespace Baloo; | 26 | using namespace Baloo; | ||
26 | 27 | | |||
27 | MTimeDB::MTimeDB(MDB_dbi dbi, MDB_txn* txn) | 28 | MTimeDB::MTimeDB(MDB_dbi dbi, MDB_txn* txn) | ||
28 | : m_txn(txn) | 29 | : m_txn(txn) | ||
29 | , m_dbi(dbi) | 30 | , m_dbi(dbi) | ||
30 | { | 31 | { | ||
31 | Q_ASSERT(txn != nullptr); | 32 | Q_ASSERT(txn != nullptr); | ||
32 | Q_ASSERT(dbi != 0); | 33 | Q_ASSERT(dbi != 0); | ||
33 | } | 34 | } | ||
34 | 35 | | |||
35 | MTimeDB::~MTimeDB() | 36 | MTimeDB::~MTimeDB() | ||
36 | { | 37 | { | ||
37 | } | 38 | } | ||
38 | 39 | | |||
39 | MDB_dbi MTimeDB::create(MDB_txn* txn) | 40 | MDB_dbi MTimeDB::create(MDB_txn* txn) | ||
40 | { | 41 | { | ||
41 | MDB_dbi dbi; | 42 | MDB_dbi dbi = 0; | ||
42 | int rc = mdb_dbi_open(txn, "mtimedb", MDB_CREATE | MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED | MDB_INTEGERDUP, &dbi); | 43 | int rc = mdb_dbi_open(txn, "mtimedb", MDB_CREATE | MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED | MDB_INTEGERDUP, &dbi); | ||
43 | Q_ASSERT_X(rc == 0, "MTimeDB::create", mdb_strerror(rc)); | 44 | if (rc) { | ||
45 | qCDebug(ENGINE) << "MTimeDB::create" << mdb_strerror(rc); | ||||
46 | return 0; | ||||
47 | } | ||||
44 | 48 | | |||
45 | return dbi; | 49 | return dbi; | ||
46 | } | 50 | } | ||
47 | 51 | | |||
48 | MDB_dbi MTimeDB::open(MDB_txn* txn) | 52 | MDB_dbi MTimeDB::open(MDB_txn* txn) | ||
49 | { | 53 | { | ||
50 | MDB_dbi dbi; | 54 | MDB_dbi dbi = 0; | ||
51 | int rc = mdb_dbi_open(txn, "mtimedb", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED | MDB_INTEGERDUP, &dbi); | 55 | int rc = mdb_dbi_open(txn, "mtimedb", MDB_INTEGERKEY | MDB_DUPSORT | MDB_DUPFIXED | MDB_INTEGERDUP, &dbi); | ||
52 | if (rc == MDB_NOTFOUND) { | 56 | if (rc) { | ||
57 | qCDebug(ENGINE) << "MTimeDB::open" << mdb_strerror(rc); | ||||
53 | return 0; | 58 | return 0; | ||
54 | } | 59 | } | ||
55 | Q_ASSERT_X(rc == 0, "MTimeDB::open", mdb_strerror(rc)); | | |||
56 | 60 | | |||
57 | return dbi; | 61 | return dbi; | ||
58 | } | 62 | } | ||
59 | 63 | | |||
60 | void MTimeDB::put(quint32 mtime, quint64 docId) | 64 | void MTimeDB::put(quint32 mtime, quint64 docId) | ||
61 | { | 65 | { | ||
62 | Q_ASSERT(mtime > 0); | 66 | Q_ASSERT(mtime > 0); | ||
63 | Q_ASSERT(docId > 0); | 67 | Q_ASSERT(docId > 0); | ||
64 | 68 | | |||
65 | MDB_val key; | 69 | MDB_val key; | ||
66 | key.mv_size = sizeof(quint32); | 70 | key.mv_size = sizeof(quint32); | ||
67 | key.mv_data = static_cast<void*>(&mtime); | 71 | key.mv_data = static_cast<void*>(&mtime); | ||
68 | 72 | | |||
69 | MDB_val val; | 73 | MDB_val val; | ||
70 | val.mv_size = sizeof(quint64); | 74 | val.mv_size = sizeof(quint64); | ||
71 | val.mv_data = static_cast<void*>(&docId); | 75 | val.mv_data = static_cast<void*>(&docId); | ||
72 | 76 | | |||
73 | int rc = mdb_put(m_txn, m_dbi, &key, &val, 0); | 77 | int rc = mdb_put(m_txn, m_dbi, &key, &val, 0); | ||
74 | Q_ASSERT_X(rc == 0, "MTimeDB::put", mdb_strerror(rc)); | 78 | if (rc) { | ||
79 | qCDebug(ENGINE) << "MTimeDB::put" << mdb_strerror(rc); | ||||
80 | } | ||||
75 | } | 81 | } | ||
76 | 82 | | |||
77 | QVector<quint64> MTimeDB::get(quint32 mtime) | 83 | QVector<quint64> MTimeDB::get(quint32 mtime) | ||
78 | { | 84 | { | ||
79 | Q_ASSERT(mtime > 0); | 85 | Q_ASSERT(mtime > 0); | ||
80 | 86 | | |||
81 | MDB_val key; | 87 | MDB_val key; | ||
82 | key.mv_size = sizeof(quint32); | 88 | key.mv_size = sizeof(quint32); | ||
83 | key.mv_data = static_cast<void*>(&mtime); | 89 | key.mv_data = static_cast<void*>(&mtime); | ||
84 | 90 | | |||
85 | QVector<quint64> values; | 91 | QVector<quint64> values; | ||
86 | 92 | | |||
87 | MDB_cursor* cursor; | 93 | MDB_cursor* cursor; | ||
88 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 94 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
89 | 95 | | |||
90 | MDB_val val; | 96 | MDB_val val{0, nullptr}; | ||
91 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | 97 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | ||
92 | if (rc == MDB_NOTFOUND) { | 98 | if (rc) { | ||
99 | qCDebug(ENGINE) << "MTimeDB::get" << mtime << mdb_strerror(rc); | ||||
93 | mdb_cursor_close(cursor); | 100 | mdb_cursor_close(cursor); | ||
94 | return values; | 101 | return values; | ||
95 | } | 102 | } | ||
96 | Q_ASSERT_X(rc == 0, "MTimeDB::get", mdb_strerror(rc)); | | |||
97 | 103 | | |||
98 | values << *static_cast<quint64*>(val.mv_data); | 104 | values << *static_cast<quint64*>(val.mv_data); | ||
99 | 105 | | |||
100 | while (1) { | 106 | while (1) { | ||
101 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT_DUP); | 107 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT_DUP); | ||
102 | if (rc == MDB_NOTFOUND) { | 108 | if (rc) { | ||
109 | qCDebug(ENGINE) << "MTimeDB::get (loop)" << mtime << mdb_strerror(rc); | ||||
bruns: `if (rc != MDB_NOTFOUND)` for the message | |||||
for the message - you now only retrieve the first entry, spew an error message, and break. In case the key does not exist, you loop infinitely. bruns: **for the message** - you now only retrieve the first entry, spew an error message, and break. | |||||
103 | break; | 110 | break; | ||
104 | } | 111 | } | ||
105 | Q_ASSERT_X(rc == 0, "MTimeDB::get while", mdb_strerror(rc)); | | |||
106 | 112 | | |||
107 | values << *static_cast<quint64*>(val.mv_data); | 113 | values << *static_cast<quint64*>(val.mv_data); | ||
108 | } | 114 | } | ||
109 | 115 | | |||
110 | mdb_cursor_close(cursor); | 116 | mdb_cursor_close(cursor); | ||
111 | std::sort(values.begin(), values.end()); | 117 | std::sort(values.begin(), values.end()); | ||
112 | values.erase(std::unique(values.begin(), values.end()), values.end()); | 118 | values.erase(std::unique(values.begin(), values.end()), values.end()); | ||
113 | return values; | 119 | return values; | ||
114 | } | 120 | } | ||
115 | 121 | | |||
116 | void MTimeDB::del(quint32 mtime, quint64 docId) | 122 | void MTimeDB::del(quint32 mtime, quint64 docId) | ||
117 | { | 123 | { | ||
118 | Q_ASSERT(mtime > 0); | 124 | Q_ASSERT(mtime > 0); | ||
119 | Q_ASSERT(docId > 0); | 125 | Q_ASSERT(docId > 0); | ||
120 | 126 | | |||
121 | MDB_val key; | 127 | MDB_val key; | ||
122 | key.mv_size = sizeof(quint32); | 128 | key.mv_size = sizeof(quint32); | ||
123 | key.mv_data = static_cast<void*>(&mtime); | 129 | key.mv_data = static_cast<void*>(&mtime); | ||
124 | 130 | | |||
125 | MDB_val val; | 131 | MDB_val val; | ||
126 | val.mv_size = sizeof(quint64); | 132 | val.mv_size = sizeof(quint64); | ||
127 | val.mv_data = static_cast<void*>(&docId); | 133 | val.mv_data = static_cast<void*>(&docId); | ||
128 | 134 | | |||
129 | int rc = mdb_del(m_txn, m_dbi, &key, &val); | 135 | int rc = mdb_del(m_txn, m_dbi, &key, &val); | ||
130 | if (rc == MDB_NOTFOUND) { | 136 | if (rc) { | ||
131 | return; | 137 | qCDebug(ENGINE) << "MTimeDB::del" << mtime << docId << mdb_strerror(rc); | ||
132 | } | 138 | } | ||
133 | Q_ASSERT_X(rc == 0, "DocumentDB::del", mdb_strerror(rc)); | | |||
134 | } | 139 | } | ||
135 | 140 | | |||
136 | // | 141 | // | ||
137 | // Posting Iterator | 142 | // Posting Iterator | ||
138 | // | 143 | // | ||
139 | 144 | | |||
140 | PostingIterator* MTimeDB::iter(quint32 mtime, MTimeDB::Comparator com) | 145 | PostingIterator* MTimeDB::iter(quint32 mtime, MTimeDB::Comparator com) | ||
141 | { | 146 | { | ||
142 | if (com == Equal) { | 147 | if (com == Equal) { | ||
143 | return new VectorPostingIterator(get(mtime)); | 148 | return new VectorPostingIterator(get(mtime)); | ||
144 | } | 149 | } | ||
145 | 150 | | |||
146 | MDB_val key; | 151 | MDB_val key; | ||
147 | key.mv_size = sizeof(quint32); | 152 | key.mv_size = sizeof(quint32); | ||
148 | key.mv_data = &mtime; | 153 | key.mv_data = &mtime; | ||
149 | 154 | | |||
150 | MDB_cursor* cursor; | 155 | MDB_cursor* cursor; | ||
151 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 156 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
152 | 157 | | |||
153 | MDB_val val; | 158 | MDB_val val{0, nullptr}; | ||
154 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | 159 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | ||
155 | if (rc == MDB_NOTFOUND) { | 160 | if (rc) { | ||
161 | qCDebug(ENGINE) << "MTimeDB::iter" << mtime << mdb_strerror(rc); | ||||
156 | mdb_cursor_close(cursor); | 162 | mdb_cursor_close(cursor); | ||
157 | return nullptr; | 163 | return nullptr; | ||
158 | } | 164 | } | ||
159 | Q_ASSERT_X(rc == 0, "MTimeDB::iter", mdb_strerror(rc)); | | |||
160 | 165 | | |||
161 | QVector<quint64> results; | 166 | QVector<quint64> results; | ||
162 | results << *static_cast<quint64*>(val.mv_data); | 167 | results << *static_cast<quint64*>(val.mv_data); | ||
163 | 168 | | |||
164 | if (com == GreaterEqual) { | 169 | if (com == GreaterEqual) { | ||
165 | while (1) { | 170 | while (1) { | ||
166 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | 171 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | ||
167 | if (rc == MDB_NOTFOUND) { | 172 | if (rc) { | ||
173 | qCDebug(ENGINE) << "MTimeDB::iter (loop)" << mtime << mdb_strerror(rc); | ||||
168 | break; | 174 | break; | ||
169 | } | 175 | } | ||
170 | Q_ASSERT_X(rc == 0, "MTimeDB::iter >=", mdb_strerror(rc)); | | |||
171 | 176 | | |||
172 | results << *static_cast<quint64*>(val.mv_data); | 177 | results << *static_cast<quint64*>(val.mv_data); | ||
173 | } | 178 | } | ||
174 | } | 179 | } | ||
175 | else { | 180 | else { | ||
176 | while (1) { | 181 | while (1) { | ||
177 | rc = mdb_cursor_get(cursor, &key, &val, MDB_PREV); | 182 | rc = mdb_cursor_get(cursor, &key, &val, MDB_PREV); | ||
178 | if (rc == MDB_NOTFOUND) { | 183 | if (rc) { | ||
184 | qCDebug(ENGINE) << "MTimeDB::iter (loop)" << mtime << mdb_strerror(rc); | ||||
179 | break; | 185 | break; | ||
180 | } | 186 | } | ||
181 | Q_ASSERT_X(rc == 0, "MTimeDB::iter >=", mdb_strerror(rc)); | | |||
182 | 187 | | |||
183 | quint64 id = *static_cast<quint64*>(val.mv_data); | 188 | quint64 id = *static_cast<quint64*>(val.mv_data); | ||
184 | results.push_front(id); | 189 | results.push_front(id); | ||
185 | } | 190 | } | ||
186 | } | 191 | } | ||
187 | 192 | | |||
188 | mdb_cursor_close(cursor); | 193 | mdb_cursor_close(cursor); | ||
189 | std::sort(results.begin(), results.end()); | 194 | std::sort(results.begin(), results.end()); | ||
190 | results.erase(std::unique(results.begin(), results.end()), results.end()); | 195 | results.erase(std::unique(results.begin(), results.end()), results.end()); | ||
191 | return new VectorPostingIterator(results); | 196 | return new VectorPostingIterator(results); | ||
192 | } | 197 | } | ||
193 | 198 | | |||
194 | PostingIterator* MTimeDB::iterRange(quint32 beginTime, quint32 endTime) | 199 | PostingIterator* MTimeDB::iterRange(quint32 beginTime, quint32 endTime) | ||
195 | { | 200 | { | ||
196 | Q_ASSERT(beginTime); | 201 | Q_ASSERT(beginTime); | ||
197 | Q_ASSERT(endTime); | 202 | Q_ASSERT(endTime); | ||
198 | 203 | | |||
199 | MDB_val key; | 204 | MDB_val key; | ||
200 | key.mv_size = sizeof(quint32); | 205 | key.mv_size = sizeof(quint32); | ||
201 | key.mv_data = &beginTime; | 206 | key.mv_data = &beginTime; | ||
202 | 207 | | |||
203 | MDB_cursor* cursor; | 208 | MDB_cursor* cursor; | ||
204 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 209 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
205 | 210 | | |||
206 | MDB_val val; | 211 | MDB_val val{0, nullptr}; | ||
207 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | 212 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | ||
208 | if (rc == MDB_NOTFOUND) { | 213 | if (rc) { | ||
214 | qCDebug(ENGINE) << "MTimeDB::iterRange" << beginTime << endTime << mdb_strerror(rc); | ||||
209 | mdb_cursor_close(cursor); | 215 | mdb_cursor_close(cursor); | ||
210 | return nullptr; | 216 | return nullptr; | ||
211 | } | 217 | } | ||
212 | Q_ASSERT_X(rc == 0, "MTimeDB::iterRange", mdb_strerror(rc)); | | |||
213 | 218 | | |||
214 | QVector<quint64> results; | 219 | QVector<quint64> results; | ||
215 | results << *static_cast<quint64*>(val.mv_data); | 220 | results << *static_cast<quint64*>(val.mv_data); | ||
216 | 221 | | |||
217 | while (1) { | 222 | while (1) { | ||
218 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | 223 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | ||
219 | if (rc == MDB_NOTFOUND) { | 224 | if (rc) { | ||
225 | qCDebug(ENGINE) << "MTimeDB::iterRange" << beginTime << endTime << mdb_strerror(rc); | ||||
220 | break; | 226 | break; | ||
221 | } | 227 | } | ||
222 | Q_ASSERT_X(rc == 0, "MTimeDB::iter >=", mdb_strerror(rc)); | | |||
223 | 228 | | |||
224 | quint32 time = *static_cast<quint32*>(key.mv_data); | 229 | quint32 time = *static_cast<quint32*>(key.mv_data); | ||
225 | if (time > endTime) { | 230 | if (time > endTime) { | ||
226 | break; | 231 | break; | ||
227 | } | 232 | } | ||
228 | results << *static_cast<quint64*>(val.mv_data); | 233 | results << *static_cast<quint64*>(val.mv_data); | ||
229 | } | 234 | } | ||
230 | 235 | | |||
Show All 9 Lines | 243 | { | |||
240 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 245 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
241 | 246 | | |||
242 | MDB_val key = {0, nullptr}; | 247 | MDB_val key = {0, nullptr}; | ||
243 | MDB_val val; | 248 | MDB_val val; | ||
244 | 249 | | |||
245 | QMap<quint32, quint64> map; | 250 | QMap<quint32, quint64> map; | ||
246 | while (1) { | 251 | while (1) { | ||
247 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | 252 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | ||
248 | if (rc == MDB_NOTFOUND) { | 253 | if (rc) { | ||
254 | qCDebug(ENGINE) << "MTimeDB::toTestMap" << mdb_strerror(rc); | ||||
249 | break; | 255 | break; | ||
250 | } | 256 | } | ||
251 | Q_ASSERT_X(rc == 0, "MTimeDB::toTestMap", mdb_strerror(rc)); | | |||
252 | 257 | | |||
253 | const quint32 time = *(static_cast<quint32*>(key.mv_data)); | 258 | const quint32 time = *(static_cast<quint32*>(key.mv_data)); | ||
254 | const quint64 id = *(static_cast<quint64*>(val.mv_data)); | 259 | const quint64 id = *(static_cast<quint64*>(val.mv_data)); | ||
255 | map.insert(time, id); | 260 | map.insert(time, id); | ||
256 | } | 261 | } | ||
257 | 262 | | |||
258 | mdb_cursor_close(cursor); | 263 | mdb_cursor_close(cursor); | ||
259 | return map; | 264 | return map; | ||
260 | } | 265 | } |
if (rc != MDB_NOTFOUND) for the message