From 5ff25e5ea0821bcef6f7093c8cccc1774cab9143 Mon Sep 17 00:00:00 2001 From: Peter Goodspeed-Niklaus Date: Mon, 27 Jul 2020 15:19:32 +0200 Subject: [PATCH] Add automatic methods to KeyValueDB which test for the existence of certain objects Motivation: We have a use case in which we care about a large object's existence, but don't care to load megabytes of data from disc. We have only a `dyn KeyValueDB` handle available; whatever we use has to be part of the trait. By adding automatic implementations for the new methods, we avoid breaking existing code which is no worse than the existing strategy. Part 2 of this fix will be tracking down the concrete type in use and implementing specializations of those methods, so that we actually do save work, but that's dependent on this PR being merged. --- kvdb/src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/kvdb/src/lib.rs b/kvdb/src/lib.rs index 7cacff666..f4d553583 100644 --- a/kvdb/src/lib.rs +++ b/kvdb/src/lib.rs @@ -139,6 +139,16 @@ pub trait KeyValueDB: Sync + Send + parity_util_mem::MallocSizeOf { fn io_stats(&self, _kind: IoStatsKind) -> IoStats { IoStats::empty() } + + /// Check for the existence of a value by key. + fn has_key(&self, col: u32, key: &[u8]) -> io::Result { + self.get(col, key).map(|opt| opt.is_some()) + } + + /// Check for the existence of a value by prefix. + fn has_prefix(&self, col: u32, prefix: &[u8]) -> bool { + self.get_by_prefix(col, prefix).is_some() + } } /// For a given start prefix (inclusive), returns the correct end prefix (non-inclusive).