Skip to content

Commit

Permalink
Shale cleanup (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Mar 25, 2024
1 parent c386102 commit 1412662
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 95 deletions.
2 changes: 1 addition & 1 deletion firewood/benches/hashops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use firewood::{
cached::InMemLinearStore,
compact::{CompactHeader, CompactSpace},
disk_address::DiskAddress,
CachedStore, ObjCache, Storable, StoredView,
LinearStore, ObjCache, Storable, StoredView,
},
storage::WalConfig,
v2::api::{Db, Proposal},
Expand Down
6 changes: 3 additions & 3 deletions firewood/benches/shale-bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use firewood::shale::{
cached::InMemLinearStore,
compact::{CompactHeader, CompactSpaceHeader},
disk_address::DiskAddress,
CachedStore, Obj, StoredView,
LinearStore, Obj, StoredView,
};
use pprof::ProfilerGuard;
use rand::Rng;
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Profiler for FlamegraphProfiler {
}
}

fn get_view<C: CachedStore>(b: &mut Bencher, mut cached: C) {
fn get_view<C: LinearStore>(b: &mut Bencher, mut cached: C) {
let mut rng = rand::thread_rng();

b.iter(|| {
Expand All @@ -78,7 +78,7 @@ fn get_view<C: CachedStore>(b: &mut Bencher, mut cached: C) {
});
}

fn serialize<T: CachedStore>(m: &T) {
fn serialize<T: LinearStore>(m: &T) {
let compact_header_obj: DiskAddress = DiskAddress::from(0x0);
#[allow(clippy::unwrap_used)]
let _: Obj<CompactSpaceHeader> =
Expand Down
14 changes: 7 additions & 7 deletions firewood/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
use crate::{
merkle,
shale::{
self, compact::CompactSpaceHeader, disk_address::DiskAddress, CachedStore, Obj, ShaleError,
self, compact::CompactSpaceHeader, disk_address::DiskAddress, LinearStore, Obj, ShaleError,
SpaceId, Storable, StoredView,
},
};
Expand Down Expand Up @@ -205,7 +205,7 @@ impl DbHeader {
}

impl Storable for DbHeader {
fn deserialize<T: CachedStore>(addr: usize, mem: &T) -> Result<Self, shale::ShaleError> {
fn deserialize<T: LinearStore>(addr: usize, mem: &T) -> Result<Self, shale::ShaleError> {
let root_bytes = mem
.get_view(addr, Self::MSIZE)
.ok_or(ShaleError::InvalidCacheView {
Expand Down Expand Up @@ -285,7 +285,7 @@ pub struct DbRev<T> {
}

#[async_trait]
impl<T: CachedStore> api::DbView for DbRev<T> {
impl<T: LinearStore> api::DbView for DbRev<T> {
type Stream<'a> = MerkleKeyValueStream<'a, T, Bincode> where Self: 'a;

async fn root_hash(&self) -> Result<api::HashKey, api::Error> {
Expand Down Expand Up @@ -338,7 +338,7 @@ impl<T: CachedStore> api::DbView for DbRev<T> {
}
}

impl<T: CachedStore> DbRev<T> {
impl<T: LinearStore> DbRev<T> {
pub fn stream(&self) -> merkle::MerkleKeyValueStream<'_, T, Bincode> {
self.merkle.key_value_iter(self.header.kv_root)
}
Expand Down Expand Up @@ -749,7 +749,7 @@ impl Db {
Ok((store, rev))
}

fn get_payload_header_ref<K: CachedStore>(
fn get_payload_header_ref<K: LinearStore>(
meta_ref: &K,
header_offset: u64,
) -> Result<Obj<CompactSpaceHeader>, DbError> {
Expand All @@ -762,12 +762,12 @@ impl Db {
.map_err(Into::into)
}

fn get_db_header_ref<K: CachedStore>(meta_ref: &K) -> Result<Obj<DbHeader>, DbError> {
fn get_db_header_ref<K: LinearStore>(meta_ref: &K) -> Result<Obj<DbHeader>, DbError> {
let db_header = DiskAddress::from(Db::PARAM_SIZE as usize);
StoredView::ptr_to_obj(meta_ref, db_header, DbHeader::MSIZE).map_err(Into::into)
}

fn new_revision<K: CachedStore, T: Into<K>>(
fn new_revision<K: LinearStore, T: Into<K>>(
header_refs: (Obj<DbHeader>, Obj<CompactSpaceHeader>),
merkle: (T, T),
payload_regn_nbit: u64,
Expand Down
2 changes: 1 addition & 1 deletion firewood/src/db/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
Universe, MERKLE_META_SPACE, MERKLE_PAYLOAD_SPACE, ROOT_HASH_SPACE,
};
use crate::merkle::{Bincode, MerkleKeyValueStream, Proof};
use crate::shale::CachedStore;
use crate::shale::LinearStore;
use crate::storage::StoreRevShared;
use crate::{
merkle::{TrieHash, TRIE_HASH_LEN},
Expand Down
10 changes: 5 additions & 5 deletions firewood/src/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See the file LICENSE.md for licensing terms.
use crate::nibbles::Nibbles;
use crate::shale::compact::CompactSpace;
use crate::shale::CachedStore;
use crate::shale::LinearStore;
use crate::shale::{self, disk_address::DiskAddress, ObjWriteSizeError, ShaleError};
use crate::storage::{StoreRevMut, StoreRevShared};
use crate::v2::api;
Expand Down Expand Up @@ -81,7 +81,7 @@ impl<T> From<Merkle<StoreRevMut, T>> for Merkle<StoreRevShared, T> {
}
}

impl<S: CachedStore, T> Merkle<S, T> {
impl<S: LinearStore, T> Merkle<S, T> {
pub fn get_node(&self, ptr: DiskAddress) -> Result<NodeObjRef, MerkleError> {
self.store.get_item(ptr).map_err(Into::into)
}
Expand All @@ -97,7 +97,7 @@ impl<S: CachedStore, T> Merkle<S, T> {

impl<'de, S, T> Merkle<S, T>
where
S: CachedStore,
S: LinearStore,
T: BinarySerde,
EncodedNode<T>: serde::Serialize + serde::Deserialize<'de>,
{
Expand Down Expand Up @@ -178,7 +178,7 @@ where
}
}

impl<S: CachedStore, T> Merkle<S, T> {
impl<S: LinearStore, T> Merkle<S, T> {
pub fn init_root(&self) -> Result<DiskAddress, MerkleError> {
self.store
.put_item(
Expand Down Expand Up @@ -1299,7 +1299,7 @@ impl<'a, S, T> RefMut<'a, S, T> {
}
}

impl<'a, S: CachedStore, T> RefMut<'a, S, T> {
impl<'a, S: LinearStore, T> RefMut<'a, S, T> {
#[allow(clippy::unwrap_used)]
pub fn get(&self) -> Ref {
Ref(self.merkle.get_node(self.ptr).unwrap())
Expand Down
12 changes: 6 additions & 6 deletions firewood/src/merkle/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{
logger::trace,
merkle::nibbles_to_bytes_iter,
shale::{compact::CompactSpace, disk_address::DiskAddress, CachedStore, ShaleError, Storable},
shale::{compact::CompactSpace, disk_address::DiskAddress, LinearStore, ShaleError, Storable},
};
use bincode::{Error, Options};
use bitflags::bitflags;
Expand Down Expand Up @@ -81,7 +81,7 @@ impl NodeType {
}
}

pub fn encode<S: CachedStore>(&self, store: &CompactSpace<Node, S>) -> Vec<u8> {
pub fn encode<S: LinearStore>(&self, store: &CompactSpace<Node, S>) -> Vec<u8> {
match &self {
NodeType::Leaf(n) => n.encode(),
NodeType::Branch(n) => n.encode(store),
Expand Down Expand Up @@ -201,18 +201,18 @@ impl Node {
})
}

pub(super) fn get_encoded<S: CachedStore>(&self, store: &CompactSpace<Node, S>) -> &[u8] {
pub(super) fn get_encoded<S: LinearStore>(&self, store: &CompactSpace<Node, S>) -> &[u8] {
self.encoded.get_or_init(|| self.inner.encode(store))
}

pub(super) fn get_root_hash<S: CachedStore>(&self, store: &CompactSpace<Node, S>) -> &TrieHash {
pub(super) fn get_root_hash<S: LinearStore>(&self, store: &CompactSpace<Node, S>) -> &TrieHash {
self.root_hash.get_or_init(|| {
self.set_dirty(true);
TrieHash(Keccak256::digest(self.get_encoded(store)).into())
})
}

fn is_encoded_longer_than_hash_len<S: CachedStore>(
fn is_encoded_longer_than_hash_len<S: LinearStore>(
&self,
store: &CompactSpace<Node, S>,
) -> bool {
Expand Down Expand Up @@ -328,7 +328,7 @@ mod type_id {
use type_id::NodeTypeId;

impl Storable for Node {
fn deserialize<T: CachedStore>(offset: usize, mem: &T) -> Result<Self, ShaleError> {
fn deserialize<T: LinearStore>(offset: usize, mem: &T) -> Result<Self, ShaleError> {
let meta_raw =
mem.get_view(offset, Meta::SIZE as u64)
.ok_or(ShaleError::InvalidCacheView {
Expand Down
6 changes: 3 additions & 3 deletions firewood/src/merkle/node/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::Node;
use crate::{
merkle::{nibbles_to_bytes_iter, to_nibble_array, Path},
nibbles::Nibbles,
shale::{compact::CompactSpace, CachedStore, DiskAddress, ShaleError, Storable},
shale::{compact::CompactSpace, DiskAddress, LinearStore, ShaleError, Storable},
};
use bincode::{Error, Options};
use serde::de::Error as DeError;
Expand Down Expand Up @@ -111,7 +111,7 @@ impl BranchNode {
})
}

pub(super) fn encode<S: CachedStore>(&self, store: &CompactSpace<Node, S>) -> Vec<u8> {
pub(super) fn encode<S: LinearStore>(&self, store: &CompactSpace<Node, S>) -> Vec<u8> {
// path + children + value
let mut list = <[Vec<u8>; Self::MSIZE]>::default();

Expand Down Expand Up @@ -219,7 +219,7 @@ impl Storable for BranchNode {
Ok(())
}

fn deserialize<T: crate::shale::CachedStore>(
fn deserialize<T: crate::shale::LinearStore>(
mut addr: usize,
mem: &T,
) -> Result<Self, crate::shale::ShaleError> {
Expand Down
2 changes: 1 addition & 1 deletion firewood/src/merkle/node/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Storable for LeafNode {
Ok(())
}

fn deserialize<T: crate::shale::CachedStore>(
fn deserialize<T: crate::shale::LinearStore>(
offset: usize,
mem: &T,
) -> Result<Self, crate::shale::ShaleError>
Expand Down
4 changes: 2 additions & 2 deletions firewood/src/merkle/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::cmp::Ordering;
use std::collections::HashMap;

use crate::shale::{disk_address::DiskAddress, ShaleError};
use crate::shale::{CachedStore, ObjWriteSizeError};
use crate::shale::{LinearStore, ObjWriteSizeError};
use crate::v2::api::HashKey;
use aiofut::AioError;
use nix::errno::Errno;
Expand Down Expand Up @@ -708,7 +708,7 @@ where
// keep the entire branch and return.
// - the fork point is a shortnode, the shortnode is excluded in the range,
// unset the entire branch.
fn unset_node_ref<K: AsRef<[u8]>, S: CachedStore, T: BinarySerde>(
fn unset_node_ref<K: AsRef<[u8]>, S: LinearStore, T: BinarySerde>(
merkle: &Merkle<S, T>,
parent: DiskAddress,
node: Option<DiskAddress>,
Expand Down
18 changes: 9 additions & 9 deletions firewood/src/merkle/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use super::{BranchNode, Key, Merkle, MerkleError, NodeObjRef, NodeType, Value};
use crate::{
nibbles::{Nibbles, NibblesIterator},
shale::{CachedStore, DiskAddress},
shale::{DiskAddress, LinearStore},
v2::api,
};
use futures::{stream::FusedStream, Stream, StreamExt};
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct MerkleNodeStream<'a, S, T> {
merkle: &'a Merkle<S, T>,
}

impl<'a, S: CachedStore, T> FusedStream for MerkleNodeStream<'a, S, T> {
impl<'a, S: LinearStore, T> FusedStream for MerkleNodeStream<'a, S, T> {
fn is_terminated(&self) -> bool {
// The top of `iter_stack` is the next node to return.
// If `iter_stack` is empty, there are no more nodes to visit.
Expand All @@ -93,7 +93,7 @@ impl<'a, S, T> MerkleNodeStream<'a, S, T> {
}
}

impl<'a, S: CachedStore, T> Stream for MerkleNodeStream<'a, S, T> {
impl<'a, S: LinearStore, T> Stream for MerkleNodeStream<'a, S, T> {
type Item = Result<(Key, NodeObjRef<'a>), api::Error>;

fn poll_next(
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<'a, S: CachedStore, T> Stream for MerkleNodeStream<'a, S, T> {

/// Returns the initial state for an iterator over the given `merkle` with root `root_node`
/// which starts at `key`.
fn get_iterator_intial_state<'a, S: CachedStore, T>(
fn get_iterator_intial_state<'a, S: LinearStore, T>(
merkle: &'a Merkle<S, T>,
root_node: DiskAddress,
key: &[u8],
Expand Down Expand Up @@ -321,7 +321,7 @@ pub struct MerkleKeyValueStream<'a, S, T> {
merkle: &'a Merkle<S, T>,
}

impl<'a, S: CachedStore, T> FusedStream for MerkleKeyValueStream<'a, S, T> {
impl<'a, S: LinearStore, T> FusedStream for MerkleKeyValueStream<'a, S, T> {
fn is_terminated(&self) -> bool {
matches!(&self.state, MerkleKeyValueStreamState::Initialized { node_iter } if node_iter.is_terminated())
}
Expand All @@ -345,7 +345,7 @@ impl<'a, S, T> MerkleKeyValueStream<'a, S, T> {
}
}

impl<'a, S: CachedStore, T> Stream for MerkleKeyValueStream<'a, S, T> {
impl<'a, S: LinearStore, T> Stream for MerkleKeyValueStream<'a, S, T> {
type Item = Result<(Key, Value), api::Error>;

fn poll_next(
Expand Down Expand Up @@ -426,7 +426,7 @@ pub struct PathIterator<'a, 'b, S, T> {
merkle: &'a Merkle<S, T>,
}

impl<'a, 'b, S: CachedStore, T> PathIterator<'a, 'b, S, T> {
impl<'a, 'b, S: LinearStore, T> PathIterator<'a, 'b, S, T> {
pub(super) fn new(
merkle: &'a Merkle<S, T>,
sentinel_node: NodeObjRef<'a>,
Expand Down Expand Up @@ -456,7 +456,7 @@ impl<'a, 'b, S: CachedStore, T> PathIterator<'a, 'b, S, T> {
}
}

impl<'a, 'b, S: CachedStore, T> Iterator for PathIterator<'a, 'b, S, T> {
impl<'a, 'b, S: LinearStore, T> Iterator for PathIterator<'a, 'b, S, T> {
type Item = Result<(Key, NodeObjRef<'a>), MerkleError>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -587,7 +587,7 @@ mod tests {
use super::*;
use test_case::test_case;

impl<S: CachedStore, T> Merkle<S, T> {
impl<S: LinearStore, T> Merkle<S, T> {
pub(crate) fn node_iter(&self, root: DiskAddress) -> MerkleNodeStream<'_, S, T> {
MerkleNodeStream::new(self, root, Box::new([]))
}
Expand Down
4 changes: 2 additions & 2 deletions firewood/src/merkle/trie_hash.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE.md for licensing terms.

use crate::shale::{CachedStore, ShaleError, Storable};
use crate::shale::{LinearStore, ShaleError, Storable};
use std::{
fmt::{self, Debug},
io::Write,
Expand All @@ -21,7 +21,7 @@ impl std::ops::Deref for TrieHash {
}

impl Storable for TrieHash {
fn deserialize<T: CachedStore>(addr: usize, mem: &T) -> Result<Self, ShaleError> {
fn deserialize<T: LinearStore>(addr: usize, mem: &T) -> Result<Self, ShaleError> {
let raw = mem
.get_view(addr, U64_TRIE_HASH_LEN)
.ok_or(ShaleError::InvalidCacheView {
Expand Down
2 changes: 1 addition & 1 deletion firewood/src/merkle_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
proof::{Proof, ProofError},
BinarySerde, EncodedNode, Merkle, Ref, RefMut, TrieHash,
},
shale::{self, cached::InMemLinearStore, disk_address::DiskAddress, CachedStore, StoredView},
shale::{self, cached::InMemLinearStore, disk_address::DiskAddress, LinearStore, StoredView},
};
use std::num::NonZeroUsize;
use thiserror::Error;
Expand Down
Loading

0 comments on commit 1412662

Please sign in to comment.