summaryrefslogtreecommitdiffstats
path: root/adenosine-pds/src/bin/adenosine-pds-import.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2022-10-29 01:17:11 -0700
committerBryan Newbold <bnewbold@robocracy.org>2022-10-29 01:17:11 -0700
commitba8f10afd51a6f629291abdcfe25a70b7b517f1c (patch)
treefd541df5e50c6710d655dc66f15e0012bf4a7846 /adenosine-pds/src/bin/adenosine-pds-import.rs
parentc86ae10560bdafbfc06fc9fac50626630cf6a8a4 (diff)
downloadadenosine-ba8f10afd51a6f629291abdcfe25a70b7b517f1c.tar.gz
adenosine-ba8f10afd51a6f629291abdcfe25a70b7b517f1c.zip
PDS: crude progress on MST parsing/serialization
Diffstat (limited to 'adenosine-pds/src/bin/adenosine-pds-import.rs')
-rw-r--r--adenosine-pds/src/bin/adenosine-pds-import.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/adenosine-pds/src/bin/adenosine-pds-import.rs b/adenosine-pds/src/bin/adenosine-pds-import.rs
new file mode 100644
index 0000000..37ea6eb
--- /dev/null
+++ b/adenosine-pds/src/bin/adenosine-pds-import.rs
@@ -0,0 +1,53 @@
+/// Helper program to import an IPLD CARv1 file in to sqlite data store
+use anyhow::{anyhow, Result};
+use futures::TryStreamExt;
+use ipfs_sqlite_block_store::BlockStore;
+use iroh_car::CarReader;
+use libipld::block::Block;
+use tokio::fs::File;
+use tokio::io::BufReader;
+
+use std::env;
+
+async fn load_car_to_sqlite(db_path: &str, car_path: &str) -> Result<()> {
+ let car_reader = {
+ let file = File::open(car_path).await?;
+ let buf_reader = BufReader::new(file);
+ CarReader::new(buf_reader).await?
+ };
+ let car_header = car_reader.header().clone();
+ let mut db: BlockStore<libipld::DefaultParams> = {
+ let path = std::path::PathBuf::from(db_path);
+ let path = ipfs_sqlite_block_store::DbPath::File(path);
+ BlockStore::open_path(path, Default::default())?
+ };
+
+ car_reader
+ .stream()
+ .try_for_each(|(cid, raw)| {
+ // TODO: error handling here instead of unwrap (?)
+ let block = Block::new(cid, raw).unwrap();
+ db.put_block(block, None).unwrap();
+ futures::future::ready(Ok(()))
+ })
+ .await?;
+
+ // pin the header
+ if car_header.roots().len() >= 1 {
+ db.alias(b"import".to_vec(), Some(&car_header.roots()[0]))?;
+ }
+
+ Ok(())
+}
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let args: Vec<String> = env::args().collect();
+ if args.len() != 3 {
+ println!("expected 2 args: <db_path> <car_path>");
+ std::process::exit(-1);
+ }
+ let db_path = &args[1];
+ let car_path = &args[2];
+ load_car_to_sqlite(db_path, car_path).await
+}