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

feat: config the max levels in memory #64

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
16 changes: 0 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,6 @@ jobs:
command: fmt
args: --all -- --check

clippy:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing the clippy check?

runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Use Nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
components: clippy
override: true
- name: Check
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features -- -D warnings

benches:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@ full = ["rand",
"ed"]
verify = ["ed",
"failure"]

[dev-dependencies]
tempdir = "0.3.7"
30 changes: 24 additions & 6 deletions src/merk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Merk {
pub(crate) tree: Cell<Option<Tree>>,
pub(crate) db: rocksdb::DB,
pub(crate) path: PathBuf,
max_levels_in_memory: u8,
}

pub type UseTreeMutResult = Result<Vec<(Vec<u8>, Option<Vec<u8>>)>>;
Expand All @@ -38,12 +39,12 @@ impl Merk {
/// path, one will be created.
pub fn open<P: AsRef<Path>>(path: P) -> Result<Merk> {
let db_opts = Merk::default_db_opts();
Merk::open_opt(path, db_opts)
Merk::open_opt(path, db_opts, 100)
}

/// Opens a store with the specified file path and the given options. If no
/// store exists at that path, one will be created.
pub fn open_opt<P>(path: P, db_opts: rocksdb::Options) -> Result<Merk>
pub fn open_opt<P>(path: P, db_opts: rocksdb::Options, levels: u8) -> Result<Merk>
where
P: AsRef<Path>,
{
Expand All @@ -55,6 +56,7 @@ impl Merk {
tree: Cell::new(None),
db,
path: path_buf,
max_levels_in_memory: levels,
};
merk.load_root()?;

Expand All @@ -81,6 +83,11 @@ impl Merk {
opts
}

#[inline]
pub fn get_max_levels_in_memory(&self) -> u8 {
self.max_levels_in_memory
}

/// Gets an auxiliary value.
pub fn get_aux(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
let aux_cf = self.db.cf_handle(AUX_CF_NAME);
Expand Down Expand Up @@ -329,8 +336,7 @@ impl Merk {
let mut to_batch = self.use_tree_mut(|maybe_tree| -> UseTreeMutResult {
// TODO: concurrent commit
if let Some(tree) = maybe_tree {
// TODO: configurable committer
let mut committer = MerkCommitter::new(tree.height(), 100);
let mut committer = MerkCommitter::new(tree.height(), self.max_levels_in_memory);
tree.commit(&mut committer)?;

// update pointer to root node
Expand Down Expand Up @@ -505,7 +511,7 @@ mod test {
use crate::test_utils::*;
use crate::Op;
use std::thread;

use tempdir::TempDir;
// TODO: Close and then reopen test

fn assert_invariants(merk: &TempMerk) {
Expand All @@ -515,13 +521,25 @@ mod test {
})
}

#[test]
fn test_configure_the_levels() {
let tmp_dir = TempDir::new("example").unwrap();
let path = tmp_dir.path().to_owned();
let opts = Merk::default_db_opts();
if let Ok(merk) = Merk::open_opt(path, opts, 20) {
assert_eq!(20, merk.get_max_levels_in_memory());
merk.destroy().unwrap();
} else {
assert!(false);
}
}

#[test]
fn simple_insert_apply() {
let batch_size = 20;

let path = thread::current().name().unwrap().to_owned();
let mut merk = TempMerk::open(path).expect("failed to open merk");

let batch = make_batch_seq(0..batch_size);
merk.apply(&batch, &[]).expect("apply failed");

Expand Down
2 changes: 1 addition & 1 deletion src/tree/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ where
.remove()?
.map(|w| w.maybe_balance())
.transpose()?;

return Ok((maybe_walker, deleted_keys));
}
}
Expand Down