From 5b177a80b9d5ab43ba9e2aac878165b1ccfc29ef Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 21 Sep 2018 14:08:07 +1000 Subject: [PATCH] Add comments, fix warning in MemoryDB --- lighthouse/db/memory_db.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lighthouse/db/memory_db.rs b/lighthouse/db/memory_db.rs index 65b2b8629..b7ca658bb 100644 --- a/lighthouse/db/memory_db.rs +++ b/lighthouse/db/memory_db.rs @@ -10,14 +10,22 @@ use super::{ type DBHashMap = HashMap, Vec>; type ColumnHashSet = HashSet; +/// An in-memory database implementing the ClientDB trait. +/// +/// It is not particularily optimized, it exists for ease and speed of testing. It's not expected +/// this DB would be used outside of tests. pub struct MemoryDB { db: RwLock, known_columns: RwLock } impl MemoryDB { + /// Open the in-memory database. + /// + /// All columns must be supplied initially, you will get an error if you try to access a column + /// that was not declared here. This condition is enforced artificially to simulate RocksDB. pub fn open(columns: Option<&[&str]>) -> Self { - let mut db: DBHashMap = HashMap::new(); + let db: DBHashMap = HashMap::new(); let mut known_columns: ColumnHashSet = HashSet::new(); if let Some(columns) = columns { for col in columns { @@ -30,6 +38,7 @@ impl MemoryDB { } } + /// Hashes a key and a column name in order to get a unique key for the supplied column. fn get_key_for_col(col: &str, key: &[u8]) -> Vec { blake2b(32, col.as_bytes(), key).as_bytes().to_vec() } @@ -42,6 +51,7 @@ impl ClientDB for MemoryDB { Ok(()) // This field is not used. Will remove from trait. } + /// Get the value of some key from the database. Returns `None` if the key does not exist. fn get(&self, col: &str, key: &[u8]) -> Result, DBError> { @@ -58,6 +68,7 @@ impl ClientDB for MemoryDB { } } + /// Puts a key in the database. fn put(&self, col: &str, key: &[u8], val: &[u8]) -> Result<(), DBError> {