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

V1 migration #83

Merged
merged 5 commits into from
Nov 16, 2024
Merged
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
65 changes: 22 additions & 43 deletions src/merk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@

let mut db = rocksdb::DB::open_cf_descriptors(&db_opts, &path_buf, column_families())?;
let format_version = load_format_version(&db)?;
let has_root = load_root(&db)?.is_some();

if has_root {
if has_root(&db)? {
if format_version == 0 {
log::info!("Migrating store from version 0 to {}...", FORMAT_VERSION);

Expand Down Expand Up @@ -305,54 +304,29 @@
}

pub fn migrate_from_v0<P: AsRef<Path>>(path: P) -> Result<()> {
use rocksdb::IteratorMode;

let path = path.as_ref().to_path_buf();
let db =
rocksdb::DB::open_cf_descriptors(&Merk::default_db_opts(), &path, column_families())?;

let create_path = |suffix| {
let mut tmp_path = path.clone();
let tmp_file_name =
format!("{}-{}", path.file_name().unwrap().to_str().unwrap(), suffix);
tmp_path.set_file_name(tmp_file_name);
tmp_path
};
rocksdb::DB::open_cf_descriptors(&Merk::default_db_opts(), path, column_families())?;

Check warning on line 309 in src/merk/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/merk/mod.rs#L309

Added line #L309 was not covered by tests

let tmp_path = create_path("migrate1");
let tmp = Merk::open(&tmp_path)?;
tmp.destroy()?;
let mut iter = db.raw_iterator();
iter.seek_to_first();

Check warning on line 312 in src/merk/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/merk/mod.rs#L311-L312

Added lines #L311 - L312 were not covered by tests

// TODO: split up batch
let batch: Vec<_> = db
.iterator(IteratorMode::Start)
.map(|entry| -> Result<_> {
dbg!();
let (key, node_bytes) = entry.unwrap(); // TODO
dbg!(&key, node_bytes.len());
let node = Tree::decode_v0(&mut &node_bytes[..])?;
dbg!();
Ok((key.to_vec(), Op::Put(node.value().to_vec())))
})
.collect::<Result<_>>()?;
while iter.valid() {
let key = iter.key().unwrap();
let mut value = iter.value().unwrap();

Check warning on line 316 in src/merk/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/merk/mod.rs#L314-L316

Added lines #L314 - L316 were not covered by tests

let aux_cf = db.cf_handle(AUX_CF_NAME).unwrap();
let aux: Vec<_> = db
.iterator_cf(aux_cf, IteratorMode::Start)
.map(|entry| {
let (key, value) = entry.unwrap(); // TODO
(key.to_vec(), Op::Put(value.to_vec()))
})
.collect();
let node = Tree::decode_v0(&mut value)?;
let new_value = node.encode();
db.put(key, new_value.as_slice())?;

Check warning on line 320 in src/merk/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/merk/mod.rs#L318-L320

Added lines #L318 - L320 were not covered by tests

let mut tmp = Self::open(&tmp_path)?;
tmp.apply(&batch, &aux)?;
drop(tmp);
iter.next();

Check warning on line 322 in src/merk/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/merk/mod.rs#L322

Added line #L322 was not covered by tests
}

let tmp_path2 = create_path("migrate2");
std::fs::rename(&path, &tmp_path2)?;
std::fs::rename(&tmp_path, &path)?;
std::fs::remove_dir_all(&tmp_path2)?;
db.put_cf(
db.cf_handle(INTERNAL_CF_NAME).unwrap(),
FORMAT_VERSION_KEY,
FORMAT_VERSION.to_be_bytes(),
)?;

Check warning on line 329 in src/merk/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/merk/mod.rs#L325-L329

Added lines #L325 - L329 were not covered by tests

Ok(())
}
Expand Down Expand Up @@ -578,6 +552,11 @@
Ok(bytes)
}

fn has_root(db: &DB) -> Result<bool> {
let internal_cf = db.cf_handle(INTERNAL_CF_NAME).unwrap();
Ok(db.get_pinned_cf(internal_cf, ROOT_KEY_KEY)?.is_some())
}

fn load_root(db: &DB) -> Result<Option<Tree>> {
let internal_cf = db.cf_handle(INTERNAL_CF_NAME).unwrap();
db.get_pinned_cf(internal_cf, ROOT_KEY_KEY)?
Expand Down
Loading