-
Notifications
You must be signed in to change notification settings - Fork 7
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
Showing
11 changed files
with
316 additions
and
278 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,123 @@ | ||
//! Kytz Database | ||
use crate::node::Node; | ||
use crate::operations::read::{get_node, root_hash, root_node}; | ||
use crate::operations::{insert, remove}; | ||
use crate::{Hash, Result}; | ||
|
||
pub struct Database { | ||
pub(crate) inner: redb::Database, | ||
} | ||
|
||
impl Database { | ||
/// Create a new in-memory database. | ||
pub fn in_memory() -> Self { | ||
let backend = redb::backends::InMemoryBackend::new(); | ||
let inner = redb::Database::builder() | ||
.create_with_backend(backend) | ||
.unwrap(); | ||
|
||
Self { inner } | ||
} | ||
|
||
pub fn begin_write(&self) -> Result<WriteTransaction> { | ||
let txn = self.inner.begin_write().unwrap(); | ||
WriteTransaction::new(txn) | ||
} | ||
|
||
pub fn iter(&self, treap: &str) -> TreapIterator<'_> { | ||
// TODO: save tables instead of opening a new one on every next() call. | ||
TreapIterator::new(self, treap.to_string()) | ||
} | ||
|
||
// === Private Methods === | ||
|
||
pub(crate) fn get_node(&self, hash: &Option<Hash>) -> Option<Node> { | ||
get_node(&self, hash) | ||
} | ||
|
||
pub(crate) fn root_hash(&self, treap: &str) -> Option<Hash> { | ||
root_hash(&self, treap) | ||
} | ||
|
||
pub(crate) fn root(&self, treap: &str) -> Option<Node> { | ||
root_node(self, treap) | ||
} | ||
} | ||
|
||
pub struct TreapIterator<'db> { | ||
db: &'db Database, | ||
treap: String, | ||
stack: Vec<Node>, | ||
} | ||
|
||
impl<'db> TreapIterator<'db> { | ||
fn new(db: &'db Database, treap: String) -> Self { | ||
let mut iter = TreapIterator { | ||
db, | ||
treap: treap.clone(), | ||
stack: Vec::new(), | ||
}; | ||
|
||
if let Some(root) = db.root(&treap) { | ||
iter.push_left(root) | ||
}; | ||
|
||
iter | ||
} | ||
|
||
fn push_left(&mut self, mut node: Node) { | ||
while let Some(left) = self.db.get_node(node.left()) { | ||
self.stack.push(node); | ||
node = left; | ||
} | ||
self.stack.push(node); | ||
} | ||
} | ||
|
||
impl<'a> Iterator for TreapIterator<'a> { | ||
type Item = Node; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
match self.stack.pop() { | ||
Some(node) => { | ||
if let Some(right) = self.db.get_node(node.right()) { | ||
self.push_left(right) | ||
} | ||
|
||
Some(node.clone()) | ||
} | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
pub struct WriteTransaction<'db> { | ||
inner: redb::WriteTransaction<'db>, | ||
} | ||
|
||
impl<'db> WriteTransaction<'db> { | ||
pub(crate) fn new(inner: redb::WriteTransaction<'db>) -> Result<Self> { | ||
Ok(Self { inner }) | ||
} | ||
|
||
pub fn insert( | ||
&mut self, | ||
treap: &str, | ||
key: impl AsRef<[u8]>, | ||
value: impl AsRef<[u8]>, | ||
) -> Option<Node> { | ||
// TODO: validate key and value length. | ||
// key and value mast be less than 2^32 bytes. | ||
|
||
insert(&mut self.inner, treap, key.as_ref(), value.as_ref()) | ||
} | ||
|
||
pub fn remove(&mut self, treap: &str, key: impl AsRef<[u8]>) -> Option<Node> { | ||
remove(&mut self.inner, treap, key.as_ref()) | ||
} | ||
|
||
pub fn commit(self) -> Result<()> { | ||
self.inner.commit().map_err(|e| e.into()) | ||
} | ||
} |
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,24 @@ | ||
//! Main Crate Error | ||
#[derive(thiserror::Error, Debug)] | ||
/// Mainline crate error enum. | ||
pub enum Error { | ||
/// For starter, to remove as code matures. | ||
#[error("Generic error: {0}")] | ||
Generic(String), | ||
/// For starter, to remove as code matures. | ||
#[error("Static error: {0}")] | ||
Static(&'static str), | ||
|
||
#[error(transparent)] | ||
/// Transparent [std::io::Error] | ||
IO(#[from] std::io::Error), | ||
|
||
#[error(transparent)] | ||
/// Transparent [redb::CommitError] | ||
CommitError(#[from] redb::CommitError), | ||
|
||
#[error(transparent)] | ||
/// Error from `redb::TransactionError`. | ||
TransactionError(#[from] redb::TransactionError), | ||
} |
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 |
---|---|---|
@@ -1,12 +1,18 @@ | ||
#![allow(unused)] | ||
|
||
pub mod db; | ||
pub mod error; | ||
mod node; | ||
mod operations; | ||
pub mod treap; | ||
|
||
#[cfg(test)] | ||
mod test; | ||
|
||
pub(crate) use blake3::{Hash, Hasher}; | ||
pub(crate) const HASH_LEN: usize = 32; | ||
|
||
pub const HASH_LEN: usize = 32; | ||
pub use db::Database; | ||
pub use error::Error; | ||
|
||
// Alias Result to be the crate Result. | ||
pub type Result<T, E = Error> = core::result::Result<T, E>; |
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 |
---|---|---|
@@ -1,6 +1,25 @@ | ||
pub mod insert; | ||
pub mod read; | ||
pub mod remove; | ||
mod search; | ||
|
||
pub(crate) use insert::insert; | ||
pub(crate) use remove::remove; | ||
|
||
use redb::{ReadableTable, TableDefinition}; | ||
|
||
// Table: Nodes v0 | ||
// stores all the hash treap nodes from all the treaps in the storage. | ||
// | ||
// Key: `[u8; 32]` # Node hash | ||
// Value: `(u64, [u8])` # (RefCount, EncodedNode) | ||
pub const NODES_TABLE: TableDefinition<&[u8], (u64, &[u8])> = | ||
TableDefinition::new("kytz:hash_treap:nodes:v0"); | ||
|
||
// Table: Roots v0 | ||
// stores all the current roots for all treaps in the storage. | ||
// | ||
// Key: `[u8; 32]` # Treap name | ||
// Value: `[u8; 32]` # Hash | ||
pub const ROOTS_TABLE: TableDefinition<&[u8], &[u8]> = | ||
TableDefinition::new("kytz:hash_treap:roots:v0"); |
Oops, something went wrong.