Skip to content

Commit

Permalink
Buffer write for db-dump. Change ref to ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
grarco committed Aug 11, 2023
1 parent 808d6bf commit 6ede9fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/src/lib/node/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub fn dump_db(
let db_path = config.shell.db_dir(&chain_id);

let db = storage::PersistentDB::open(db_path, None);
db.dump_block(out_file_path, historic, &mut block_height.clone());
db.dump_block(out_file_path, historic, block_height);
}

/// Roll Namada state back to the previous height
Expand Down
18 changes: 10 additions & 8 deletions apps/src/lib/node/ledger/storage/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
//! - `header`: block's header
use std::fs::File;
use std::io::BufWriter;
use std::path::Path;
use std::str::FromStr;
use std::sync::Mutex;
Expand Down Expand Up @@ -257,7 +258,7 @@ impl RocksDB {
&self,
out_file_path: std::path::PathBuf,
historic: bool,
height: &mut Option<BlockHeight>,
height: Option<BlockHeight>,
) {
// Find the last block height
let state_cf = self
Expand All @@ -272,7 +273,7 @@ impl RocksDB {
)
.expect("Unable to decode block height");

let height = height.get_or_insert(last_height);
let height = height.unwrap_or(last_height);

let full_path = out_file_path
.with_file_name(format!(
Expand Down Expand Up @@ -311,18 +312,17 @@ impl RocksDB {
}

// subspace
if *height != last_height {
if height != last_height {
// Restoring subspace at specified height

let restored_subspace = self
.iter_prefix(&Key::default())
.iter_prefix(None)
.par_bridge()
.fold(
|| "".to_string(),
|mut cur, (key, _value, _gas)| match self
.read_subspace_val_with_height(
&Key::from(key.to_db_key()),
*height,
height,
last_height,
)
.expect("Unable to find subspace key")
Expand Down Expand Up @@ -374,16 +374,18 @@ impl RocksDB {
self.0.iterator_cf_opt(cf, read_opts, IteratorMode::Start)
};

let mut buf = BufWriter::new(file);
for (key, raw_val, _gas) in PersistentPrefixIterator(
PrefixIterator::new(iter, String::default()),
// Empty string to prevent prefix stripping, the prefix is
// already in the enclosed iterator
) {
let val = HEXLOWER.encode(&raw_val);
let bytes = format!("\"{key}\" = \"{val}\"\n");
file.write_all(bytes.as_bytes())
.expect("Unable to write to output file");
buf.write_all(bytes.as_bytes())
.expect("Unable to write to buffer");
}
buf.flush().expect("Unable to write to output file");
}

/// Rollback to previous block. Given the inner working of tendermint
Expand Down

0 comments on commit 6ede9fa

Please sign in to comment.