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 | qCWarning(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 | qCWarning(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 | qCWarning(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 | if (rc != MDB_NOTFOUND) { | ||||
100 | qCDebug(ENGINE) << "MTimeDB::get" << mtime << mdb_strerror(rc); | ||||
101 | } | ||||
93 | mdb_cursor_close(cursor); | 102 | mdb_cursor_close(cursor); | ||
94 | return values; | 103 | return values; | ||
95 | } | 104 | } | ||
96 | Q_ASSERT_X(rc == 0, "MTimeDB::get", mdb_strerror(rc)); | | |||
97 | 105 | | |||
98 | values << *static_cast<quint64*>(val.mv_data); | 106 | values << *static_cast<quint64*>(val.mv_data); | ||
99 | 107 | | |||
100 | while (1) { | 108 | while (1) { | ||
101 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT_DUP); | 109 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT_DUP); | ||
102 | if (rc == MDB_NOTFOUND) { | 110 | if (rc) { | ||
111 | if (rc != MDB_NOTFOUND) { | ||||
112 | qCDebug(ENGINE) << "MTimeDB::get (loop)" << mtime << mdb_strerror(rc); | ||||
113 | } | ||||
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; | 114 | break; | ||
104 | } | 115 | } | ||
105 | Q_ASSERT_X(rc == 0, "MTimeDB::get while", mdb_strerror(rc)); | | |||
106 | | ||||
107 | values << *static_cast<quint64*>(val.mv_data); | 116 | values << *static_cast<quint64*>(val.mv_data); | ||
108 | } | 117 | } | ||
109 | 118 | | |||
110 | mdb_cursor_close(cursor); | 119 | mdb_cursor_close(cursor); | ||
111 | std::sort(values.begin(), values.end()); | 120 | std::sort(values.begin(), values.end()); | ||
112 | values.erase(std::unique(values.begin(), values.end()), values.end()); | 121 | values.erase(std::unique(values.begin(), values.end()), values.end()); | ||
113 | return values; | 122 | return values; | ||
114 | } | 123 | } | ||
115 | 124 | | |||
116 | void MTimeDB::del(quint32 mtime, quint64 docId) | 125 | void MTimeDB::del(quint32 mtime, quint64 docId) | ||
117 | { | 126 | { | ||
118 | Q_ASSERT(mtime > 0); | 127 | Q_ASSERT(mtime > 0); | ||
119 | Q_ASSERT(docId > 0); | 128 | Q_ASSERT(docId > 0); | ||
120 | 129 | | |||
121 | MDB_val key; | 130 | MDB_val key; | ||
122 | key.mv_size = sizeof(quint32); | 131 | key.mv_size = sizeof(quint32); | ||
123 | key.mv_data = static_cast<void*>(&mtime); | 132 | key.mv_data = static_cast<void*>(&mtime); | ||
124 | 133 | | |||
125 | MDB_val val; | 134 | MDB_val val; | ||
126 | val.mv_size = sizeof(quint64); | 135 | val.mv_size = sizeof(quint64); | ||
127 | val.mv_data = static_cast<void*>(&docId); | 136 | val.mv_data = static_cast<void*>(&docId); | ||
128 | 137 | | |||
129 | int rc = mdb_del(m_txn, m_dbi, &key, &val); | 138 | int rc = mdb_del(m_txn, m_dbi, &key, &val); | ||
130 | if (rc == MDB_NOTFOUND) { | 139 | if (rc != 0 && rc != MDB_NOTFOUND) { | ||
131 | return; | 140 | qCDebug(ENGINE) << "MTimeDB::del" << mtime << docId << mdb_strerror(rc); | ||
132 | } | 141 | } | ||
133 | Q_ASSERT_X(rc == 0, "DocumentDB::del", mdb_strerror(rc)); | | |||
134 | } | 142 | } | ||
135 | 143 | | |||
136 | // | 144 | // | ||
137 | // Posting Iterator | 145 | // Posting Iterator | ||
138 | // | 146 | // | ||
139 | 147 | | |||
140 | PostingIterator* MTimeDB::iter(quint32 mtime, MTimeDB::Comparator com) | 148 | PostingIterator* MTimeDB::iter(quint32 mtime, MTimeDB::Comparator com) | ||
141 | { | 149 | { | ||
142 | if (com == Equal) { | 150 | if (com == Equal) { | ||
143 | return new VectorPostingIterator(get(mtime)); | 151 | return new VectorPostingIterator(get(mtime)); | ||
144 | } | 152 | } | ||
145 | 153 | | |||
146 | MDB_val key; | 154 | MDB_val key; | ||
147 | key.mv_size = sizeof(quint32); | 155 | key.mv_size = sizeof(quint32); | ||
148 | key.mv_data = &mtime; | 156 | key.mv_data = &mtime; | ||
149 | 157 | | |||
150 | MDB_cursor* cursor; | 158 | MDB_cursor* cursor; | ||
151 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 159 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
152 | 160 | | |||
153 | MDB_val val; | 161 | MDB_val val{0, nullptr}; | ||
154 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | 162 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | ||
155 | if (rc == MDB_NOTFOUND) { | 163 | if (rc) { | ||
164 | qCDebug(ENGINE) << "MTimeDB::iter" << mtime << mdb_strerror(rc); | ||||
156 | mdb_cursor_close(cursor); | 165 | mdb_cursor_close(cursor); | ||
157 | return nullptr; | 166 | return nullptr; | ||
158 | } | 167 | } | ||
159 | Q_ASSERT_X(rc == 0, "MTimeDB::iter", mdb_strerror(rc)); | | |||
160 | 168 | | |||
161 | QVector<quint64> results; | 169 | QVector<quint64> results; | ||
162 | results << *static_cast<quint64*>(val.mv_data); | 170 | results << *static_cast<quint64*>(val.mv_data); | ||
163 | 171 | | |||
164 | if (com == GreaterEqual) { | 172 | if (com == GreaterEqual) { | ||
165 | while (1) { | 173 | while (1) { | ||
166 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | 174 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | ||
167 | if (rc == MDB_NOTFOUND) { | 175 | if (rc) { | ||
176 | qCDebug(ENGINE) << "MTimeDB::iter (loop)" << mtime << mdb_strerror(rc); | ||||
168 | break; | 177 | break; | ||
169 | } | 178 | } | ||
170 | Q_ASSERT_X(rc == 0, "MTimeDB::iter >=", mdb_strerror(rc)); | | |||
171 | 179 | | |||
172 | results << *static_cast<quint64*>(val.mv_data); | 180 | results << *static_cast<quint64*>(val.mv_data); | ||
173 | } | 181 | } | ||
174 | } | 182 | } | ||
175 | else { | 183 | else { | ||
176 | while (1) { | 184 | while (1) { | ||
177 | rc = mdb_cursor_get(cursor, &key, &val, MDB_PREV); | 185 | rc = mdb_cursor_get(cursor, &key, &val, MDB_PREV); | ||
178 | if (rc == MDB_NOTFOUND) { | 186 | if (rc) { | ||
187 | qCDebug(ENGINE) << "MTimeDB::iter (loop)" << mtime << mdb_strerror(rc); | ||||
179 | break; | 188 | break; | ||
180 | } | 189 | } | ||
181 | Q_ASSERT_X(rc == 0, "MTimeDB::iter >=", mdb_strerror(rc)); | | |||
182 | 190 | | |||
183 | quint64 id = *static_cast<quint64*>(val.mv_data); | 191 | quint64 id = *static_cast<quint64*>(val.mv_data); | ||
184 | results.push_front(id); | 192 | results.push_front(id); | ||
185 | } | 193 | } | ||
186 | } | 194 | } | ||
187 | 195 | | |||
188 | mdb_cursor_close(cursor); | 196 | mdb_cursor_close(cursor); | ||
189 | std::sort(results.begin(), results.end()); | 197 | std::sort(results.begin(), results.end()); | ||
190 | results.erase(std::unique(results.begin(), results.end()), results.end()); | 198 | results.erase(std::unique(results.begin(), results.end()), results.end()); | ||
191 | return new VectorPostingIterator(results); | 199 | return new VectorPostingIterator(results); | ||
192 | } | 200 | } | ||
193 | 201 | | |||
194 | PostingIterator* MTimeDB::iterRange(quint32 beginTime, quint32 endTime) | 202 | PostingIterator* MTimeDB::iterRange(quint32 beginTime, quint32 endTime) | ||
195 | { | 203 | { | ||
196 | Q_ASSERT(beginTime); | 204 | Q_ASSERT(beginTime); | ||
197 | Q_ASSERT(endTime); | 205 | Q_ASSERT(endTime); | ||
198 | 206 | | |||
199 | MDB_val key; | 207 | MDB_val key; | ||
200 | key.mv_size = sizeof(quint32); | 208 | key.mv_size = sizeof(quint32); | ||
201 | key.mv_data = &beginTime; | 209 | key.mv_data = &beginTime; | ||
202 | 210 | | |||
203 | MDB_cursor* cursor; | 211 | MDB_cursor* cursor; | ||
204 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 212 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
205 | 213 | | |||
206 | MDB_val val; | 214 | MDB_val val{0, nullptr}; | ||
207 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | 215 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_SET_RANGE); | ||
208 | if (rc == MDB_NOTFOUND) { | 216 | if (rc) { | ||
217 | qCDebug(ENGINE) << "MTimeDB::iterRange" << beginTime << endTime << mdb_strerror(rc); | ||||
209 | mdb_cursor_close(cursor); | 218 | mdb_cursor_close(cursor); | ||
210 | return nullptr; | 219 | return nullptr; | ||
211 | } | 220 | } | ||
212 | Q_ASSERT_X(rc == 0, "MTimeDB::iterRange", mdb_strerror(rc)); | | |||
213 | 221 | | |||
214 | QVector<quint64> results; | 222 | QVector<quint64> results; | ||
215 | results << *static_cast<quint64*>(val.mv_data); | 223 | results << *static_cast<quint64*>(val.mv_data); | ||
216 | 224 | | |||
217 | while (1) { | 225 | while (1) { | ||
218 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | 226 | rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | ||
219 | if (rc == MDB_NOTFOUND) { | 227 | if (rc) { | ||
228 | qCDebug(ENGINE) << "MTimeDB::iterRange" << beginTime << endTime << mdb_strerror(rc); | ||||
220 | break; | 229 | break; | ||
221 | } | 230 | } | ||
222 | Q_ASSERT_X(rc == 0, "MTimeDB::iter >=", mdb_strerror(rc)); | | |||
223 | 231 | | |||
224 | quint32 time = *static_cast<quint32*>(key.mv_data); | 232 | quint32 time = *static_cast<quint32*>(key.mv_data); | ||
225 | if (time > endTime) { | 233 | if (time > endTime) { | ||
226 | break; | 234 | break; | ||
227 | } | 235 | } | ||
228 | results << *static_cast<quint64*>(val.mv_data); | 236 | results << *static_cast<quint64*>(val.mv_data); | ||
229 | } | 237 | } | ||
230 | 238 | | |||
Show All 9 Lines | 246 | { | |||
240 | mdb_cursor_open(m_txn, m_dbi, &cursor); | 248 | mdb_cursor_open(m_txn, m_dbi, &cursor); | ||
241 | 249 | | |||
242 | MDB_val key = {0, nullptr}; | 250 | MDB_val key = {0, nullptr}; | ||
243 | MDB_val val; | 251 | MDB_val val; | ||
244 | 252 | | |||
245 | QMap<quint32, quint64> map; | 253 | QMap<quint32, quint64> map; | ||
246 | while (1) { | 254 | while (1) { | ||
247 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | 255 | int rc = mdb_cursor_get(cursor, &key, &val, MDB_NEXT); | ||
248 | if (rc == MDB_NOTFOUND) { | 256 | if (rc) { | ||
257 | qCDebug(ENGINE) << "MTimeDB::toTestMap" << mdb_strerror(rc); | ||||
249 | break; | 258 | break; | ||
250 | } | 259 | } | ||
251 | Q_ASSERT_X(rc == 0, "MTimeDB::toTestMap", mdb_strerror(rc)); | | |||
252 | 260 | | |||
253 | const quint32 time = *(static_cast<quint32*>(key.mv_data)); | 261 | const quint32 time = *(static_cast<quint32*>(key.mv_data)); | ||
254 | const quint64 id = *(static_cast<quint64*>(val.mv_data)); | 262 | const quint64 id = *(static_cast<quint64*>(val.mv_data)); | ||
255 | map.insert(time, id); | 263 | map.insert(time, id); | ||
256 | } | 264 | } | ||
257 | 265 | | |||
258 | mdb_cursor_close(cursor); | 266 | mdb_cursor_close(cursor); | ||
259 | return map; | 267 | return map; | ||
260 | } | 268 | } |
if (rc != MDB_NOTFOUND) for the message