Speed-up cleaning DB/indexes via `mdb_drop` instead of manual deletions

This commit is contained in:
Martchus 2022-01-26 00:43:21 +01:00
parent 31a45912dc
commit adcb9338e8
1 changed files with 21 additions and 10 deletions

View File

@ -80,6 +80,13 @@ struct LMDBIndexOps
} }
} }
void clear(MDBRWTransaction& txn)
{
if (const auto rc = mdb_drop(*txn, d_idx, 0)) {
throw LMDBError("Error clearing index: ", rc);
}
}
void openDB(std::shared_ptr<MDBEnv>& env, string_view str, unsigned int flags) void openDB(std::shared_ptr<MDBEnv>& env, string_view str, unsigned int flags)
{ {
d_idx = env->openDB(str, flags); d_idx = env->openDB(str, flags);
@ -136,6 +143,11 @@ struct nullindex_t
(void)t; (void)t;
(void)id; (void)id;
} }
template<typename Class>
void clear(Class& txn)
{
(void)txn;
}
void openDB(std::shared_ptr<MDBEnv>& env, string_view str, unsigned int flags) void openDB(std::shared_ptr<MDBEnv>& env, string_view str, unsigned int flags)
{ {
@ -614,19 +626,18 @@ public:
clearIndex(id, t); clearIndex(id, t);
} }
//! clear database & indexes (by hand!) //! clear database & indexes
void clear() void clear()
{ {
auto cursor = (*d_txn)->getRWCursor(d_parent->d_main); if (const auto rc = mdb_drop(**d_txn, d_parent->d_main, 0)) {
bool first = true; throw LMDBError("Error database: ", rc);
MDBOutVal key, data;
while(!cursor.get(key, data, first ? MDB_FIRST : MDB_NEXT)) {
first = false;
T t;
serFromString(data.get<string_view>(), t);
clearIndex(key.get<uint32_t>(), t);
cursor.del();
} }
#define clearMacro(N) std::get<N>(d_parent->d_tuple).clear(*d_txn);
clearMacro(0);
clearMacro(1);
clearMacro(2);
clearMacro(3);
#undef clearMacro
} }
//! commit this transaction //! commit this transaction