bulk_append/
bulk_append.rs1mod common;
16
17use std::time::Duration;
18
19use scopedb_client::AppendState;
20
21#[tokio::main(flavor = "current_thread")]
22async fn main() -> Result<(), Box<dyn std::error::Error>> {
23 let client = common::client()?;
24 let table = common::write_table(&client)?;
25
26 let stream = table
30 .append_stream()
31 .target_batch_bytes(32 * 1024)
32 .max_batch_rows(10_000)
33 .flush_interval(Duration::from_secs(1))
34 .max_buffered_bytes(1024 * 1024)
35 .max_concurrent_batches(4)
36 .attempt_timeout(Duration::from_secs(30))
37 .build()?;
38
39 let rows = (1..=10_000).map(|id| {
40 serde_json::json!({
41 "id": id,
42 "occurred_at": jiff::Timestamp::now(),
43 "name": format!("example-{id}"),
44 })
45 });
46
47 let admission_error = match stream.send_all(rows).await {
48 Ok(result) => {
49 println!("accepted locally: {} rows", result.accepted_rows);
50 None
51 }
52 Err(error) => {
53 eprintln!("row admission stopped; settling the accepted prefix: {error}");
56 Some(error)
57 }
58 };
59
60 match stream.shutdown().await {
61 Ok(report) => {
62 println!("committed remotely: {} rows", report.committed_rows);
63 println!("final stream stats: {:?}", stream.stats());
64 }
65 Err(error) => {
66 if matches!(
67 error.append_details().map(|details| details.status),
68 Some(AppendState::Unknown)
69 ) {
70 eprintln!("commit outcome is unknown; reconcile before replaying");
71 } else {
72 eprintln!("bulk append failed: {error}");
73 }
74 eprintln!("final stream stats: {:?}", stream.stats());
75 return Err(error.into());
76 }
77 }
78
79 if let Some(error) = admission_error {
80 return Err(error.into());
81 }
82 Ok(())
83}