Skip to content

Commit

Permalink
Refactor buffer.rs for better readability and maintainability (#287)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Pringle <[email protected]>
  • Loading branch information
gubatron and richardpringle authored Sep 28, 2023
1 parent 44850d1 commit 1c6016d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 79 deletions.
77 changes: 37 additions & 40 deletions firewood/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,49 +418,46 @@ impl Db {
disk_buffer.run()
}));

let root_hash_cache = Arc::new(
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(cfg.root_hash_ncached_pages)
.ncached_files(cfg.root_hash_ncached_files)
.space_id(ROOT_HASH_SPACE)
.file_nbit(params.root_hash_file_nbit)
.rootdir(root_hash_path)
.build(),
disk_requester.clone(),
)
.unwrap(),
);
let root_hash_cache: Arc<CachedSpace> = CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(cfg.root_hash_ncached_pages)
.ncached_files(cfg.root_hash_ncached_files)
.space_id(ROOT_HASH_SPACE)
.file_nbit(params.root_hash_file_nbit)
.rootdir(root_hash_path)
.build(),
disk_requester.clone(),
)
.unwrap()
.into();

// setup disk buffer
let data_cache = Universe {
merkle: SubUniverse::new(
Arc::new(
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(cfg.meta_ncached_pages)
.ncached_files(cfg.meta_ncached_files)
.space_id(MERKLE_META_SPACE)
.file_nbit(params.meta_file_nbit)
.rootdir(merkle_meta_path)
.build(),
disk_requester.clone(),
)
.unwrap(),
),
Arc::new(
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(cfg.payload_ncached_pages)
.ncached_files(cfg.payload_ncached_files)
.space_id(MERKLE_PAYLOAD_SPACE)
.file_nbit(params.payload_file_nbit)
.rootdir(merkle_payload_path)
.build(),
disk_requester.clone(),
)
.unwrap(),
),
merkle: SubUniverse::<Arc<CachedSpace>>::new(
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(cfg.meta_ncached_pages)
.ncached_files(cfg.meta_ncached_files)
.space_id(MERKLE_META_SPACE)
.file_nbit(params.meta_file_nbit)
.rootdir(merkle_meta_path)
.build(),
disk_requester.clone(),
)
.unwrap()
.into(),
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(cfg.payload_ncached_pages)
.ncached_files(cfg.payload_ncached_files)
.space_id(MERKLE_PAYLOAD_SPACE)
.file_nbit(params.payload_file_nbit)
.rootdir(merkle_payload_path)
.build(),
disk_requester.clone(),
)
.unwrap()
.into(),
),
};

Expand Down
71 changes: 32 additions & 39 deletions firewood/src/storage/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,24 @@ mod tests {
.join("firewood")
}

fn new_cached_space_for_test(
state_path: PathBuf,
disk_requester: DiskBufferRequester,
) -> Arc<CachedSpace> {
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(1)
.ncached_files(1)
.space_id(STATE_SPACE)
.file_nbit(1)
.rootdir(state_path)
.build(),
disk_requester,
)
.unwrap()
.into()
}

#[test]
#[ignore = "ref: https://github.com/ava-labs/firewood/issues/45"]
fn test_buffer_with_undo() {
Expand All @@ -662,19 +680,7 @@ mod tests {
disk_requester.init_wal("wal", &root_db_path);

// create a new state cache which tracks on disk state.
let state_cache = Arc::new(
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(1)
.ncached_files(1)
.space_id(STATE_SPACE)
.file_nbit(1)
.rootdir(state_path)
.build(),
disk_requester.clone(),
)
.unwrap(),
);
let state_cache = new_cached_space_for_test(state_path, disk_requester.clone());

// add an in memory cached space. this will allow us to write to the
// disk buffer then later persist the change to disk.
Expand Down Expand Up @@ -750,19 +756,7 @@ mod tests {
disk_requester.init_wal("wal", &root_db_path);

// create a new state cache which tracks on disk state.
let state_cache = Arc::new(
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(1)
.ncached_files(1)
.space_id(STATE_SPACE)
.file_nbit(1)
.rootdir(state_path)
.build(),
disk_requester.clone(),
)
.unwrap(),
);
let state_cache = new_cached_space_for_test(state_path, disk_requester.clone());

// add an in memory cached space. this will allow us to write to the
// disk buffer then later persist the change to disk.
Expand Down Expand Up @@ -835,19 +829,18 @@ mod tests {
disk_requester.init_wal("wal", &root_db_path);

// create a new state cache which tracks on disk state.
let state_cache = Arc::new(
CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(1)
.ncached_files(1)
.space_id(STATE_SPACE)
.file_nbit(1)
.rootdir(state_path)
.build(),
disk_requester.clone(),
)
.unwrap(),
);
let state_cache: Arc<CachedSpace> = CachedSpace::new(
&StoreConfig::builder()
.ncached_pages(1)
.ncached_files(1)
.space_id(STATE_SPACE)
.file_nbit(1)
.rootdir(state_path)
.build(),
disk_requester.clone(),
)
.unwrap()
.into();

// add an in memory cached space. this will allow us to write to the
// disk buffer then later persist the change to disk.
Expand Down

0 comments on commit 1c6016d

Please sign in to comment.