Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #862

Merged
merged 3 commits into from
Sep 7, 2024
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
25 changes: 11 additions & 14 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ impl Database {
FreedPageList::fixed_width(),
mem.clone(),
)?;
mem.mark_pages_allocated(freed_pages_iter, true)?;
for page in freed_pages_iter {
mem.mark_page_allocated(page?);
}
}

let freed_table: ReadOnlyTable<FreedTableKey, FreedPageList<'static>> = ReadOnlyTable::new(
Expand All @@ -530,11 +532,9 @@ impl Database {
)?;
for result in freed_table.range::<FreedTableKey>(..)? {
let (_, freed_page_list) = result?;
let mut freed_page_list_as_vec = vec![];
for i in 0..freed_page_list.value().len() {
freed_page_list_as_vec.push(Ok(freed_page_list.value().get(i)));
mem.mark_page_allocated(freed_page_list.value().get(i));
}
mem.mark_pages_allocated(freed_page_list_as_vec.into_iter(), true)?;
}

Ok(())
Expand Down Expand Up @@ -565,15 +565,13 @@ impl Database {
Ok(())
}

fn mark_tables_recursive(
root: PageNumber,
mem: Arc<TransactionalMemory>,
allow_duplicates: bool,
) -> Result {
fn mark_tables_recursive(root: PageNumber, mem: Arc<TransactionalMemory>) -> Result {
// Repair the allocator state
// All pages in the master table
let master_pages_iter = AllPageNumbersBtreeIter::new(root, None, None, mem.clone())?;
mem.mark_pages_allocated(master_pages_iter, allow_duplicates)?;
for page in master_pages_iter {
mem.mark_page_allocated(page?);
}

// Iterate over all other tables
let iter: BtreeRangeIter<&str, InternalTableDefinition> =
Expand All @@ -583,8 +581,7 @@ impl Database {
for entry in iter {
let definition = entry?.value();
definition.visit_all_pages(mem.clone(), |path| {
// TODO: simplify mark_pages_allocated()
mem.mark_pages_allocated([Ok(path.page_number())].into_iter(), allow_duplicates)?;
mem.mark_page_allocated(path.page_number());
Ok(())
})?;
}
Expand Down Expand Up @@ -626,7 +623,7 @@ impl Database {

let data_root = mem.get_data_root();
if let Some(header) = data_root {
Self::mark_tables_recursive(header.root, mem.clone(), false)?;
Self::mark_tables_recursive(header.root, mem.clone())?;
}

let freed_root = mem.get_freed_root();
Expand All @@ -650,7 +647,7 @@ impl Database {

let system_root = mem.get_system_root();
if let Some(header) = system_root {
Self::mark_tables_recursive(header.root, mem.clone(), false)?;
Self::mark_tables_recursive(header.root, mem.clone())?;
}
#[cfg(debug_assertions)]
{
Expand Down
23 changes: 4 additions & 19 deletions src/tree_store/page_store/page_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,26 +356,11 @@ impl TransactionalMemory {
Ok(())
}

pub(crate) fn mark_pages_allocated(
&self,
allocated_pages: impl Iterator<Item = Result<PageNumber>>,
allow_duplicates: bool,
) -> Result<()> {
pub(crate) fn mark_page_allocated(&self, page_number: PageNumber) {
let mut state = self.state.lock().unwrap();

for page_number in allocated_pages {
let page_number = page_number?;
let region_index = page_number.region;
let allocator = state.get_region_mut(region_index);
if allow_duplicates
&& allocator.is_allocated(page_number.page_index, page_number.page_order)
{
continue;
}
allocator.record_alloc(page_number.page_index, page_number.page_order);
}

Ok(())
let region_index = page_number.region;
let allocator = state.get_region_mut(region_index);
allocator.record_alloc(page_number.page_index, page_number.page_order);
}

fn write_header(&self, header: &DatabaseHeader, swap_primary: bool) -> Result {
Expand Down
Loading