diff --git a/src/btree.rs b/src/btree.rs index 791fc92..e2ac1a5 100644 --- a/src/btree.rs +++ b/src/btree.rs @@ -30,6 +30,38 @@ where } } + pub fn calculate_depth(&mut self) -> usize { + let mut depth = 0; + let mut loc = self.header.root; + + loop { + match self.storage.read_node(loc) { + Some(Node::Internal(internal)) => { + depth += 1; + loc = internal.children[0]; + } + Some(Node::Leaf(_)) => { + return depth + 1; + } + _ => panic!("Corrupt tree"), + } + } + } + + pub fn count_total_keys(&mut self) -> usize { + let mut count = 0; + let mut loc = self.leftmost_leaf(); + + while let Some(Node::Leaf(leaf)) = self.storage.read_node(loc) { + count += leaf.keys.len(); + match leaf.next { + Some(next) => loc = next, + None => break, + } + } + count + } + pub fn find(&mut self, key: i32) -> Option { let mut current_loc = self.header.root; loop { diff --git a/src/config.rs b/src/config.rs index 94e0fb3..a28e36d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ pub const DEGREE: usize = 2; pub const MAX_KEYS: usize = DEGREE * 2 + 1; -pub const PAGE_SIZE: usize = 1024; +pub const PAGE_SIZE: usize = 4 * 16384; #[cfg(test)] pub mod test_config { diff --git a/src/main.rs b/src/main.rs index 1343356..7dba957 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,27 @@ fn repl(tree: &mut BPlusTree) { continue; } + if input == "depth" { + let depth = tree.calculate_depth(); + println!("Tree depth: {}", depth); + continue; + } + + if input == "keys" { + let count = tree.count_total_keys(); + println!("total keys: {}", count); + continue; + } + + if input == "stats" { + let reads = tree.storage.page_reads; + let writes = tree.storage.page_writes; + println!("Page reads: {}", reads); + println!("Page writes: {}", writes); + println!("Total I/O: {}", reads + writes); + continue; + } + match handle_command(tree, input) { Ok(()) => {} Err(err) => println!("Error: {}", err),