Skip to content

Commit

Permalink
simplify trait bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
LevKruglyak committed Apr 3, 2024
1 parent a1b34bd commit 09c8acc
Show file tree
Hide file tree
Showing 21 changed files with 200 additions and 241 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ anyhow = "1.0.79"

sorted_array = { path = "../utils/sorted_array", version = "0.1", features = ["serde"] }
id_allocator = { path = "../utils/id_allocator", version = "0.1", features = ["serde"] }
lazy_static = "1.4.0"

[dev-dependencies]
tempfile = "3.0"
Expand Down
10 changes: 5 additions & 5 deletions core/src/classical/btree_top.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::component::{PropagateInsert, TopComponent};
use crate::node_layer::NodeLayer;
use crate::traits::Address;
use crate::{component::*, Key};
use std::collections::BTreeMap;
use std::ops::Bound;

Expand All @@ -14,10 +14,10 @@ pub struct BTreeTopComponent<K, X, A> {
impl<K, X, Base, BA: Copy> TopComponent<K, Base, BA, ()> for BTreeTopComponent<K, X, BA>
where
Base: NodeLayer<K, BA, ()>,
K: Key,
BA: Address + std::fmt::Debug,
K: Ord + Clone,
BA: Address,
{
fn search(&self, _: &Base, key: K) -> BA {
fn search(&self, _: &Base, key: &K) -> BA {
*self
.inner
.range(..=key)
Expand All @@ -44,7 +44,7 @@ where
let mut iter = base.range_mut(Bound::Unbounded, Bound::Unbounded);

while let Some((key, address, parent)) = iter.next() {
inner.insert(key, address);
inner.insert(key.clone(), address);
parent.set(());
}

Expand Down
43 changes: 18 additions & 25 deletions core/src/classical/disk/boundary_layer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, ops::Bound};
use std::ops::Bound;

use crate::{
classical::node::BTreeNode,
Expand All @@ -9,25 +9,10 @@ use crate::{
impl_node_layer, Address, Key, KeyBounded, NodeLayer, Persisted, StaticBounded,
};

pub struct BoundaryDiskBTreeLayer<K, V, const FANOUT: usize, PA> {
pub struct BoundaryDiskBTreeLayer<K: Ord, V, const FANOUT: usize, PA> {
inner: BoundaryDiskList<BTreeNode<K, V, FANOUT>, PA>,
}

impl<K: Debug, V: Debug, const FANOUT: usize, PA> Debug for BoundaryDiskBTreeLayer<K, V, FANOUT, PA>
where
K: Persisted + Key,
V: Persisted,
PA: Address,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (node, ptr) in self.inner.range(Bound::Unbounded, Bound::Unbounded) {
writeln!(f, "node: [0x{ptr:?}]: {node:?}")?;
}

write!(f, "")
}
}

impl<K, V, const FANOUT: usize, PA> BoundaryDiskBTreeLayer<K, V, FANOUT, PA>
where
K: Persisted + Key,
Expand All @@ -42,7 +27,7 @@ where

pub fn fill(&mut self, iter: impl Iterator<Item = (K, V)>) -> crate::Result<()>
where
K: Copy + Ord,
K: Clone + Ord,
{
if let Some(mut ptr) = self.inner.is_empty()? {
for (key, address) in iter {
Expand All @@ -60,7 +45,7 @@ where

pub fn fill_with_parent<B>(&mut self, base: &mut B) -> crate::Result<()>
where
K: Copy + Ord,
K: Clone + Ord,
V: Address,
B: NodeLayer<K, V, StoreID>,
{
Expand All @@ -73,7 +58,7 @@ where
ptr = self.inner.insert_after(BTreeNode::empty(), ptr)?;
}

self.insert_into_node(key, &address, ptr)?;
self.insert_into_node(key.clone(), &address, ptr)?;
parent.set(ptr);
}
}
Expand All @@ -83,7 +68,7 @@ where

fn insert_into_node(&mut self, key: K, value: &V, ptr: StoreID) -> crate::Result<Option<V>> {
self.inner
.transform_node(ptr, |node| node.insert(key, value.clone()))
.transform_node(ptr, |node| node.insert(key.clone(), value.clone()))
}

pub fn get_node(&self, ptr: StoreID) -> crate::Result<BTreeNode<K, V, FANOUT>> {
Expand Down Expand Up @@ -111,7 +96,11 @@ where
}

return Ok(Some((
*self.inner.get_node(new_node_ptr)?.unwrap().lower_bound(),
self.inner
.get_node(new_node_ptr)?
.unwrap()
.lower_bound()
.clone(),
new_node_ptr,
parent,
)));
Expand All @@ -130,7 +119,7 @@ where
ptr: StoreID,
) -> crate::Result<Option<(K, StoreID, PA)>>
where
K: Copy + Ord + StaticBounded,
K: Clone + Ord + StaticBounded,
V: Address,
PA: Address,
B: NodeLayer<K, V, StoreID>,
Expand All @@ -157,7 +146,11 @@ where
}

return Ok(Some((
*self.inner.get_node(new_node_ptr)?.unwrap().lower_bound(),
self.inner
.get_node(new_node_ptr)?
.unwrap()
.lower_bound()
.clone(),
new_node_ptr,
parent,
)));
Expand All @@ -173,7 +166,7 @@ where
impl<K, V, const FANOUT: usize, PA> NodeLayer<K, StoreID, PA>
for BoundaryDiskBTreeLayer<K, V, FANOUT, PA>
where
K: Persisted + Key,
K: Persisted + StaticBounded,
V: Persisted,
PA: Address,
{
Expand Down
53 changes: 19 additions & 34 deletions core/src/classical/disk/deep_layer.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
use std::{fmt::Debug, ops::Bound};
use std::ops::Bound;

use crate::{
classical::node::BTreeNode,
common::{
list::deep_disk::DeepDiskList,
storage::{GlobalStore, StoreID},
},
impl_node_layer, Address, Key, KeyBounded, NodeLayer, Persisted, StaticBounded,
impl_node_layer, Address, KeyBounded, NodeLayer, Persisted, StaticBounded,
};

pub struct DeepDiskBTreeLayer<K, V, const FANOUT: usize, PA>
where
PA: Persisted + Address,
{
pub struct DeepDiskBTreeLayer<K: Ord, V, const FANOUT: usize, PA: Persisted> {
inner: DeepDiskList<BTreeNode<K, V, FANOUT>, PA>,
}

impl<K: Debug, V: Debug, const FANOUT: usize, PA> Debug for DeepDiskBTreeLayer<K, V, FANOUT, PA>
where
K: Persisted + Key,
V: Persisted,
PA: Persisted + Address,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (node, ptr) in self.inner.range(Bound::Unbounded, Bound::Unbounded) {
writeln!(f, "node: [0x{ptr:?}]: {node:?}")?;
}

write!(f, "")
}
}

impl<K, V, const FANOUT: usize, PA> DeepDiskBTreeLayer<K, V, FANOUT, PA>
where
K: Persisted + Key,
K: Persisted + Clone + Ord + StaticBounded,
V: Persisted,
PA: Persisted + Address,
{
Expand All @@ -43,10 +25,7 @@ where
})
}

