Skip to content

Commit

Permalink
trie log fixed, snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
cchudant committed Oct 21, 2024
1 parent f4c3492 commit 00c1f8b
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 345 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ log = "0.4.20"
rayon = { version = "1.9.0", optional = true }
smallvec = { version = "1.11.2", features = ["serde"] }
slotmap = "1.0.7"
thiserror = "1.0"

parity-scale-codec = { version = "3.0.0", default-features = false, features = [
"derive",
Expand Down
10 changes: 6 additions & 4 deletions src/bonsai_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ pub trait BonsaiDatabase: core::fmt::Debug {
#[cfg(feature = "std")]
type DatabaseError: Error + DBError;
#[cfg(not(feature = "std"))]
type DatabaseError: DBError;
type DatabaseError: DBError;



/// Create a new empty batch of changes to be used in `insert`, `remove` and applied in database using `write_batch`.
fn create_batch(&self) -> Self::Batch;
Expand Down Expand Up @@ -79,7 +81,7 @@ pub trait BonsaiDatabase: core::fmt::Debug {
}

pub trait BonsaiPersistentDatabase<ID: Id> {
type Transaction: BonsaiDatabase<DatabaseError = Self::DatabaseError>;
type Transaction<'a>: BonsaiDatabase<DatabaseError = Self::DatabaseError> where Self: 'a;
#[cfg(feature = "std")]
type DatabaseError: Error + DBError;
#[cfg(not(feature = "std"))]
Expand All @@ -89,8 +91,8 @@ pub trait BonsaiPersistentDatabase<ID: Id> {
fn snapshot(&mut self, id: ID);

/// Create a transaction based on the given snapshot id
fn transaction(&self, id: ID) -> Option<Self::Transaction>;
fn transaction(&self, id: ID) -> Option<(ID, Self::Transaction<'_>)>;

/// Merge a transaction in the current persistent database
fn merge(&mut self, transaction: Self::Transaction) -> Result<(), Self::DatabaseError>;
fn merge<'a>(&mut self, transaction: Self::Transaction<'a>) -> Result<(), Self::DatabaseError> where Self: 'a;
}
15 changes: 3 additions & 12 deletions src/changes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{hash_map::Entry, id::Id, trie::TrieKey, ByteVec, HashMap, Vec, VecDeque};
use crate::{hash_map::Entry, id::Id, trie::TrieKey, ByteVec, HashMap, Vec};
use core::iter;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -115,22 +115,13 @@ pub fn key_new_value<ID: Id>(id: &ID, key: &TrieKey) -> ByteVec {

#[cfg_attr(feature = "bench", derive(Clone))]
#[derive(Debug)]
pub struct ChangeStore<ID>
where
ID: Id,
{
// Newest are inserted at the back
pub id_queue: VecDeque<ID>,
pub struct ChangeStore {
pub current_changes: ChangeBatch,
}

impl<ID> ChangeStore<ID>
where
ID: Id,
{
impl ChangeStore {
pub fn new() -> Self {
Self {
id_queue: VecDeque::new(),
current_changes: ChangeBatch(HashMap::new()),
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/databases/hashmap_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,22 @@ impl<ID: Id> BonsaiDatabase for HashMapDb<ID> {

impl<ID: Id> BonsaiPersistentDatabase<ID> for HashMapDb<ID> {
type DatabaseError = HashMapDbError;
type Transaction = HashMapDb<ID>;
type Transaction<'a> = HashMapDb<ID> where ID: 'a;
fn snapshot(&mut self, id: ID) {
self.snapshots.insert(id, self.clone());
}

fn transaction(&self, id: ID) -> Option<Self::Transaction> {
self.snapshots.get(&id).cloned()
fn transaction(&self, id: ID) -> Option<(ID, Self::Transaction<'_>)> {
self.snapshots
.range(..&id)
.next()
.map(|(id, snapshot)| (*id, snapshot.clone()))
}

fn merge(&mut self, transaction: Self::Transaction) -> Result<(), Self::DatabaseError> {
fn merge<'a>(&mut self, transaction: Self::Transaction<'a>) -> Result<(), Self::DatabaseError>
where
ID: 'a,
{
self.trie_db = transaction.trie_db;
self.flat_db = transaction.flat_db;
self.trie_log_db = transaction.trie_log_db;
Expand Down
13 changes: 8 additions & 5 deletions src/databases/rocks_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ impl<'db, ID> BonsaiPersistentDatabase<ID> for RocksDB<'db, ID>
where
ID: Id,
{
type Transaction = RocksDBTransaction<'db>;
type Transaction<'a> = RocksDBTransaction<'a> where Self: 'a;
type DatabaseError = RocksDBError;

fn snapshot(&mut self, id: ID) {
Expand All @@ -465,9 +465,9 @@ where
}
}

fn transaction(&self, id: ID) -> Option<Self::Transaction> {
fn transaction(&self, id: ID) -> Option<(ID, Self::Transaction<'_>)> {
trace!("Generating RocksDB transaction");
if let Some(snapshot) = self.snapshots.get(&id) {
if let Some((id, snapshot)) = self.snapshots.range(..&id).next() {
let write_opts = WriteOptions::default();
let mut txn_opts = OptimisticTransactionOptions::default();
txn_opts.set_snapshot(true);
Expand All @@ -494,13 +494,16 @@ where
column_families,
read_options,
};
Some(boxed_txn)
Some((*id, boxed_txn))
} else {
None
}
}

fn merge(&mut self, transaction: Self::Transaction) -> Result<(), Self::DatabaseError> {
fn merge<'a>(&mut self, transaction: Self::Transaction<'a>) -> Result<(), Self::DatabaseError>
where
Self: 'a,
{
transaction.txn.commit()?;
Ok(())
}
Expand Down
41 changes: 41 additions & 0 deletions src/dd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pub trait Database {
type View;
type WriteBatch;
}

pub trait BonsaiDbStrategy {
type DB: Database;
fn get_node(&self, path: &BitSlice, db_view: &Self::DB::View) -> Result<Node>;
fn write_to_batch(&self, modifications: MerkleTree<Self>, db_write: &mut Self::DB::WriteBatch) -> Result<()>;
}

pub struct GlobalTrie<DBStrategy: BonsaiDbStrategy> {
prefix_len: usize,
strategy: DBStrategy,
}

impl<DBStrategy: BonsaiDbStrategy> GlobalTrie<DBStrategy> {
pub fn get_tree_at(&self, db: &DBStrategy::DB::View, prefix: &BitSlice) -> MerkleTree<DBStrategy>;
}

pub trait DBForLayeredState: Database {
type Instance;
pub fn new_instance(&self, at_version: u64) -> Result<Self::Instance>;
pub fn get_closest(&self, iter: &mut Self::Instance, key: &[u8]) -> Result<ByteVec>;
pub fn delete_range(&self, iter: &mut Self::Instance, write_to: &mut Self::WriteBatch, prefix: &[u8]) -> Result<()>;
}

pub struct LayeredStateStrategy<DB: DBForLayeredState> {

}

impl<DB: DBForLayeredState> BonsaiDbStrategy for DBForLayeredState<DB> {
type DB = DB;
fn get_node(&self, path: &BitSlice, db_view: &Self::DB::View) -> Result<Node> {
todo!()
}

fn write_to_batch(&self, modifications: MerkleTree<Self>, db_write: &mut Self::DB::WriteBatch) -> Result<()> {
todo!()
}
}
5 changes: 0 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ where
Database(DatabaseError),
/// Error when decoding a node
NodeDecodeError(parity_scale_codec::Error),
/// Error when creating a storage proof.
CreateProofKeyNotInTree { key: BitVec },
/// Malformated trie key.
KeyLength { expected: usize, got: usize },
}
Expand Down Expand Up @@ -56,9 +54,6 @@ where
BonsaiStorageError::Merge(e) => write!(f, "Merge error: {}", e),
BonsaiStorageError::Database(e) => write!(f, "Database error: {}", e),
BonsaiStorageError::NodeDecodeError(e) => write!(f, "Node decode error: {}", e),
BonsaiStorageError::CreateProofKeyNotInTree { key } => {
write!(f, "Key not in tree: {key:b}")
}
BonsaiStorageError::KeyLength { expected, got } => {
write!(f, "Malformated key length: expected {expected}, got {got}")
}
Expand Down
8 changes: 8 additions & 0 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use core::{fmt::Debug, hash};
/// Trait to be implemented on any type that can be used as an ID.
pub trait Id: hash::Hash + PartialEq + Eq + PartialOrd + Ord + Debug + Copy + Default {
fn to_bytes(&self) -> ByteVec;
fn as_u64(self) -> u64;
fn from_u64(v: u64) -> Self;
}

/// A basic ID type that can be used for testing.
Expand All @@ -20,6 +22,12 @@ impl Id for BasicId {
fn to_bytes(&self) -> ByteVec {
ByteVec::from(&self.0.to_be_bytes() as &[_])
}
fn as_u64(self) -> u64 {
self.0
}
fn from_u64(v: u64) -> Self {
Self(v)
}
}

/// A builder for basic IDs.
Expand Down
Loading

0 comments on commit 00c1f8b

Please sign in to comment.