Skip to content
Open
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
20 changes: 19 additions & 1 deletion fuzz/fuzz_targets/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct Slot {
parent: Option<usize>,
committed: bool,
prev_root: Node,
/// For an overlay without a live parent: the DB's root version when its session began.
db_root_version: Option<u64>,
/// The full state as of this overlay.
model: Model,
}
Expand All @@ -30,6 +32,9 @@ struct State {
committed: Model,
/// The overlay committed most recently, unless a session was committed directly after it.
last_committed_overlay: Option<usize>,
/// Counts the commits that changed the DB's root. An overlay built directly on the DB is
/// stale once the root has changed, even if it has since returned to the same value.
root_version: u64,
slots: Vec<Slot>,
keys: Vec<KeyPath>,
}
Expand All @@ -49,6 +54,7 @@ fn run(u: &mut Unstructured, path: &std::path::Path) -> Result<()> {
db: Nomt::open(options).unwrap(),
committed: Model::new(),
last_committed_overlay: None,
root_version: 0,
slots: Vec::new(),
keys: Vec::new(),
};
Expand Down Expand Up @@ -200,6 +206,7 @@ fn session(u: &mut Unstructured, state: &mut State) -> Result<()> {
parent: base,
committed: false,
prev_root: base_root,
db_root_version: chain.is_empty().then_some(state.root_version),
model,
});
}
Expand All @@ -208,6 +215,9 @@ fn session(u: &mut Unstructured, state: &mut State) -> Result<()> {
// Equal roots mean the DB holds the same state the session was based on.
if db_root == base_root {
result.unwrap();
if state.db.root().into_inner() != db_root {
state.root_version += 1;
}
state.committed = model;
state.last_committed_overlay = None;
} else {
Expand All @@ -227,7 +237,12 @@ fn commit_overlay(u: &mut Unstructured, state: &mut State) -> Result<()> {
let parent_ok = slot
.parent
.map_or(true, |p| state.last_committed_overlay == Some(p));
let expect_ok = parent_ok && slot.prev_root == state.db.root().into_inner();
let db_root = state.db.root().into_inner();
let expect_ok = parent_ok
&& slot.prev_root == db_root
&& slot
.db_root_version
.map_or(true, |version| version == state.root_version);

// A commit attempt consumes the overlay, whether or not it succeeds.
let overlay = slot.overlay.take().unwrap();
Expand All @@ -240,6 +255,9 @@ fn commit_overlay(u: &mut Unstructured, state: &mut State) -> Result<()> {
};
if expect_ok {
result.unwrap();
if state.db.root().into_inner() != db_root {
state.root_version += 1;
}
slot.committed = true;
state.committed = slot.model.clone();
state.last_committed_overlay = Some(i);
Expand Down
65 changes: 42 additions & 23 deletions nomt/src/beatree/branch/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::BRANCH_NODE_SIZE;
use crate::{
beatree::{
allocator::PageNumber,
ops::bit_ops::{bitwise_memcpy, reconstruct_key},
ops::bit_ops::{bitwise_memcpy, reconstruct_key, separator_len},
Key,
},
io::{page_pool::Page, FatPage, PagePool},
Expand Down Expand Up @@ -129,8 +129,7 @@ impl BranchNode {
// This is ensured to be within the bounds of the page by the assertion above.
unsafe {
std::slice::from_raw_parts_mut(
self.page[BRANCH_NODE_HEADER_SIZE..BRANCH_NODE_HEADER_SIZE + 2].as_ptr()
as *mut [u8; 2],
self.page[BRANCH_NODE_HEADER_SIZE..cells_end].as_mut_ptr() as *mut [u8; 2],
self.n().into(),
)
}
Expand Down Expand Up @@ -417,6 +416,16 @@ pub fn get_key(node: &BranchNode, index: usize) -> Key {
reconstruct_key(prefix, node.raw_separator(index))
}

/// The length `bitwise_memcpy` requires of a source slice holding `bit_len` bits that start at
/// bit `bit_start` of its first byte.
fn source_byte_len(bit_start: usize, bit_len: usize) -> usize {
if bit_len == 0 {
0
} else {
((bit_start + bit_len + 7) / 8).next_multiple_of(8)
}
}

pub struct BranchNodeBuilder {
branch: BranchNode,
index: usize,
Expand Down Expand Up @@ -514,21 +523,28 @@ impl BranchNodeBuilder {
base_prev_cell_pointer = base.view().cell(from - 1)
}

for (base_cell, cell) in (&base.view().cells()[from..to])
.iter()
.zip(&mut self.branch.cells_mut()[self.index..self.index + n_items])
{
let prefix_len = self.branch.prefix_len() as usize;
for (base_index, (base_cell, cell)) in (from..to).zip(
(&base.view().cells()[from..to])
.iter()
.zip(&mut self.branch.cells_mut()[self.index..self.index + n_items]),
) {
let base_cell_pointer = u16::from_le_bytes(*base_cell) as usize;
let mut separator_len = base_cell_pointer - base_prev_cell_pointer;
let base_len = base_cell_pointer - base_prev_cell_pointer;
base_prev_cell_pointer = base_cell_pointer;

if is_prefix_extension {
separator_len += bit_prefix_len_difference;
let len = if base_index == 0 && base_len == 0 {
// The first separator of a node can be shorter than the node's prefix, due to
// trailing zero compression, and is then stored with no bits at all. How long it
// is under another prefix can only be told from the key.
separator_len(&get_key(base, 0)).saturating_sub(prefix_len)
} else if is_prefix_extension {
base_len + bit_prefix_len_difference
} else {
separator_len = separator_len.saturating_sub(bit_prefix_len_difference);
}
base_len.saturating_sub(bit_prefix_len_difference)
};

cell_pointer += separator_len as usize;
cell_pointer += len;
cell.copy_from_slice(&u16::try_from(cell_pointer).unwrap().to_le_bytes());
}

Expand Down Expand Up @@ -587,7 +603,7 @@ impl BranchNodeBuilder {
return;
}

let carry_prefix: Option<(usize, usize, usize)> = if is_prefix_extension {
let carry_prefix: Option<(usize, usize)> = if is_prefix_extension {
// bit_prefix_len_difference is the amount of bits that need to be taken from
// the base prefix and added in front of every separator.
// The interested bits are the last bits of the prefix
Expand All @@ -597,10 +613,8 @@ impl BranchNodeBuilder {
let prefix_bit_start = bits_to_skip % 8;

let base_prefix_start = BRANCH_NODE_HEADER_SIZE + base.n() as usize * 2;
let start = base_prefix_start + bytes_to_skip;
let byte_len = (((bit_prefix_len_difference as usize) + 7) / 8).next_multiple_of(8);

Some((start, start + byte_len, prefix_bit_start))
Some((base_prefix_start + bytes_to_skip, prefix_bit_start))
} else {
None
};
Expand All @@ -616,21 +630,24 @@ impl BranchNodeBuilder {

let RawSeparatorsData {
start: mut base_separator_bytes_start,
byte_len: base_separator_bytes_len,
byte_len: mut base_separator_bytes_len,
bit_start: mut base_separator_bit_start,
bit_len: base_separator_bit_len,
} = base.view().raw_separators_data(base_index, base_index + 1);

// copy the prefix in place if this is_prefix_extension
if let Some((base_prefix_start, base_prefix_end, base_prefix_bit_start)) = carry_prefix
{
//
// A first separator shorter than the base's prefix takes only part of those bits.
let carried_bits = bit_prefix_len_difference.min(separator_bit_len);
if let Some((base_prefix_start, base_prefix_bit_start)) = carry_prefix {
let byte_len = source_byte_len(base_prefix_bit_start, carried_bits);
bitwise_memcpy(
&mut self.branch.page
[separator_bytes_start..separator_bytes_start + separator_bytes_len],
separator_bit_start,
&base.page[base_prefix_start..base_prefix_end],
&base.page[base_prefix_start..base_prefix_start + byte_len],
base_prefix_bit_start,
bit_prefix_len_difference,
carried_bits,
);
}

Expand All @@ -652,7 +669,7 @@ impl BranchNodeBuilder {
let bit_len;
if is_prefix_extension {
// carry_prefix is already being applied, so skip bits in the new node's separator
separator_bit_start += bit_prefix_len_difference;
separator_bit_start += carried_bits;
handle_prefix_offset(&mut separator_bit_start, &mut separator_bytes_start);
bit_len = base_separator_bit_len;
} else {
Expand All @@ -663,6 +680,8 @@ impl BranchNodeBuilder {
&mut base_separator_bytes_start,
);
bit_len = separator_bit_len;
// fewer bits are left to copy, from a later byte
base_separator_bytes_len = source_byte_len(base_separator_bit_start, bit_len);
}

bitwise_memcpy(
Expand Down
10 changes: 4 additions & 6 deletions nomt/src/beatree/ops/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ impl AsyncReader {

/// Submit a request over the I/O handle.
///
/// Returns `Some` with an index if a request was submitted. Otherwise, `None`.
/// Returns `Some` with an index if a request was submitted. Otherwise, `None`: either every
/// page has been requested, or the numbers of the remaining pages are in pages that have not
/// been completed yet.
pub fn submit(&mut self, io_handle: &IoHandle, user_data: u64) -> Option<usize> {
if self.is_done_requesting() {
if self.request_index == self.pages.len() {
return None;
}

Expand Down Expand Up @@ -275,10 +277,6 @@ impl AsyncReader {
}
}

fn is_done_requesting(&self) -> bool {
self.request_index == self.total_pages
}

fn is_done(&self) -> bool {
self.process_index == self.total_pages
}
Expand Down
Loading