-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f10bc0
commit 5b0ed02
Showing
5 changed files
with
360 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use firewood::v2::{ | ||
api::{Db, Error}, | ||
emptydb::{EmptyDb, HistoricalImpl}, | ||
}; | ||
use std::{ | ||
collections::HashMap, | ||
sync::{ | ||
atomic::{AtomicU64, Ordering}, | ||
Arc, | ||
}, | ||
}; | ||
use tokio::sync::Mutex; | ||
use tonic::Status; | ||
|
||
pub mod database; | ||
pub mod db; | ||
|
||
trait IntoStatusResultExt<T> { | ||
fn into_status_result(self) -> Result<T, Status>; | ||
} | ||
|
||
impl<T> IntoStatusResultExt<T> for Result<T, Error> { | ||
fn into_status_result(self) -> Result<T, Status> { | ||
self.map_err(|err| match err { | ||
Error::HashNotFound { provided: _ } => todo!(), | ||
Error::IncorrectRootHash { | ||
provided: _, | ||
current: _, | ||
} => todo!(), | ||
Error::IO(_) => todo!(), | ||
Error::InvalidProposal => todo!(), | ||
_ => todo!(), | ||
}) | ||
} | ||
} | ||
pub struct Database { | ||
db: EmptyDb, | ||
iterators: Arc<Mutex<Iterators>>, | ||
} | ||
|
||
impl Default for Database { | ||
fn default() -> Self { | ||
Self { | ||
db: EmptyDb, | ||
iterators: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Database { | ||
async fn revision(&self) -> Result<Arc<HistoricalImpl>, Error> { | ||
let root_hash = self.db.root_hash().await?; | ||
self.db.revision(root_hash).await | ||
} | ||
} | ||
|
||
// TODO: implement Iterator | ||
struct Iter; | ||
|
||
#[derive(Default)] | ||
struct Iterators { | ||
map: HashMap<u64, Iter>, | ||
next_id: AtomicU64, | ||
} | ||
|
||
impl Iterators { | ||
fn insert(&mut self, iter: Iter) -> u64 { | ||
let id = self.next_id.fetch_add(1, Ordering::Relaxed); | ||
self.map.insert(id, iter); | ||
id | ||
} | ||
|
||
fn _get(&self, id: u64) -> Option<&Iter> { | ||
self.map.get(&id) | ||
} | ||
|
||
fn remove(&mut self, id: u64) { | ||
self.map.remove(&id); | ||
} | ||
} |
Oops, something went wrong.