-
Notifications
You must be signed in to change notification settings - Fork 188
feat: forest-tool db import
#6161
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
Changes from 3 commits
725317f
9bf03ba
7ae2f6e
d4c8ce4
0c8c24c
cd6f004
f32a746
46cf6a0
d46d557
0b1e490
35dd565
d92390f
2ab52ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,14 @@ use std::path::PathBuf; | |
|
|
||
| use crate::cli::subcommands::prompt_confirm; | ||
| use crate::cli_shared::{chain_path, read_config}; | ||
| use crate::db::db_engine::db_root; | ||
| use crate::db::BlockstoreWithWriteBuffer; | ||
| use crate::db::db_engine::{db_root, open_db}; | ||
| use crate::networks::NetworkChain; | ||
| use crate::utils::db::car_stream::CarStream; | ||
| use clap::Subcommand; | ||
| use fvm_ipld_blockstore::Blockstore; | ||
| use indicatif::{ProgressBar, ProgressStyle}; | ||
| use tokio_stream::StreamExt; | ||
| use tracing::error; | ||
|
|
||
| #[derive(Debug, Subcommand)] | ||
|
|
@@ -33,13 +38,28 @@ pub enum DBCommands { | |
| #[arg(long)] | ||
| chain: Option<NetworkChain>, | ||
| }, | ||
| /// Import CAR files into the key-value store | ||
| Import { | ||
| /// Snapshot input paths. Supports `.car`, `.car.zst`, and `.forest.car.zst`. | ||
| #[arg(num_args = 1.., required = true)] | ||
| snapshot_files: Vec<PathBuf>, | ||
| /// Filecoin network chain | ||
| #[arg(long, required = true)] | ||
| chain: NetworkChain, | ||
| /// Optional path to the database folder that powers a Forest node | ||
| #[arg(long)] | ||
| db: Option<PathBuf>, | ||
| /// No block validation | ||
| #[arg(long)] | ||
| no_validation: bool, | ||
| }, | ||
| } | ||
|
|
||
| impl DBCommands { | ||
| pub async fn run(&self) -> anyhow::Result<()> { | ||
| pub async fn run(self) -> anyhow::Result<()> { | ||
| match self { | ||
| Self::Stats { config, chain } => { | ||
| use human_repr::HumanCount; | ||
| use human_repr::HumanCount as _; | ||
|
|
||
| let (_, config) = read_config(config.as_ref(), chain.clone())?; | ||
|
|
||
|
|
@@ -80,6 +100,49 @@ impl DBCommands { | |
| } | ||
| } | ||
| } | ||
| Self::Import { | ||
| snapshot_files, | ||
| chain, | ||
| db, | ||
| no_validation, | ||
| } => { | ||
| const DB_WRITE_BUFFER_CAPACITY: usize = 10000; | ||
|
|
||
| let db_root = if let Some(db) = db { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| db | ||
| } else { | ||
| let (_, config) = read_config(None, Some(chain.clone()))?; | ||
| db_root(&chain_path(&config))? | ||
| }; | ||
| println!("Opening parity-db at {}", db_root.display()); | ||
| let db_writer = BlockstoreWithWriteBuffer::new_with_capacity( | ||
| open_db(db_root, &Default::default())?, | ||
| DB_WRITE_BUFFER_CAPACITY, | ||
| ); | ||
|
|
||
| let pb = ProgressBar::new_spinner().with_style( | ||
| ProgressStyle::with_template("{spinner} {msg}") | ||
| .expect("indicatif template must be valid"), | ||
| ); | ||
| pb.enable_steady_tick(std::time::Duration::from_millis(100)); | ||
|
|
||
| let mut total = 0; | ||
| for snap in snapshot_files { | ||
| let mut car = CarStream::new_from_path(&snap).await?; | ||
| while let Some(b) = car.try_next().await? { | ||
| if !no_validation { | ||
| b.validate()?; | ||
| } | ||
| db_writer.put_keyed(&b.cid, &b.data)?; | ||
| total += 1; | ||
| let text = format!("{total} blocks imported"); | ||
| pb.set_message(text); | ||
| } | ||
| } | ||
| drop(db_writer); | ||
| pb.finish(); | ||
| Ok(()) | ||
|
hanabi1224 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: will
skip_validationbe more appropriate here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated