Skip to content

Commit

Permalink
Remove swap_primary, which is no longer needed
Browse files Browse the repository at this point in the history
Now that commit() has its own in-memory copy of the database header,
there's no need for the special-case swap_primary flag
  • Loading branch information
mconst committed Nov 9, 2024
1 parent 007d887 commit 6a1cd05
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
9 changes: 1 addition & 8 deletions src/tree_store/page_store/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,12 @@ impl DatabaseHeader {
Ok((result, repair))
}

pub(super) fn to_bytes(
&self,
include_magic_number: bool,
swap_primary: bool,
) -> [u8; DB_HEADER_SIZE] {
pub(super) fn to_bytes(&self, include_magic_number: bool) -> [u8; DB_HEADER_SIZE] {
let mut result = [0; DB_HEADER_SIZE];
if include_magic_number {
result[..MAGICNUMBER.len()].copy_from_slice(&MAGICNUMBER);
}
result[GOD_BYTE_OFFSET] = self.primary_slot.try_into().unwrap();
if swap_primary {
result[GOD_BYTE_OFFSET] ^= PRIMARY_BIT;
}
if self.recovery_required {
result[GOD_BYTE_OFFSET] |= RECOVERY_REQUIRED;
}
Expand Down
31 changes: 16 additions & 15 deletions src/tree_store/page_store/page_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl TransactionalMemory {
storage
.write(0, DB_HEADER_SIZE, true)?
.mem_mut()
.copy_from_slice(&header.to_bytes(false, false));
.copy_from_slice(&header.to_bytes(false));
allocators.flush_to(tracker_page, layout, &storage)?;

storage.flush(false)?;
Expand All @@ -201,7 +201,7 @@ impl TransactionalMemory {
storage
.write(0, DB_HEADER_SIZE, true)?
.mem_mut()
.copy_from_slice(&header.to_bytes(true, false));
.copy_from_slice(&header.to_bytes(true));
storage.flush(false)?;
}
let header_bytes = storage.read_direct(0, DB_HEADER_SIZE)?;
Expand Down Expand Up @@ -237,7 +237,7 @@ impl TransactionalMemory {
storage
.write(0, DB_HEADER_SIZE, true)?
.mem_mut()
.copy_from_slice(&header.to_bytes(true, false));
.copy_from_slice(&header.to_bytes(true));
storage.flush(false)?;
}

Expand Down Expand Up @@ -316,7 +316,7 @@ impl TransactionalMemory {
self.storage
.write(0, DB_HEADER_SIZE, true)?
.mem_mut()
.copy_from_slice(&header.to_bytes(true, false));
.copy_from_slice(&header.to_bytes(true));
self.storage.flush(false)?;
}

Expand All @@ -331,7 +331,7 @@ impl TransactionalMemory {
let mut state = self.state.lock().unwrap();
assert!(!state.header.recovery_required);
state.header.recovery_required = true;
self.write_header(&state.header, false)?;
self.write_header(&state.header)?;
self.storage.flush(false)
}

Expand Down Expand Up @@ -367,11 +367,11 @@ impl TransactionalMemory {
allocator.record_alloc(page_number.page_index, page_number.page_order);
}

fn write_header(&self, header: &DatabaseHeader, swap_primary: bool) -> Result {
fn write_header(&self, header: &DatabaseHeader) -> Result {
self.storage
.write(0, DB_HEADER_SIZE, true)?
.mem_mut()
.copy_from_slice(&header.to_bytes(true, swap_primary));
.copy_from_slice(&header.to_bytes(true));

Ok(())
}
Expand All @@ -391,7 +391,7 @@ impl TransactionalMemory {

let mut state = self.state.lock().unwrap();
state.header.set_region_tracker(new_tracker_page);
self.write_header(&state.header, false)?;
self.write_header(&state.header)?;
self.storage.flush(false)?;
} else {
allocator.record_alloc(tracker_page.page_index, tracker_page.page_order);
Expand All @@ -405,7 +405,7 @@ impl TransactionalMemory {
.flush_to(tracker_page, state.header.layout(), &self.storage)?;

state.header.recovery_required = false;
self.write_header(&state.header, false)?;
self.write_header(&state.header)?;
let result = self.storage.flush(false);
self.needs_recovery.store(false, Ordering::Release);

Expand Down Expand Up @@ -519,19 +519,20 @@ impl TransactionalMemory {
secondary.system_root = system_root;
secondary.freed_root = freed_root;

self.write_header(&header, false)?;
self.write_header(&header)?;

// Use 2-phase commit, if checksums are disabled
if two_phase {
self.storage.flush(eventual)?;
}

// Swap the primary bit on-disk
self.write_header(&header, true)?;
self.storage.flush(eventual)?;
// Only swap the in-memory primary bit after the fsync is successful
// Make our new commit the primary
header.swap_primary_slot();

// Write the new header to disk
self.write_header(&header)?;
self.storage.flush(eventual)?;

if shrunk {
let result = self.storage.resize(header.layout().len());
if result.is_err() {
Expand Down Expand Up @@ -1058,7 +1059,7 @@ impl Drop for TransactionalMemory {

if self.storage.flush(false).is_ok() && !self.needs_recovery.load(Ordering::Acquire) {
state.header.recovery_required = false;
let _ = self.write_header(&state.header, false);
let _ = self.write_header(&state.header);
let _ = self.storage.flush(false);
}
}
Expand Down

0 comments on commit 6a1cd05

Please sign in to comment.