append/append.rs
1// Copyright 2024 ScopeDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod common;
16
17use scopedb_client::AppendState;
18use scopedb_client::ErrorKind;
19
20#[tokio::main(flavor = "current_thread")]
21async fn main() -> Result<(), Box<dyn std::error::Error>> {
22 let client = common::client()?;
23 let table = common::write_table(&client)?;
24
25 // Direct append sends exactly one caller-owned NDJSON request. Each value
26 // is serialized as one line; JSON arrays are not accepted by this API.
27 let ndjson = [
28 serde_json::json!({
29 "id": 1,
30 "occurred_at": jiff::Timestamp::now(),
31 "name": "first",
32 }),
33 serde_json::json!({
34 "id": 2,
35 "occurred_at": jiff::Timestamp::now(),
36 "name": "second",
37 }),
38 ]
39 .iter()
40 .map(serde_json::to_string)
41 .collect::<Result<Vec<_>, _>>()?
42 .join("\n");
43
44 match table.append(ndjson).await {
45 Ok(result) => {
46 println!("committed remotely: {} rows", result.num_rows_inserted);
47 Ok(())
48 }
49 Err(error) => {
50 if error.kind() == ErrorKind::AppendRowsFailed
51 && matches!(
52 error.append_details().map(|details| details.status),
53 Some(AppendState::Unknown)
54 )
55 {
56 eprintln!("commit outcome is unknown; reconcile before replaying this payload");
57 }
58 Err(error.into())
59 }
60 }
61}