Implement all methods on Store
This commit is contained in:
parent
ebe47a5b34
commit
d6664cb4ac
@ -83,8 +83,6 @@ where
|
|||||||
T: ClientDB,
|
T: ClientDB,
|
||||||
{
|
{
|
||||||
/// Put `item` in the store as `key`.
|
/// Put `item` in the store as `key`.
|
||||||
///
|
|
||||||
/// The `item` must implement `DBRecord` which defines the db column used.
|
|
||||||
fn put<I>(&self, key: &Hash256, item: &I) -> Result<(), Error>
|
fn put<I>(&self, key: &Hash256, item: &I) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
I: DBRecord,
|
I: DBRecord,
|
||||||
@ -96,9 +94,7 @@ where
|
|||||||
self.db.put(column, key, &val).map_err(|e| e.into())
|
self.db.put(column, key, &val).map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves an `Ok(Some(item)` from the store if `key` exists, otherwise returns `Ok(None)`.
|
/// Retrieves an `Ok(Some(item))` from the store if `key` exists, otherwise returns `Ok(None)`.
|
||||||
///
|
|
||||||
/// The `item` must implement `DBRecord` which defines the db column used.
|
|
||||||
fn get<I>(&self, key: &Hash256) -> Result<Option<I>, Error>
|
fn get<I>(&self, key: &Hash256) -> Result<Option<I>, Error>
|
||||||
where
|
where
|
||||||
I: DBRecord,
|
I: DBRecord,
|
||||||
@ -114,6 +110,28 @@ where
|
|||||||
None => Ok(None),
|
None => Ok(None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `Ok(true)` `key` exists in the store.
|
||||||
|
fn exists<I>(&self, key: &Hash256) -> Result<bool, Error>
|
||||||
|
where
|
||||||
|
I: DBRecord,
|
||||||
|
{
|
||||||
|
let column = I::db_column().into();
|
||||||
|
let key = key.as_bytes();
|
||||||
|
|
||||||
|
self.db.exists(column, key).map_err(|e| e.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `Ok(())` if `key` was deleted from the database or did not exist.
|
||||||
|
fn delete<I>(&self, key: &Hash256) -> Result<(), Error>
|
||||||
|
where
|
||||||
|
I: DBRecord,
|
||||||
|
{
|
||||||
|
let column = I::db_column().into();
|
||||||
|
let key = key.as_bytes();
|
||||||
|
|
||||||
|
self.db.delete(column, key).map_err(|e| e.into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -136,7 +154,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn memorydb_can_store() {
|
fn memorydb_can_store_and_retrieve() {
|
||||||
let store = Store::new_in_memory();
|
let store = Store::new_in_memory();
|
||||||
|
|
||||||
let key = Hash256::random();
|
let key = Hash256::random();
|
||||||
@ -148,4 +166,21 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(item, retrieved);
|
assert_eq!(item, retrieved);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn exists() {
|
||||||
|
let store = Store::new_in_memory();
|
||||||
|
let key = Hash256::random();
|
||||||
|
let item = StorableThing { a: 1, b: 42 };
|
||||||
|
|
||||||
|
assert_eq!(store.exists::<StorableThing>(&key).unwrap(), false);
|
||||||
|
|
||||||
|
store.put(&key, &item).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(store.exists::<StorableThing>(&key).unwrap(), true);
|
||||||
|
|
||||||
|
store.delete::<StorableThing>(&key).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(store.exists::<StorableThing>(&key).unwrap(), false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user