diff --git a/firewood/src/db.rs b/firewood/src/db.rs index 600702174..cbab34d65 100644 --- a/firewood/src/db.rs +++ b/firewood/src/db.rs @@ -14,8 +14,9 @@ use crate::{ CachedSpace, MemStoreR, SpaceWrite, StoreConfig, StoreDelta, StoreRevMut, StoreRevShared, ZeroStore, PAGE_SIZE_NBIT, }, - v2::api::Proof, + v2::api::{self, Proof}, }; +use async_trait::async_trait; use bytemuck::{cast_slice, AnyBitPattern}; use metered::{metered, HitCount}; use parking_lot::{Mutex, RwLock}; @@ -28,7 +29,7 @@ use std::{ collections::VecDeque, error::Error, fmt, - io::{Cursor, Write}, + io::{Cursor, ErrorKind, Write}, mem::size_of, num::NonZeroUsize, os::fd::{AsFd, BorrowedFd}, @@ -272,6 +273,43 @@ pub struct DbRev { merkle: Merkle, } +#[async_trait] +impl + Send + Sync> api::DbView for DbRev { + async fn root_hash(&self) -> Result { + self.merkle + .root_hash(self.header.kv_root) + .map(|h| *h) + .map_err(|e| api::Error::IO(std::io::Error::new(ErrorKind::Other, e))) + } + + async fn val(&self, key: K) -> Result>, api::Error> { + let obj_ref = self.merkle.get(key, self.header.kv_root); + match obj_ref { + Err(e) => Err(api::Error::IO(std::io::Error::new(ErrorKind::Other, e))), + Ok(obj) => match obj { + None => Ok(None), + Some(inner) => Ok(Some(inner.as_ref().to_owned())), + }, + } + } + + async fn single_key_proof + Send>( + &self, + _key: K, + ) -> Result>, api::Error> { + todo!() + } + + async fn range_proof( + &self, + _first_key: Option, + _last_key: Option, + _limit: usize, + ) -> Result>, api::Error> { + todo!() + } +} + impl + Send + Sync> DbRev { fn flush_dirty(&mut self) -> Option<()> { self.header.flush_dirty(); diff --git a/firewood/src/v2/api.rs b/firewood/src/v2/api.rs index 51fea04e0..7ce46a4a9 100644 --- a/firewood/src/v2/api.rs +++ b/firewood/src/v2/api.rs @@ -136,7 +136,7 @@ pub trait DbView { async fn root_hash(&self) -> Result; /// Get the value of a specific key - async fn val(&self, key: K) -> Result, Error>; + async fn val(&self, key: K) -> Result>, Error>; /// Obtain a proof for a single key async fn single_key_proof( diff --git a/firewood/src/v2/db.rs b/firewood/src/v2/db.rs index dfa342242..ad5e4ac76 100644 --- a/firewood/src/v2/db.rs +++ b/firewood/src/v2/db.rs @@ -75,7 +75,7 @@ impl api::DbView for DbView { todo!() } - async fn val(&self, _key: K) -> Result, api::Error> { + async fn val(&self, _key: K) -> Result>, api::Error> { todo!() } diff --git a/firewood/src/v2/emptydb.rs b/firewood/src/v2/emptydb.rs index c7c8839b0..677f6de85 100644 --- a/firewood/src/v2/emptydb.rs +++ b/firewood/src/v2/emptydb.rs @@ -58,7 +58,7 @@ impl DbView for HistoricalImpl { Ok(ROOT_HASH) } - async fn val(&self, _key: K) -> Result, Error> { + async fn val(&self, _key: K) -> Result>, Error> { Ok(None) } diff --git a/firewood/src/v2/propose.rs b/firewood/src/v2/propose.rs index f2cb1f6c7..6467a6920 100644 --- a/firewood/src/v2/propose.rs +++ b/firewood/src/v2/propose.rs @@ -105,12 +105,12 @@ impl api::DbView for Proposal { todo!() } - async fn val(&self, key: K) -> Result, api::Error> { + async fn val(&self, key: K) -> Result>, api::Error> { // see if this key is in this proposal match self.delta.get(key.as_ref()) { Some(change) => match change { // key in proposal, check for Put or Delete - KeyOp::Put(val) => Ok(Some(val)), + KeyOp::Put(val) => Ok(Some(val.to_owned())), KeyOp::Delete => Ok(None), // key was deleted in this proposal }, None => match &self.base {