kill me please

This commit is contained in:
bronku 2025-12-15 14:10:30 +01:00
parent 3b8da0abe2
commit 82e386a48c
3 changed files with 54 additions and 1 deletions

View file

@ -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<Record> { pub fn find(&mut self, key: i32) -> Option<Record> {
let mut current_loc = self.header.root; let mut current_loc = self.header.root;
loop { loop {

View file

@ -1,6 +1,6 @@
pub const DEGREE: usize = 2; pub const DEGREE: usize = 2;
pub const MAX_KEYS: usize = DEGREE * 2 + 1; pub const MAX_KEYS: usize = DEGREE * 2 + 1;
pub const PAGE_SIZE: usize = 1024; pub const PAGE_SIZE: usize = 4 * 16384;
#[cfg(test)] #[cfg(test)]
pub mod test_config { pub mod test_config {

View file

@ -60,6 +60,27 @@ fn repl(tree: &mut BPlusTree<FileStorage>) {
continue; 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) { match handle_command(tree, input) {
Ok(()) => {} Ok(()) => {}
Err(err) => println!("Error: {}", err), Err(err) => println!("Error: {}", err),