From: Fred Morcos Date: Wed, 16 Oct 2024 11:08:26 +0000 (+0200) Subject: Clang-tidy cleanups of lmdb-typed X-Git-Tag: rec-5.2.0-alpha1~26^2~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0e006de9f23b0cc49b4a4cf8dae4f92923f905f9;p=thirdparty%2Fpdns.git Clang-tidy cleanups of lmdb-typed --- diff --git a/ext/lmdb-safe/lmdb-typed.hh b/ext/lmdb-safe/lmdb-typed.hh index 14e775e1bc..a55f67d63a 100644 --- a/ext/lmdb-safe/lmdb-typed.hh +++ b/ext/lmdb-safe/lmdb-typed.hh @@ -1,9 +1,6 @@ #pragma once #include -#include -#include -#include #include #include @@ -13,6 +10,7 @@ #include #include #include +#include #include "lmdb-safe.hh" @@ -31,7 +29,7 @@ /** * LMDB ID Vector Type. */ -typedef std::vector LmdbIdVec; +using LmdbIdVec = std::vector; /** * Return the highest ID used in a database. Returns 0 for an empty DB. This makes us @@ -75,7 +73,7 @@ inline std::string keyConv(const T& value); template ::value, T>::type* = nullptr> inline std::string keyConv(const T& value) { - return std::string((char*)&value, sizeof(value)); + return string{(char*)&value, sizeof(value)}; } /** @@ -93,7 +91,7 @@ namespace { throw std::runtime_error("combined key too short to get ID from"); } - MDBOutVal ret; + MDBOutVal ret{}; ret.d_mdbval.mv_data = combined.d_mdbval.mv_data; ret.d_mdbval.mv_size = combined.d_mdbval.mv_size - sizeof(uint32_t); @@ -105,7 +103,7 @@ namespace { throw std::runtime_error("combined key too short to get ID from"); } - MDBOutVal ret; + MDBOutVal ret{}; ret.d_mdbval.mv_data = (char*) combined.d_mdbval.mv_data + combined.d_mdbval.mv_size - sizeof(uint32_t); ret.d_mdbval.mv_size = sizeof(uint32_t); @@ -149,7 +147,7 @@ struct LMDBIndexOps void put(MDBRWTransaction& txn, const Class& t, uint32_t id, int flags=0) { - std::string sempty(""); + std::string sempty; MDBInVal empty(sempty); auto scombined = makeCombinedKey(keyConv(d_parent->getMember(t)), id); @@ -164,8 +162,9 @@ struct LMDBIndexOps auto scombined = makeCombinedKey(keyConv(d_parent->getMember(t)), id); MDBInVal combined(scombined); - if(int rc = txn->del(d_idx, combined)) { - throw std::runtime_error("Error deleting from index: " + std::string(mdb_strerror(rc))); + int errCode = txn->del(d_idx, combined); + if (errCode != 0) { + throw std::runtime_error("Error deleting from index: " + std::string(mdb_strerror(errCode))); } } @@ -190,7 +189,7 @@ struct index_on : LMDBIndexOps> return c.*PtrToMember; } - typedef Type type; + using type = Type; }; /** This is a calculated index */ @@ -205,7 +204,7 @@ struct index_on_function : LMDBIndexOps etc - typedef std::tuple tuple_t; + using tuple_t = std::tuple; tuple_t d_tuple; // We support readonly and rw transactions. Here we put the Readonly operations @@ -279,9 +278,10 @@ public: //! Get item with id, from main table directly bool get(uint32_t id, T& t) { - MDBOutVal data; - if((*d_parent.d_txn)->get(d_parent.d_parent->d_main, id, data)) + MDBOutVal data{}; + if((*d_parent.d_txn)->get(d_parent.d_parent->d_main, id, data)) { return false; + } deserializeFromBuffer(data.get(), t); return true; @@ -367,44 +367,46 @@ public: // id.d_mdbval.mv_size -= LS_HEADER_SIZE; // id.d_mdbval.mv_data = (char*)d_id.d_mdbval.mv_data + LS_HEADER_SIZE; - - if(d_on_index) { - if((*d_parent->d_txn)->get(d_parent->d_parent->d_main, d_id, d_data)) + if (d_on_index) { + if ((*d_parent->d_txn)->get(d_parent->d_parent->d_main, d_id, d_data)) { throw std::runtime_error("Missing id in constructor"); + } deserializeFromBuffer(d_data.get(), d_t); } - else + else { deserializeFromBuffer(d_id.get(), d_t); + } } - explicit iter_t(Parent* parent, typename Parent::cursor_t&& cursor, const std::string& prefix) : + explicit iter_t(Parent* parent, typename Parent::cursor_t&& cursor, std::string prefix) : d_parent(parent), d_cursor(std::move(cursor)), d_on_index(true), // is this an iterator on main database or on index? d_one_key(false), - d_prefix(prefix), - d_end(false) + d_prefix(std::move(prefix)) { - if(d_end) + if (d_end) { return; + } - if(d_cursor.get(d_key, d_id, MDB_GET_CURRENT)) { + if (d_cursor.get(d_key, d_id, MDB_GET_CURRENT)) { d_end = true; return; } d_id = getIDFromCombinedKey(d_key); - if(d_on_index) { - if((*d_parent->d_txn)->get(d_parent->d_parent->d_main, d_id, d_data)) + if (d_on_index) { + if ((*d_parent->d_txn)->get(d_parent->d_parent->d_main, d_id, d_data)) { throw std::runtime_error("Missing id in constructor"); + } deserializeFromBuffer(d_data.get(), d_t); } - else + else { deserializeFromBuffer(d_id.get(), d_t); + } } - // std::function filter; void del() { @@ -434,8 +436,8 @@ public: // implements generic ++ or -- iter_t& genoperator(MDB_cursor_op op) { - MDBOutVal data; - int rc; + MDBOutVal data{}; + int rc = 0; // next:; if (!d_one_key) { rc = d_cursor.get(d_key, d_id, op); @@ -443,7 +445,7 @@ public: if(d_one_key || rc == MDB_NOTFOUND) { d_end = true; } - else if(rc) { + else if(rc != 0) { throw std::runtime_error("in genoperator, " + std::string(mdb_strerror(rc))); } else if(!d_prefix.empty() && @@ -461,8 +463,9 @@ public: if(d_on_index) { d_id = getIDFromCombinedKey(d_key); - if((*d_parent->d_txn)->get(d_parent->d_parent->d_main, d_id, data)) + if ((*d_parent->d_txn)->get(d_parent->d_parent->d_main, d_id, data)) { throw std::runtime_error("Missing id field"); + } // if(filter && !filter(data)) // goto next; @@ -490,13 +493,11 @@ public: // get ID this iterator points to uint32_t getID() { - if(d_on_index) { + if (d_on_index) { // return d_id.get(); return d_id.getNoStripHeader(); } - else { - return d_key.getNoStripHeader(); - } + return d_key.getNoStripHeader(); } const MDBOutVal& getKey() @@ -504,13 +505,14 @@ public: return d_key; } - // transaction we are part of Parent* d_parent; typename Parent::cursor_t d_cursor; // gcc complains if I don't zero-init these, which is worrying XXX - MDBOutVal d_key{{0,0}}, d_data{{0,0}}, d_id{{0,0}}; + MDBOutVal d_key{{0, nullptr}}; + MDBOutVal d_data{{0, nullptr}}; + MDBOutVal d_id{{0, nullptr}}; bool d_on_index; bool d_one_key; std::string d_prefix; @@ -523,7 +525,8 @@ public: { typename Parent::cursor_t cursor = (*d_parent.d_txn)->getCursor(std::get(d_parent.d_parent->d_tuple).d_idx); - MDBOutVal out, id; + MDBOutVal out{}; + MDBOutVal id{}; if(cursor.get(out, id, op)) { // on_index, one_key, end @@ -549,7 +552,8 @@ public: { typename Parent::cursor_t cursor = (*d_parent.d_txn)->getCursor(d_parent.d_parent->d_main); - MDBOutVal out, id; + MDBOutVal out{}; + MDBOutVal id{}; if(cursor.get(out, id, MDB_FIRST)) { // on_index, one_key, end @@ -572,7 +576,8 @@ public: std::string keystr = makeCombinedKey(keyConv(key), MDBInVal("")); MDBInVal in(keystr); - MDBOutVal out, id; + MDBOutVal out{}; + MDBOutVal id{}; out.d_mdbval = in.d_mdbval; if(cursor.get(out, id, op)) { @@ -604,7 +609,8 @@ public: std::string keyString=makeCombinedKey(keyConv(key), MDBInVal("")); MDBInVal in(keyString); - MDBOutVal out, id; + MDBOutVal out{}; + MDBOutVal id{}; out.d_mdbval = in.d_mdbval; if(cursor.get(out, id, MDB_SET)) { @@ -623,7 +629,8 @@ public: std::string keyString=makeCombined(keyConv(key), MDBInVal("")); MDBInVal in(keyString); - MDBOutVal out, id; + MDBOutVal out{}; + MDBOutVal id{}; out.d_mdbval = in.d_mdbval; if(cursor.get(out, id, MDB_SET_RANGE) || @@ -643,7 +650,8 @@ public: std::string keyString=makeCombinedKey(keyConv(key), MDBInVal("")); MDBInVal in(keyString); - MDBOutVal out, id; + MDBOutVal out{}; + MDBOutVal id{}; out.d_mdbval = in.d_mdbval; int rc = cursor.get(out, id, MDB_SET_RANGE); @@ -664,7 +672,7 @@ public: if (sthiskey == keyString) { auto _id = getIDFromCombinedKey(out); uint64_t ts = LMDBLS::LSgetTimestamp(id.getNoStripHeader()); - uint32_t __id = _id.getNoStripHeader(); + auto __id = _id.getNoStripHeader(); if (onlyOldest) { if (ts < oldestts) { @@ -715,26 +723,24 @@ public: return d_txn; } - typedef MDBROCursor cursor_t; + using cursor_t = MDBROCursor; TypedDBI* d_parent; std::shared_ptr d_txn; }; - class RWTransaction : public ReadonlyOperations { public: - explicit RWTransaction(TypedDBI* parent) : ReadonlyOperations(*this), d_parent(parent) + explicit RWTransaction(TypedDBI* parent) : + ReadonlyOperations(*this), d_parent(parent), d_txn(std::make_shared(d_parent->d_env->getRWTransaction())) { - d_txn = std::make_shared(d_parent->d_env->getRWTransaction()); } explicit RWTransaction(TypedDBI* parent, std::shared_ptr txn) : ReadonlyOperations(*this), d_parent(parent), d_txn(txn) { } - RWTransaction(RWTransaction&& rhs) : ReadonlyOperations(*this), d_parent(rhs.d_parent), d_txn(std::move(rhs.d_txn)) @@ -772,8 +778,9 @@ public: void modify(uint32_t id, std::function func) { T t; - if(!this->get(id, t)) - throw std::runtime_error("Could not modify id "+std::to_string(id)); + if (!this->get(id, t)) { + throw std::runtime_error("Could not modify id " + std::to_string(id)); + } func(t); del(id); // this is the lazy way. We could test for changed index fields @@ -784,8 +791,9 @@ public: void del(uint32_t id) { T t; - if(!this->get(id, t)) + if (!this->get(id, t)) { return; + } (*d_txn)->del(d_parent->d_main, id); clearIndex(id, t); @@ -796,7 +804,8 @@ public: { auto cursor = (*d_txn)->getRWCursor(d_parent->d_main); bool first = true; - MDBOutVal key, data; + MDBOutVal key{}; + MDBOutVal data{}; while(!cursor.get(key, data, first ? MDB_FIRST : MDB_NEXT)) { first = false; T t; @@ -818,7 +827,7 @@ public: (*d_txn)->abort(); } - typedef MDBRWCursor cursor_t; + using cursor_t = MDBRWCursor; std::shared_ptr getTransactionHandle() {