pub fn fill(&mut self, iter: impl Iterator<Item = (K, V)>) -> crate::Result<()>
where
K: Copy + Ord,
{
pub fn fill(&mut self, iter: impl Iterator<Item = (K, V)>) -> crate::Result<()> {
// Add empty cap node
let mut ptr = self.inner.clear()?;

Expand All @@ -64,8 +43,6 @@ where

pub fn fill_with_parent<B>(&mut self, base: &mut B) -> crate::Result<()>
where
K: Copy + Ord,
V: Address,
B: NodeLayer<K, V, StoreID>,
{
if let Some(mut ptr) = self.inner.is_empty()? {
Expand All @@ -77,7 +54,7 @@ where
ptr = self.inner.insert_after(BTreeNode::empty(), ptr)?;
}

self.insert_into_node(key, &address, ptr)?;
self.insert_into_node(key.clone(), &address, ptr)?;
parent.set(ptr);
}
}
Expand All @@ -87,7 +64,7 @@ where

fn insert_into_node(&mut self, key: K, value: &V, ptr: StoreID) -> crate::Result<Option<V>> {
self.inner
.transform_node(ptr, |node| node.insert(key, value.clone()))
.transform_node(ptr, |node| node.insert(key.clone(), value.clone()))
}

pub fn get_node(&self, ptr: StoreID) -> crate::Result<BTreeNode<K, V, FANOUT>> {
Expand Down Expand Up @@ -115,7 +92,11 @@ where
}

return Ok(Some((
*self.inner.get_node(new_node_ptr)?.unwrap().lower_bound(),
self.inner
.get_node(new_node_ptr)?
.unwrap()
.lower_bound()
.clone(),
new_node_ptr,
parent,
)));
Expand All @@ -134,7 +115,7 @@ where
ptr: StoreID,
) -> crate::Result<Option<(K, StoreID, PA)>>
where
K: Copy + Ord + StaticBounded,
K: Clone + Ord + StaticBounded,
V: Address,
PA: Address,
B: NodeLayer<K, V, StoreID>,
Expand All @@ -161,7 +142,11 @@ where
}

return Ok(Some((
*self.inner.get_node(new_node_ptr)?.unwrap().lower_bound(),
self.inner
.get_node(new_node_ptr)?
.unwrap()
.lower_bound()
.clone(),
new_node_ptr,
parent,
)));
Expand All @@ -177,7 +162,7 @@ where
impl<K, V, const FANOUT: usize, PA> NodeLayer<K, StoreID, PA>
for DeepDiskBTreeLayer<K, V, FANOUT, PA>
where
K: Persisted + Key,
K: Persisted + StaticBounded,
V: Persisted,
PA: Persisted + Address,
{
Expand Down
Loading

0 comments on commit 09c8acc

Please sign in to comment.