(&mut self, key: K, value: V)
| 41 | } |
| 42 | |
| 43 | pub fn insert(&mut self, key: K, value: V) -> Result<(), io::Error> { |
| 44 | // println!("Inserting key: {}, value: {}", key, value); |
| 45 | let mut root = self |
| 46 | .storage_manager |
| 47 | .load_node(self.storage_manager.root_offset())?; |
| 48 | |
| 49 | // println!("Root offset: {}, {}", self.root_offset, root.offset); |
| 50 | |
| 51 | if root.is_full() { |
| 52 | // println!("Root is full, needs splitting"); |
| 53 | let mut new_root = Node::new_internal(0); |
| 54 | new_root.is_root = true; |
| 55 | let (median, mut sibling) = root.split(self.b)?; |
| 56 | // println!("Root split: median = {}, new sibling created", median); |
| 57 | // println!("Root split: median = {}, new sibling created", median); |
| 58 | root.is_root = false; |
| 59 | self.storage_manager.store_node(&mut root)?; |
| 60 | // println!("Root stored"); |
| 61 | let sibling_offset = self.storage_manager.store_node(&mut sibling)?; |
| 62 | new_root.keys.push(median); |
| 63 | new_root.children.push(self.storage_manager.root_offset()); // old root offset |
| 64 | new_root.children.push(sibling_offset); // new sibling offset |
| 65 | new_root.is_root = true; |
| 66 | self.storage_manager.store_node(&mut new_root)?; |
| 67 | self.storage_manager.set_root_offset(new_root.offset); |
| 68 | |
| 69 | root.parent_offset = Some(new_root.offset); |
| 70 | sibling.parent_offset = Some(new_root.offset); |
| 71 | self.storage_manager.store_node(&mut root)?; |
| 72 | self.storage_manager.store_node(&mut sibling)?; |
| 73 | // println!( |
| 74 | // "New root created with children offsets: {} and {}", |
| 75 | // self.root_offset, sibling_offset |
| 76 | // ); |
| 77 | } |
| 78 | // println!("Inserting into non-full root"); |
| 79 | self.insert_non_full(self.storage_manager.root_offset(), key, value, 0)?; |
| 80 | |
| 81 | // println!("Inserted key, root offset: {}", self.root_offset); |
| 82 | |
| 83 | Ok(()) |
| 84 | } |
| 85 | |
| 86 | fn insert_non_full( |
| 87 | &mut self, |
no test coverage detected