| 177 | } |
| 178 | |
| 179 | fn search_node(&mut self, node_offset: usize, key: K) -> Result<Option<V>, io::Error> { |
| 180 | // println!("Searching for key: {} at offset: {}", key, node_offset); |
| 181 | let node = self.storage_manager.load_node(node_offset)?; |
| 182 | |
| 183 | match node.node_type { |
| 184 | NodeType::Internal => { |
| 185 | let idx = node.keys.binary_search(&key).unwrap_or_else(|x| x); // Find the child to go to |
| 186 | self.search_node(node.children[idx], key) |
| 187 | } |
| 188 | NodeType::Leaf => match node.keys.binary_search(&key) { |
| 189 | Ok(idx) => Ok(node.values[idx].clone()), |
| 190 | Err(_) => Ok(None), |
| 191 | }, |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | pub fn has_key(&mut self, key: K) -> Result<bool, io::Error> { |
| 196 | self.has_key_node(self.storage_manager.root_offset(), key) |