From aebcd036df1baf5701c802c2e948251064947b3f Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Fri, 10 Feb 2023 00:00:51 -0800 Subject: [PATCH 1/7] Start conversion --- src/latch_manager/latch_interval_btree.rs | 202 ++++++++++++++++++---- 1 file changed, 165 insertions(+), 37 deletions(-) diff --git a/src/latch_manager/latch_interval_btree.rs b/src/latch_manager/latch_interval_btree.rs index 4d0b085..8686de0 100644 --- a/src/latch_manager/latch_interval_btree.rs +++ b/src/latch_manager/latch_interval_btree.rs @@ -1,7 +1,9 @@ use std::{ borrow::{Borrow, BorrowMut}, cell::RefCell, + mem, rc::{Rc, Weak}, + sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard}, }; use self::Test::{print_node, print_tree}; @@ -12,9 +14,10 @@ pub trait NodeKey: std::fmt::Debug + Clone + Eq + PartialOrd + Ord {} impl NodeKey for i32 {} -type NodeLink = RefCell>>>; +type LatchNode = Arc>>; +type NodeLink = RefCell>>; // RefCell>>> -type WeakNodeLink = RefCell>>>; +type WeakNodeLink = RefCell>>>>; // RefCell>>>, #[derive(Debug, Clone)] @@ -29,7 +32,42 @@ pub enum Direction { Right, } +#[derive(Debug, Clone)] +pub enum LatchIntent { + DELETE, + INSERT, + SEARCH, +} + impl Node { + pub fn acquire_read_lock(&self) -> RwLockReadGuard<()> { + match self { + Node::Internal(internal_node) => internal_node.acquire_read_lock(), + Node::Leaf(leaf_node) => leaf_node.acquire_read_lock(), + } + } + + pub fn acquire_write_lock(&self) -> RwLockWriteGuard<()> { + match self { + Node::Internal(internal_node) => internal_node.acquire_write_lock(), + Node::Leaf(leaf_node) => leaf_node.acquire_write_lock(), + } + } + + /** + * A thread can release latch on a parent node if its child node + * considered safe. It is safe when: + * - the node won't split or merge when updated + * - not full (on insertion) + * - more than half full (on deletion) + */ + pub fn is_safe_to_release_parent_latch(&self, intent: LatchIntent) -> bool { + match self { + Node::Internal(internal_node) => internal_node.is_safe_to_release_parent_latch(intent), + Node::Leaf(leaf_node) => leaf_node.is_safe_to_release_parent_latch(intent), + } + } + pub fn as_internal_node(&self) -> &InternalNode { match self { Node::Internal(ref node) => node, @@ -136,6 +174,7 @@ pub struct InternalNode { // than the key edges: RefCell>>, order: u16, + rw_lock: Rc>, } #[derive(Debug, Clone)] @@ -145,6 +184,7 @@ pub struct LeafNode { left_ptr: WeakNodeLink, right_ptr: WeakNodeLink, order: u16, + rw_lock: Rc>, } // impl internal @@ -154,6 +194,32 @@ impl InternalNode { keys: RefCell::new(Vec::new()), edges: RefCell::new(Vec::new()), order: capacity, + rw_lock: Rc::new(RwLock::new(())), + } + } + + pub fn acquire_read_lock(&self) -> RwLockReadGuard<()> { + let lock = self.rw_lock.as_ref(); + lock.read().unwrap() + } + + pub fn acquire_write_lock(&self) -> RwLockWriteGuard<()> { + let lock = self.rw_lock.as_ref(); + lock.write().unwrap() + } + + /** + * A thread can release latch on a parent node if its child node + * considered safe. It is safe when: + * - the node won't split or merge when updated + * - not full (on insertion) + * - more than half full (on deletion) + */ + pub fn is_safe_to_release_parent_latch(&self, intent: LatchIntent) -> bool { + match intent { + LatchIntent::DELETE => self.has_spare_key(), + LatchIntent::INSERT => self.keys.borrow().len() + 1 < usize::from(self.order), + LatchIntent::SEARCH => true, } } @@ -167,7 +233,7 @@ impl InternalNode { // If the insert index of key K is n, then the corresponding // position for the node is n - 1. Note that n will never be 0 // because insert_node gets called after a split - pub fn insert_node(&self, node: Rc>, insert_key: K) -> () { + pub fn insert_node(&self, node: LatchNode, insert_key: K) -> () { // if key is greater than all elements, then the index is length of the keys (push) let mut insert_idx = self.keys.borrow().len(); for (pos, k) in self.keys.borrow().iter().enumerate() { @@ -289,7 +355,7 @@ impl InternalNode { /** * Find the left sibling provided the index of the corresponding edge in the parent's node */ - pub fn find_child_left_sibling(&self, edge_idx: usize) -> Option>> { + pub fn find_child_left_sibling(&self, edge_idx: usize) -> Option> { if edge_idx == 0 { return None; } @@ -299,7 +365,7 @@ impl InternalNode { /** * Find the right sibling provided the index of the corresponding edge in the parent's node */ - pub fn find_child_right_sibling(&self, edge_idx: usize) -> Option>> { + pub fn find_child_right_sibling(&self, edge_idx: usize) -> Option> { if edge_idx == self.edges.borrow().len() - 1 { return None; } @@ -392,6 +458,32 @@ impl LeafNode { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: capacity, + rw_lock: Rc::new(RwLock::new(())), + } + } + + pub fn acquire_read_lock(&self) -> RwLockReadGuard<()> { + let lock = self.rw_lock.as_ref(); + lock.read().unwrap() + } + + pub fn acquire_write_lock(&self) -> RwLockWriteGuard<()> { + let lock = self.rw_lock.as_ref(); + lock.write().unwrap() + } + + /** + * A thread can release latch on a parent node if its child node + * considered safe. It is safe when: + * - the node won't split or merge when updated + * - not full (on insertion) + * - more than half full (on deletion) + */ + pub fn is_safe_to_release_parent_latch(&self, intent: LatchIntent) -> bool { + match intent { + LatchIntent::DELETE => self.has_spare_key(), + LatchIntent::INSERT => self.start_keys.borrow().len() + 1 < usize::from(self.order), + LatchIntent::SEARCH => true, } } @@ -739,14 +831,17 @@ impl BTree { pub fn find_leaf_to_delete( &self, key_to_delete: &K, - ) -> (Option>>, Vec<(usize, Direction, Rc>)>) { + ) -> (Option>, Vec<(usize, Direction, LatchNode)>) { let mut temp_node = self.root.borrow().clone(); let mut next = None; let mut stack = Vec::new(); loop { - match temp_node { - Some(ref node) => match node.as_ref() { + if let Some(node) = temp_node { + let lock = node.as_ref(); + let guard = lock.write().unwrap(); + + match &*guard { Node::Internal(internal_node) => { for (idx, k) in internal_node.keys.borrow().iter().enumerate() { if key_to_delete < k { @@ -762,14 +857,12 @@ impl BTree { } } } - Node::Leaf(_) => break, - }, - None => panic!("should not be undefined"), - } - match next { - Some(ref v) => temp_node = next.clone(), - None => panic!("next is not provided"), + }; + match next { + Some(ref v) => temp_node = next.clone(), + None => panic!("next is not provided"), + } } } @@ -814,19 +907,38 @@ impl BTree { (None, stack) } - // determines which leaf node a new key should go into - // we assume there will at least always be one root. + // Determines which leaf node a new key should go into we assume there will at least always be one root. + // As it traverses down the nodes, acquire write locks. But if it's safe to release parent lock, it will. // Returns the leaf node to add and the stack of parent nodes - pub fn find_leaf_to_add(&self, key_to_add: &K) -> (Option>>, Vec>>) { + pub fn find_leaf_to_add<'a>( + &self, + key_to_add: &K, + ) -> ( + Option>>, + Vec<(Rc>, Option>)>, + ) { let mut temp_node = self.root.borrow().clone(); let mut next = None; - let mut stack = Vec::new(); + let mut stack: Vec<(Rc>, Option>)> = Vec::new(); loop { - match temp_node { - Some(ref node) => match node.as_ref() { + match &temp_node { + Some(ref node_rc) => match node_rc.as_ref() { Node::Internal(internal_node) => { - stack.push(node.clone()); + let acquired_lock = internal_node.acquire_write_lock(); + if internal_node.is_safe_to_release_parent_latch(LatchIntent::INSERT) { + let len = stack.len(); + if len > 0 { + let idx = len - 1; + let parent_lock = mem::replace(&mut stack[idx].1, None); + if let Some(write_lock) = parent_lock { + drop(write_lock) + } + } + } + stack.push((node_rc.clone(), Some(acquired_lock))); + // stack.push((node_rc.clone(), None)); + for (idx, k) in internal_node.keys.borrow().iter().enumerate() { if key_to_add < k { next = internal_node.edges.borrow()[idx].borrow().clone(); @@ -838,16 +950,15 @@ impl BTree { } } } - Node::Leaf(_) => break, }, None => panic!("should not be undefined"), - } + }; match next { Some(_) => temp_node = next.clone(), None => panic!("next is not provided"), - } + }; } (temp_node, stack) @@ -878,7 +989,7 @@ impl BTree { loop { if parent_stack.len() - offset > 0 { let idx = parent_stack.len() - 1 - offset; - current_node = parent_stack[idx].clone(); + current_node = parent_stack[idx].0.clone(); // this is the node we want to insert the let curr_parent = current_node.as_ref().as_internal_node(); curr_parent.insert_node(split_node.clone(), median.clone()); @@ -898,6 +1009,7 @@ impl BTree { RefCell::new(Some(split_node.clone())), ])), order: self.order, + rw_lock: Rc::new(RwLock::new(())), }))); break; } @@ -935,6 +1047,7 @@ impl BTree { keys: RefCell::new(right_keys), edges: RefCell::new(right_edges), order: internal_node.order, + rw_lock: Rc::new(RwLock::new(())), }; (Rc::new(Node::Internal(new_right_node)), right_start) } @@ -952,6 +1065,7 @@ impl BTree { left_ptr: RefCell::new(Some(Rc::downgrade(&node))), // TODO: set the left_sibling to the current leaf node later right_ptr: RefCell::new(right_sibling), order: leaf_node.order, + rw_lock: Rc::new(RwLock::new(())), }; let right_rc = Rc::new(Node::Leaf(new_right_node)); leaf_node @@ -982,7 +1096,9 @@ impl BTree { pub fn delete(&self, key_to_delete: K) -> () { let (node_to_delete, stack) = self.find_leaf_to_delete(&key_to_delete); if let Some(ref node_ref) = node_to_delete { - let leaf_node = node_ref.as_ref().as_leaf_node(); + let leaf_guard = node_ref.as_ref().write().unwrap(); + + let leaf_node = leaf_guard.as_leaf_node(); let did_leaf_merge = leaf_node.delete_key(&key_to_delete, &stack); if did_leaf_merge { @@ -1013,7 +1129,7 @@ impl BTree { } mod Test { - use std::{borrow::Borrow, cell::RefCell, process::Child, rc::Rc}; + use std::{borrow::Borrow, cell::RefCell, process::Child, rc::Rc, sync::RwLock}; use super::{BTree, InternalNode, LeafNode, Node, NodeKey, NodeLink, WeakNodeLink}; @@ -1132,6 +1248,7 @@ mod Test { keys: RefCell::new(internal_node.keys.clone()), edges: RefCell::new(edges), order, + rw_lock: Rc::new(RwLock::new(())), }; (Rc::new(Node::Internal(ret_node)), leaves) } @@ -1142,6 +1259,7 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: order, + rw_lock: Rc::new(RwLock::new(())), }); let leaf_rc = Rc::new(leaf); (leaf_rc.clone(), Vec::from([leaf_rc.clone()])) @@ -1463,7 +1581,7 @@ mod Test { let (leaf1, stack) = tree.find_leaf_to_add(&0); assert_eq!(stack.len(), 1); - assert_internal(stack[0].clone(), Vec::from([12, 15, 19])); + assert_internal(stack[0].0.clone(), Vec::from([12, 15, 19])); assert_leaf(leaf1.unwrap(), &Vec::from([11])); @@ -1478,13 +1596,13 @@ mod Test { } mod split { - use std::{borrow::Borrow, cell::RefCell, rc::Rc}; + use std::{borrow::Borrow, cell::RefCell, rc::Rc, sync::RwLock}; use crate::latch_manager::latch_interval_btree::{ BTree, LeafNode, Node, Test::{ - assert_leaf_with_siblings, assert_node, get_all_leaf_nodes, get_all_leaves, - get_start_keys_from_weak_link, print_node, + assert_leaf_with_siblings, assert_node, assert_tree, get_all_leaf_nodes, + get_all_leaves, get_start_keys_from_weak_link, print_node, }, }; @@ -1551,6 +1669,7 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: 4, + rw_lock: Rc::new(RwLock::new(())), }; let leaf_rc = Rc::new(Node::Leaf(leaf)); @@ -1560,6 +1679,7 @@ mod Test { left_ptr: RefCell::new(Some(Rc::downgrade(&leaf_rc))), right_ptr: RefCell::new(None), order: 4, + rw_lock: Rc::new(RwLock::new(())), }; let right_sibling_rc = Rc::new(Node::Leaf(right_sibling)); match leaf_rc.as_ref() { @@ -1731,7 +1851,7 @@ mod Test { } mod leaf_underflow { - use std::cell::RefCell; + use std::{cell::RefCell, rc::Rc, sync::RwLock}; use crate::latch_manager::latch_interval_btree::LeafNode; @@ -1743,6 +1863,7 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: 4, + rw_lock: Rc::new(RwLock::new(())), }; assert!(leaf.is_underflow()); } @@ -2068,7 +2189,7 @@ mod Test { }; mod has_spare_keys { - use std::cell::RefCell; + use std::{cell::RefCell, rc::Rc, sync::RwLock}; use crate::latch_manager::latch_interval_btree::{ LeafNode, @@ -2088,6 +2209,7 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: 3, + rw_lock: Rc::new(RwLock::new(())), }; assert_eq!(leaf_node.has_spare_key(), true); } @@ -2100,6 +2222,7 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: 3, + rw_lock: Rc::new(RwLock::new(())), }; assert_eq!(leaf_node.has_spare_key(), false); } @@ -2654,8 +2777,13 @@ mod Test { #[test] fn experiment() { - for idx in 0..5 { - println!("{}", idx); - } + let my_rwlock = RwLock::new(5); + + let read1 = my_rwlock.read().unwrap(); // one .read() is fine + let read2 = my_rwlock.read().unwrap(); // two .read()s is also fine + + println!("{:?}, {:?}", read1, read2); + drop(read1); + drop(read2); } } From 786cea3fb8f826a211b0c345647b5ddff35f53fe Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Fri, 10 Feb 2023 00:29:38 -0800 Subject: [PATCH 2/7] More fixes --- src/latch_manager/latch_interval_btree.rs | 62 +++++++++++------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/src/latch_manager/latch_interval_btree.rs b/src/latch_manager/latch_interval_btree.rs index 8686de0..54eb125 100644 --- a/src/latch_manager/latch_interval_btree.rs +++ b/src/latch_manager/latch_interval_btree.rs @@ -306,13 +306,12 @@ impl InternalNode { pub fn steal_from_left_sibling( &self, left_sibling: &InternalNode, - parent_rc: Rc>, + parent_node: &InternalNode, edge_idx: usize, ) -> bool { if !left_sibling.has_spare_key() { return false; } - let parent_node = parent_rc.as_internal_node(); // this will be the new split key for the current node let parent_split_key = parent_node.keys.borrow()[edge_idx - 1].clone(); let left_size = left_sibling.edges.borrow().len(); @@ -372,6 +371,10 @@ impl InternalNode { self.edges.borrow()[edge_idx + 1].borrow().clone() } + pub fn update_key_at_index(&self, idx: usize, new_key: K) { + self.keys.borrow_mut()[idx] = new_key; + } + /** * We first try to merge with left sibling if there is one. * Otherwise we try to merge with the right node. @@ -384,8 +387,9 @@ impl InternalNode { pub fn merge_with_sibling(&self, parent_rc: Rc>, edge_idx: usize) { let parent_node = parent_rc.as_internal_node(); let left_sibling = parent_node.find_child_left_sibling(edge_idx); - if let Some(ref left_rc) = left_sibling { - let left_node = left_rc.as_internal_node(); + if let Some(ref left_latch_node) = left_sibling { + let left_guard = left_latch_node.as_ref().write().unwrap(); + let left_node = left_guard.as_internal_node(); let parent_split_key = parent_node.keys.borrow_mut().remove(edge_idx - 1); let mut left_keys = left_node.keys.borrow_mut(); left_keys.push(parent_split_key); @@ -399,8 +403,9 @@ impl InternalNode { parent_node.edges.borrow_mut().remove(edge_idx); } else { let right_sibling = parent_node.find_child_right_sibling(edge_idx); - if let Some(right_rc) = right_sibling { - let right_node = right_rc.as_internal_node(); + if let Some(right_latch_node) = right_sibling { + let right_guard = right_latch_node.as_ref().write().unwrap(); + let right_node = right_guard.as_internal_node(); // we merge right node into the current node let parent_split_key = parent_node.keys.borrow_mut().remove(edge_idx); let mut current_keys = self.keys.borrow_mut(); @@ -420,17 +425,14 @@ impl InternalNode { self.keys.borrow().contains(key) } - pub fn deal_with_underflow(&self, parent_rc: Rc>, edge_idx: usize) { + pub fn deal_with_underflow(&self, parent_node: &InternalNode, edge_idx: usize) { if self.is_underflow() { - let parent_node = parent_rc.as_internal_node(); let left_sibling = parent_node.find_child_left_sibling(edge_idx); let mut is_stolen = false; - if let Some(ref left_rc) = left_sibling { - is_stolen = self.steal_from_left_sibling( - left_rc.as_internal_node(), - parent_rc.clone(), - edge_idx, - ); + if let Some(ref left_latch_node) = left_sibling { + let write_guard = left_latch_node.as_ref().write().unwrap(); + let left_node = write_guard.as_internal_node(); + is_stolen = self.steal_from_left_sibling(left_node, parent_node, edge_idx); } if !is_stolen { let right_sibling = parent_node.find_child_right_sibling(edge_idx); @@ -624,7 +626,7 @@ impl LeafNode { &self, key_to_delete: &K, left_sibling: Rc>, - stack: &Vec<(usize, Direction, Rc>)>, + stack: &Vec<(usize, Direction, &InternalNode)>, ) -> bool { if left_sibling.has_spare_key() { let left_leaf_sibling = left_sibling.as_ref().as_leaf_node(); @@ -635,9 +637,7 @@ impl LeafNode { let (idx, direction, parent_node) = stack[stack.len() - 1].clone(); // Update parent's split key. Since we are stealing from left sibling, // the new split_key will be the stolen key - parent_node - .as_ref() - .update_key_at_index(idx - 1, stolen_key); + parent_node.update_key_at_index(idx - 1, stolen_key); return true; } false @@ -705,18 +705,17 @@ impl LeafNode { pub fn update_ancestors_after_delete( &self, key_to_delete: &K, - stack: &Vec<(usize, Direction, Rc>)>, - right_sibling_option: &Option>>, + stack: &Vec<(usize, Direction, &InternalNode)>, + right_sibling_option: &Option>, ) -> () { let right_sibling = self.right_ptr.borrow(); let next_largest_key = self.find_next_largest_key(key_to_delete, right_sibling_option); // if the leaf to delete is in the right subtree and the // current node is equal to the key to delete, then we update to the next biggest node - for (iter_idx, (idx, direction, node)) in stack.iter().enumerate() { + for (iter_idx, (idx, direction, internal_node)) in stack.iter().enumerate() { match direction { Direction::Left => {} Direction::Right => { - let internal_node = node.as_internal_node(); let key_idx = *idx - 1; let mut keys = internal_node.keys.borrow_mut(); if &keys[key_idx] == key_to_delete { @@ -731,7 +730,7 @@ impl LeafNode { pub fn find_next_largest_key( &self, key_to_delete: &K, - right_sibling_option: &Option>>, + right_sibling_option: &Option>, ) -> K { let idx = self.find_next_larger_key(key_to_delete); @@ -762,7 +761,7 @@ impl LeafNode { pub fn delete_key( &self, key_to_delete: &K, - stack: &Vec<(usize, Direction, Rc>)>, + stack: &Vec<(usize, Direction, &InternalNode)>, ) -> bool { let is_deleted = self.remove_key(key_to_delete.clone()); @@ -774,8 +773,7 @@ impl LeafNode { if stack.len() == 0 { return false; } - let (edge_idx, _, parent_node) = stack[stack.len() - 1].clone(); - let internal_node = parent_node.as_internal_node(); + let (edge_idx, _, internal_node) = stack[stack.len() - 1].clone(); let right_sibling_option = internal_node.find_child_right_sibling(edge_idx); let left_sibling_option = internal_node.find_child_left_sibling(edge_idx); if !self.is_underflow() { @@ -831,7 +829,10 @@ impl BTree { pub fn find_leaf_to_delete( &self, key_to_delete: &K, - ) -> (Option>, Vec<(usize, Direction, LatchNode)>) { + ) -> ( + Option>, + Vec<(usize, Direction, &InternalNode)>, + ) { let mut temp_node = self.root.borrow().clone(); let mut next = None; @@ -845,13 +846,13 @@ impl BTree { Node::Internal(internal_node) => { for (idx, k) in internal_node.keys.borrow().iter().enumerate() { if key_to_delete < k { - stack.push((idx, Direction::Left, node.clone())); + stack.push((idx, Direction::Left, internal_node)); next = internal_node.edges.borrow()[idx].borrow().clone(); break; } if idx == internal_node.keys.borrow().len() - 1 { - stack.push((idx + 1, Direction::Right, node.clone())); + stack.push((idx + 1, Direction::Right, internal_node)); next = internal_node.edges.borrow()[idx + 1].borrow().clone(); break; } @@ -1107,9 +1108,8 @@ impl BTree { if i < 1 { break; } - let (_, _, node) = &stack[i]; + let (_, _, internal_node) = &stack[i]; let (edge_idx, _, parent_node) = &stack[i - 1]; - let internal_node = node.as_internal_node(); internal_node.deal_with_underflow(parent_node.clone(), *edge_idx); println!("dealing with underflow!!"); From e22a04686c9ec1b038282ce61b660441cf393af4 Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Fri, 10 Feb 2023 00:47:36 -0800 Subject: [PATCH 3/7] More conversions --- src/latch_manager/latch_interval_btree.rs | 97 ++++++++++++----------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/src/latch_manager/latch_interval_btree.rs b/src/latch_manager/latch_interval_btree.rs index 54eb125..cabfeb1 100644 --- a/src/latch_manager/latch_interval_btree.rs +++ b/src/latch_manager/latch_interval_btree.rs @@ -265,14 +265,14 @@ impl InternalNode { // Tries to steal nodes from siblings if they have spares. // Returns whether or not it successfully stole from sibling - pub fn steal_from_sibling(&self, parent_rc: Rc>, edge_idx: usize) -> bool { - let parent_node = parent_rc.as_internal_node(); + pub fn steal_from_sibling(&self, parent_node: &InternalNode, edge_idx: usize) -> bool { let left_sibling = parent_node.find_child_left_sibling(edge_idx); let mut is_stolen = false; match left_sibling { - Some(left_rc) => { - let left_node = left_rc.as_internal_node(); - is_stolen = self.steal_from_left_sibling(left_node, parent_rc.clone(), edge_idx); + Some(left_latch_node) => { + let left_guard = left_latch_node.as_ref().write().unwrap(); + let left_node = left_guard.as_internal_node(); + is_stolen = self.steal_from_left_sibling(left_node, parent_node, edge_idx); if is_stolen { return true; } @@ -282,9 +282,11 @@ impl InternalNode { let right_sibling = parent_node.find_child_right_sibling(edge_idx); let mut is_stolen = false; match right_sibling { - Some(right_rc) => { - let right_node = right_rc.as_internal_node(); - is_stolen = self.steal_from_right_sibling(right_node, parent_rc.clone(), edge_idx); + Some(right_latch_node) => { + let right_guard = right_latch_node.as_ref().write().unwrap(); + let right_node = right_guard.as_internal_node(); + is_stolen = self.steal_from_right_sibling(right_node, parent_node, edge_idx); + if is_stolen { return true; } @@ -333,13 +335,12 @@ impl InternalNode { pub fn steal_from_right_sibling( &self, right_sibling: &InternalNode, - parent_rc: Rc>, + parent_node: &InternalNode, edge_idx: usize, ) -> bool { if !right_sibling.has_spare_key() { return false; } - let parent_node = parent_rc.as_internal_node(); // this will be the new split key for the current node let parent_split_key = parent_node.keys.borrow()[edge_idx].clone(); let stolen_edge = right_sibling.edges.borrow_mut().remove(0); @@ -384,8 +385,7 @@ impl InternalNode { * - Add the keys and edges from right_node to left_node * - Remove the edge corresponding to the right_node */ - pub fn merge_with_sibling(&self, parent_rc: Rc>, edge_idx: usize) { - let parent_node = parent_rc.as_internal_node(); + pub fn merge_with_sibling(&self, parent_node: &InternalNode, edge_idx: usize) { let left_sibling = parent_node.find_child_left_sibling(edge_idx); if let Some(ref left_latch_node) = left_sibling { let left_guard = left_latch_node.as_ref().write().unwrap(); @@ -436,16 +436,14 @@ impl InternalNode { } if !is_stolen { let right_sibling = parent_node.find_child_right_sibling(edge_idx); - if let Some(ref right_rc) = right_sibling { - is_stolen = self.steal_from_right_sibling( - right_rc.as_internal_node(), - parent_rc.clone(), - edge_idx, - ); + if let Some(ref right_latch_node) = right_sibling { + let right_guard = right_latch_node.as_ref().write().unwrap(); + let right_node = right_guard.as_internal_node(); + is_stolen = self.steal_from_right_sibling(right_node, parent_node, edge_idx); } } if !is_stolen { - self.merge_with_sibling(parent_rc.clone(), edge_idx); + self.merge_with_sibling(parent_node, edge_idx); } } } @@ -586,29 +584,29 @@ impl LeafNode { pub fn steal_from_right_leaf_sibling( &self, key_to_delete: &K, - right_sibling: Rc>, - stack: &Vec<(usize, Direction, Rc>)>, + right_sibling: LatchNode, + stack: &Vec<(usize, Direction, &InternalNode)>, ) -> bool { + let right_write_guard = right_sibling.as_ref().write().unwrap(); + let right_sibling = right_write_guard.as_leaf_node(); if right_sibling.has_spare_key() { - let right_leaf_sibling = right_sibling.as_ref().as_leaf_node(); - let stolen_range = right_leaf_sibling.steal_smallest_key(); + let stolen_range = right_sibling.steal_smallest_key(); let stolen_key = stolen_range.start_key.clone(); self.insert_range(stolen_range); // Update any parent's split key. Since we are stealing from right sibling, // if the split key is the key to delete, it is now the stolen key from right sibling - for (iter_idx, (idx, direction, node)) in stack.iter().enumerate() { + for (iter_idx, (idx, direction, internal_node)) in stack.iter().enumerate() { let key_idx = match direction { Direction::Left => *idx, Direction::Right => *idx - 1, }; - let internal_node = node.as_ref().as_internal_node(); if iter_idx == stack.len() - 1 { // Update parent's split key. Since we are stealing from right sibling, // the new split_key will be the right sibling's new smallest key - node.as_ref() - .update_key_at_index(key_idx, right_sibling.get_lower().unwrap()); + internal_node + .update_key_at_index(key_idx, right_write_guard.get_lower().unwrap()); } else { let mut keys = internal_node.keys.borrow_mut(); let key = &keys[key_idx]; @@ -625,12 +623,13 @@ impl LeafNode { pub fn steal_from_left_leaf_sibling( &self, key_to_delete: &K, - left_sibling: Rc>, + left_sibling: LatchNode, stack: &Vec<(usize, Direction, &InternalNode)>, ) -> bool { + let left_write_guard = left_sibling.as_ref().write().unwrap(); + let left_sibling = left_write_guard.as_leaf_node(); if left_sibling.has_spare_key() { - let left_leaf_sibling = left_sibling.as_ref().as_leaf_node(); - let stolen_range = left_leaf_sibling.steal_biggest_key(); + let stolen_range = left_sibling.steal_biggest_key(); let stolen_key = stolen_range.start_key.clone(); self.insert_range(stolen_range); @@ -652,13 +651,13 @@ impl LeafNode { * * We apply the same to the right node if there is no left node */ - pub fn merge_node(&self, parent_rc: Rc>, edge_idx: usize) { - let internal_node = parent_rc.as_internal_node(); - let left_sibling = internal_node.find_child_left_sibling(edge_idx); + pub fn merge_node(&self, parent_node: &InternalNode, edge_idx: usize) { + let left_sibling = parent_node.find_child_left_sibling(edge_idx); match left_sibling { - Some(left_rc) => { + Some(left_latch_node) => { + let left_guard = left_latch_node.as_ref().write().unwrap(); // merge current node into left node - let left_node = left_rc.as_ref().as_leaf_node(); + let left_node = left_guard.as_leaf_node(); left_node .start_keys .borrow_mut() @@ -669,17 +668,17 @@ impl LeafNode { .append(&mut self.end_keys.borrow_mut()); // edge_idx - 1 | split_key | edge_idx // We want to remove edge_idx and split_key (will be edge_idx - 1 in coresponding keys vec) - let parent_node = parent_rc.as_ref().as_internal_node(); parent_node.edges.borrow_mut().remove(edge_idx); parent_node.keys.borrow_mut().remove(edge_idx - 1); *left_node.right_ptr.borrow_mut() = self.right_ptr.take(); } None => { - let right_sibling = internal_node.find_child_right_sibling(edge_idx); + let right_sibling = parent_node.find_child_right_sibling(edge_idx); match right_sibling { - Some(right_rc) => { + Some(right_latch_node) => { + let right_guard = right_latch_node.as_ref().write().unwrap(); // merge right node into current node - let right_node = right_rc.as_ref().as_leaf_node(); + let right_node = right_guard.as_leaf_node(); self.start_keys .borrow_mut() .append(&mut right_node.start_keys.borrow_mut()); @@ -689,7 +688,6 @@ impl LeafNode { // edge_idx | split_key | edge_idx + 1 // We want to remove edge_idx + 1 and split_key (will be edge_idx in coresponding keys vec) - let parent_node = parent_rc.as_ref().as_internal_node(); parent_node.edges.borrow_mut().remove(edge_idx + 1); parent_node.keys.borrow_mut().remove(edge_idx); *self.right_ptr.borrow_mut() = right_node.right_ptr.take(); @@ -741,8 +739,9 @@ impl LeafNode { None => { // This means that the next biggest key is not in the same leaf node let right_leaf_option = right_sibling_option.clone(); - let right_leaf = right_leaf_option.unwrap(); - return right_leaf.as_leaf_node().start_keys.borrow()[0].clone(); + let right_latch_node = right_leaf_option.unwrap(); + let right_read_guard = right_latch_node.as_ref().read().unwrap(); + return right_read_guard.as_leaf_node().start_keys.borrow()[0].clone(); } } } @@ -773,9 +772,9 @@ impl LeafNode { if stack.len() == 0 { return false; } - let (edge_idx, _, internal_node) = stack[stack.len() - 1].clone(); - let right_sibling_option = internal_node.find_child_right_sibling(edge_idx); - let left_sibling_option = internal_node.find_child_left_sibling(edge_idx); + let (edge_idx, _, parent_node) = stack[stack.len() - 1].clone(); + let right_sibling_option = parent_node.find_child_right_sibling(edge_idx); + let left_sibling_option = parent_node.find_child_left_sibling(edge_idx); if !self.is_underflow() { self.update_ancestors_after_delete(&key_to_delete, &stack, &right_sibling_option); return false; @@ -789,13 +788,13 @@ impl LeafNode { if !is_stolen { if let Some(right_sibling) = right_sibling_option { is_stolen = - self.steal_from_right_leaf_sibling(&key_to_delete, right_sibling, &stack); + self.steal_from_right_leaf_sibling(&key_to_delete, right_sibling, stack); } } // Can't borrow from either siblings. In this case we merge if !is_stolen { - self.merge_node(parent_node.clone(), edge_idx); + self.merge_node(parent_node, edge_idx); return true; } return false; @@ -816,7 +815,9 @@ pub struct Range { impl BTree { pub fn new(capacity: u16) -> Self { BTree { - root: RefCell::new(Some(Rc::new(Node::Leaf(LeafNode::new(capacity))))), + root: RefCell::new(Some(Arc::new(RwLock::new(Node::Leaf(LeafNode::new( + capacity, + )))))), order: capacity, } } From bc26825d50bf0bb06104e5389ed92fb735be7c47 Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Fri, 10 Feb 2023 21:34:58 -0800 Subject: [PATCH 4/7] Convert to LatchNode --- src/latch_manager/latch_interval_btree.rs | 409 ++++++++++------------ 1 file changed, 187 insertions(+), 222 deletions(-) diff --git a/src/latch_manager/latch_interval_btree.rs b/src/latch_manager/latch_interval_btree.rs index cabfeb1..c411794 100644 --- a/src/latch_manager/latch_interval_btree.rs +++ b/src/latch_manager/latch_interval_btree.rs @@ -1,13 +1,16 @@ use std::{ borrow::{Borrow, BorrowMut}, cell::RefCell, + fmt::write, mem, - rc::{Rc, Weak}, - sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard}, + sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard, Weak}, + thread::current, }; use self::Test::{print_node, print_tree}; +use super::latch_manager::LatchGuard; + struct Foo {} pub trait NodeKey: std::fmt::Debug + Clone + Eq + PartialOrd + Ord {} @@ -174,7 +177,6 @@ pub struct InternalNode { // than the key edges: RefCell>>, order: u16, - rw_lock: Rc>, } #[derive(Debug, Clone)] @@ -184,7 +186,6 @@ pub struct LeafNode { left_ptr: WeakNodeLink, right_ptr: WeakNodeLink, order: u16, - rw_lock: Rc>, } // impl internal @@ -194,18 +195,36 @@ impl InternalNode { keys: RefCell::new(Vec::new()), edges: RefCell::new(Vec::new()), order: capacity, - rw_lock: Rc::new(RwLock::new(())), } } - pub fn acquire_read_lock(&self) -> RwLockReadGuard<()> { - let lock = self.rw_lock.as_ref(); - lock.read().unwrap() - } - - pub fn acquire_write_lock(&self) -> RwLockWriteGuard<()> { - let lock = self.rw_lock.as_ref(); - lock.write().unwrap() + pub fn split_node(&self) -> (LatchNode, K) { + // + // Suppose we have an internal node: + // a 0 b 5 c 10 d + // where numbers represents nodes and letters represent edges. + // After splitting, we get: + // left: a 0 b + // right: e 5 c 10 d + // The reason for this is that 5 will be pushed up and since + // node corresponding to b must be less than 5 it must be + // to the left of the mid key that gets pushed up + // + let mid_idx = self.keys.borrow().len() / 2; + let mut right_keys = self.keys.borrow_mut().split_off(mid_idx); + let mut right_edges = self.edges.borrow_mut().split_off(mid_idx + 1); + right_edges.insert(0, RefCell::new(None)); + let right_start = right_keys.remove(0); + right_edges.remove(0); + let new_right_node = InternalNode { + keys: RefCell::new(right_keys), + edges: RefCell::new(right_edges), + order: self.order, + }; + ( + Arc::new(RwLock::new(Node::Internal(new_right_node))), + right_start, + ) } /** @@ -458,18 +477,28 @@ impl LeafNode { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: capacity, - rw_lock: Rc::new(RwLock::new(())), } } - pub fn acquire_read_lock(&self) -> RwLockReadGuard<()> { - let lock = self.rw_lock.as_ref(); - lock.read().unwrap() - } + pub fn split_node(&self, latch_node: LatchNode) -> (LatchNode, K) { + let mid = self.start_keys.borrow().len() / 2; + let right_start_keys = self.start_keys.borrow_mut().split_off(mid); - pub fn acquire_write_lock(&self) -> RwLockWriteGuard<()> { - let lock = self.rw_lock.as_ref(); - lock.write().unwrap() + let right_end_keys = self.end_keys.borrow_mut().split_off(mid); + let right_sibling = self.right_ptr.borrow_mut().take(); + let right_start = right_start_keys[0].clone(); + let new_right_node = LeafNode { + start_keys: RefCell::new(right_start_keys), + end_keys: RefCell::new(right_end_keys), + left_ptr: RefCell::new(Some(Arc::downgrade(&latch_node))), + right_ptr: RefCell::new(right_sibling), + order: self.order, + }; + let right_arc = Arc::new(RwLock::new(Node::Leaf(new_right_node))); + self.right_ptr + .borrow_mut() + .replace(Arc::downgrade(&right_arc)); + (right_arc, right_start) } /** @@ -871,44 +900,6 @@ impl BTree { (temp_node, stack) } - pub fn find_internal_node(&self, search_key: &K) -> (Option>>, Vec>>) { - let mut temp_node = self.root.borrow().clone(); - - let mut next = None; - let mut stack = Vec::new(); - loop { - match temp_node { - Some(ref node) => match node.as_ref() { - Node::Internal(ref internal_node) => { - if internal_node.contains_key(search_key) { - return (temp_node.clone(), stack); - } - stack.push(node.clone()); - for (idx, k) in internal_node.keys.borrow().iter().enumerate() { - if search_key < k { - next = internal_node.edges.borrow()[idx].borrow().clone(); - break; - } - - if idx == internal_node.keys.borrow().len() - 1 { - next = internal_node.edges.borrow()[idx + 1].borrow().clone(); - } - } - } - - Node::Leaf(_) => break, - }, - None => panic!("should not be undefined"), - } - - match next { - Some(_) => temp_node = next.clone(), - None => panic!("next is not provided"), - } - } - (None, stack) - } - // Determines which leaf node a new key should go into we assume there will at least always be one root. // As it traverses down the nodes, acquire write locks. But if it's safe to release parent lock, it will. // Returns the leaf node to add and the stack of parent nodes @@ -916,44 +907,50 @@ impl BTree { &self, key_to_add: &K, ) -> ( - Option>>, - Vec<(Rc>, Option>)>, + Option>, + Vec<(Option, Option>>)>, ) { let mut temp_node = self.root.borrow().clone(); let mut next = None; - let mut stack: Vec<(Rc>, Option>)> = Vec::new(); + let mut last_idx = None; + + let mut stack: Vec<(Option, Option>>)> = Vec::new(); loop { match &temp_node { - Some(ref node_rc) => match node_rc.as_ref() { - Node::Internal(internal_node) => { - let acquired_lock = internal_node.acquire_write_lock(); - if internal_node.is_safe_to_release_parent_latch(LatchIntent::INSERT) { - let len = stack.len(); - if len > 0 { - let idx = len - 1; - let parent_lock = mem::replace(&mut stack[idx].1, None); - if let Some(write_lock) = parent_lock { - drop(write_lock) + Some(ref latch_node) => { + let write_guard = latch_node.write().unwrap(); + match *write_guard { + Node::Internal(internal_node) => { + // if internal_node.is_safe_to_release_parent_latch(LatchIntent::INSERT) { + // let len = stack.len(); + // if len > 0 { + // let idx = len - 1; + // let parent_lock = mem::replace(&mut stack[idx].1, None); + // if let Some(write_lock) = parent_lock { + // drop(write_lock) + // } + // } + // } + stack.push((last_idx, Some(write_guard))); + // stack.push((node_rc.clone(), None)); + + for (idx, k) in internal_node.keys.borrow().iter().enumerate() { + if key_to_add < k { + next = internal_node.edges.borrow()[idx].borrow().clone(); + last_idx = Some(idx); + break; } - } - } - stack.push((node_rc.clone(), Some(acquired_lock))); - // stack.push((node_rc.clone(), None)); - for (idx, k) in internal_node.keys.borrow().iter().enumerate() { - if key_to_add < k { - next = internal_node.edges.borrow()[idx].borrow().clone(); - break; - } - - if idx == internal_node.keys.borrow().len() - 1 { - next = internal_node.edges.borrow()[idx + 1].borrow().clone(); + if idx == internal_node.keys.borrow().len() - 1 { + next = internal_node.edges.borrow()[idx + 1].borrow().clone(); + last_idx = Some(idx + 1); + } } } + Node::Leaf(_) => break, } - Node::Leaf(_) => break, - }, + } None => panic!("should not be undefined"), }; @@ -978,42 +975,56 @@ impl BTree { pub fn insert(&self, range: Range) -> () { // TODO: We need the parent node let (leaf, parent_stack) = self.find_leaf_to_add(&range.start_key); - let leaf = leaf.unwrap(); - match leaf.as_ref() { + let leaf_arc = leaf.unwrap(); + let write_guard = leaf_arc.write().unwrap(); + match *write_guard { Node::Internal(_) => panic!("There must be at least one leaf node in the btree"), Node::Leaf(leaf_node) => { leaf_node.insert_range(range); if !leaf_node.has_capacity() { - let (mut split_node, mut median) = BTree::split_node(leaf.clone()); + let (mut split_node, mut median) = leaf_node.split_node(leaf_arc.clone()); let mut offset = 0; - let mut current_node = leaf.clone(); + let mut current_node = Some(write_guard); loop { - if parent_stack.len() - offset > 0 { - let idx = parent_stack.len() - 1 - offset; - current_node = parent_stack[idx].0.clone(); - // this is the node we want to insert the - let curr_parent = current_node.as_ref().as_internal_node(); - curr_parent.insert_node(split_node.clone(), median.clone()); - if curr_parent.has_capacity() { + if let Some(current_guard) = current_node { + if parent_stack.len() - offset > 0 { + let idx = parent_stack.len() - 1 - offset; + let (edge_idx, node) = parent_stack[idx]; + current_node = node; + // this is the node we want to insert the + + let curr_parent = current_guard.as_internal_node(); + let latch_node = curr_parent.edges.borrow()[edge_idx.unwrap()] + .borrow() + .clone() + .unwrap(); + curr_parent.insert_node(split_node.clone(), median.clone()); + if curr_parent.has_capacity() { + break; + } + (split_node, median) = match *current_guard { + Node::Internal(ref internal) => internal.split_node(), + Node::Leaf(ref leaf) => leaf.split_node(latch_node), + }; + offset = offset + 1; + } else { + // root needs to split. Create a new root with one key and 2 children + self.root.borrow_mut().replace(Arc::new(RwLock::new( + Node::Internal(InternalNode { + keys: RefCell::new(Vec::from([median.clone()])), + edges: RefCell::new(Vec::from([ + RefCell::new(self.root.borrow().clone()), + RefCell::new(Some(split_node.clone())), + ])), + order: self.order, + }), + ))); break; } - (split_node, median) = BTree::split_node(current_node.clone()); - offset = offset + 1; } else { - // root needs to split. Create a new root with one key and 2 children - self.root - .borrow_mut() - .replace(Rc::new(Node::Internal(InternalNode { - keys: RefCell::new(Vec::from([median.clone()])), - edges: RefCell::new(Vec::from([ - RefCell::new(Some(current_node.clone())), - RefCell::new(Some(split_node.clone())), - ])), - order: self.order, - rw_lock: Rc::new(RwLock::new(())), - }))); - break; + // once we see a null, then this means we won't need to update parents as the write lock is released + return; } } } @@ -1021,64 +1032,6 @@ impl BTree { } } - /** - * Allocate a new leaf node and move half keys to the new node. - * Returns the new node and the smallest key in the new node. - */ - pub fn split_node(node: Rc>) -> (Rc>, K) { - match node.as_ref() { - Node::Internal(internal_node) => { - // - // Suppose we have an internal node: - // a 0 b 5 c 10 d - // where numbers represents nodes and letters represent edges. - // After splitting, we get: - // left: a 0 b - // right: e 5 c 10 d - // The reason for this is that 5 will be pushed up and since - // node corresponding to b must be less than 5 it must be - // to the left of the mid key that gets pushed up - // - let mid_idx = internal_node.keys.borrow().len() / 2; - let mut right_keys = internal_node.keys.borrow_mut().split_off(mid_idx); - let mut right_edges = internal_node.edges.borrow_mut().split_off(mid_idx + 1); - right_edges.insert(0, RefCell::new(None)); - let right_start = right_keys.remove(0); - right_edges.remove(0); - let new_right_node = InternalNode { - keys: RefCell::new(right_keys), - edges: RefCell::new(right_edges), - order: internal_node.order, - rw_lock: Rc::new(RwLock::new(())), - }; - (Rc::new(Node::Internal(new_right_node)), right_start) - } - Node::Leaf(leaf_node) => { - let mid = leaf_node.start_keys.borrow().len() / 2; - let right_start_keys = leaf_node.start_keys.borrow_mut().split_off(mid); - - let right_end_keys = leaf_node.end_keys.borrow_mut().split_off(mid); - let right_sibling = leaf_node.right_ptr.borrow_mut().take(); - let right_start = right_start_keys[0].clone(); - - let new_right_node = LeafNode { - start_keys: RefCell::new(right_start_keys), - end_keys: RefCell::new(right_end_keys), - left_ptr: RefCell::new(Some(Rc::downgrade(&node))), // TODO: set the left_sibling to the current leaf node later - right_ptr: RefCell::new(right_sibling), - order: leaf_node.order, - rw_lock: Rc::new(RwLock::new(())), - }; - let right_rc = Rc::new(Node::Leaf(new_right_node)); - leaf_node - .right_ptr - .borrow_mut() - .replace(Rc::downgrade(&right_rc)); - (right_rc, right_start) - } - } - } - /** * - Find the leaf where the key exists * - Remove the key @@ -1130,9 +1083,14 @@ impl BTree { } mod Test { - use std::{borrow::Borrow, cell::RefCell, process::Child, rc::Rc, sync::RwLock}; + use std::{ + borrow::Borrow, + cell::RefCell, + process::Child, + sync::{Arc, RwLock}, + }; - use super::{BTree, InternalNode, LeafNode, Node, NodeKey, NodeLink, WeakNodeLink}; + use super::{BTree, InternalNode, LatchNode, LeafNode, Node, NodeKey, NodeLink, WeakNodeLink}; pub fn find_node_and_parent_with_indices( tree: &BTree, @@ -1196,11 +1154,11 @@ mod Test { } } - pub fn create_test_node(node: &TestNode, order: u16) -> Rc> { + pub fn create_test_node(node: &TestNode, order: u16) -> LatchNode { let (node, mut leaves) = create_tree_from_test_node_internal(node, order); for (idx, child) in leaves.iter().enumerate() { - match child.as_ref() { + match *child.read().unwrap() { Node::Internal(_) => panic!("Node must be a leaf"), Node::Leaf(leaf_node) => { if idx > 0 { @@ -1226,7 +1184,7 @@ mod Test { pub fn create_tree_from_test_node_internal( node: &TestNode, order: u16, - ) -> (Rc>, Vec>>) { + ) -> (LatchNode, Vec>) { match node { TestNode::Internal(internal_node) => { let mut leaves = Vec::new(); @@ -1251,7 +1209,7 @@ mod Test { order, rw_lock: Rc::new(RwLock::new(())), }; - (Rc::new(Node::Internal(ret_node)), leaves) + (Arc::new(RwLock::new(Node::Internal(ret_node))), leaves) } TestNode::Leaf(leaf_node) => { let leaf = Node::Leaf(LeafNode { @@ -1262,8 +1220,8 @@ mod Test { order: order, rw_lock: Rc::new(RwLock::new(())), }); - let leaf_rc = Rc::new(leaf); - (leaf_rc.clone(), Vec::from([leaf_rc.clone()])) + let leaf_latch = Arc::new(RwLock::new(leaf)); + (leaf_latch, Vec::from([leaf_latch.clone()])) } } } @@ -1276,7 +1234,7 @@ mod Test { print_tree_internal(&tree.root, 0); } - pub fn print_node_recursive(node: Rc>) { + pub fn print_node_recursive(node: LatchNode) { let tree = BTree { root: RefCell::new(Some(node.clone())), order: 4, @@ -1303,10 +1261,10 @@ mod Test { pub fn get_start_keys_from_weak_link(link: &WeakNodeLink) -> Option> { let edge = &*link.borrow(); - if let Some(ref rc) = edge { - let upgraded_ref = rc.upgrade(); - let unwrapped = upgraded_ref.unwrap(); - match unwrapped.as_ref() { + if let Some(ref weak_latch) = edge { + let upgraded_ref = weak_latch.upgrade(); + let unwrapped = upgraded_ref.unwrap().as_ref().read().unwrap(); + match *unwrapped { Node::Internal(_) => { panic!("Cannot get sibling from internal node"); } @@ -1322,11 +1280,11 @@ mod Test { fn get_first_key_from_weak_link(link: &WeakNodeLink) -> Option { let edge = &*link.borrow(); - if let Some(ref rc) = edge { - let upgraded_ref = rc.upgrade()?; + if let Some(ref weak_latch) = edge { + let upgraded_ref = weak_latch.upgrade()?; - let unwrapped = upgraded_ref; - match unwrapped.as_ref() { + let unwrapped = upgraded_ref.as_ref().read().unwrap(); + match *unwrapped { Node::Internal(_) => { panic!("Cannot get sibling from internal node"); } @@ -1346,9 +1304,9 @@ mod Test { fn print_tree_internal(link: &NodeLink, depth: usize) { let edge = link.borrow().clone(); - if let Some(ref rc) = edge { - let node = rc.as_ref(); - match node { + if let Some(ref weak_latch) = edge { + let node = weak_latch.read().unwrap(); + match *node { Node::Internal(ref node) => { println!( "{}Internal. Keys: {:?}", @@ -1373,15 +1331,16 @@ mod Test { } } - fn assert_node_and_leaves_siblings(node: Rc>, test_node: &TestNode) { + fn assert_node_and_leaves_siblings(node: LatchNode, test_node: &TestNode) { assert_node(node.clone(), test_node); let test_leaves = get_all_test_leaves(test_node); let leaves = get_all_leaf_nodes(node.clone()); assert_eq!(test_leaves.len(), leaves.len()); for (idx, current_test_node) in test_leaves.iter().enumerate() { let curr_node = leaves[idx].clone(); - let left_sibling = &*curr_node.as_leaf_node().left_ptr.borrow(); - let right_sibling = &*curr_node.as_leaf_node().right_ptr.borrow(); + let guard = curr_node.read().unwrap(); + let left_sibling = &*guard.as_leaf_node().left_ptr.borrow(); + let right_sibling = &*guard.as_leaf_node().right_ptr.borrow(); if idx == 0 { assert!(left_sibling.is_none()); } else { @@ -1403,12 +1362,11 @@ mod Test { * Given a node link and a test node structure, verify if if the node link * has the expected shape and properties */ - fn assert_node(node: Rc>, test_node: &TestNode) { + fn assert_node(node: LatchNode, test_node: &TestNode) { match test_node { TestNode::Internal(test_internal_node) => { - let node_rc = node.clone(); - let node_ref = node_rc.as_ref(); - let internal_node = node_ref.as_internal_node(); + let guard = node.clone().as_ref().read().unwrap(); + let internal_node = guard.as_internal_node(); assert_eq!(&*internal_node.keys.borrow(), &test_internal_node.keys); for (idx, child) in internal_node.edges.borrow().iter().enumerate() { let node = child.borrow(); @@ -1438,9 +1396,10 @@ mod Test { assert_node(root, test_node); } - fn get_all_leaves(node: Rc>) -> Vec>>> { + fn get_all_leaves(node: LatchNode) -> Vec>> { let mut leaves = Vec::new(); - match node.as_ref() { + let guard = node.as_ref().read().unwrap(); + match *guard { Node::Internal(internal_node) => { for edge in internal_node.edges.borrow().iter() { match &*edge.borrow() { @@ -1460,13 +1419,14 @@ mod Test { } fn assert_leaf_with_siblings( - node: Rc>, + node: LatchNode, test_leaf: &TestLeafNode, test_left_sibling: &Option>, test_right_sibling: &Option>, ) { assert_leaf(node.clone(), &test_leaf.keys); - let leaf_node = node.as_ref().as_leaf_node(); + let guard = node.as_ref().read().unwrap(); + let leaf_node = guard.as_leaf_node(); let left_sibling = &*leaf_node.left_ptr.borrow(); match left_sibling { Some(left_node) => { @@ -1494,9 +1454,10 @@ mod Test { }; } - fn get_all_leaf_nodes(node: Rc>) -> Vec>> { + fn get_all_leaf_nodes(latch_node: LatchNode) -> Vec> { let mut leaves = Vec::new(); - match node.as_ref() { + let guard = latch_node.read().unwrap(); + match *guard { Node::Internal(internal_node) => { for edge in internal_node.edges.borrow().iter() { if let Some(child) = &*edge.borrow() { @@ -1506,7 +1467,7 @@ mod Test { } } Node::Leaf(_) => { - leaves.push(node.clone()); + leaves.push(latch_node.clone()); } }; leaves @@ -1530,8 +1491,9 @@ mod Test { leaves } - fn assert_leaf(node: Rc>, start_keys: &Vec) { - match &node.as_ref() { + fn assert_leaf(node: LatchNode, start_keys: &Vec) { + let guard = node.read().unwrap(); + match *guard { Node::Internal(_) => panic!("not a leaf node"), Node::Leaf(leaf) => { assert_eq!(&*leaf.start_keys.borrow(), start_keys) @@ -1539,8 +1501,9 @@ mod Test { } } - fn assert_internal(node: Rc>, start_keys: Vec) { - match &node.as_ref() { + fn assert_internal(node: LatchNode, start_keys: Vec) { + let guard = node.read().unwrap(); + match *guard { Node::Internal(internal_node) => { assert_eq!(&*internal_node.keys.borrow(), &start_keys) } @@ -1549,7 +1512,7 @@ mod Test { } mod search { - use std::{cell::RefCell, rc::Rc}; + use std::cell::RefCell; use crate::latch_manager::latch_interval_btree::{ BTree, InternalNode, LeafNode, Node, @@ -1597,7 +1560,11 @@ mod Test { } mod split { - use std::{borrow::Borrow, cell::RefCell, rc::Rc, sync::RwLock}; + use std::{ + borrow::Borrow, + cell::RefCell, + sync::{Arc, RwLock}, + }; use crate::latch_manager::latch_interval_btree::{ BTree, LeafNode, Node, @@ -1630,7 +1597,9 @@ mod Test { ]), }); let node = create_test_node(&test_node, 4); - let (split_node, median) = BTree::split_node(node.clone()); + let read_guard = node.read().unwrap(); + + let (split_node, median) = read_guard.as_internal_node().split_node(); assert_eq!(median, 20); let split_test_node = TestNode::Internal(TestInternalNode { @@ -1670,10 +1639,9 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: 4, - rw_lock: Rc::new(RwLock::new(())), }; - let leaf_rc = Rc::new(Node::Leaf(leaf)); + let leaf_rc = Arc::new(Node::Leaf(leaf)); let right_sibling = LeafNode { start_keys: RefCell::new(Vec::from([4, 5, 6])), end_keys: RefCell::new(Vec::from([0, 1, 2])), @@ -1852,7 +1820,7 @@ mod Test { } mod leaf_underflow { - use std::{cell::RefCell, rc::Rc, sync::RwLock}; + use std::{cell::RefCell, sync::RwLock}; use crate::latch_manager::latch_interval_btree::LeafNode; @@ -1864,7 +1832,6 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: 4, - rw_lock: Rc::new(RwLock::new(())), }; assert!(leaf.is_underflow()); } @@ -2190,7 +2157,7 @@ mod Test { }; mod has_spare_keys { - use std::{cell::RefCell, rc::Rc, sync::RwLock}; + use std::{cell::RefCell, sync::RwLock}; use crate::latch_manager::latch_interval_btree::{ LeafNode, @@ -2210,7 +2177,6 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: 3, - rw_lock: Rc::new(RwLock::new(())), }; assert_eq!(leaf_node.has_spare_key(), true); } @@ -2223,7 +2189,6 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: 3, - rw_lock: Rc::new(RwLock::new(())), }; assert_eq!(leaf_node.has_spare_key(), false); } @@ -2724,8 +2689,8 @@ mod Test { }); let tree = create_test_tree(&test_node, 3); let (node, stack) = tree.find_leaf_to_delete(&16); - let unwrapped = node.unwrap(); - let leaf = unwrapped.as_leaf_node(); + let guard = node.unwrap().as_ref().read().unwrap(); + let leaf = guard.as_leaf_node(); let (edge_idx, dir, parent) = stack.last().unwrap(); leaf.merge_node(parent.clone(), *edge_idx); print_tree(&tree); @@ -2759,8 +2724,8 @@ mod Test { }); let tree = create_test_tree(&test_node, 3); let (node, stack) = tree.find_leaf_to_delete(&16); - let unwrapped = node.unwrap(); - let leaf = unwrapped.as_leaf_node(); + let guard = node.unwrap().as_ref().read().unwrap(); + let leaf = guard.as_leaf_node(); let (edge_idx, dir, parent) = stack.last().unwrap(); leaf.merge_node(parent.clone(), *edge_idx); print_tree(&tree); From a72e14564bad051386614dafd424bd23ce38f2a7 Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Sat, 11 Feb 2023 11:02:10 -0800 Subject: [PATCH 5/7] More attempts... --- src/latch_manager/latch_interval_btree.rs | 267 +++++++++++----------- 1 file changed, 133 insertions(+), 134 deletions(-) diff --git a/src/latch_manager/latch_interval_btree.rs b/src/latch_manager/latch_interval_btree.rs index c411794..0fce70c 100644 --- a/src/latch_manager/latch_interval_btree.rs +++ b/src/latch_manager/latch_interval_btree.rs @@ -23,6 +23,8 @@ type NodeLink = RefCell>>; type WeakNodeLink = RefCell>>>>; // RefCell>>>, +type DeleteStack<'a, K: NodeKey> = Vec<(usize, Direction, RwLockWriteGuard<'a, Node>)>; + #[derive(Debug, Clone)] pub enum Node { Internal(InternalNode), @@ -43,20 +45,6 @@ pub enum LatchIntent { } impl Node { - pub fn acquire_read_lock(&self) -> RwLockReadGuard<()> { - match self { - Node::Internal(internal_node) => internal_node.acquire_read_lock(), - Node::Leaf(leaf_node) => leaf_node.acquire_read_lock(), - } - } - - pub fn acquire_write_lock(&self) -> RwLockWriteGuard<()> { - match self { - Node::Internal(internal_node) => internal_node.acquire_write_lock(), - Node::Leaf(leaf_node) => leaf_node.acquire_write_lock(), - } - } - /** * A thread can release latch on a parent node if its child node * considered safe. It is safe when: @@ -614,7 +602,7 @@ impl LeafNode { &self, key_to_delete: &K, right_sibling: LatchNode, - stack: &Vec<(usize, Direction, &InternalNode)>, + stack: &DeleteStack, ) -> bool { let right_write_guard = right_sibling.as_ref().write().unwrap(); let right_sibling = right_write_guard.as_leaf_node(); @@ -625,7 +613,8 @@ impl LeafNode { // Update any parent's split key. Since we are stealing from right sibling, // if the split key is the key to delete, it is now the stolen key from right sibling - for (iter_idx, (idx, direction, internal_node)) in stack.iter().enumerate() { + for (iter_idx, (idx, direction, write_guard)) in stack.iter().enumerate() { + let internal_node = write_guard.as_internal_node(); let key_idx = match direction { Direction::Left => *idx, Direction::Right => *idx - 1, @@ -653,7 +642,7 @@ impl LeafNode { &self, key_to_delete: &K, left_sibling: LatchNode, - stack: &Vec<(usize, Direction, &InternalNode)>, + stack: &DeleteStack, ) -> bool { let left_write_guard = left_sibling.as_ref().write().unwrap(); let left_sibling = left_write_guard.as_leaf_node(); @@ -662,7 +651,8 @@ impl LeafNode { let stolen_key = stolen_range.start_key.clone(); self.insert_range(stolen_range); - let (idx, direction, parent_node) = stack[stack.len() - 1].clone(); + let (idx, direction, write_guard) = &stack[stack.len() - 1]; + let parent_node = write_guard.as_internal_node(); // Update parent's split key. Since we are stealing from left sibling, // the new split_key will be the stolen key parent_node.update_key_at_index(idx - 1, stolen_key); @@ -732,14 +722,15 @@ impl LeafNode { pub fn update_ancestors_after_delete( &self, key_to_delete: &K, - stack: &Vec<(usize, Direction, &InternalNode)>, + stack: &DeleteStack, right_sibling_option: &Option>, ) -> () { let right_sibling = self.right_ptr.borrow(); let next_largest_key = self.find_next_largest_key(key_to_delete, right_sibling_option); // if the leaf to delete is in the right subtree and the // current node is equal to the key to delete, then we update to the next biggest node - for (iter_idx, (idx, direction, internal_node)) in stack.iter().enumerate() { + for (iter_idx, (idx, direction, write_guard)) in stack.iter().enumerate() { + let internal_node = write_guard.as_internal_node(); match direction { Direction::Left => {} Direction::Right => { @@ -786,11 +777,7 @@ impl LeafNode { * - if it underflows, steal from either left or right sibling if they have a node to spare * - otherwise, merge node */ - pub fn delete_key( - &self, - key_to_delete: &K, - stack: &Vec<(usize, Direction, &InternalNode)>, - ) -> bool { + pub fn delete_key(&self, key_to_delete: &K, stack: &DeleteStack) -> bool { let is_deleted = self.remove_key(key_to_delete.clone()); if !is_deleted { @@ -801,9 +788,10 @@ impl LeafNode { if stack.len() == 0 { return false; } - let (edge_idx, _, parent_node) = stack[stack.len() - 1].clone(); - let right_sibling_option = parent_node.find_child_right_sibling(edge_idx); - let left_sibling_option = parent_node.find_child_left_sibling(edge_idx); + let (edge_idx, _, write_guard) = &stack[stack.len() - 1]; + let parent_node = write_guard.as_internal_node(); + let right_sibling_option = parent_node.find_child_right_sibling(*edge_idx); + let left_sibling_option = parent_node.find_child_left_sibling(*edge_idx); if !self.is_underflow() { self.update_ancestors_after_delete(&key_to_delete, &stack, &right_sibling_option); return false; @@ -823,7 +811,7 @@ impl LeafNode { // Can't borrow from either siblings. In this case we merge if !is_stolen { - self.merge_node(parent_node, edge_idx); + self.merge_node(parent_node, *edge_idx); return true; } return false; @@ -856,19 +844,13 @@ impl BTree { * corresponds to the index of the parent_node. This is useful when we need to find the siblings * of the nodes when borrowing / merging. */ - pub fn find_leaf_to_delete( - &self, - key_to_delete: &K, - ) -> ( - Option>, - Vec<(usize, Direction, &InternalNode)>, - ) { + pub fn find_leaf_to_delete(&self, key_to_delete: &K) -> (Option>, DeleteStack) { let mut temp_node = self.root.borrow().clone(); let mut next = None; let mut stack = Vec::new(); loop { - if let Some(node) = temp_node { + if let Some(ref node) = temp_node { let lock = node.as_ref(); let guard = lock.write().unwrap(); @@ -876,13 +858,13 @@ impl BTree { Node::Internal(internal_node) => { for (idx, k) in internal_node.keys.borrow().iter().enumerate() { if key_to_delete < k { - stack.push((idx, Direction::Left, internal_node)); + stack.push((idx, Direction::Left, guard)); next = internal_node.edges.borrow()[idx].borrow().clone(); break; } if idx == internal_node.keys.borrow().len() - 1 { - stack.push((idx + 1, Direction::Right, internal_node)); + stack.push((idx + 1, Direction::Right, guard)); next = internal_node.edges.borrow()[idx + 1].borrow().clone(); break; } @@ -1062,19 +1044,24 @@ impl BTree { if i < 1 { break; } - let (_, _, internal_node) = &stack[i]; - let (edge_idx, _, parent_node) = &stack[i - 1]; - internal_node.deal_with_underflow(parent_node.clone(), *edge_idx); + let (_, _, write_guard) = &stack[i]; + let internal_node = write_guard.as_internal_node(); + let (edge_idx, _, parent_guard) = &stack[i - 1]; + let parent_node = parent_guard.as_internal_node(); + internal_node.deal_with_underflow(parent_node, *edge_idx); println!("dealing with underflow!!"); i = i - 1; } } if stack.len() > 1 { - let root = self.root.borrow().clone().unwrap(); - let internal_root = root.as_internal_node(); - if internal_root.keys.borrow().len() == 0 { - let new_root = internal_root.edges.borrow()[0].borrow().clone().unwrap(); + let (_, _, root_guard) = &stack[0]; + let root_internal_node = root_guard.as_internal_node(); + if root_internal_node.keys.borrow().len() == 0 { + let new_root = root_internal_node.edges.borrow()[0] + .borrow() + .clone() + .unwrap(); self.root.borrow_mut().replace(new_root); } } @@ -1095,7 +1082,7 @@ mod Test { pub fn find_node_and_parent_with_indices( tree: &BTree, indices: Vec, - ) -> (Rc>, Rc>, usize) { + ) -> (LatchNode, LatchNode, usize) { let last_index = indices.last().unwrap().clone(); let (node, stack) = find_node_with_indices(tree, indices); let last = stack.last().unwrap(); @@ -1105,20 +1092,23 @@ mod Test { pub fn find_node_with_indices( tree: &BTree, indices: Vec, - ) -> (Option>>, Vec>>) { + ) -> (Option>, Vec>) { let mut temp_node = tree.root.borrow().clone(); let mut stack = Vec::new(); let mut next = None; for idx in indices.iter() { match temp_node { - Some(ref node) => match node.as_ref() { - Node::Internal(internal_node) => { - stack.push(node.clone()); - next = internal_node.edges.borrow()[*idx].borrow().clone(); - } + Some(ref node) => { + let guard = node.as_ref().read().unwrap(); + match &*guard { + Node::Internal(internal_node) => { + stack.push(node.clone()); + next = internal_node.edges.borrow()[*idx].borrow().clone(); + } - Node::Leaf(_) => break, - }, + Node::Leaf(_) => break, + } + } None => panic!("should not be undefined"), } match next { @@ -1158,21 +1148,21 @@ mod Test { let (node, mut leaves) = create_tree_from_test_node_internal(node, order); for (idx, child) in leaves.iter().enumerate() { - match *child.read().unwrap() { + match &*child.read().unwrap() { Node::Internal(_) => panic!("Node must be a leaf"), Node::Leaf(leaf_node) => { if idx > 0 { leaf_node .left_ptr .borrow_mut() - .replace(Rc::downgrade(&leaves[idx - 1].clone())); + .replace(Arc::downgrade(&leaves[idx - 1].clone())); } if idx < leaves.len() - 1 { leaf_node .right_ptr .borrow_mut() - .replace(Rc::downgrade(&leaves[idx + 1].clone())); + .replace(Arc::downgrade(&leaves[idx + 1].clone())); } } } @@ -1207,7 +1197,6 @@ mod Test { keys: RefCell::new(internal_node.keys.clone()), edges: RefCell::new(edges), order, - rw_lock: Rc::new(RwLock::new(())), }; (Arc::new(RwLock::new(Node::Internal(ret_node))), leaves) } @@ -1218,7 +1207,6 @@ mod Test { left_ptr: RefCell::new(None), right_ptr: RefCell::new(None), order: order, - rw_lock: Rc::new(RwLock::new(())), }); let leaf_latch = Arc::new(RwLock::new(leaf)); (leaf_latch, Vec::from([leaf_latch.clone()])) @@ -1243,8 +1231,9 @@ mod Test { } // Doesn't print recursively. Just prints that single node's attributes - pub fn print_node(node: Rc>) { - match node.as_ref() { + pub fn print_node(node: LatchNode) { + let guard = node.as_ref().read().unwrap(); + match &*guard { Node::Internal(node) => { println!("Internal. Keys: {:?}", node.keys); } @@ -1501,14 +1490,8 @@ mod Test { } } - fn assert_internal(node: LatchNode, start_keys: Vec) { - let guard = node.read().unwrap(); - match *guard { - Node::Internal(internal_node) => { - assert_eq!(&*internal_node.keys.borrow(), &start_keys) - } - Node::Leaf(_) => panic!("not an internal node"), - } + fn assert_internal(internal_node: &InternalNode, start_keys: Vec) { + assert_eq!(&*internal_node.keys.borrow(), &start_keys) } mod search { @@ -1545,7 +1528,10 @@ mod Test { let (leaf1, stack) = tree.find_leaf_to_add(&0); assert_eq!(stack.len(), 1); - assert_internal(stack[0].0.clone(), Vec::from([12, 15, 19])); + assert_internal( + stack[0].1.unwrap().as_internal_node(), + Vec::from([12, 15, 19]), + ); assert_leaf(leaf1.unwrap(), &Vec::from([11])); @@ -1633,58 +1619,58 @@ mod Test { #[test] fn split_leaf() { - let leaf = LeafNode { - start_keys: RefCell::new(Vec::from([0, 1, 2])), - end_keys: RefCell::new(Vec::from([0, 1, 2])), - left_ptr: RefCell::new(None), - right_ptr: RefCell::new(None), - order: 4, - }; - - let leaf_rc = Arc::new(Node::Leaf(leaf)); - let right_sibling = LeafNode { - start_keys: RefCell::new(Vec::from([4, 5, 6])), - end_keys: RefCell::new(Vec::from([0, 1, 2])), - left_ptr: RefCell::new(Some(Rc::downgrade(&leaf_rc))), - right_ptr: RefCell::new(None), - order: 4, - rw_lock: Rc::new(RwLock::new(())), - }; - let right_sibling_rc = Rc::new(Node::Leaf(right_sibling)); - match leaf_rc.as_ref() { - Node::Internal(_) => panic!("Leaf is somehow internal"), - Node::Leaf(leaf) => leaf - .right_ptr - .borrow_mut() - .replace(Rc::downgrade(&right_sibling_rc)), - }; - - let (split_node, right_start_key) = BTree::split_node(leaf_rc.clone()); - assert_eq!(right_start_key, 1); - - match split_node.as_ref() { - Node::Internal(_) => panic!("Split node cannot be internal"), - Node::Leaf(leaf) => { - assert_eq!(&*leaf.start_keys.borrow(), &Vec::from([1, 2])); - assert_eq!(&*leaf.end_keys.borrow(), &Vec::from([1, 2])); - let left_start_keys = get_start_keys_from_weak_link(&leaf.left_ptr); - match left_start_keys.clone() { - Some(left_start_keys) => { - assert_eq!(left_start_keys, Vec::from([0])); - } - None => panic!("Left key has start keys"), - } - let right_start_keys = get_start_keys_from_weak_link(&leaf.right_ptr); - match right_start_keys.clone() { - Some(left_start_keys) => { - assert_eq!(left_start_keys, Vec::from([4, 5, 6])); - } - None => panic!("Right key has start keys"), - } - } - } - - print_node(split_node.clone()); + // TODO: Rewrite this test with TestNode + // let leaf = LeafNode { + // start_keys: RefCell::new(Vec::from([0, 1, 2])), + // end_keys: RefCell::new(Vec::from([0, 1, 2])), + // left_ptr: RefCell::new(None), + // right_ptr: RefCell::new(None), + // order: 4, + // }; + + // let leaf_latch_node = Arc::new(RwLock::new(Node::Leaf(leaf))); + // let right_sibling = LeafNode { + // start_keys: RefCell::new(Vec::from([4, 5, 6])), + // end_keys: RefCell::new(Vec::from([0, 1, 2])), + // left_ptr: RefCell::new(Some(Arc::downgrade(&leaf_latch_node))), + // right_ptr: RefCell::new(None), + // order: 4, + // }; + // let right_sibling_rc = Arc::new(RwLock::new(Node::Leaf(right_sibling))); + // match leaf_latch_node.as_ref() { + // Node::Internal(_) => panic!("Leaf is somehow internal"), + // Node::Leaf(leaf) => leaf + // .right_ptr + // .borrow_mut() + // .replace(Rc::downgrade(&right_sibling_rc)), + // }; + + // let (split_node, right_start_key) = BTree::split_node(leaf_latch_node.clone()); + // assert_eq!(right_start_key, 1); + + // match split_node.as_ref() { + // Node::Internal(_) => panic!("Split node cannot be internal"), + // Node::Leaf(leaf) => { + // assert_eq!(&*leaf.start_keys.borrow(), &Vec::from([1, 2])); + // assert_eq!(&*leaf.end_keys.borrow(), &Vec::from([1, 2])); + // let left_start_keys = get_start_keys_from_weak_link(&leaf.left_ptr); + // match left_start_keys.clone() { + // Some(left_start_keys) => { + // assert_eq!(left_start_keys, Vec::from([0])); + // } + // None => panic!("Left key has start keys"), + // } + // let right_start_keys = get_start_keys_from_weak_link(&leaf.right_ptr); + // match right_start_keys.clone() { + // Some(left_start_keys) => { + // assert_eq!(left_start_keys, Vec::from([4, 5, 6])); + // } + // None => panic!("Right key has start keys"), + // } + // } + // } + + // print_node(split_node.clone()); } } @@ -2421,10 +2407,13 @@ mod Test { ]), }); let tree = create_test_tree(&test_node, 3); - let (node_rc, parent, edge_idx) = + let (node_latch, parent, edge_idx) = find_node_and_parent_with_indices(&tree, Vec::from([1])); - let temp = node_rc.as_internal_node(); - let did_steal = temp.steal_from_sibling(parent, edge_idx); + let node_guard = node_latch.as_ref().read().unwrap(); + let temp = node_guard.as_internal_node(); + let parent_guard = parent.as_ref().read().unwrap(); + + let did_steal = temp.steal_from_sibling(parent_guard.as_internal_node(), edge_idx); println!("Did steal {}", did_steal); let expected_node = TestNode::Internal(TestInternalNode { keys: Vec::from([15]), @@ -2495,9 +2484,13 @@ mod Test { ]), }); let tree = create_test_tree(&test_node, 3); - let (node_rc, parent, edge_idx) = + let (node_latch, parent_latch, edge_idx) = find_node_and_parent_with_indices(&tree, Vec::from([1])); - let temp = node_rc.as_internal_node(); + let node_guard = node_latch.as_ref().read().unwrap(); + let temp = node_guard.as_internal_node(); + + let parent_guard = parent_latch.as_ref().read().unwrap(); + let parent = parent_guard.as_internal_node(); let did_steal = temp.steal_from_sibling(parent, edge_idx); println!("Did steal {}", did_steal); @@ -2576,10 +2569,13 @@ mod Test { ]), }); let tree = create_test_tree(&test_node, 3); - let (node, parent, edge_idx) = + let (node_latch, parent_latch, edge_idx) = find_node_and_parent_with_indices(&tree, Vec::from([1])); - let internal_node = node.as_internal_node(); - internal_node.merge_with_sibling(parent.clone(), edge_idx); + let node_guard = node_latch.read().unwrap(); + let internal_node = node_guard.as_internal_node(); + + let parent_guard = parent_latch.as_ref().read().unwrap(); + internal_node.merge_with_sibling(parent_guard.as_internal_node(), edge_idx); let expected_tree = TestNode::Internal(TestInternalNode { keys: Vec::from([]), edges: Vec::from([ @@ -2633,10 +2629,13 @@ mod Test { ]), }); let tree = create_test_tree(&test_node, 3); - let (node, parent, edge_idx) = + let (node_latch, parent_latch, edge_idx) = find_node_and_parent_with_indices(&tree, Vec::from([0])); - let internal_node = node.as_internal_node(); - internal_node.merge_with_sibling(parent.clone(), edge_idx); + let node_guard = node_latch.as_ref().read().unwrap(); + let internal_node = node_guard.as_internal_node(); + + let parent_guard = parent_latch.as_ref().read().unwrap(); + internal_node.merge_with_sibling(parent_guard.as_internal_node(), edge_idx); let expected_tree = TestNode::Internal(TestInternalNode { keys: Vec::from([]), edges: Vec::from([ From c699ae8cbc648babcfb15155d534611259e5e1a6 Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Sat, 11 Feb 2023 17:01:21 -0800 Subject: [PATCH 6/7] Return reference to write_guard --- src/latch_manager/latch_interval_btree.rs | 68 ++++++++++++----------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/latch_manager/latch_interval_btree.rs b/src/latch_manager/latch_interval_btree.rs index 0fce70c..5fccd05 100644 --- a/src/latch_manager/latch_interval_btree.rs +++ b/src/latch_manager/latch_interval_btree.rs @@ -890,51 +890,53 @@ impl BTree { key_to_add: &K, ) -> ( Option>, - Vec<(Option, Option>>)>, + Vec<(Option, Option<&RwLockWriteGuard>>)>, ) { let mut temp_node = self.root.borrow().clone(); let mut next = None; let mut last_idx = None; + let mut last_guard = None; - let mut stack: Vec<(Option, Option>>)> = Vec::new(); + let mut stack: Vec<(Option, Option<&RwLockWriteGuard>>)> = Vec::new(); loop { - match &temp_node { - Some(ref latch_node) => { - let write_guard = latch_node.write().unwrap(); - match *write_guard { - Node::Internal(internal_node) => { - // if internal_node.is_safe_to_release_parent_latch(LatchIntent::INSERT) { - // let len = stack.len(); - // if len > 0 { - // let idx = len - 1; - // let parent_lock = mem::replace(&mut stack[idx].1, None); - // if let Some(write_lock) = parent_lock { - // drop(write_lock) - // } - // } - // } - stack.push((last_idx, Some(write_guard))); - // stack.push((node_rc.clone(), None)); - - for (idx, k) in internal_node.keys.borrow().iter().enumerate() { - if key_to_add < k { - next = internal_node.edges.borrow()[idx].borrow().clone(); - last_idx = Some(idx); - break; - } + if let Some(ref latch_node) = temp_node { + let write_guard = latch_node.write().unwrap(); + match &*write_guard { + Node::Internal(internal_node) => { + // if internal_node.is_safe_to_release_parent_latch(LatchIntent::INSERT) { + // let len = stack.len(); + // if len > 0 { + // let idx = len - 1; + // let parent_lock = mem::replace(&mut stack[idx].1, None); + // if let Some(write_lock) = parent_lock { + // drop(write_lock) + // } + // } + // } + // stack.push((node_rc.clone(), None)); - if idx == internal_node.keys.borrow().len() - 1 { - next = internal_node.edges.borrow()[idx + 1].borrow().clone(); - last_idx = Some(idx + 1); - } + for (idx, k) in internal_node.keys.borrow().iter().enumerate() { + if key_to_add < k { + next = internal_node.edges.borrow()[idx].borrow().clone(); + last_idx = Some(idx); + break; + } + + if idx == internal_node.keys.borrow().len() - 1 { + next = internal_node.edges.borrow()[idx + 1].borrow().clone(); + last_idx = Some(idx + 1); } } - Node::Leaf(_) => break, } + Node::Leaf(_) => break, } - None => panic!("should not be undefined"), - }; + last_guard = Some(&write_guard); + } else { + panic!("") + } + + stack.push((last_idx, last_guard)); match next { Some(_) => temp_node = next.clone(), From 86d70f4cd9b1241c60218ee394ed8da8d9c9f8ed Mon Sep 17 00:00:00 2001 From: Brian Shih Date: Sun, 12 Feb 2023 16:54:31 -0800 Subject: [PATCH 7/7] More attempt --- src/latch_manager/latch_interval_btree.rs | 3444 +++++++++++---------- 1 file changed, 1746 insertions(+), 1698 deletions(-) diff --git a/src/latch_manager/latch_interval_btree.rs b/src/latch_manager/latch_interval_btree.rs index 5fccd05..033e58d 100644 --- a/src/latch_manager/latch_interval_btree.rs +++ b/src/latch_manager/latch_interval_btree.rs @@ -7,7 +7,7 @@ use std::{ thread::current, }; -use self::Test::{print_node, print_tree}; +// use self::Test::{print_node, print_tree}; use super::latch_manager::LatchGuard; @@ -23,6 +23,7 @@ type NodeLink = RefCell>>; type WeakNodeLink = RefCell>>>>; // RefCell>>>, +type AddStack<'a, K: NodeKey> = Vec<(Option, Option>>)>; type DeleteStack<'a, K: NodeKey> = Vec<(usize, Direction, RwLockWriteGuard<'a, Node>)>; #[derive(Debug, Clone)] @@ -885,23 +886,18 @@ impl BTree { // Determines which leaf node a new key should go into we assume there will at least always be one root. // As it traverses down the nodes, acquire write locks. But if it's safe to release parent lock, it will. // Returns the leaf node to add and the stack of parent nodes - pub fn find_leaf_to_add<'a>( - &self, - key_to_add: &K, - ) -> ( - Option>, - Vec<(Option, Option<&RwLockWriteGuard>>)>, - ) { + pub fn find_leaf_to_add<'a>(&self, key_to_add: &K) -> AddStack { let mut temp_node = self.root.borrow().clone(); let mut next = None; let mut last_idx = None; let mut last_guard = None; - let mut stack: Vec<(Option, Option<&RwLockWriteGuard>>)> = Vec::new(); + let mut stack: AddStack = Vec::new(); loop { if let Some(ref latch_node) = temp_node { - let write_guard = latch_node.write().unwrap(); + let foo = latch_node.as_ref(); + let write_guard = foo.write().unwrap(); match &*write_guard { Node::Internal(internal_node) => { // if internal_node.is_safe_to_release_parent_latch(LatchIntent::INSERT) { @@ -931,7 +927,7 @@ impl BTree { } Node::Leaf(_) => break, } - last_guard = Some(&write_guard); + last_guard = Some(write_guard); } else { panic!("") } @@ -944,7 +940,7 @@ impl BTree { }; } - (temp_node, stack) + stack } /** @@ -957,8 +953,59 @@ impl BTree { * - if the root splits, create a new root with one key and two children */ pub fn insert(&self, range: Range) -> () { - // TODO: We need the parent node - let (leaf, parent_stack) = self.find_leaf_to_add(&range.start_key); + let mut temp_node = self.root.borrow().clone(); + + let mut next = None; + let mut last_idx = None; + let mut last_guard = None; + let key_to_add = &range.start_key; + + let mut parent_stack: AddStack = Vec::new(); + loop { + if let Some(ref latch_node) = temp_node { + let foo = latch_node.as_ref(); + let write_guard = foo.write().unwrap(); + match &*write_guard { + Node::Internal(internal_node) => { + // if internal_node.is_safe_to_release_parent_latch(LatchIntent::INSERT) { + // let len = stack.len(); + // if len > 0 { + // let idx = len - 1; + // let parent_lock = mem::replace(&mut stack[idx].1, None); + // if let Some(write_lock) = parent_lock { + // drop(write_lock) + // } + // } + // } + // stack.push((node_rc.clone(), None)); + + for (idx, k) in internal_node.keys.borrow().iter().enumerate() { + if key_to_add < k { + next = internal_node.edges.borrow()[idx].borrow().clone(); + last_idx = Some(idx); + break; + } + + if idx == internal_node.keys.borrow().len() - 1 { + next = internal_node.edges.borrow()[idx + 1].borrow().clone(); + last_idx = Some(idx + 1); + } + } + } + Node::Leaf(_) => break, + } + last_guard = Some(write_guard); + } else { + panic!("") + } + + parent_stack.push((last_idx, last_guard)); + + match next { + Some(_) => temp_node = next.clone(), + None => panic!("next is not provided"), + }; + } let leaf_arc = leaf.unwrap(); let write_guard = leaf_arc.write().unwrap(); match *write_guard { @@ -987,7 +1034,8 @@ impl BTree { if curr_parent.has_capacity() { break; } - (split_node, median) = match *current_guard { + + (split_node, median) = match &*current_guard { Node::Internal(ref internal) => internal.split_node(), Node::Leaf(ref leaf) => leaf.split_node(latch_node), }; @@ -1071,1686 +1119,1686 @@ impl BTree { } } -mod Test { - use std::{ - borrow::Borrow, - cell::RefCell, - process::Child, - sync::{Arc, RwLock}, - }; - - use super::{BTree, InternalNode, LatchNode, LeafNode, Node, NodeKey, NodeLink, WeakNodeLink}; - - pub fn find_node_and_parent_with_indices( - tree: &BTree, - indices: Vec, - ) -> (LatchNode, LatchNode, usize) { - let last_index = indices.last().unwrap().clone(); - let (node, stack) = find_node_with_indices(tree, indices); - let last = stack.last().unwrap(); - (node.unwrap(), last.clone(), last_index) - } - - pub fn find_node_with_indices( - tree: &BTree, - indices: Vec, - ) -> (Option>, Vec>) { - let mut temp_node = tree.root.borrow().clone(); - let mut stack = Vec::new(); - let mut next = None; - for idx in indices.iter() { - match temp_node { - Some(ref node) => { - let guard = node.as_ref().read().unwrap(); - match &*guard { - Node::Internal(internal_node) => { - stack.push(node.clone()); - next = internal_node.edges.borrow()[*idx].borrow().clone(); - } - - Node::Leaf(_) => break, - } - } - None => panic!("should not be undefined"), - } - match next { - Some(_) => temp_node = next.clone(), - None => panic!("next is not provided"), - } - } - (temp_node, stack) - } - - #[derive(Debug, Clone)] - pub enum TestNode { - Internal(TestInternalNode), - Leaf(TestLeafNode), - } - - #[derive(Debug, Clone)] - pub struct TestInternalNode { - keys: Vec, - edges: Vec>>, - } - - #[derive(Debug, Clone)] - pub struct TestLeafNode { - keys: Vec, - } - - pub fn create_test_tree(node: &TestNode, order: u16) -> BTree { - let node = create_test_node(node, order); - BTree { - root: RefCell::new(Some(node)), - order, - } - } - - pub fn create_test_node(node: &TestNode, order: u16) -> LatchNode { - let (node, mut leaves) = create_tree_from_test_node_internal(node, order); - - for (idx, child) in leaves.iter().enumerate() { - match &*child.read().unwrap() { - Node::Internal(_) => panic!("Node must be a leaf"), - Node::Leaf(leaf_node) => { - if idx > 0 { - leaf_node - .left_ptr - .borrow_mut() - .replace(Arc::downgrade(&leaves[idx - 1].clone())); - } - - if idx < leaves.len() - 1 { - leaf_node - .right_ptr - .borrow_mut() - .replace(Arc::downgrade(&leaves[idx + 1].clone())); - } - } - } - } - node - } - - // Returns the created node and any leaves it has - pub fn create_tree_from_test_node_internal( - node: &TestNode, - order: u16, - ) -> (LatchNode, Vec>) { - match node { - TestNode::Internal(internal_node) => { - let mut leaves = Vec::new(); - let edges = internal_node - .edges - .iter() - .map(|e| match e { - Some(child) => { - let (child_node, mut child_leaves) = - create_tree_from_test_node_internal(child, order); - leaves.append(&mut child_leaves); - RefCell::new(Some(child_node)) - // todo!() - } - None => RefCell::new(None), - }) - .collect::>>(); - - let ret_node = InternalNode { - keys: RefCell::new(internal_node.keys.clone()), - edges: RefCell::new(edges), - order, - }; - (Arc::new(RwLock::new(Node::Internal(ret_node))), leaves) - } - TestNode::Leaf(leaf_node) => { - let leaf = Node::Leaf(LeafNode { - start_keys: RefCell::new(leaf_node.keys.clone()), - end_keys: RefCell::new(leaf_node.keys.clone()), - left_ptr: RefCell::new(None), - right_ptr: RefCell::new(None), - order: order, - }); - let leaf_latch = Arc::new(RwLock::new(leaf)); - (leaf_latch, Vec::from([leaf_latch.clone()])) - } - } - } - - pub fn get_indent(depth: usize) -> String { - " ".repeat(depth * 2) - } - - pub fn print_tree(tree: &BTree) { - print_tree_internal(&tree.root, 0); - } - - pub fn print_node_recursive(node: LatchNode) { - let tree = BTree { - root: RefCell::new(Some(node.clone())), - order: 4, - }; - print_tree(&tree); - } - - // Doesn't print recursively. Just prints that single node's attributes - pub fn print_node(node: LatchNode) { - let guard = node.as_ref().read().unwrap(); - match &*guard { - Node::Internal(node) => { - println!("Internal. Keys: {:?}", node.keys); - } - Node::Leaf(ref node) => { - println!( - "Leaf. Keys: {:?}. Left start: {:?} Right start: {:?}", - node.start_keys, - get_first_key_from_weak_link(&node.left_ptr), - get_first_key_from_weak_link(&node.right_ptr) - ); - } - } - } - - pub fn get_start_keys_from_weak_link(link: &WeakNodeLink) -> Option> { - let edge = &*link.borrow(); - if let Some(ref weak_latch) = edge { - let upgraded_ref = weak_latch.upgrade(); - let unwrapped = upgraded_ref.unwrap().as_ref().read().unwrap(); - match *unwrapped { - Node::Internal(_) => { - panic!("Cannot get sibling from internal node"); - } - Node::Leaf(ref node) => { - let keys = node.start_keys.borrow(); - Some(keys.clone()) - } - } - } else { - None - } - } - - fn get_first_key_from_weak_link(link: &WeakNodeLink) -> Option { - let edge = &*link.borrow(); - if let Some(ref weak_latch) = edge { - let upgraded_ref = weak_latch.upgrade()?; - - let unwrapped = upgraded_ref.as_ref().read().unwrap(); - match *unwrapped { - Node::Internal(_) => { - panic!("Cannot get sibling from internal node"); - } - Node::Leaf(ref node) => { - let keys = node.start_keys.borrow(); - let first = keys.get(0); - match first { - Some(k) => Some(k.clone()), - None => None, - } - } - } - } else { - None - } - } - - fn print_tree_internal(link: &NodeLink, depth: usize) { - let edge = link.borrow().clone(); - if let Some(ref weak_latch) = edge { - let node = weak_latch.read().unwrap(); - match *node { - Node::Internal(ref node) => { - println!( - "{}Internal. Keys: {:?}", - get_indent(depth), - node.keys.borrow() - ); - - for edge in &*node.edges.borrow() { - print_tree_internal(edge, depth + 1); - } - } - Node::Leaf(ref node) => { - println!( - "{}Leaf. Keys: {:?}. Left start: {:?} Right start: {:?}", - get_indent(depth), - node.start_keys.borrow(), - get_first_key_from_weak_link(&node.left_ptr), - get_first_key_from_weak_link(&node.right_ptr) - ); - } - } - } - } - - fn assert_node_and_leaves_siblings(node: LatchNode, test_node: &TestNode) { - assert_node(node.clone(), test_node); - let test_leaves = get_all_test_leaves(test_node); - let leaves = get_all_leaf_nodes(node.clone()); - assert_eq!(test_leaves.len(), leaves.len()); - for (idx, current_test_node) in test_leaves.iter().enumerate() { - let curr_node = leaves[idx].clone(); - let guard = curr_node.read().unwrap(); - let left_sibling = &*guard.as_leaf_node().left_ptr.borrow(); - let right_sibling = &*guard.as_leaf_node().right_ptr.borrow(); - if idx == 0 { - assert!(left_sibling.is_none()); - } else { - let test_left_sibling = test_leaves[idx - 1]; - let left_node = right_sibling.as_ref().unwrap().upgrade().unwrap().clone(); - assert_leaf(left_node, &test_left_sibling.keys); - } - - if idx == test_leaves.len() - 1 { - assert!(right_sibling.is_none()); - } else { - let test_right_sibling = test_leaves[idx + 1]; - let right_node = right_sibling.as_ref().unwrap().upgrade().unwrap().clone(); - assert_leaf(right_node, &test_right_sibling.keys); - } - } - } - /** - * Given a node link and a test node structure, verify if if the node link - * has the expected shape and properties - */ - fn assert_node(node: LatchNode, test_node: &TestNode) { - match test_node { - TestNode::Internal(test_internal_node) => { - let guard = node.clone().as_ref().read().unwrap(); - let internal_node = guard.as_internal_node(); - assert_eq!(&*internal_node.keys.borrow(), &test_internal_node.keys); - for (idx, child) in internal_node.edges.borrow().iter().enumerate() { - let node = child.borrow(); - match &*node { - Some(child_node) => { - let test_child = test_internal_node.edges[idx].clone(); - let unwrapped = test_child.unwrap(); - assert_node(child_node.clone(), &unwrapped); - } - None => { - if test_internal_node.edges[idx].is_some() { - let foo = ""; - } - assert_eq!(test_internal_node.edges[idx].is_none(), true); - } - }; - } - } - TestNode::Leaf(test_leaf) => { - assert_leaf(node.clone(), &test_leaf.keys); - } - }; - } - - fn assert_tree(tree: &BTree, test_node: &TestNode) { - let root = tree.root.borrow().clone().unwrap(); - assert_node(root, test_node); - } - - fn get_all_leaves(node: LatchNode) -> Vec>> { - let mut leaves = Vec::new(); - let guard = node.as_ref().read().unwrap(); - match *guard { - Node::Internal(internal_node) => { - for edge in internal_node.edges.borrow().iter() { - match &*edge.borrow() { - Some(child) => { - let mut child_leaves = get_all_leaves(child.clone()); - leaves.append(&mut child_leaves); - } - None => leaves.push(None), - }; - } - } - Node::Leaf(_) => { - leaves.push(Some(node.clone())); - } - }; - leaves - } - - fn assert_leaf_with_siblings( - node: LatchNode, - test_leaf: &TestLeafNode, - test_left_sibling: &Option>, - test_right_sibling: &Option>, - ) { - assert_leaf(node.clone(), &test_leaf.keys); - let guard = node.as_ref().read().unwrap(); - let leaf_node = guard.as_leaf_node(); - let left_sibling = &*leaf_node.left_ptr.borrow(); - match left_sibling { - Some(left_node) => { - assert_leaf( - left_node.upgrade().unwrap().clone(), - &test_left_sibling.as_ref().unwrap().keys, - ); - } - None => { - assert!(test_left_sibling.is_none()); - } - }; - - let right_sibling = &*leaf_node.right_ptr.borrow(); - match right_sibling { - Some(right_node) => { - assert_leaf( - right_node.upgrade().unwrap().clone(), - &test_right_sibling.as_ref().unwrap().keys, - ); - } - None => { - assert!(test_left_sibling.is_none()); - } - }; - } - - fn get_all_leaf_nodes(latch_node: LatchNode) -> Vec> { - let mut leaves = Vec::new(); - let guard = latch_node.read().unwrap(); - match *guard { - Node::Internal(internal_node) => { - for edge in internal_node.edges.borrow().iter() { - if let Some(child) = &*edge.borrow() { - let mut child_leaves = get_all_leaf_nodes(child.clone()); - leaves.append(&mut child_leaves); - } - } - } - Node::Leaf(_) => { - leaves.push(latch_node.clone()); - } - }; - leaves - } - - fn get_all_test_leaves(test_node: &TestNode) -> Vec<&TestLeafNode> { - let mut leaves = Vec::new(); - match test_node { - TestNode::Internal(internal_node) => { - for edge in internal_node.edges.iter() { - if let Some(child) = edge { - let mut child_leaves = get_all_test_leaves(child); - leaves.append(&mut child_leaves); - } - } - } - TestNode::Leaf(test_leaf) => { - leaves.push(test_leaf); - } - }; - leaves - } - - fn assert_leaf(node: LatchNode, start_keys: &Vec) { - let guard = node.read().unwrap(); - match *guard { - Node::Internal(_) => panic!("not a leaf node"), - Node::Leaf(leaf) => { - assert_eq!(&*leaf.start_keys.borrow(), start_keys) - } - } - } - - fn assert_internal(internal_node: &InternalNode, start_keys: Vec) { - assert_eq!(&*internal_node.keys.borrow(), &start_keys) - } - - mod search { - use std::cell::RefCell; - - use crate::latch_manager::latch_interval_btree::{ - BTree, InternalNode, LeafNode, Node, - Test::{ - assert_internal, assert_leaf, create_test_node, create_test_tree, print_tree, - TestInternalNode, TestLeafNode, TestNode, - }, - }; - - #[test] - fn one_level_deep() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([12, 15, 19]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([11]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([14]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([18]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([25]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 4); - - let (leaf1, stack) = tree.find_leaf_to_add(&0); - assert_eq!(stack.len(), 1); - assert_internal( - stack[0].1.unwrap().as_internal_node(), - Vec::from([12, 15, 19]), - ); - - assert_leaf(leaf1.unwrap(), &Vec::from([11])); - - let leaf2 = tree.find_leaf_to_add(&15).0.unwrap(); - assert_leaf(leaf2, &Vec::from([18])); - - let leaf4 = tree.find_leaf_to_add(&100).0.unwrap(); - assert_leaf(leaf4, &Vec::from([25])); - - print_tree(&tree); - } - } - - mod split { - use std::{ - borrow::Borrow, - cell::RefCell, - sync::{Arc, RwLock}, - }; - - use crate::latch_manager::latch_interval_btree::{ - BTree, LeafNode, Node, - Test::{ - assert_leaf_with_siblings, assert_node, assert_tree, get_all_leaf_nodes, - get_all_leaves, get_start_keys_from_weak_link, print_node, - }, - }; - - use super::{ - create_test_node, create_test_tree, print_node_recursive, print_tree, TestInternalNode, - TestLeafNode, TestNode, - }; - - #[test] - fn split_internal() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([5, 20, 30]), - edges: Vec::from([ - None, - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([6, 8, 10]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([21, 25]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([35]), - })), - ]), - }); - let node = create_test_node(&test_node, 4); - let read_guard = node.read().unwrap(); - - let (split_node, median) = read_guard.as_internal_node().split_node(); - assert_eq!(median, 20); - - let split_test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([30]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([21, 25]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([35]), - })), - ]), - }); - assert_node(split_node.clone(), &split_test_node); - let leaves = get_all_leaves(split_node.clone()); - assert_eq!(leaves.len(), 2); - assert_leaf_with_siblings( - leaves[0].as_ref().unwrap().clone(), - &TestLeafNode { - keys: Vec::from([21, 25]), - }, - &Some(TestLeafNode { - keys: Vec::from([6, 8, 10]), - }), - &Some(TestLeafNode { - keys: Vec::from([35]), - }), - ); - // print_node_recursive(split_node.clone()); - } - - #[test] - fn split_leaf() { - // TODO: Rewrite this test with TestNode - // let leaf = LeafNode { - // start_keys: RefCell::new(Vec::from([0, 1, 2])), - // end_keys: RefCell::new(Vec::from([0, 1, 2])), - // left_ptr: RefCell::new(None), - // right_ptr: RefCell::new(None), - // order: 4, - // }; - - // let leaf_latch_node = Arc::new(RwLock::new(Node::Leaf(leaf))); - // let right_sibling = LeafNode { - // start_keys: RefCell::new(Vec::from([4, 5, 6])), - // end_keys: RefCell::new(Vec::from([0, 1, 2])), - // left_ptr: RefCell::new(Some(Arc::downgrade(&leaf_latch_node))), - // right_ptr: RefCell::new(None), - // order: 4, - // }; - // let right_sibling_rc = Arc::new(RwLock::new(Node::Leaf(right_sibling))); - // match leaf_latch_node.as_ref() { - // Node::Internal(_) => panic!("Leaf is somehow internal"), - // Node::Leaf(leaf) => leaf - // .right_ptr - // .borrow_mut() - // .replace(Rc::downgrade(&right_sibling_rc)), - // }; - - // let (split_node, right_start_key) = BTree::split_node(leaf_latch_node.clone()); - // assert_eq!(right_start_key, 1); - - // match split_node.as_ref() { - // Node::Internal(_) => panic!("Split node cannot be internal"), - // Node::Leaf(leaf) => { - // assert_eq!(&*leaf.start_keys.borrow(), &Vec::from([1, 2])); - // assert_eq!(&*leaf.end_keys.borrow(), &Vec::from([1, 2])); - // let left_start_keys = get_start_keys_from_weak_link(&leaf.left_ptr); - // match left_start_keys.clone() { - // Some(left_start_keys) => { - // assert_eq!(left_start_keys, Vec::from([0])); - // } - // None => panic!("Left key has start keys"), - // } - // let right_start_keys = get_start_keys_from_weak_link(&leaf.right_ptr); - // match right_start_keys.clone() { - // Some(left_start_keys) => { - // assert_eq!(left_start_keys, Vec::from([4, 5, 6])); - // } - // None => panic!("Right key has start keys"), - // } - // } - // } - - // print_node(split_node.clone()); - } - } - - mod insert { - use crate::latch_manager::latch_interval_btree::{BTree, Range}; - - use super::{ - assert_node, assert_tree, print_tree, TestInternalNode, TestLeafNode, TestNode, - }; - - #[test] - fn insert_and_split() { - let tree = BTree::::new(3); - tree.insert(Range { - start_key: 5, - end_key: 5, - }); - tree.insert(Range { - start_key: 10, - end_key: 10, - }); - tree.insert(Range { - start_key: 20, - end_key: 20, - }); - print_tree(&tree); - - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10, 20]), - })), - ]), - }); - - assert_tree(&tree, &test_node); - } - - #[test] - fn insert_and_split_internal() { - let tree = BTree::::new(3); - tree.insert(Range { - start_key: 5, - end_key: 5, - }); - tree.insert(Range { - start_key: 10, - end_key: 10, - }); - tree.insert(Range { - start_key: 20, - end_key: 20, - }); - - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10, 20]), - })), - ]), - }); - - print_tree(&tree); - - assert_tree(&tree, &test_node); - - // here - tree.insert(Range { - start_key: 15, - end_key: 15, - }); - print_tree(&tree); - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([10, 15]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15, 20]), - })), - ]), - }); - assert_tree(&tree, &test_node); - - tree.insert(Range { - start_key: 25, - end_key: 25, - }); - print_tree(&tree); - - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([15]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([20]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20, 25]), - })), - ]), - })), - ]), - }); - - assert_tree(&tree, &test_node); - } - } - - mod leaf_underflow { - use std::{cell::RefCell, sync::RwLock}; - - use crate::latch_manager::latch_interval_btree::LeafNode; - - #[test] - fn underflows() { - let leaf = LeafNode { - start_keys: RefCell::new(Vec::from([0])), - end_keys: RefCell::new(Vec::from([0])), - left_ptr: RefCell::new(None), - right_ptr: RefCell::new(None), - order: 4, - }; - assert!(leaf.is_underflow()); - } - } - - mod delete { - mod core_delete { - use crate::latch_manager::latch_interval_btree::Test::{ - assert_tree, create_test_tree, print_tree, TestInternalNode, TestLeafNode, TestNode, - }; - - #[test] - fn internal_node_stealing_from_left_sibling_3_layers() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([20]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10, 15]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15, 18]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([30]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([30]), - })), - ]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - tree.delete(30); - - let expected_tree = TestNode::Internal(TestInternalNode { - keys: Vec::from([15]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([20]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15, 18]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - })), - ]), - })), - ]), - }); - assert_tree(&tree, &expected_tree); - } - - #[test] - fn internal_node_stealing_from_right_sibling_4_layers() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([40]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([15]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([18]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([18, 20]), - })), - ]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([60]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([50]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40, 45]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([70]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([60]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([70]), - })), - ]), - })), - ]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - tree.delete(70); - print_tree(&tree); - - let expected_tree = TestNode::Internal(TestInternalNode { - keys: Vec::from([15, 40]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([18]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([18, 20]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([50, 60]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40, 45]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([60]), - })), - ]), - })), - ]), - }); - assert_tree(&tree, &expected_tree); - } - - // hello - #[test] - fn internal_node_stealing_from_right_sibling_3_layers() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([20, 40]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([30]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([30]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([45, 50]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([45, 49]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50, 60]), - })), - ]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - tree.delete(30); - - let expected_tree = TestNode::Internal(TestInternalNode { - keys: Vec::from([20, 45]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([40]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([50]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([45, 49]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50, 60]), - })), - ]), - })), - ]), - }); - assert_tree(&tree, &expected_tree); - } - } - - mod find_leaf_to_delete { - use crate::latch_manager::latch_interval_btree::Test::{ - create_test_tree, TestInternalNode, TestLeafNode, TestNode, - }; - - #[test] - fn test_leaf() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([15]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([20]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20, 25]), - })), - ]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - let (node, path) = tree.find_leaf_to_delete(&20); - let indices = path - .iter() - .map(|(idx, _, _)| idx.clone()) - .collect::>(); - assert_eq!(indices, Vec::from([1, 1])); - } - } - - mod leaf_stealing { - use crate::latch_manager::latch_interval_btree::{ - Node, - Test::{create_test_tree, print_tree, TestInternalNode, TestLeafNode, TestNode}, - }; - - mod has_spare_keys { - use std::{cell::RefCell, sync::RwLock}; - - use crate::latch_manager::latch_interval_btree::{ - LeafNode, - Test::{ - assert_tree, create_test_tree, TestInternalNode, TestLeafNode, TestNode, - }, - }; - - #[test] - fn internal_node() {} - - #[test] - fn leaf_node_has_spare_key() { - let leaf_node = LeafNode { - start_keys: RefCell::new(Vec::from([0, 1])), - end_keys: RefCell::new(Vec::from([0, 1])), - left_ptr: RefCell::new(None), - right_ptr: RefCell::new(None), - order: 3, - }; - assert_eq!(leaf_node.has_spare_key(), true); - } - - #[test] - fn leaf_node_has_no_spare_key() { - let leaf_node = LeafNode { - start_keys: RefCell::new(Vec::from([0])), - end_keys: RefCell::new(Vec::from([0])), - left_ptr: RefCell::new(None), - right_ptr: RefCell::new(None), - order: 3, - }; - assert_eq!(leaf_node.has_spare_key(), false); - } - - #[test] - fn requires_updating_ancestor() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([4]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([2]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([1]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([2, 3]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([4, 5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10, 13]), - })), - ]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - tree.delete(4); - - let expected_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([5]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([2]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([1]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([2, 3]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10, 13]), - })), - ]), - })), - ]), - }); - assert_tree(&tree, &expected_node); - } - } - - mod stealing_core { - use crate::latch_manager::latch_interval_btree::Test::{ - assert_tree, create_test_tree, print_tree, TestInternalNode, TestLeafNode, - TestNode, - }; - - #[test] - fn leaf_steals_left_sibling() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([8]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([5]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([1, 3]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([8, 9]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10, 15]), - })), - ]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - tree.delete(5); - let expected_tree_after_delete = TestNode::Internal(TestInternalNode { - keys: Vec::from([8]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([3]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([1]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([3]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([8, 9]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10, 15]), - })), - ]), - })), - ]), - }); - assert_tree(&tree, &expected_tree_after_delete); - } - - #[test] - fn leaf_steals_right_sibling() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([5]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([2]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5, 6]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([12]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([12, 20]), - })), - ]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - tree.delete(10); - print_tree(&tree); - let expected_tree_after_delete = TestNode::Internal(TestInternalNode { - keys: Vec::from([12]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([5]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([2]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5, 6]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([20]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([12]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - })), - ]), - })), - ]), - }); - assert_tree(&tree, &expected_tree_after_delete); - } - } - } - - mod internal_node_stealing { - use crate::latch_manager::latch_interval_btree::Test::{ - assert_tree, create_test_tree, find_node_and_parent_with_indices, print_tree, - TestInternalNode, TestLeafNode, TestNode, - }; - - #[test] - fn simple_steal_from_left_sibling() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([20]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10, 15]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15, 18]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - }))]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - let (node_latch, parent, edge_idx) = - find_node_and_parent_with_indices(&tree, Vec::from([1])); - let node_guard = node_latch.as_ref().read().unwrap(); - let temp = node_guard.as_internal_node(); - let parent_guard = parent.as_ref().read().unwrap(); - - let did_steal = temp.steal_from_sibling(parent_guard.as_internal_node(), edge_idx); - println!("Did steal {}", did_steal); - let expected_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([15]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([20]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([15, 18]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - })), - ]), - })), - ]), - }); - assert_tree(&tree, &expected_node); - } - - #[test] - fn simple_steal_from_right_sibling() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([20, 40]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - }))]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([45, 50]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([45, 49]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50, 60]), - })), - ]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - let (node_latch, parent_latch, edge_idx) = - find_node_and_parent_with_indices(&tree, Vec::from([1])); - let node_guard = node_latch.as_ref().read().unwrap(); - let temp = node_guard.as_internal_node(); - - let parent_guard = parent_latch.as_ref().read().unwrap(); - let parent = parent_guard.as_internal_node(); - let did_steal = temp.steal_from_sibling(parent, edge_idx); - println!("Did steal {}", did_steal); - - let expected_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([20, 45]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([10]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([10]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([40]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([50]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([45, 49]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50, 60]), - })), - ]), - })), - ]), - }); - assert_tree(&tree, &expected_node); - } - } - } - - mod merge { - mod internal_node { - use crate::latch_manager::latch_interval_btree::Test::{ - assert_tree, create_test_tree, find_node_and_parent_with_indices, - find_node_with_indices, print_tree, TestInternalNode, TestLeafNode, TestNode, - }; - - #[test] - fn merge_with_left() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([60]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([50]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40, 45]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([60]), - }))]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - let (node_latch, parent_latch, edge_idx) = - find_node_and_parent_with_indices(&tree, Vec::from([1])); - let node_guard = node_latch.read().unwrap(); - let internal_node = node_guard.as_internal_node(); - - let parent_guard = parent_latch.as_ref().read().unwrap(); - internal_node.merge_with_sibling(parent_guard.as_internal_node(), edge_idx); - let expected_tree = TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([50, 60]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40, 45]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([60]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([60]), - }))]), - })), - ]), - }); - assert_tree(&tree, &expected_tree); - } - - #[test] - fn merge_with_right() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([60]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([50]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40, 45]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([60]), - }))]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - let (node_latch, parent_latch, edge_idx) = - find_node_and_parent_with_indices(&tree, Vec::from([0])); - let node_guard = node_latch.as_ref().read().unwrap(); - let internal_node = node_guard.as_internal_node(); - - let parent_guard = parent_latch.as_ref().read().unwrap(); - internal_node.merge_with_sibling(parent_guard.as_internal_node(), edge_idx); - let expected_tree = TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([ - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([50, 60]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([40, 45]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([50]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([60]), - })), - ]), - })), - Some(TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([60]), - }))]), - })), - ]), - }); - assert_tree(&tree, &expected_tree); - } - } - - mod leaf { - use crate::latch_manager::latch_interval_btree::Test::{ - assert_tree, create_test_tree, print_tree, TestInternalNode, TestLeafNode, TestNode, - }; - - #[test] - fn merge_with_left_leaf() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([16, 20]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5, 10]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([16]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20, 30, 40]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - let (node, stack) = tree.find_leaf_to_delete(&16); - let guard = node.unwrap().as_ref().read().unwrap(); - let leaf = guard.as_leaf_node(); - let (edge_idx, dir, parent) = stack.last().unwrap(); - leaf.merge_node(parent.clone(), *edge_idx); - print_tree(&tree); - - let expected_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([20]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([5, 10, 16]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([20, 30, 40]), - })), - ]), - }); - assert_tree(&tree, &expected_node); - } - - #[test] - fn merge_with_right_leaf() { - let test_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([25]), - edges: Vec::from([ - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([]), - })), - Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([25]), - })), - ]), - }); - let tree = create_test_tree(&test_node, 3); - let (node, stack) = tree.find_leaf_to_delete(&16); - let guard = node.unwrap().as_ref().read().unwrap(); - let leaf = guard.as_leaf_node(); - let (edge_idx, dir, parent) = stack.last().unwrap(); - leaf.merge_node(parent.clone(), *edge_idx); - print_tree(&tree); - - let expected_node = TestNode::Internal(TestInternalNode { - keys: Vec::from([]), - edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { - keys: Vec::from([25]), - }))]), - }); - assert_tree(&tree, &expected_node); - } - } - } - - #[test] - fn experiment() { - let my_rwlock = RwLock::new(5); - - let read1 = my_rwlock.read().unwrap(); // one .read() is fine - let read2 = my_rwlock.read().unwrap(); // two .read()s is also fine - - println!("{:?}, {:?}", read1, read2); - drop(read1); - drop(read2); - } -} +// mod Test { +// use std::{ +// borrow::Borrow, +// cell::RefCell, +// process::Child, +// sync::{Arc, RwLock}, +// }; + +// use super::{BTree, InternalNode, LatchNode, LeafNode, Node, NodeKey, NodeLink, WeakNodeLink}; + +// pub fn find_node_and_parent_with_indices( +// tree: &BTree, +// indices: Vec, +// ) -> (LatchNode, LatchNode, usize) { +// let last_index = indices.last().unwrap().clone(); +// let (node, stack) = find_node_with_indices(tree, indices); +// let last = stack.last().unwrap(); +// (node.unwrap(), last.clone(), last_index) +// } + +// pub fn find_node_with_indices( +// tree: &BTree, +// indices: Vec, +// ) -> (Option>, Vec>) { +// let mut temp_node = tree.root.borrow().clone(); +// let mut stack = Vec::new(); +// let mut next = None; +// for idx in indices.iter() { +// match temp_node { +// Some(ref node) => { +// let guard = node.as_ref().read().unwrap(); +// match &*guard { +// Node::Internal(internal_node) => { +// stack.push(node.clone()); +// next = internal_node.edges.borrow()[*idx].borrow().clone(); +// } + +// Node::Leaf(_) => break, +// } +// } +// None => panic!("should not be undefined"), +// } +// match next { +// Some(_) => temp_node = next.clone(), +// None => panic!("next is not provided"), +// } +// } +// (temp_node, stack) +// } + +// #[derive(Debug, Clone)] +// pub enum TestNode { +// Internal(TestInternalNode), +// Leaf(TestLeafNode), +// } + +// #[derive(Debug, Clone)] +// pub struct TestInternalNode { +// keys: Vec, +// edges: Vec>>, +// } + +// #[derive(Debug, Clone)] +// pub struct TestLeafNode { +// keys: Vec, +// } + +// pub fn create_test_tree(node: &TestNode, order: u16) -> BTree { +// let node = create_test_node(node, order); +// BTree { +// root: RefCell::new(Some(node)), +// order, +// } +// } + +// pub fn create_test_node(node: &TestNode, order: u16) -> LatchNode { +// let (node, mut leaves) = create_tree_from_test_node_internal(node, order); + +// for (idx, child) in leaves.iter().enumerate() { +// match &*child.read().unwrap() { +// Node::Internal(_) => panic!("Node must be a leaf"), +// Node::Leaf(leaf_node) => { +// if idx > 0 { +// leaf_node +// .left_ptr +// .borrow_mut() +// .replace(Arc::downgrade(&leaves[idx - 1].clone())); +// } + +// if idx < leaves.len() - 1 { +// leaf_node +// .right_ptr +// .borrow_mut() +// .replace(Arc::downgrade(&leaves[idx + 1].clone())); +// } +// } +// } +// } +// node +// } + +// // Returns the created node and any leaves it has +// pub fn create_tree_from_test_node_internal( +// node: &TestNode, +// order: u16, +// ) -> (LatchNode, Vec>) { +// match node { +// TestNode::Internal(internal_node) => { +// let mut leaves = Vec::new(); +// let edges = internal_node +// .edges +// .iter() +// .map(|e| match e { +// Some(child) => { +// let (child_node, mut child_leaves) = +// create_tree_from_test_node_internal(child, order); +// leaves.append(&mut child_leaves); +// RefCell::new(Some(child_node)) +// // todo!() +// } +// None => RefCell::new(None), +// }) +// .collect::>>(); + +// let ret_node = InternalNode { +// keys: RefCell::new(internal_node.keys.clone()), +// edges: RefCell::new(edges), +// order, +// }; +// (Arc::new(RwLock::new(Node::Internal(ret_node))), leaves) +// } +// TestNode::Leaf(leaf_node) => { +// let leaf = Node::Leaf(LeafNode { +// start_keys: RefCell::new(leaf_node.keys.clone()), +// end_keys: RefCell::new(leaf_node.keys.clone()), +// left_ptr: RefCell::new(None), +// right_ptr: RefCell::new(None), +// order: order, +// }); +// let leaf_latch = Arc::new(RwLock::new(leaf)); +// (leaf_latch, Vec::from([leaf_latch.clone()])) +// } +// } +// } + +// pub fn get_indent(depth: usize) -> String { +// " ".repeat(depth * 2) +// } + +// pub fn print_tree(tree: &BTree) { +// print_tree_internal(&tree.root, 0); +// } + +// pub fn print_node_recursive(node: LatchNode) { +// let tree = BTree { +// root: RefCell::new(Some(node.clone())), +// order: 4, +// }; +// print_tree(&tree); +// } + +// // Doesn't print recursively. Just prints that single node's attributes +// pub fn print_node(node: LatchNode) { +// let guard = node.as_ref().read().unwrap(); +// match &*guard { +// Node::Internal(node) => { +// println!("Internal. Keys: {:?}", node.keys); +// } +// Node::Leaf(ref node) => { +// println!( +// "Leaf. Keys: {:?}. Left start: {:?} Right start: {:?}", +// node.start_keys, +// get_first_key_from_weak_link(&node.left_ptr), +// get_first_key_from_weak_link(&node.right_ptr) +// ); +// } +// } +// } + +// pub fn get_start_keys_from_weak_link(link: &WeakNodeLink) -> Option> { +// let edge = &*link.borrow(); +// if let Some(ref weak_latch) = edge { +// let upgraded_ref = weak_latch.upgrade(); +// let unwrapped = upgraded_ref.unwrap().as_ref().read().unwrap(); +// match *unwrapped { +// Node::Internal(_) => { +// panic!("Cannot get sibling from internal node"); +// } +// Node::Leaf(ref node) => { +// let keys = node.start_keys.borrow(); +// Some(keys.clone()) +// } +// } +// } else { +// None +// } +// } + +// fn get_first_key_from_weak_link(link: &WeakNodeLink) -> Option { +// let edge = &*link.borrow(); +// if let Some(ref weak_latch) = edge { +// let upgraded_ref = weak_latch.upgrade()?; + +// let unwrapped = upgraded_ref.as_ref().read().unwrap(); +// match *unwrapped { +// Node::Internal(_) => { +// panic!("Cannot get sibling from internal node"); +// } +// Node::Leaf(ref node) => { +// let keys = node.start_keys.borrow(); +// let first = keys.get(0); +// match first { +// Some(k) => Some(k.clone()), +// None => None, +// } +// } +// } +// } else { +// None +// } +// } + +// fn print_tree_internal(link: &NodeLink, depth: usize) { +// let edge = link.borrow().clone(); +// if let Some(ref weak_latch) = edge { +// let node = weak_latch.read().unwrap(); +// match *node { +// Node::Internal(ref node) => { +// println!( +// "{}Internal. Keys: {:?}", +// get_indent(depth), +// node.keys.borrow() +// ); + +// for edge in &*node.edges.borrow() { +// print_tree_internal(edge, depth + 1); +// } +// } +// Node::Leaf(ref node) => { +// println!( +// "{}Leaf. Keys: {:?}. Left start: {:?} Right start: {:?}", +// get_indent(depth), +// node.start_keys.borrow(), +// get_first_key_from_weak_link(&node.left_ptr), +// get_first_key_from_weak_link(&node.right_ptr) +// ); +// } +// } +// } +// } + +// fn assert_node_and_leaves_siblings(node: LatchNode, test_node: &TestNode) { +// assert_node(node.clone(), test_node); +// let test_leaves = get_all_test_leaves(test_node); +// let leaves = get_all_leaf_nodes(node.clone()); +// assert_eq!(test_leaves.len(), leaves.len()); +// for (idx, current_test_node) in test_leaves.iter().enumerate() { +// let curr_node = leaves[idx].clone(); +// let guard = curr_node.read().unwrap(); +// let left_sibling = &*guard.as_leaf_node().left_ptr.borrow(); +// let right_sibling = &*guard.as_leaf_node().right_ptr.borrow(); +// if idx == 0 { +// assert!(left_sibling.is_none()); +// } else { +// let test_left_sibling = test_leaves[idx - 1]; +// let left_node = right_sibling.as_ref().unwrap().upgrade().unwrap().clone(); +// assert_leaf(left_node, &test_left_sibling.keys); +// } + +// if idx == test_leaves.len() - 1 { +// assert!(right_sibling.is_none()); +// } else { +// let test_right_sibling = test_leaves[idx + 1]; +// let right_node = right_sibling.as_ref().unwrap().upgrade().unwrap().clone(); +// assert_leaf(right_node, &test_right_sibling.keys); +// } +// } +// } +// /** +// * Given a node link and a test node structure, verify if if the node link +// * has the expected shape and properties +// */ +// fn assert_node(node: LatchNode, test_node: &TestNode) { +// match test_node { +// TestNode::Internal(test_internal_node) => { +// let guard = node.clone().as_ref().read().unwrap(); +// let internal_node = guard.as_internal_node(); +// assert_eq!(&*internal_node.keys.borrow(), &test_internal_node.keys); +// for (idx, child) in internal_node.edges.borrow().iter().enumerate() { +// let node = child.borrow(); +// match &*node { +// Some(child_node) => { +// let test_child = test_internal_node.edges[idx].clone(); +// let unwrapped = test_child.unwrap(); +// assert_node(child_node.clone(), &unwrapped); +// } +// None => { +// if test_internal_node.edges[idx].is_some() { +// let foo = ""; +// } +// assert_eq!(test_internal_node.edges[idx].is_none(), true); +// } +// }; +// } +// } +// TestNode::Leaf(test_leaf) => { +// assert_leaf(node.clone(), &test_leaf.keys); +// } +// }; +// } + +// fn assert_tree(tree: &BTree, test_node: &TestNode) { +// let root = tree.root.borrow().clone().unwrap(); +// assert_node(root, test_node); +// } + +// fn get_all_leaves(node: LatchNode) -> Vec>> { +// let mut leaves = Vec::new(); +// let guard = node.as_ref().read().unwrap(); +// match *guard { +// Node::Internal(internal_node) => { +// for edge in internal_node.edges.borrow().iter() { +// match &*edge.borrow() { +// Some(child) => { +// let mut child_leaves = get_all_leaves(child.clone()); +// leaves.append(&mut child_leaves); +// } +// None => leaves.push(None), +// }; +// } +// } +// Node::Leaf(_) => { +// leaves.push(Some(node.clone())); +// } +// }; +// leaves +// } + +// fn assert_leaf_with_siblings( +// node: LatchNode, +// test_leaf: &TestLeafNode, +// test_left_sibling: &Option>, +// test_right_sibling: &Option>, +// ) { +// assert_leaf(node.clone(), &test_leaf.keys); +// let guard = node.as_ref().read().unwrap(); +// let leaf_node = guard.as_leaf_node(); +// let left_sibling = &*leaf_node.left_ptr.borrow(); +// match left_sibling { +// Some(left_node) => { +// assert_leaf( +// left_node.upgrade().unwrap().clone(), +// &test_left_sibling.as_ref().unwrap().keys, +// ); +// } +// None => { +// assert!(test_left_sibling.is_none()); +// } +// }; + +// let right_sibling = &*leaf_node.right_ptr.borrow(); +// match right_sibling { +// Some(right_node) => { +// assert_leaf( +// right_node.upgrade().unwrap().clone(), +// &test_right_sibling.as_ref().unwrap().keys, +// ); +// } +// None => { +// assert!(test_left_sibling.is_none()); +// } +// }; +// } + +// fn get_all_leaf_nodes(latch_node: LatchNode) -> Vec> { +// let mut leaves = Vec::new(); +// let guard = latch_node.read().unwrap(); +// match *guard { +// Node::Internal(internal_node) => { +// for edge in internal_node.edges.borrow().iter() { +// if let Some(child) = &*edge.borrow() { +// let mut child_leaves = get_all_leaf_nodes(child.clone()); +// leaves.append(&mut child_leaves); +// } +// } +// } +// Node::Leaf(_) => { +// leaves.push(latch_node.clone()); +// } +// }; +// leaves +// } + +// fn get_all_test_leaves(test_node: &TestNode) -> Vec<&TestLeafNode> { +// let mut leaves = Vec::new(); +// match test_node { +// TestNode::Internal(internal_node) => { +// for edge in internal_node.edges.iter() { +// if let Some(child) = edge { +// let mut child_leaves = get_all_test_leaves(child); +// leaves.append(&mut child_leaves); +// } +// } +// } +// TestNode::Leaf(test_leaf) => { +// leaves.push(test_leaf); +// } +// }; +// leaves +// } + +// fn assert_leaf(node: LatchNode, start_keys: &Vec) { +// let guard = node.read().unwrap(); +// match *guard { +// Node::Internal(_) => panic!("not a leaf node"), +// Node::Leaf(leaf) => { +// assert_eq!(&*leaf.start_keys.borrow(), start_keys) +// } +// } +// } + +// fn assert_internal(internal_node: &InternalNode, start_keys: Vec) { +// assert_eq!(&*internal_node.keys.borrow(), &start_keys) +// } + +// mod search { +// use std::cell::RefCell; + +// use crate::latch_manager::latch_interval_btree::{ +// BTree, InternalNode, LeafNode, Node, +// Test::{ +// assert_internal, assert_leaf, create_test_node, create_test_tree, print_tree, +// TestInternalNode, TestLeafNode, TestNode, +// }, +// }; + +// #[test] +// fn one_level_deep() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([12, 15, 19]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([11]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([14]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([18]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([25]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 4); + +// let (leaf1, stack) = tree.find_leaf_to_add(&0); +// assert_eq!(stack.len(), 1); +// assert_internal( +// stack[0].1.unwrap().as_internal_node(), +// Vec::from([12, 15, 19]), +// ); + +// assert_leaf(leaf1.unwrap(), &Vec::from([11])); + +// let leaf2 = tree.find_leaf_to_add(&15).0.unwrap(); +// assert_leaf(leaf2, &Vec::from([18])); + +// let leaf4 = tree.find_leaf_to_add(&100).0.unwrap(); +// assert_leaf(leaf4, &Vec::from([25])); + +// print_tree(&tree); +// } +// } + +// mod split { +// use std::{ +// borrow::Borrow, +// cell::RefCell, +// sync::{Arc, RwLock}, +// }; + +// use crate::latch_manager::latch_interval_btree::{ +// BTree, LeafNode, Node, +// Test::{ +// assert_leaf_with_siblings, assert_node, assert_tree, get_all_leaf_nodes, +// get_all_leaves, get_start_keys_from_weak_link, print_node, +// }, +// }; + +// use super::{ +// create_test_node, create_test_tree, print_node_recursive, print_tree, TestInternalNode, +// TestLeafNode, TestNode, +// }; + +// #[test] +// fn split_internal() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([5, 20, 30]), +// edges: Vec::from([ +// None, +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([6, 8, 10]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([21, 25]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([35]), +// })), +// ]), +// }); +// let node = create_test_node(&test_node, 4); +// let read_guard = node.read().unwrap(); + +// let (split_node, median) = read_guard.as_internal_node().split_node(); +// assert_eq!(median, 20); + +// let split_test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([30]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([21, 25]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([35]), +// })), +// ]), +// }); +// assert_node(split_node.clone(), &split_test_node); +// let leaves = get_all_leaves(split_node.clone()); +// assert_eq!(leaves.len(), 2); +// assert_leaf_with_siblings( +// leaves[0].as_ref().unwrap().clone(), +// &TestLeafNode { +// keys: Vec::from([21, 25]), +// }, +// &Some(TestLeafNode { +// keys: Vec::from([6, 8, 10]), +// }), +// &Some(TestLeafNode { +// keys: Vec::from([35]), +// }), +// ); +// // print_node_recursive(split_node.clone()); +// } + +// #[test] +// fn split_leaf() { +// // TODO: Rewrite this test with TestNode +// // let leaf = LeafNode { +// // start_keys: RefCell::new(Vec::from([0, 1, 2])), +// // end_keys: RefCell::new(Vec::from([0, 1, 2])), +// // left_ptr: RefCell::new(None), +// // right_ptr: RefCell::new(None), +// // order: 4, +// // }; + +// // let leaf_latch_node = Arc::new(RwLock::new(Node::Leaf(leaf))); +// // let right_sibling = LeafNode { +// // start_keys: RefCell::new(Vec::from([4, 5, 6])), +// // end_keys: RefCell::new(Vec::from([0, 1, 2])), +// // left_ptr: RefCell::new(Some(Arc::downgrade(&leaf_latch_node))), +// // right_ptr: RefCell::new(None), +// // order: 4, +// // }; +// // let right_sibling_rc = Arc::new(RwLock::new(Node::Leaf(right_sibling))); +// // match leaf_latch_node.as_ref() { +// // Node::Internal(_) => panic!("Leaf is somehow internal"), +// // Node::Leaf(leaf) => leaf +// // .right_ptr +// // .borrow_mut() +// // .replace(Rc::downgrade(&right_sibling_rc)), +// // }; + +// // let (split_node, right_start_key) = BTree::split_node(leaf_latch_node.clone()); +// // assert_eq!(right_start_key, 1); + +// // match split_node.as_ref() { +// // Node::Internal(_) => panic!("Split node cannot be internal"), +// // Node::Leaf(leaf) => { +// // assert_eq!(&*leaf.start_keys.borrow(), &Vec::from([1, 2])); +// // assert_eq!(&*leaf.end_keys.borrow(), &Vec::from([1, 2])); +// // let left_start_keys = get_start_keys_from_weak_link(&leaf.left_ptr); +// // match left_start_keys.clone() { +// // Some(left_start_keys) => { +// // assert_eq!(left_start_keys, Vec::from([0])); +// // } +// // None => panic!("Left key has start keys"), +// // } +// // let right_start_keys = get_start_keys_from_weak_link(&leaf.right_ptr); +// // match right_start_keys.clone() { +// // Some(left_start_keys) => { +// // assert_eq!(left_start_keys, Vec::from([4, 5, 6])); +// // } +// // None => panic!("Right key has start keys"), +// // } +// // } +// // } + +// // print_node(split_node.clone()); +// } +// } + +// mod insert { +// use crate::latch_manager::latch_interval_btree::{BTree, Range}; + +// use super::{ +// assert_node, assert_tree, print_tree, TestInternalNode, TestLeafNode, TestNode, +// }; + +// #[test] +// fn insert_and_split() { +// let tree = BTree::::new(3); +// tree.insert(Range { +// start_key: 5, +// end_key: 5, +// }); +// tree.insert(Range { +// start_key: 10, +// end_key: 10, +// }); +// tree.insert(Range { +// start_key: 20, +// end_key: 20, +// }); +// print_tree(&tree); + +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10, 20]), +// })), +// ]), +// }); + +// assert_tree(&tree, &test_node); +// } + +// #[test] +// fn insert_and_split_internal() { +// let tree = BTree::::new(3); +// tree.insert(Range { +// start_key: 5, +// end_key: 5, +// }); +// tree.insert(Range { +// start_key: 10, +// end_key: 10, +// }); +// tree.insert(Range { +// start_key: 20, +// end_key: 20, +// }); + +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10, 20]), +// })), +// ]), +// }); + +// print_tree(&tree); + +// assert_tree(&tree, &test_node); + +// // here +// tree.insert(Range { +// start_key: 15, +// end_key: 15, +// }); +// print_tree(&tree); +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([10, 15]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15, 20]), +// })), +// ]), +// }); +// assert_tree(&tree, &test_node); + +// tree.insert(Range { +// start_key: 25, +// end_key: 25, +// }); +// print_tree(&tree); + +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([15]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([20]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20, 25]), +// })), +// ]), +// })), +// ]), +// }); + +// assert_tree(&tree, &test_node); +// } +// } + +// mod leaf_underflow { +// use std::{cell::RefCell, sync::RwLock}; + +// use crate::latch_manager::latch_interval_btree::LeafNode; + +// #[test] +// fn underflows() { +// let leaf = LeafNode { +// start_keys: RefCell::new(Vec::from([0])), +// end_keys: RefCell::new(Vec::from([0])), +// left_ptr: RefCell::new(None), +// right_ptr: RefCell::new(None), +// order: 4, +// }; +// assert!(leaf.is_underflow()); +// } +// } + +// mod delete { +// mod core_delete { +// use crate::latch_manager::latch_interval_btree::Test::{ +// assert_tree, create_test_tree, print_tree, TestInternalNode, TestLeafNode, TestNode, +// }; + +// #[test] +// fn internal_node_stealing_from_left_sibling_3_layers() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([20]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10, 15]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15, 18]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([30]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([30]), +// })), +// ]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// tree.delete(30); + +// let expected_tree = TestNode::Internal(TestInternalNode { +// keys: Vec::from([15]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([20]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15, 18]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// })), +// ]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_tree); +// } + +// #[test] +// fn internal_node_stealing_from_right_sibling_4_layers() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([40]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([15]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([18]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([18, 20]), +// })), +// ]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([60]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([50]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40, 45]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([70]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([60]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([70]), +// })), +// ]), +// })), +// ]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// tree.delete(70); +// print_tree(&tree); + +// let expected_tree = TestNode::Internal(TestInternalNode { +// keys: Vec::from([15, 40]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([18]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([18, 20]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([50, 60]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40, 45]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([60]), +// })), +// ]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_tree); +// } + +// // hello +// #[test] +// fn internal_node_stealing_from_right_sibling_3_layers() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([20, 40]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([30]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([30]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([45, 50]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([45, 49]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50, 60]), +// })), +// ]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// tree.delete(30); + +// let expected_tree = TestNode::Internal(TestInternalNode { +// keys: Vec::from([20, 45]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([40]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([50]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([45, 49]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50, 60]), +// })), +// ]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_tree); +// } +// } + +// mod find_leaf_to_delete { +// use crate::latch_manager::latch_interval_btree::Test::{ +// create_test_tree, TestInternalNode, TestLeafNode, TestNode, +// }; + +// #[test] +// fn test_leaf() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([15]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([20]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20, 25]), +// })), +// ]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// let (node, path) = tree.find_leaf_to_delete(&20); +// let indices = path +// .iter() +// .map(|(idx, _, _)| idx.clone()) +// .collect::>(); +// assert_eq!(indices, Vec::from([1, 1])); +// } +// } + +// mod leaf_stealing { +// use crate::latch_manager::latch_interval_btree::{ +// Node, +// Test::{create_test_tree, print_tree, TestInternalNode, TestLeafNode, TestNode}, +// }; + +// mod has_spare_keys { +// use std::{cell::RefCell, sync::RwLock}; + +// use crate::latch_manager::latch_interval_btree::{ +// LeafNode, +// Test::{ +// assert_tree, create_test_tree, TestInternalNode, TestLeafNode, TestNode, +// }, +// }; + +// #[test] +// fn internal_node() {} + +// #[test] +// fn leaf_node_has_spare_key() { +// let leaf_node = LeafNode { +// start_keys: RefCell::new(Vec::from([0, 1])), +// end_keys: RefCell::new(Vec::from([0, 1])), +// left_ptr: RefCell::new(None), +// right_ptr: RefCell::new(None), +// order: 3, +// }; +// assert_eq!(leaf_node.has_spare_key(), true); +// } + +// #[test] +// fn leaf_node_has_no_spare_key() { +// let leaf_node = LeafNode { +// start_keys: RefCell::new(Vec::from([0])), +// end_keys: RefCell::new(Vec::from([0])), +// left_ptr: RefCell::new(None), +// right_ptr: RefCell::new(None), +// order: 3, +// }; +// assert_eq!(leaf_node.has_spare_key(), false); +// } + +// #[test] +// fn requires_updating_ancestor() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([4]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([2]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([1]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([2, 3]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([4, 5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10, 13]), +// })), +// ]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// tree.delete(4); + +// let expected_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([5]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([2]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([1]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([2, 3]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10, 13]), +// })), +// ]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_node); +// } +// } + +// mod stealing_core { +// use crate::latch_manager::latch_interval_btree::Test::{ +// assert_tree, create_test_tree, print_tree, TestInternalNode, TestLeafNode, +// TestNode, +// }; + +// #[test] +// fn leaf_steals_left_sibling() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([8]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([5]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([1, 3]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([8, 9]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10, 15]), +// })), +// ]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// tree.delete(5); +// let expected_tree_after_delete = TestNode::Internal(TestInternalNode { +// keys: Vec::from([8]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([3]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([1]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([3]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([8, 9]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10, 15]), +// })), +// ]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_tree_after_delete); +// } + +// #[test] +// fn leaf_steals_right_sibling() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([5]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([2]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5, 6]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([12]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([12, 20]), +// })), +// ]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// tree.delete(10); +// print_tree(&tree); +// let expected_tree_after_delete = TestNode::Internal(TestInternalNode { +// keys: Vec::from([12]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([5]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([2]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5, 6]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([20]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([12]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// })), +// ]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_tree_after_delete); +// } +// } +// } + +// mod internal_node_stealing { +// use crate::latch_manager::latch_interval_btree::Test::{ +// assert_tree, create_test_tree, find_node_and_parent_with_indices, print_tree, +// TestInternalNode, TestLeafNode, TestNode, +// }; + +// #[test] +// fn simple_steal_from_left_sibling() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([20]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10, 15]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15, 18]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// }))]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// let (node_latch, parent, edge_idx) = +// find_node_and_parent_with_indices(&tree, Vec::from([1])); +// let node_guard = node_latch.as_ref().read().unwrap(); +// let temp = node_guard.as_internal_node(); +// let parent_guard = parent.as_ref().read().unwrap(); + +// let did_steal = temp.steal_from_sibling(parent_guard.as_internal_node(), edge_idx); +// println!("Did steal {}", did_steal); +// let expected_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([15]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([20]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([15, 18]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// })), +// ]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_node); +// } + +// #[test] +// fn simple_steal_from_right_sibling() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([20, 40]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// }))]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([45, 50]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([45, 49]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50, 60]), +// })), +// ]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// let (node_latch, parent_latch, edge_idx) = +// find_node_and_parent_with_indices(&tree, Vec::from([1])); +// let node_guard = node_latch.as_ref().read().unwrap(); +// let temp = node_guard.as_internal_node(); + +// let parent_guard = parent_latch.as_ref().read().unwrap(); +// let parent = parent_guard.as_internal_node(); +// let did_steal = temp.steal_from_sibling(parent, edge_idx); +// println!("Did steal {}", did_steal); + +// let expected_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([20, 45]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([10]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([10]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([40]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([50]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([45, 49]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50, 60]), +// })), +// ]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_node); +// } +// } +// } + +// mod merge { +// mod internal_node { +// use crate::latch_manager::latch_interval_btree::Test::{ +// assert_tree, create_test_tree, find_node_and_parent_with_indices, +// find_node_with_indices, print_tree, TestInternalNode, TestLeafNode, TestNode, +// }; + +// #[test] +// fn merge_with_left() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([60]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([50]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40, 45]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([60]), +// }))]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// let (node_latch, parent_latch, edge_idx) = +// find_node_and_parent_with_indices(&tree, Vec::from([1])); +// let node_guard = node_latch.read().unwrap(); +// let internal_node = node_guard.as_internal_node(); + +// let parent_guard = parent_latch.as_ref().read().unwrap(); +// internal_node.merge_with_sibling(parent_guard.as_internal_node(), edge_idx); +// let expected_tree = TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([50, 60]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40, 45]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([60]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([60]), +// }))]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_tree); +// } + +// #[test] +// fn merge_with_right() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([60]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([50]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40, 45]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([60]), +// }))]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// let (node_latch, parent_latch, edge_idx) = +// find_node_and_parent_with_indices(&tree, Vec::from([0])); +// let node_guard = node_latch.as_ref().read().unwrap(); +// let internal_node = node_guard.as_internal_node(); + +// let parent_guard = parent_latch.as_ref().read().unwrap(); +// internal_node.merge_with_sibling(parent_guard.as_internal_node(), edge_idx); +// let expected_tree = TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([ +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([50, 60]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([40, 45]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([50]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([60]), +// })), +// ]), +// })), +// Some(TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([60]), +// }))]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_tree); +// } +// } + +// mod leaf { +// use crate::latch_manager::latch_interval_btree::Test::{ +// assert_tree, create_test_tree, print_tree, TestInternalNode, TestLeafNode, TestNode, +// }; + +// #[test] +// fn merge_with_left_leaf() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([16, 20]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5, 10]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([16]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20, 30, 40]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// let (node, stack) = tree.find_leaf_to_delete(&16); +// let guard = node.unwrap().as_ref().read().unwrap(); +// let leaf = guard.as_leaf_node(); +// let (edge_idx, dir, parent) = stack.last().unwrap(); +// leaf.merge_node(parent.clone(), *edge_idx); +// print_tree(&tree); + +// let expected_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([20]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([5, 10, 16]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([20, 30, 40]), +// })), +// ]), +// }); +// assert_tree(&tree, &expected_node); +// } + +// #[test] +// fn merge_with_right_leaf() { +// let test_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([25]), +// edges: Vec::from([ +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([]), +// })), +// Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([25]), +// })), +// ]), +// }); +// let tree = create_test_tree(&test_node, 3); +// let (node, stack) = tree.find_leaf_to_delete(&16); +// let guard = node.unwrap().as_ref().read().unwrap(); +// let leaf = guard.as_leaf_node(); +// let (edge_idx, dir, parent) = stack.last().unwrap(); +// leaf.merge_node(parent.clone(), *edge_idx); +// print_tree(&tree); + +// let expected_node = TestNode::Internal(TestInternalNode { +// keys: Vec::from([]), +// edges: Vec::from([Some(TestNode::Leaf(TestLeafNode { +// keys: Vec::from([25]), +// }))]), +// }); +// assert_tree(&tree, &expected_node); +// } +// } +// } + +// #[test] +// fn experiment() { +// let my_rwlock = RwLock::new(5); + +// let read1 = my_rwlock.read().unwrap(); // one .read() is fine +// let read2 = my_rwlock.read().unwrap(); // two .read()s is also fine + +// println!("{:?}, {:?}", read1, read2); +// drop(read1); +// drop(read2); +// } +// }