From 81911ae3998f760578a0fb2fe303f23fda1ab188 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Thu, 14 Nov 2024 21:09:42 -0600 Subject: [PATCH 1/3] Check for root key in a format version-agnostic way --- src/merk/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/merk/mod.rs b/src/merk/mod.rs index bfc6cec..0f737b9 100644 --- a/src/merk/mod.rs +++ b/src/merk/mod.rs @@ -86,9 +86,8 @@ impl Merk { 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); @@ -578,6 +577,11 @@ where Ok(bytes) } +fn has_root(db: &DB) -> Result { + 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> { let internal_cf = db.cf_handle(INTERNAL_CF_NAME).unwrap(); db.get_pinned_cf(internal_cf, ROOT_KEY_KEY)? From d30c427663cd8be99963ae9ee196b86f538a50a5 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Thu, 14 Nov 2024 21:10:00 -0600 Subject: [PATCH 2/3] Change migration to use raw iterator and store operations --- src/merk/mod.rs | 55 ++++++++++++++----------------------------------- 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/src/merk/mod.rs b/src/merk/mod.rs index 0f737b9..e1deb13 100644 --- a/src/merk/mod.rs +++ b/src/merk/mod.rs @@ -304,54 +304,29 @@ impl Merk { } pub fn migrate_from_v0>(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 - }; + let mut iter = db.raw_iterator(); + iter.seek_to_first(); - let tmp_path = create_path("migrate1"); - let tmp = Merk::open(&tmp_path)?; - tmp.destroy()?; + while iter.valid() { + let key = iter.key().unwrap(); + let mut value = iter.value().unwrap(); - // 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::>()?; + let node = Tree::decode_v0(&mut value)?; + let new_value = node.encode(); + db.put(key, new_value.as_slice())?; - 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 mut tmp = Self::open(&tmp_path)?; - tmp.apply(&batch, &aux)?; - drop(tmp); + iter.next(); + } - 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(), + )?; Ok(()) } From d975c8336aec3d2bc9f30402555dd3153efc4aef Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 15 Nov 2024 18:34:37 -0600 Subject: [PATCH 3/3] Clippy fix --- src/merk/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/merk/mod.rs b/src/merk/mod.rs index e1deb13..88834b2 100644 --- a/src/merk/mod.rs +++ b/src/merk/mod.rs @@ -306,7 +306,7 @@ impl Merk { pub fn migrate_from_v0>(path: P) -> Result<()> { let path = path.as_ref().to_path_buf(); let db = - rocksdb::DB::open_cf_descriptors(&Merk::default_db_opts(), &path, column_families())?; + rocksdb::DB::open_cf_descriptors(&Merk::default_db_opts(), path, column_families())?; let mut iter = db.raw_iterator(); iter.seek_to_first();