From: Peter van Dijk Date: Fri, 28 Apr 2023 12:15:11 +0000 (+0200) Subject: lmdb-typed: teach get() to optionally fetch just the oldest entry X-Git-Tag: auth-4.8.0-beta1~1^2~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=33f44dc6c07ea9ae3c6e245b672ca413af9549e1;p=thirdparty%2Fpdns.git lmdb-typed: teach get() to optionally fetch just the oldest entry --- diff --git a/ext/lmdb-safe/lmdb-typed.hh b/ext/lmdb-safe/lmdb-typed.hh index a0845273b3..c83ed39859 100644 --- a/ext/lmdb-safe/lmdb-typed.hh +++ b/ext/lmdb-safe/lmdb-typed.hh @@ -310,7 +310,7 @@ public: // auto range = prefix_range(key); LMDBIDvec ids; - get_multi(key, ids); + get_multi(key, ids, true); if (ids.size() == 0) { return 0; @@ -646,7 +646,7 @@ public: }; template - void get_multi(const typename std::tuple_element::type::type& key, LMDBIDvec& ids) + void get_multi(const typename std::tuple_element::type::type& key, LMDBIDvec& ids, bool onlyOldest=false) { // std::cerr<<"in get_multi"<getCursor(std::get(d_parent.d_parent->d_tuple).d_idx); @@ -658,6 +658,9 @@ public: int rc = cursor.get(out, id, MDB_SET_RANGE); + uint64_t oldestts = UINT64_MAX; + uint32_t oldestid = 0; + while (rc == 0) { auto sout = out.getNoStripHeader(); // FIXME: this (and many others) could probably be string_view auto thiskey = getKeyFromCombinedKey(out); @@ -670,7 +673,19 @@ public: if (sthiskey == keyString) { auto _id = getIDFromCombinedKey(out); - ids.push_back(_id.getNoStripHeader()); + uint64_t ts = LMDBLS::LSgetTimestamp(id.getNoStripHeader()); + uint32_t __id = _id.getNoStripHeader(); + + if (ts < oldestts) { + oldestts = ts; + oldestid = __id; + } + ids.push_back(__id); + } + + if (onlyOldest && ids.size() > 1) { + ids.clear(); + ids.push_back(oldestid); } rc = cursor.get(out, id, MDB_NEXT);