mod common;
use std::time::Duration;
use scopedb_client::AppendState;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = common::client()?;
let table = common::write_table(&client)?;
let stream = table
.append_stream()
.batch_bytes(32 * 1024)
.flush_interval(Duration::from_secs(1))
.max_pending_bytes(1024 * 1024)
.max_in_flight_requests(4)
.attempt_timeout(Duration::from_secs(30))
.build()?;
let rows = (1..=10_000).map(|id| {
serde_json::json!({
"id": id,
"occurred_at": jiff::Timestamp::now(),
"name": format!("example-{id}"),
})
});
let admission_error = match stream.send_all(rows).await {
Ok(result) => {
println!("accepted locally: {} rows", result.accepted_rows);
None
}
Err(error) => {
eprintln!("row admission stopped; settling the accepted prefix: {error}");
Some(error)
}
};
match stream.shutdown().await {
Ok(report) => {
println!("committed remotely: {} rows", report.committed_rows);
println!("final stream stats: {:?}", stream.stats());
}
Err(error) => {
if matches!(
error.append_details().map(|details| details.append_state),
Some(AppendState::Unknown)
) {
eprintln!("commit outcome is unknown; reconcile before replaying");
} else {
eprintln!("bulk append failed: {error}");
}
eprintln!("final stream stats: {:?}", stream.stats());
return Err(error.into());
}
}
if let Some(error) = admission_error {
return Err(error.into());
}
Ok(())
}