Skip to content

Commit

Permalink
Resolve TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Sep 7, 2024
1 parent d3bae13 commit 8a611be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
15 changes: 8 additions & 7 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, false)?;
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(), false)?;
}

Ok(())
Expand Down Expand Up @@ -569,7 +569,9 @@ impl Database {
// 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, false)?;
for page in master_pages_iter {
mem.mark_page_allocated(page?);
}

// Iterate over all other tables
let iter: BtreeRangeIter<&str, InternalTableDefinition> =
Expand All @@ -579,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(), false)?;
mem.mark_page_allocated(path.page_number());
Ok(())
})?;
}
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

0 comments on commit 8a611be

Please sign in to comment.