Skip to content

Commit

Permalink
chore: refactor meta creation
Browse files Browse the repository at this point in the history
the goal of this refactor is to  avoid duplicating
meta write logic. Most of the time, when adding a field
to meta, you need to update it here as well. Not anymore.

also cleans up the store module as well.
  • Loading branch information
pepyakin authored and rphmeier committed Dec 6, 2024
1 parent 7833cd8 commit e9d9838
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
18 changes: 18 additions & 0 deletions nomt/src/store/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ pub struct Meta {
}

impl Meta {
/// Returns a newly initialized [`Meta`] instance with the given bitbox seed and number of
/// pages.
pub fn create_new(bitbox_seed: [u8; 16], bitbox_num_pages: u32) -> Self {
Self {
magic: MAGIC,
version: VERSION,
ln_freelist_pn: 0,
ln_bump: 1,
bbn_freelist_pn: 0,
bbn_bump: 1,
sync_seqn: 0,
bitbox_num_pages,
bitbox_seed,
rollback_start_live: 0,
rollback_end_live: 0,
}
}

pub fn encode_to(&self, buf: &mut [u8]) {
assert!(buf.len() >= META_SIZE);
buf[0..4].copy_from_slice(&self.magic);
Expand Down
27 changes: 5 additions & 22 deletions nomt/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Store {
/// Open the store with the provided `Options`.
pub fn open(o: &crate::Options, page_pool: PagePool) -> anyhow::Result<Self> {
if !o.path.exists() {
create(o)?;
create(&page_pool, &o)?;
}

let db_dir_fd = {
Expand Down Expand Up @@ -308,30 +308,13 @@ impl MerkleTransaction {
}
}

fn create(o: &crate::Options) -> anyhow::Result<()> {
use std::io::Write as _;

fn create(page_pool: &PagePool, o: &crate::Options) -> anyhow::Result<()> {
// Create the directory and its parent directories.
std::fs::create_dir_all(&o.path)?;

let mut meta_fd = std::fs::File::create(o.path.join("meta"))?;
let mut buf = [0u8; 4096];
Meta {
magic: meta::MAGIC,
version: meta::VERSION,
ln_freelist_pn: 0,
ln_bump: 1,
bbn_freelist_pn: 0,
bbn_bump: 1,
sync_seqn: 0,
bitbox_num_pages: o.bitbox_num_pages,
bitbox_seed: o.bitbox_seed,
rollback_start_live: 0,
rollback_end_live: 0,
}
.encode_to(&mut buf[0..meta::META_SIZE]);
meta_fd.write_all(&buf)?;
meta_fd.sync_all()?;
let meta_fd = std::fs::File::create(o.path.join("meta"))?;
let meta = Meta::create_new(o.bitbox_seed, o.bitbox_num_pages);
Meta::write(page_pool, &meta_fd, &meta)?;
drop(meta_fd);

bitbox::create(o.path.clone(), o.bitbox_num_pages, o.preallocate_ht)?;
Expand Down

0 comments on commit e9d9838

Please sign in to comment.