Skip to content

Commit 1d9c6d6

Browse files
committed
Merge 'btree: move some blocks of code to more reasonable places' from Jussi Saurio
Reviewed-by: Preston Thorpe (@PThorpe92) Closes #1343
2 parents 9133674 + 5628cc2 commit 1d9c6d6

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

core/storage/btree.rs

+26-39
Original file line numberDiff line numberDiff line change
@@ -366,30 +366,6 @@ pub struct BTreeCursor {
366366
empty_record: Cell<bool>,
367367
}
368368

369-
/// Stack of pages representing the tree traversal order.
370-
/// current_page represents the current page being used in the tree and current_page - 1 would be
371-
/// the parent. Using current_page + 1 or higher is undefined behaviour.
372-
struct PageStack {
373-
/// Pointer to the current page being consumed
374-
current_page: Cell<i32>,
375-
/// List of pages in the stack. Root page will be in index 0
376-
stack: RefCell<[Option<PageRef>; BTCURSOR_MAX_DEPTH + 1]>,
377-
/// List of cell indices in the stack.
378-
/// cell_indices[current_page] is the current cell index being consumed. Similarly
379-
/// cell_indices[current_page-1] is the cell index of the parent of the current page
380-
/// that we save in case of going back up.
381-
/// There are two points that need special attention:
382-
/// If cell_indices[current_page] = -1, it indicates that the current iteration has reached the start of the current_page
383-
/// If cell_indices[current_page] = `cell_count`, it means that the current iteration has reached the end of the current_page
384-
cell_indices: RefCell<[i32; BTCURSOR_MAX_DEPTH + 1]>,
385-
}
386-
387-
struct CellArray {
388-
cells: Vec<&'static mut [u8]>, // TODO(pere): make this with references
389-
390-
number_of_cells_per_page: Vec<u16>, // number of cells in each page
391-
}
392-
393369
impl BTreeCursor {
394370
pub fn new(
395371
mv_cursor: Option<Rc<RefCell<MvCursor>>>,
@@ -3935,6 +3911,24 @@ fn validate_cells_after_insertion(cell_array: &CellArray, leaf_data: bool) {
39353911
}
39363912
}
39373913

3914+
/// Stack of pages representing the tree traversal order.
3915+
/// current_page represents the current page being used in the tree and current_page - 1 would be
3916+
/// the parent. Using current_page + 1 or higher is undefined behaviour.
3917+
struct PageStack {
3918+
/// Pointer to the current page being consumed
3919+
current_page: Cell<i32>,
3920+
/// List of pages in the stack. Root page will be in index 0
3921+
stack: RefCell<[Option<PageRef>; BTCURSOR_MAX_DEPTH + 1]>,
3922+
/// List of cell indices in the stack.
3923+
/// cell_indices[current_page] is the current cell index being consumed. Similarly
3924+
/// cell_indices[current_page-1] is the cell index of the parent of the current page
3925+
/// that we save in case of going back up.
3926+
/// There are two points that need special attention:
3927+
/// If cell_indices[current_page] = -1, it indicates that the current iteration has reached the start of the current_page
3928+
/// If cell_indices[current_page] = `cell_count`, it means that the current iteration has reached the end of the current_page
3929+
cell_indices: RefCell<[i32; BTCURSOR_MAX_DEPTH + 1]>,
3930+
}
3931+
39383932
impl PageStack {
39393933
fn increment_current(&self) {
39403934
self.current_page.set(self.current_page.get() + 1);
@@ -4056,6 +4050,13 @@ impl PageStack {
40564050
}
40574051
}
40584052

4053+
/// Used for redistributing cells during a balance operation.
4054+
struct CellArray {
4055+
cells: Vec<&'static mut [u8]>, // TODO(pere): make this with references
4056+
4057+
number_of_cells_per_page: Vec<u16>, // number of cells in each page
4058+
}
4059+
40594060
impl CellArray {
40604061
pub fn cell_size(&self, cell_idx: usize) -> u16 {
40614062
self.cells[cell_idx].len() as u16
@@ -4739,7 +4740,7 @@ fn fill_cell_payload(
47394740
}
47404741

47414742
// we still have bytes to add, we will need to allocate new overflow page
4742-
let overflow_page = allocate_overflow_page(pager.clone());
4743+
let overflow_page = pager.allocate_overflow_page();
47434744
overflow_pages.push(overflow_page.clone());
47444745
{
47454746
let id = overflow_page.get().id as u32;
@@ -4762,20 +4763,6 @@ fn fill_cell_payload(
47624763
assert_eq!(cell_size, cell_payload.len());
47634764
}
47644765

4765-
/// Allocate a new overflow page.
4766-
/// This is done when a cell overflows and new space is needed.
4767-
fn allocate_overflow_page(pager: Rc<Pager>) -> PageRef {
4768-
let page = pager.allocate_page().unwrap();
4769-
tracing::debug!("allocate_overflow_page(id={})", page.get().id);
4770-
4771-
// setup overflow page
4772-
let contents = page.get().contents.as_mut().unwrap();
4773-
let buf = contents.as_ptr();
4774-
buf.fill(0);
4775-
4776-
page
4777-
}
4778-
47794766
/// Returns the maximum payload size (X) that can be stored directly on a b-tree page without spilling to overflow pages.
47804767
///
47814768
/// For table leaf pages: X = usable_size - 35

core/storage/pager.rs

+14
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ impl Pager {
217217
id as u32
218218
}
219219

220+
/// Allocate a new overflow page.
221+
/// This is done when a cell overflows and new space is needed.
222+
pub fn allocate_overflow_page(&self) -> PageRef {
223+
let page = self.allocate_page().unwrap();
224+
tracing::debug!("Pager::allocate_overflow_page(id={})", page.get().id);
225+
226+
// setup overflow page
227+
let contents = page.get().contents.as_mut().unwrap();
228+
let buf = contents.as_ptr();
229+
buf.fill(0);
230+
231+
page
232+
}
233+
220234
/// Allocate a new page to the btree via the pager.
221235
/// This marks the page as dirty and writes the page header.
222236
pub fn do_allocate_page(&self, page_type: PageType, offset: usize) -> PageRef {

0 commit comments

Comments
 (0)