Skip to content

Commit

Permalink
1/3 Implement improved API implementation for database (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkuris authored Sep 28, 2023
1 parent 1c6016d commit 646a04a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
42 changes: 40 additions & 2 deletions firewood/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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},
Expand Down Expand Up @@ -272,6 +273,43 @@ pub struct DbRev<S> {
merkle: Merkle<S>,
}

#[async_trait]
impl<S: ShaleStore<Node> + Send + Sync> api::DbView for DbRev<S> {
async fn root_hash(&self) -> Result<api::HashKey, api::Error> {
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<K: api::KeyType>(&self, key: K) -> Result<Option<Vec<u8>>, 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<K: api::KeyType, N: AsRef<[u8]> + Send>(
&self,
_key: K,
) -> Result<Option<Proof<N>>, api::Error> {
todo!()
}

async fn range_proof<K: api::KeyType, V, N>(
&self,
_first_key: Option<K>,
_last_key: Option<K>,
_limit: usize,
) -> Result<Option<api::RangeProof<K, V, N>>, api::Error> {
todo!()
}
}

impl<S: ShaleStore<Node> + Send + Sync> DbRev<S> {
fn flush_dirty(&mut self) -> Option<()> {
self.header.flush_dirty();
Expand Down
2 changes: 1 addition & 1 deletion firewood/src/v2/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub trait DbView {
async fn root_hash(&self) -> Result<HashKey, Error>;

/// Get the value of a specific key
async fn val<K: KeyType>(&self, key: K) -> Result<Option<&[u8]>, Error>;
async fn val<K: KeyType>(&self, key: K) -> Result<Option<Vec<u8>>, Error>;

/// Obtain a proof for a single key
async fn single_key_proof<K: KeyType, V: ValueType>(
Expand Down
2 changes: 1 addition & 1 deletion firewood/src/v2/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl api::DbView for DbView {
todo!()
}

async fn val<K: KeyType>(&self, _key: K) -> Result<Option<&[u8]>, api::Error> {
async fn val<K: KeyType>(&self, _key: K) -> Result<Option<Vec<u8>>, api::Error> {
todo!()
}

Expand Down
2 changes: 1 addition & 1 deletion firewood/src/v2/emptydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl DbView for HistoricalImpl {
Ok(ROOT_HASH)
}

async fn val<K: KeyType>(&self, _key: K) -> Result<Option<&[u8]>, Error> {
async fn val<K: KeyType>(&self, _key: K) -> Result<Option<Vec<u8>>, Error> {
Ok(None)
}

Expand Down
4 changes: 2 additions & 2 deletions firewood/src/v2/propose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ impl<T: api::DbView + Send + Sync> api::DbView for Proposal<T> {
todo!()
}

async fn val<K: KeyType>(&self, key: K) -> Result<Option<&[u8]>, api::Error> {
async fn val<K: KeyType>(&self, key: K) -> Result<Option<Vec<u8>>, 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 {
Expand Down

0 comments on commit 646a04a

Please sign in to comment.