Skip to content

Commit

Permalink
Merge pull request #14778 from fredmorcos/lmdb-safe-cleanups
Browse files Browse the repository at this point in the history
Some cleanups for lmdb-safe
  • Loading branch information
fredmorcos authored Oct 17, 2024
2 parents 3108147 + 7b71bce commit 271961a
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 215 deletions.
68 changes: 32 additions & 36 deletions ext/lmdb-safe/lmdb-safe.hh
Original file line number Diff line number Diff line change
Expand Up @@ -174,54 +174,50 @@ struct MDBOutVal
}

#ifndef DNSDIST
template <class T,
typename std::enable_if<std::is_integral<T>::value,
T>::type* = nullptr> const
T get()
{
T ret;

size_t offset = LMDBLS::LScheckHeaderAndGetSize(this, sizeof(T));

memcpy(&ret, reinterpret_cast<const char *>(d_mdbval.mv_data)+offset, sizeof(T));

static_assert(sizeof(T) == 4, "this code currently only supports 32 bit integers");
ret = ntohl(ret);
return ret;
}

template <class T,
typename std::enable_if<std::is_integral<T>::value,
T>::type* = nullptr> const
T getNoStripHeader()
{
T ret;
if(d_mdbval.mv_size != sizeof(T))
throw std::runtime_error("MDB data has wrong length for type");

memcpy(&ret, d_mdbval.mv_data, sizeof(T));

static_assert(sizeof(T) == 4, "this code currently only supports 32 bit integers");
ret = ntohl(ret);
return ret;
}
template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
T get() const;

template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* = nullptr>
T getNoStripHeader() const;
#endif /* ifndef DNSDIST */

template <class T,
typename std::enable_if<std::is_class<T>::value,T>::type* = nullptr>
template <class T, typename std::enable_if<std::is_class<T>::value, T>::type* = nullptr>
T get() const;


#ifndef DNSDIST
template <class T,
typename std::enable_if<std::is_class<T>::value,T>::type* = nullptr>
template <class T, typename std::enable_if<std::is_class<T>::value, T>::type* = nullptr>
T getNoStripHeader() const;
#endif

MDB_val d_mdbval;
};

#ifndef DNSDIST
template <>
inline uint32_t MDBOutVal::get<uint32_t>() const
{
uint32_t ret = 0;
size_t offset = LMDBLS::LScheckHeaderAndGetSize(this, sizeof(uint32_t));
// NOLINTNEXTLINE
memcpy(&ret, reinterpret_cast<const char*>(d_mdbval.mv_data) + offset, sizeof(uint32_t));
ret = ntohl(ret);
return ret;
}

template <>
inline uint32_t MDBOutVal::getNoStripHeader() const
{
uint32_t ret = 0;
if (d_mdbval.mv_size != sizeof(uint32_t)) {
throw std::runtime_error("MDB data has wrong length for type");
}

memcpy(&ret, d_mdbval.mv_data, sizeof(uint32_t));
ret = ntohl(ret);
return ret;
}
#endif /* ifndef DNSDIST */

template<> inline std::string MDBOutVal::get<std::string>() const
{
#ifndef DNSDIST
Expand Down
15 changes: 8 additions & 7 deletions ext/lmdb-safe/lmdb-typed.cc
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
#include "lmdb-typed.hh"
#include "pdns/dns_random.hh"

unsigned int MDBGetMaxID(MDBRWTransaction& txn, MDBDbi& dbi)
uint32_t MDBGetMaxID(MDBRWTransaction& txn, MDBDbi& dbi)
{
auto cursor = txn->getRWCursor(dbi);
MDBOutVal maxidval{};
MDBOutVal maxcontent{};
unsigned int maxid{0};
uint32_t maxid{0};
if (cursor.get(maxidval, maxcontent, MDB_LAST) == 0) {
maxid = maxidval.getNoStripHeader<unsigned int>();
maxid = maxidval.getNoStripHeader<uint32_t>();
}
return maxid;
}

unsigned int MDBGetRandomID(MDBRWTransaction& txn, MDBDbi& dbi)
uint32_t MDBGetRandomID(MDBRWTransaction& txn, MDBDbi& dbi)
{
auto cursor = txn->getRWCursor(dbi);
unsigned int newID = 0;
uint32_t newID = 0;
for (int attempts = 0; attempts < 20; attempts++) {
MDBOutVal key{};
MDBOutVal content{};

// dns_random generates a random number in [0..signed_int_max-1]. We add 1 to avoid 0 and allow type_max.
// 0 is avoided because the put() interface uses it to mean "please allocate a number for me"
// dns_random generates a random number in [0..signed_int_max-1]. We add 1 to avoid 0
// and allow type_max. 0 is avoided because the put() interface uses it to mean
// "please allocate a number for me".
newID = dns_random(std::numeric_limits<signed int>::max()) + 1;
if (cursor.find(MDBInVal(newID), key, content) != 0) {
return newID;
Expand Down
Loading

0 comments on commit 271961a

Please sign in to comment.