From d961baf5ac77da6d4d9a116b15570dfe7f026ff8 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Mon, 14 Apr 2025 15:18:21 +0300 Subject: [PATCH 1/4] btree: move PageStack struct declaration next to impl --- core/storage/btree.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/core/storage/btree.rs b/core/storage/btree.rs index 3af102854..211f9ab68 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -366,24 +366,6 @@ pub struct BTreeCursor { empty_record: Cell, } -/// Stack of pages representing the tree traversal order. -/// current_page represents the current page being used in the tree and current_page - 1 would be -/// the parent. Using current_page + 1 or higher is undefined behaviour. -struct PageStack { - /// Pointer to the current page being consumed - current_page: Cell, - /// List of pages in the stack. Root page will be in index 0 - stack: RefCell<[Option; BTCURSOR_MAX_DEPTH + 1]>, - /// List of cell indices in the stack. - /// cell_indices[current_page] is the current cell index being consumed. Similarly - /// cell_indices[current_page-1] is the cell index of the parent of the current page - /// that we save in case of going back up. - /// There are two points that need special attention: - /// If cell_indices[current_page] = -1, it indicates that the current iteration has reached the start of the current_page - /// If cell_indices[current_page] = `cell_count`, it means that the current iteration has reached the end of the current_page - cell_indices: RefCell<[i32; BTCURSOR_MAX_DEPTH + 1]>, -} - struct CellArray { cells: Vec<&'static mut [u8]>, // TODO(pere): make this with references @@ -3762,6 +3744,24 @@ fn validate_cells_after_insertion(cell_array: &CellArray, leaf_data: bool) { } } +/// Stack of pages representing the tree traversal order. +/// current_page represents the current page being used in the tree and current_page - 1 would be +/// the parent. Using current_page + 1 or higher is undefined behaviour. +struct PageStack { + /// Pointer to the current page being consumed + current_page: Cell, + /// List of pages in the stack. Root page will be in index 0 + stack: RefCell<[Option; BTCURSOR_MAX_DEPTH + 1]>, + /// List of cell indices in the stack. + /// cell_indices[current_page] is the current cell index being consumed. Similarly + /// cell_indices[current_page-1] is the cell index of the parent of the current page + /// that we save in case of going back up. + /// There are two points that need special attention: + /// If cell_indices[current_page] = -1, it indicates that the current iteration has reached the start of the current_page + /// If cell_indices[current_page] = `cell_count`, it means that the current iteration has reached the end of the current_page + cell_indices: RefCell<[i32; BTCURSOR_MAX_DEPTH + 1]>, +} + impl PageStack { fn increment_current(&self) { self.current_page.set(self.current_page.get() + 1); From 930f1d79b46069a60a74e6c8e0aae8f2b5b83b4f Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Mon, 14 Apr 2025 15:19:56 +0300 Subject: [PATCH 2/4] btree: move CellArray struct declaration next to impl --- core/storage/btree.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/storage/btree.rs b/core/storage/btree.rs index 211f9ab68..043e52e57 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -366,12 +366,6 @@ pub struct BTreeCursor { empty_record: Cell, } -struct CellArray { - cells: Vec<&'static mut [u8]>, // TODO(pere): make this with references - - number_of_cells_per_page: Vec, // number of cells in each page -} - impl BTreeCursor { pub fn new( mv_cursor: Option>>, @@ -3883,6 +3877,12 @@ impl PageStack { } } +struct CellArray { + cells: Vec<&'static mut [u8]>, // TODO(pere): make this with references + + number_of_cells_per_page: Vec, // number of cells in each page +} + impl CellArray { pub fn cell_size(&self, cell_idx: usize) -> u16 { self.cells[cell_idx].len() as u16 From bf26e6246577e09c1fcc8806d17428aeeed517c4 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Mon, 14 Apr 2025 15:21:07 +0300 Subject: [PATCH 3/4] btree: add doc comment about CellArray struct --- core/storage/btree.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/core/storage/btree.rs b/core/storage/btree.rs index 043e52e57..7e2b2b566 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -3877,6 +3877,7 @@ impl PageStack { } } +/// Used for redistributing cells during a balance operation. struct CellArray { cells: Vec<&'static mut [u8]>, // TODO(pere): make this with references From 5628cc27a6b3d2a07aada507d62819f6c443ba3a Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Mon, 14 Apr 2025 15:25:15 +0300 Subject: [PATCH 4/4] btree: move allocate_overflow_page to Pager impl --- core/storage/btree.rs | 16 +--------------- core/storage/pager.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/storage/btree.rs b/core/storage/btree.rs index 7e2b2b566..6869cf298 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -4567,7 +4567,7 @@ fn fill_cell_payload( } // we still have bytes to add, we will need to allocate new overflow page - let overflow_page = allocate_overflow_page(pager.clone()); + let overflow_page = pager.allocate_overflow_page(); overflow_pages.push(overflow_page.clone()); { let id = overflow_page.get().id as u32; @@ -4590,20 +4590,6 @@ fn fill_cell_payload( assert_eq!(cell_size, cell_payload.len()); } -/// Allocate a new overflow page. -/// This is done when a cell overflows and new space is needed. -fn allocate_overflow_page(pager: Rc) -> PageRef { - let page = pager.allocate_page().unwrap(); - tracing::debug!("allocate_overflow_page(id={})", page.get().id); - - // setup overflow page - let contents = page.get().contents.as_mut().unwrap(); - let buf = contents.as_ptr(); - buf.fill(0); - - page -} - /// Returns the maximum payload size (X) that can be stored directly on a b-tree page without spilling to overflow pages. /// /// For table leaf pages: X = usable_size - 35 diff --git a/core/storage/pager.rs b/core/storage/pager.rs index 9d7affa95..47dc5451c 100644 --- a/core/storage/pager.rs +++ b/core/storage/pager.rs @@ -217,6 +217,20 @@ impl Pager { id as u32 } + /// Allocate a new overflow page. + /// This is done when a cell overflows and new space is needed. + pub fn allocate_overflow_page(&self) -> PageRef { + let page = self.allocate_page().unwrap(); + tracing::debug!("Pager::allocate_overflow_page(id={})", page.get().id); + + // setup overflow page + let contents = page.get().contents.as_mut().unwrap(); + let buf = contents.as_ptr(); + buf.fill(0); + + page + } + /// Allocate a new page to the btree via the pager. /// This marks the page as dirty and writes the page header. pub fn do_allocate_page(&self, page_type: PageType, offset: usize) -> PageRef {