]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Handle marked-as-deleted elements in ReadonlyOperations::get<> 15347/head
authorMiod Vallat <miod.vallat@powerdns.com>
Fri, 21 Mar 2025 11:00:56 +0000 (12:00 +0100)
committerMiod Vallat <miod.vallat@powerdns.com>
Mon, 24 Mar 2025 06:36:18 +0000 (07:36 +0100)
(cherry picked from commit 1d8242cd7f4e4235c6a2aeb43574121dce2122c8)

ext/lmdb-safe/lmdb-typed.hh

index ae32d78fd67b26a37bdcd969a6c2f4b2253d74d5..7b8b86704487b17a51f4fa07140e407f895d0638 100644 (file)
@@ -284,14 +284,19 @@ public:
     // }
 
     //! Get item with id, from main table directly
-    bool get(uint32_t id, T& t)
+    int get2(uint32_t itemId, T& value)
     {
-      MDBOutVal data;
-      if((*d_parent.d_txn)->get(d_parent.d_parent->d_main, id, data))
-        return false;
-
-      serFromString(data.get<std::string>(), t);
-      return true;
+      MDBOutVal data{};
+      int rc;
+      rc = (*d_parent.d_txn)->get(d_parent.d_parent->d_main, itemId, data);
+      if (rc == 0) {
+        serFromString(data.get<std::string>(), value);
+      }
+      return rc;
+    }
+    bool get(uint32_t itemId, T& value)
+    {
+      return get2(itemId, value) == 0;
     }
 
     //! Get item through index N, then via the main database
@@ -309,17 +314,24 @@ public:
       // because we know we only want one item, pass onlyOldest=true to consistently get the same one out of a set of duplicates
       get_multi<N>(key, ids, true);
 
-      if (ids.size() == 0) {
+      switch (ids.size()) {
+      case 0:
         return 0;
-      }
-
-      if (ids.size() == 1) {
-        if (get(ids[0], out)) {
+      case 1: {
+        auto rc = get2(ids[0], out);
+        if (rc == 0) {
           return ids[0];
         }
+        if (rc == MDB_NOTFOUND) {
+          /* element not present, or has been marked deleted */
+          return 0;
+        }
+        throw std::runtime_error("in index get, failed (" + std::to_string(rc) + ")");
+        break;
+      }
+      default:
+        throw std::runtime_error("in index get, found more than one item");
       }
-
-      throw std::runtime_error("in index get, found more than one item");
     }
 
     // //! Cardinality of index N