remove confusing casting from usize -> u16 -> usize for usable space

This commit is contained in:
pedrocarlo
2025-07-17 12:42:32 -03:00
parent 2aca28a86b
commit 28ae96f49f
2 changed files with 15 additions and 15 deletions

View File

@@ -837,8 +837,8 @@ impl BTreeCursor {
page_type: PageType,
usable_size: usize,
) -> Result<(usize, usize)> {
let max_local = payload_overflow_threshold_max(page_type, usable_size as u16);
let min_local = payload_overflow_threshold_min(page_type, usable_size as u16);
let max_local = payload_overflow_threshold_max(page_type, usable_size);
let min_local = payload_overflow_threshold_min(page_type, usable_size);
// This matches btreeParseCellAdjustSizeForOverflow logic
let n_local = if payload_len <= max_local {
@@ -2206,7 +2206,7 @@ impl BTreeCursor {
&mut cell_payload,
cell_idx,
record,
self.usable_space() as u16,
self.usable_space(),
self.pager.clone(),
);
@@ -4890,7 +4890,7 @@ impl BTreeCursor {
&mut new_payload,
cell_idx,
record,
self.usable_space() as u16,
self.usable_space(),
self.pager.clone(),
);
@@ -6403,7 +6403,7 @@ fn fill_cell_payload(
cell_payload: &mut Vec<u8>,
cell_idx: usize,
record: &ImmutableRecord,
usable_space: u16,
usable_space: usize,
pager: Rc<Pager>,
) {
// TODO: make record raw from start, having to serialize is not good
@@ -6498,13 +6498,13 @@ fn fill_cell_payload(
/// - Give a minimum fanout of 4 for index b-trees
/// - Ensure enough payload is on the b-tree page that the record header can usually be accessed
/// without consulting an overflow page
pub fn payload_overflow_threshold_max(page_type: PageType, usable_space: u16) -> usize {
pub fn payload_overflow_threshold_max(page_type: PageType, usable_space: usize) -> usize {
match page_type {
PageType::IndexInterior | PageType::IndexLeaf => {
((usable_space as usize - 12) * 64 / 255) - 23 // Index page formula
((usable_space - 12) * 64 / 255) - 23 // Index page formula
}
PageType::TableInterior | PageType::TableLeaf => {
usable_space as usize - 35 // Table leaf page formula
usable_space - 35 // Table leaf page formula
}
}
}
@@ -6518,9 +6518,9 @@ pub fn payload_overflow_threshold_max(page_type: PageType, usable_space: u16) ->
/// - Otherwise: store M bytes on page
///
/// The remaining bytes are stored on overflow pages in both cases.
pub fn payload_overflow_threshold_min(_page_type: PageType, usable_space: u16) -> usize {
pub fn payload_overflow_threshold_min(_page_type: PageType, usable_space: usize) -> usize {
// Same formula for all page types
((usable_space as usize - 12) * 32 / 255) - 23
((usable_space - 12) * 32 / 255) - 23
}
/// Drop a cell from a page.
@@ -8746,7 +8746,7 @@ mod tests {
&mut payload,
cell_idx as usize,
&record,
pager.usable_space() as u16,
pager.usable_space(),
pager.clone(),
);
insert_into_cell(

View File

@@ -622,9 +622,9 @@ impl PageContent {
let cell_pointer = self.read_u16_no_offset(cell_pointer) as usize;
let start = cell_pointer;
let payload_overflow_threshold_max =
payload_overflow_threshold_max(self.page_type(), usable_size as u16);
payload_overflow_threshold_max(self.page_type(), usable_size);
let payload_overflow_threshold_min =
payload_overflow_threshold_min(self.page_type(), usable_size as u16);
payload_overflow_threshold_min(self.page_type(), usable_size);
let len = match self.page_type() {
PageType::IndexInterior => {
let (len_payload, n_payload) = read_varint(&buf[cell_pointer + 4..]).unwrap();
@@ -879,8 +879,8 @@ pub fn read_btree_cell(
usable_size: usize,
) -> Result<BTreeCell> {
let page_type = page_content.page_type();
let max_local = payload_overflow_threshold_max(page_type, usable_size as u16);
let min_local = payload_overflow_threshold_min(page_type, usable_size as u16);
let max_local = payload_overflow_threshold_max(page_type, usable_size);
let min_local = payload_overflow_threshold_min(page_type, usable_size);
match page_type {
PageType::IndexInterior => {
let mut pos = pos;