#[cfg(feature = "serde")]
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use {
crate::{
prolly::{
create::CreateTree,
node::{AsNode, Node, NodeA, NodeWithMeta},
read::Tree as ReadTree,
roller::{Config as RollerConfig, Roller},
},
storage::{Storage, StorageRead, StorageWrite},
Addr, Error,
},
multibase::Base,
std::{cmp::Eq, collections::HashMap, hash::Hash},
};
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum Update<V> {
Insert(V),
Remove,
}
pub struct Tree<'s, S, K, V> {
storage: &'s S,
updates: HashMap<K, Update<V>>,
}
impl<'s, S, K, V> Tree<'s, S, K, V>
where
K: std::fmt::Debug + Eq + Hash,
{
pub fn new<A>(storage: &'s S, addr: A) -> Self
where
A: Into<Addr>,
{
Self {
storage,
updates: HashMap::new(),
}
}
pub fn insert(&mut self, k: K, v: V) {
self.updates.insert(k, Update::Insert(v));
}
pub fn remove(&mut self, k: K) {
self.updates.insert(k, Update::Remove);
}
}
impl<'s, S, K, V> Tree<'s, S, K, V>
where
S: StorageRead + StorageWrite,
K: std::fmt::Debug + DeserializeOwned + Serialize + Clone,
V: std::fmt::Debug + DeserializeOwned + Serialize,
{
fn flush_ret_storage<R>(self) -> Result<(S, Option<Node<K, V>>), Error>
where
R: DeserializeOwned + AsNode<K = K, V = V>,
{
todo!("flush update")
}
pub fn flush<R>(&mut self) -> Result<Option<Addr>, Error>
where
R: DeserializeOwned + AsNode<K = K, V = V>,
{
todo!("flush")
}
pub fn commit<R>(self) -> Result<Option<Addr>, Error>
where
R: DeserializeOwned + AsNode<K = K, V = V>,
{
todo!("commit")
}
}
struct Leaf<'s, S, K, V> {
storage: &'s S,
cursor_addr: Addr,
parent_branch: Option<Box<Branch<'s, S, K, V>>>,
leaf_window: Vec<(K, V)>,
}
impl<'s, S, K, V> Leaf<'s, S, K, V>
where
K: DeserializeOwned,
V: DeserializeOwned,
{
pub fn new<R>(storage: &'s S, init_cursor_addr: Addr, init_key: K) -> Self
where
R: DeserializeOwned + AsNode<K = K, V = V>,
{
todo!("")
}
pub fn flush(self) -> Result<Self, Error> {
todo!()
}
}
enum MaybeBranch<'s, S, K, V> {
Some(Box<Branch<'s, S, K, V>>),
None,
}
struct Branch<'s, S, K, V> {
storage: &'s S,
cursor_addr: Addr,
parent_branch: Option<Box<Branch<'s, S, K, V>>>,
window: Vec<(K, Addr)>,
_tmp_phantom: std::marker::PhantomData<V>,
}
struct Level<'s, S, K, V> {
state: LevelState<'s, S, K, V>,
}
impl<'s, S, K, V> Level<'s, S, K, V> {
pub fn new(storage: &'s S) -> Self {
Self {
state: LevelState::Leaf {
storage,
window: Vec::new(),
},
}
}
pub fn flush(self) -> Result<Self, Error> {
todo!()
}
}
impl<'s, S, K, V> Level<'s, S, K, V>
where
K: Eq + Ord,
{
pub fn insert(&mut self, k: K, v: Option<V>) -> Option<(K, Addr)> {
self.state.insert(k, v);
todo!("level insert")
}
}
struct Block<K, V> {
block: Vec<(K, V)>,
}
enum LevelState<'s, S, K, V> {
Branch {
storage: &'s S,
child: Box<Level<'s, S, K, V>>,
window: Vec<(K, Addr)>,
},
Leaf {
storage: &'s S,
window: Vec<(K, V)>,
},
}
enum InsertResult<K, V> {
WindowMutated,
WindowDangling((K, V)),
WindowClosed((K, V)),
}
enum WindowMut<K> {
LeftSideSlid((K, Addr)),
NoBorderFound,
Closed((K, Addr)),
}
impl<'s, S, K, V> LevelState<'s, S, K, V>
where
K: Eq + Ord,
{
pub fn expand_window(&mut self, node: Node<K, V>) -> Option<(K, Addr)> {
todo!()
}
pub fn insert(&mut self, k: K, v: Option<V>) {
match self {
LevelState::Branch { child, .. } => {
child.insert(k, v);
todo!("branch insert")
}
Self::Leaf { window, .. } => {
use std::cmp::Ordering;
enum KeyIndex {
Exists(usize),
Before(usize),
End,
}
let change = window
.iter()
.enumerate()
.find_map(|(i, kv)| match kv.0.cmp(&k) {
Ordering::Less => None,
Ordering::Equal => Some(KeyIndex::Exists(i)),
Ordering::Greater => Some(KeyIndex::Before(i)),
})
.unwrap_or(KeyIndex::End);
match (change, v) {
(KeyIndex::Exists(i), Some(v)) => {
window[i] = (k, v);
}
(KeyIndex::Exists(i), None) => {
window.remove(i);
}
(KeyIndex::Before(i), Some(v)) => {
window.insert(i, (k, v));
}
(KeyIndex::Before(i), None) => {}
(KeyIndex::End, Some(v)) => {
window.push((k, v));
}
(KeyIndex::End, None) => {}
}
}
}
}
}