Add ClientDB exists() method

This commit is contained in:
Paul Hauner 2018-09-20 17:36:23 +10:00
parent f64b8e30a1
commit f6330ce967
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
2 changed files with 18 additions and 0 deletions

View File

@ -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<bool, DBError>
{
/*
* 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())
}
}
}

View File

@ -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<bool, DBError>;
}