This commit is contained in:
bronku 2025-12-11 20:25:22 +01:00
parent 6da9247ab0
commit 78132349d3
4 changed files with 16 additions and 9 deletions

View file

@ -13,12 +13,7 @@ where
S: Storage,
{
pub fn open(mut storage: S) -> Self {
// Initialize with an empty root node (leaf)
let root = Node::Leaf(LeafNode {
keys: Vec::new(),
values: Vec::new(),
next: None,
});
let root = Node::Leaf(LeafNode::new());
storage.write_node(0, &root);
BPlusTree {
storage,
@ -55,7 +50,6 @@ where
let mut current_loc = self.root_loc;
let mut current_node = self.storage.read_node(current_loc).unwrap();
// Traverse to the leaf node, recording the path
while let Node::Internal(internal) = current_node {
path.push((current_loc, internal.clone()));
let mut i = 0;
@ -87,11 +81,9 @@ where
leaf.values.push(value);
}
// Write the updated leaf back to storage
self.storage
.write_node(current_loc, &Node::Leaf(leaf.clone()));
// Check if the leaf needs to be split
if leaf.keys.len() > MAX_KEYS {
self.split_leaf(current_loc, leaf, &mut path);
}

1
src/config.rs Normal file
View file

@ -0,0 +1 @@
pub const DEGREE: usize = 2;

View file

@ -1,4 +1,5 @@
mod btree;
mod config;
mod node;
mod record;
mod storage;

View file

@ -1,3 +1,4 @@
use crate::config::DEGREE;
use crate::record::Record;
#[derive(Debug, Clone)]
@ -13,8 +14,20 @@ pub struct LeafNode {
pub next: Option<usize>,
}
impl LeafNode {
pub fn new() -> Self {
Self {
keys: Vec::with_capacity(DEGREE * 2),
values: Vec::with_capacity(DEGREE * 2),
next: None,
}
}
}
#[derive(Debug, Clone)]
pub struct InternalNode {
pub keys: Vec<i32>,
pub children: Vec<usize>,
}