From f6330ce9679409259834e59c8a109d8337b3a69f Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 20 Sep 2018 17:36:23 +1000 Subject: [PATCH] Add ClientDB `exists()` method --- lighthouse/db/disk_db.rs | 15 +++++++++++++++ lighthouse/db/traits.rs | 3 +++ 2 files changed, 18 insertions(+) diff --git a/lighthouse/db/disk_db.rs b/lighthouse/db/disk_db.rs index e4ebdedec..d0f6eb970 100644 --- a/lighthouse/db/disk_db.rs +++ b/lighthouse/db/disk_db.rs @@ -107,6 +107,21 @@ impl ClientDB for DiskDB { Some(handle) => self.db.put_cf(handle, key, val).map_err(|e| e.into()) } } + + /// Return true if some key exists in some column. + fn exists(&self, col: &str, key: &[u8]) + -> Result + { + /* + * I'm not sure if this is the correct way to read if some + * block exists. Naievely I would expect this to unncessarily + * copy some data, but I could be wrong. + */ + match self.db.cf_handle(col) { + None => Err(DBError{ message: "Unknown column".to_string() }), + Some(handle) => Ok(self.db.get_cf(handle, key)?.is_some()) + } + } } diff --git a/lighthouse/db/traits.rs b/lighthouse/db/traits.rs index 97759d3b7..35feda243 100644 --- a/lighthouse/db/traits.rs +++ b/lighthouse/db/traits.rs @@ -26,5 +26,8 @@ pub trait ClientDB: Sync + Send { fn put(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), DBError>; + + fn exists(&self, col: &str, key: &[u8]) + -> Result; }