Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/tree_store/page_store/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,38 @@ impl BtreeBitmap {
num_pages = num_pages.div_ceil(64);
}

// Reverse so that the root as index 0
// Reverse so that the root is at index 0
heights.reverse();

Self { heights }
}

// Like new(), but pads the tree height for max_capacity so resize()
// never needs to insert new levels.
pub(crate) fn new_padded(num_pages: u32, capacity: u32, max_capacity: u32) -> Self {
let mut result = Self::new(num_pages, capacity);

let max_height = Self::height_for_capacity(max_capacity);
while result.heights.len() < max_height {
let root_len = result.heights[0].len();
let parent_len = root_len.div_ceil(64);
result
.heights
.insert(0, U64GroupedBitmap::new_full(parent_len, parent_len));
}

result
}

fn height_for_capacity(mut capacity: u32) -> usize {
let mut height = 1;
while capacity > 64 {
capacity = capacity.div_ceil(64);
height += 1;
}
height
}

pub(crate) fn resize(&mut self, mut new_len: u32, full: bool) {
for height in self.heights.iter_mut().rev() {
height.resize(new_len, full);
Expand Down Expand Up @@ -428,6 +454,41 @@ mod test {
assert_eq!(allocator.find_first_unset().unwrap(), 0);
}

#[test]
fn resize_beyond_initial_capacity() {
let mut bm = BtreeBitmap::new_padded(60, 60, 1_048_576);
let height = bm.heights.len();

bm.clear(0);
bm.clear(30);
bm.clear(59);

bm.resize(5000, true);

// Height is pre-padded at construction, stays the same
assert_eq!(bm.heights.len(), height);

// Previously cleared bits survive the resize
assert!(!bm.get(0));
assert!(!bm.get(30));
assert!(!bm.get(59));

// Newly grown region should be marked full (true)
assert!(bm.get(60));
assert!(bm.get(4999));

// Alloc should return the previously cleared entries
assert_eq!(bm.alloc(), Some(0));
assert_eq!(bm.alloc(), Some(30));
assert_eq!(bm.alloc(), Some(59));
assert!(bm.alloc().is_none());

// Clear a bit in the newly grown area, alloc should find it
bm.clear(3000);
assert_eq!(bm.alloc(), Some(3000));
assert!(bm.alloc().is_none());
}

#[test]
fn random_pattern() {
let seed = rand::rng().random();
Expand Down
2 changes: 1 addition & 1 deletion src/tree_store/page_store/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl RegionTracker {
pub(crate) fn new(regions: u32, orders: u8) -> Self {
let mut data = vec![];
for _ in 0..orders {
data.push(BtreeBitmap::new(regions, MAX_REGIONS));
data.push(BtreeBitmap::new_padded(regions, regions, MAX_REGIONS));
}
Self {
order_trackers: data,
Expand Down
Loading