Something went wrong. Try again.
in-pds plc directory archive bundles
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151use anyhow::{Context, Result, bail};use bytes::Bytes;
use crate::{ atproto, bundle, config::{Args, PloxCommand}, db,};
pub struct PreparedBundle { pub after: u64, pub until: u64, pub bundle: bundle::Bundle,}
pub fn prepare_next_bundle( conn: &rusqlite::Connection, bundle_size: u64, after: u64,) -> Result<Option<PreparedBundle>> { let until = after + bundle_size; let ops = db::get_ops_range(conn, after, until)?;
if ops.len() != (until - after) as usize { return Ok(None); }
let bundle = bundle::encode_bundle(&ops)?;
tracing::debug!(size = %bundle.dag_cbor.len(), after, cid = %bundle.cid, "encoded bundle");
Ok(Some(PreparedBundle { after, until, bundle, }))}
pub async fn upload_bundle( client: &mut atproto::Client, prepared: &PreparedBundle, bundle_size: u64,) -> Result<()> { let upload_size = prepared.bundle.dag_cbor.len(); let upload_start = std::time::Instant::now(); let resp = atproto::upload_blob( client, Bytes::from(prepared.bundle.dag_cbor.clone()), "application/cbor", ) .await .with_context(|| format!("uploading blob for bundle after={}", prepared.after))?; let upload_secs = upload_start.elapsed().as_secs_f64(); let mb = upload_size as f64 / 1_000_000.0; tracing::info!( "uploaded {mb:.1}MB in {upload_secs:.1}s ({:.1} MB/s)", mb / upload_secs );
let record = serde_json::json!({ "$type": "blue.cerulea.plox.bundle", "after": prepared.after, "count": prepared.bundle.count, "blob": resp.blob, }); let rkey = format!("{:09}", (prepared.after / bundle_size) + 1);
atproto::create_record(client, "blue.cerulea.plox.bundle", record, &rkey) .await .with_context(|| format!("create_record failed for after={}", prepared.after))?;
tracing::info!( "pushed bundle after={} (seq {}-{}, {} ops) -> {}", prepared.after, prepared.bundle.seq_start, prepared.bundle.seq_end, prepared.bundle.count, prepared.bundle.cid, );
Ok(())}
pub fn finalize_bundle( conn: &rusqlite::Connection, prepared: &PreparedBundle, prune: bool,) -> Result<()> { if prune { let deleted = db::delete_ops_range(conn, prepared.after, prepared.until)?; tracing::debug!( "pruned {deleted} ops (seq {}..={})", prepared.after + 1, prepared.until ); db::set_last_pruned_seq(conn, prepared.until)?; }
db::set_last_pushed_after(conn, prepared.until)?; Ok(())}
pub async fn push_bundles(args: &Args) -> Result<u64> { let PloxCommand::Push { pds_host, handle, app_password, prune, } = &args.command else { bail!("expected Push command"); };
let conn = db::open(&args.db_path)?; let mut last_pushed = db::last_pushed_after(&conn)?;
tracing::info!( handle, pds_host = pds_host.as_deref().unwrap_or("(auto)"), "authenticating" ); let http = reqwest::Client::builder().no_proxy().build()?; let mut client = atproto::Client::new( http, pds_host.as_deref(), &args.plc_url, handle, app_password, ) .await?;
let mut pushed = 0u64;
while let Some(prepared) = prepare_next_bundle(&conn, args.bundle_size, last_pushed)? { upload_bundle(&mut client, &prepared, args.bundle_size).await?; finalize_bundle(&conn, &prepared, *prune)?; last_pushed = prepared.until; pushed += 1; }
if pushed == 0 { let max_seq = db::effective_max_seq(&conn)?.unwrap_or(0); let needed = last_pushed + args.bundle_size; tracing::info!( "no complete bundles ready to push (have up to seq {max_seq}, need {needed})" ); }
Ok(pushed)}