Make the rocksdb and leveldb stores generic over hash

This commit is contained in:
Jeremy Wall 2022-08-24 15:43:00 -04:00
parent 0e3e252570
commit 3110917edb
2 changed files with 12 additions and 8 deletions

View File

@ -18,11 +18,11 @@ use std::cell::RefCell;
use std::path::Path;
use crate::{
hash::HashWriter,
node::Node,
store::{Result, Store, StoreError},
};
use crate::blake2::*;
use ciborium;
use rusty_leveldb;
@ -42,12 +42,15 @@ impl LevelStore {
}
}
impl Store<Blake2b512> for LevelStore {
impl<HW> Store<HW> for LevelStore
where
HW: HashWriter,
{
fn contains(&self, id: &[u8]) -> Result<bool> {
Ok(self.store.borrow_mut().get(id).is_some())
}
fn get(&self, id: &[u8]) -> Result<Option<Node<Blake2b512>>> {
fn get(&self, id: &[u8]) -> Result<Option<Node<HW>>> {
Ok(match self.store.borrow_mut().get(id) {
Some(bs) => ciborium::de::from_reader(bs.as_slice())
.map_err(|e| StoreError::StoreFailure(format!("Invalid serialization {:?}", e)))?,
@ -55,7 +58,7 @@ impl Store<Blake2b512> for LevelStore {
})
}
fn store(&mut self, node: Node<Blake2b512>) -> Result<()> {
fn store(&mut self, node: Node<HW>) -> Result<()> {
let mut buf = Vec::new();
ciborium::ser::into_writer(&node, &mut buf).unwrap();
self.store.borrow_mut().put(node.id(), &buf)?;

View File

@ -16,8 +16,8 @@
use std::path::Path;
use crate::blake2::*;
use crate::{
hash::HashWriter,
node::Node,
store::{Result as StoreResult, Store, StoreError},
};
@ -53,9 +53,10 @@ where
}
}
impl<TM> Store<Blake2b512> for RocksStore<TM>
impl<TM, HW> Store<HW> for RocksStore<TM>
where
TM: ThreadMode,
HW: HashWriter,
{
fn contains(&self, id: &[u8]) -> StoreResult<bool> {
Ok(self
@ -65,7 +66,7 @@ where
.is_some())
}
fn get(&self, id: &[u8]) -> StoreResult<Option<Node<Blake2b512>>> {
fn get(&self, id: &[u8]) -> StoreResult<Option<Node<HW>>> {
Ok(
match self
.store
@ -80,7 +81,7 @@ where
)
}
fn store(&mut self, node: Node<Blake2b512>) -> StoreResult<()> {
fn store(&mut self, node: Node<HW>) -> StoreResult<()> {
let mut buf = Vec::new();
ciborium::ser::into_writer(&node, &mut buf).unwrap();
self.store.put(node.id(), &buf)?;