arcadedb-protocol 0.4.0

A lightweight gRPC client for ArcadeDB: tonic stubs, a typed record-decode path (RecordDecode derive), and a serde_json bridge.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Explicit transactions — raw begin/commit/rollback, the [`Transaction`]
//! drop-guard, the [`TxCommands`] attempt handle, and the retrying
//! [`ArcadeDbClient::run_transaction`] runner.

use super::*;

impl ArcadeDbClient {
    /// Begin an explicit server-side transaction.
    ///
    /// Returns a transaction id; pass it to [`execute_in_transaction`](Self::execute_in_transaction)
    /// and [`query_in_transaction`](Self::query_in_transaction), then finish with
    /// [`commit_transaction`](Self::commit_transaction) or [`rollback_transaction`](Self::rollback_transaction).
    ///
    /// NOTE: ArcadeDB keeps schema changes (DDL) outside transaction scope —
    /// they commit immediately in their own sub-transaction. Use the explicit
    /// transaction for *record* operations (the copy phase of an override
    /// rebuild) and verify the copy before committing; do not rely on it to
    /// make batch DDL atomic.
    pub async fn begin_transaction(
        &self,
        database: &str,
        isolation: TransactionIsolation,
    ) -> Result<String> {
        let req = tonic::Request::new(BeginTransactionRequest {
            database: database.into(),
            credentials: Some(self.auth.creds.clone()),
            isolation: isolation as i32,
        });

        let resp = self
            .data
            .clone()
            .begin_transaction(self.add_auth_headers(req))
            .await
            .map_err(|e| map_rpc_error("begin transaction", "", e))?
            .into_inner();

        tracing::info!(tx = %resp.transaction_id, "transaction begun");
        Ok(resp.transaction_id)
    }

    /// Execute a command inside an explicitly-begun transaction.
    ///
    /// The command runs with the transaction attached; it is only durable when
    /// the transaction is later committed. (Schema statements still commit
    /// immediately — see [`begin_transaction`](Self::begin_transaction).)
    pub async fn execute_in_transaction(
        &self,
        database: &str,
        language: &str,
        command: &str,
        transaction_id: &str,
    ) -> Result<ExecuteCommandResponse> {
        let req = tonic::Request::new(ExecuteCommandRequest {
            database: database.into(),
            command: command.into(),
            credentials: Some(self.auth.creds.clone()),
            language: language.into(),
            return_rows: true,
            transaction: Some(TransactionContext {
                transaction_id: transaction_id.into(),
                ..Default::default()
            }),
            ..Default::default()
        });

        let resp = self
            .data
            .clone()
            .execute_command(self.add_auth_headers(req))
            .await
            .map_err(|e| map_rpc_error("execute command (in tx)", command, e))?
            .into_inner();

        if !resp.success {
            return Err(command_failed(
                "execute command (in tx)",
                resp.message,
                resp.affected_records,
            ));
        }

        Ok(resp)
    }

    /// Run a read query *inside* an explicitly-begun transaction, so the
    /// caller can verify what the transaction has written before committing.
    pub async fn query_in_transaction(
        &self,
        database: &str,
        query: &str,
        transaction_id: &str,
    ) -> Result<QueryResult> {
        let req = tonic::Request::new(ExecuteQueryRequest {
            database: database.into(),
            query: query.into(),
            credentials: Some(self.auth.creds.clone()),
            language: "sql".into(),
            transaction: Some(TransactionContext {
                transaction_id: transaction_id.into(),
                ..Default::default()
            }),
            ..Default::default()
        });

        let resp = self
            .data
            .clone()
            .execute_query(self.add_auth_headers(req))
            .await
            .map_err(|e| map_rpc_error("query (in tx)", query, e))?
            .into_inner();

        let mut records = Vec::with_capacity(resp.results.iter().map(|qr| qr.records.len()).sum());
        records.extend(resp.results.into_iter().flat_map(|qr| qr.records));

        Ok(QueryResult {
            records,
            execution_time_ms: resp.execution_time_ms,
        })
    }

    /// Commit an explicitly-begun transaction, making its record operations durable.
    pub async fn commit_transaction(&self, database: &str, transaction_id: &str) -> Result<()> {
        let req = tonic::Request::new(CommitTransactionRequest {
            transaction: Some(TransactionContext {
                transaction_id: transaction_id.into(),
                database: database.into(),
                ..Default::default()
            }),
            credentials: Some(self.auth.creds.clone()),
        });

        let resp = self
            .data
            .clone()
            .commit_transaction(self.add_auth_headers(req))
            .await
            .map_err(|e| map_rpc_error("commit transaction", transaction_id, e))?
            .into_inner();

        tracing::info!(tx = %transaction_id, ok = resp.success, committed = resp.committed, "transaction committed");
        if !resp.success {
            return Err(ArcadeDbError::CommandFailed {
                operation: "commit transaction".into(),
                message: resp.message,
                affected: None,
            });
        }
        Ok(())
    }

    /// Roll back an explicitly-begun transaction, discarding its record operations.
    pub async fn rollback_transaction(&self, database: &str, transaction_id: &str) -> Result<()> {
        let req = tonic::Request::new(RollbackTransactionRequest {
            transaction: Some(TransactionContext {
                transaction_id: transaction_id.into(),
                database: database.into(),
                ..Default::default()
            }),
            credentials: Some(self.auth.creds.clone()),
        });

        let resp = self
            .data
            .clone()
            .rollback_transaction(self.add_auth_headers(req))
            .await
            .map_err(|e| map_rpc_error("rollback transaction", transaction_id, e))?
            .into_inner();

        tracing::info!(tx = %transaction_id, ok = resp.success, rolled_back = resp.rolled_back, "transaction rolled back");
        if !resp.success {
            return Err(ArcadeDbError::CommandFailed {
                operation: "rollback transaction".into(),
                message: resp.message,
                affected: None,
            });
        }
        Ok(())
    }

    /// Begin an explicit server-side transaction as a guard ([`Transaction`]).
    ///
    /// The guard owns the transaction lifecycle: run commands with
    /// [`Transaction::execute`] / [`Transaction::query`], then finish with
    /// [`Transaction::commit`] or [`Transaction::rollback`]. Dropping the
    /// guard without finishing fires a best-effort background rollback (the
    /// server's idle-reaper is the backstop), so forgotten transactions
    /// cannot leak.
    pub async fn transaction(
        &self,
        database: &str,
        isolation: TransactionIsolation,
    ) -> Result<Transaction<'_>> {
        let id = self.begin_transaction(database, isolation).await?;
        Ok(Transaction {
            client: self,
            database: database.into(),
            id,
            finished: false,
        })
    }

    /// Run `body` inside an explicit transaction, retrying the whole block
    /// up to `retries` extra times when the server reports an MVCC conflict
    /// (concurrent modification) — the Java API's
    /// `transaction(txBlock, retries)`. A fresh transaction is begun per
    /// attempt; every failed attempt is rolled back before retrying.
    ///
    /// Conflict detection is best-effort message matching (the gRPC surface
    /// exposes no structured conflict code yet): `concurrent`, `mvcc`, or
    /// `needretry` in the server message. Non-conflict errors and exhausted
    /// retries return the last error, transaction rolled back.
    ///
    /// DDL is outside transaction scope (see
    /// [`begin_transaction`](Self::begin_transaction)) — record operations
    /// only. Pairs with the [`Transaction`](crate::Transaction) drop-guard:
    /// the guard makes multi-statement units atomic, this makes them
    /// self-healing under concurrent writers.
    pub async fn run_transaction<T, F, Fut>(
        &self,
        database: &str,
        isolation: TransactionIsolation,
        retries: u32,
        body: F,
    ) -> Result<T>
    where
        F: Fn(TxCommands) -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        let attempts = retries.saturating_add(1);
        let mut last_err: Option<ArcadeDbError> = None;
        for attempt in 0..attempts {
            let tx_id = self.begin_transaction(database, isolation).await?;
            let commands = TxCommands::new(self, database, &tx_id);
            match body(commands).await {
                Ok(value) => match self.commit_transaction(database, &tx_id).await {
                    Ok(()) => return Ok(value),
                    Err(e) => {
                        let _ = self.rollback_transaction(database, &tx_id).await;
                        if is_retryable_conflict(&e) && attempt + 1 < attempts {
                            tracing::warn!(attempt, "transaction commit conflict — retrying");
                            last_err = Some(e);
                            continue;
                        }
                        return Err(e);
                    }
                },
                Err(e) => {
                    let _ = self.rollback_transaction(database, &tx_id).await;
                    if is_retryable_conflict(&e) && attempt + 1 < attempts {
                        tracing::warn!(attempt, "transaction body conflict — retrying");
                        last_err = Some(e);
                        continue;
                    }
                    return Err(e);
                }
            }
        }
        Err(last_err.unwrap_or_else(|| ArcadeDbError::InvalidInput {
            message: "run_transaction: retries exhausted without an error".into(),
        }))
    }
}

/// Commands issued inside [`ArcadeDbClient::run_transaction`]'s current
/// attempt — the [`Transaction`] guard's operations without the lifecycle
/// (begin/commit/rollback are the runner's job).
///
/// Owns its data (client handle is a cheap `Arc` clone, plus two small
/// `String`s) so the [`run_transaction`](ArcadeDbClient::run_transaction)
/// body can be a plain `|tx| async move { … }` closure — a borrowed
/// `TxCommands<'_>` would make the closure's future tie to the argument's
/// lifetime, which stable Rust's closure generics cannot express (no HRTB
/// over futures). Owned state also means every retry attempt gets a fresh
/// set of commands, which is exactly the retry semantics.
pub struct TxCommands {
    client: ArcadeDbClient,
    database: String,
    tx_id: String,
}

impl TxCommands {
    pub(crate) fn new(client: &ArcadeDbClient, database: &str, tx_id: &str) -> Self {
        Self {
            client: client.clone(),
            database: database.to_string(),
            tx_id: tx_id.to_string(),
        }
    }

    /// Run a command in the current transaction attempt.
    pub async fn execute(&self, language: &str, command: &str) -> Result<ExecuteCommandResponse> {
        self.client
            .execute_in_transaction(&self.database, language, command, &self.tx_id)
            .await
    }

    /// Run a parameterized SQL command in the current transaction attempt.
    pub async fn execute_with_values(
        &self,
        command: &str,
        parameters: impl Into<crate::encode::Params>,
    ) -> Result<ExecuteCommandResponse> {
        // `execute_in_transaction` has no parameter path — inline its request.
        let parameters = parameters.into();
        let req = tonic::Request::new(ExecuteCommandRequest {
            database: self.database.clone(),
            command: command.into(),
            parameters: parameters.0,
            credentials: Some(self.client.creds()),
            language: "sql".into(),
            return_rows: true,
            transaction: Some(TransactionContext {
                transaction_id: self.tx_id.clone(),
                ..Default::default()
            }),
            ..Default::default()
        });
        let resp = self
            .client
            .data
            .clone()
            .execute_command(self.client.add_auth_headers(req))
            .await
            .map_err(|e| map_rpc_error("execute command (in tx)", command, e))?
            .into_inner();
        if !resp.success {
            return Err(command_failed(
                "execute command (in tx)",
                resp.message,
                resp.affected_records,
            ));
        }
        Ok(resp)
    }

    /// Run a SQL read in the current transaction attempt.
    pub async fn query(&self, query: &str) -> Result<QueryResult> {
        self.client
            .query_in_transaction(&self.database, query, &self.tx_id)
            .await
    }
}

/// Best-effort MVCC-conflict detection from the server message (no
/// structured gRPC code yet — see
/// [`run_transaction`](ArcadeDbClient::run_transaction)).
pub(crate) fn is_retryable_conflict(err: &ArcadeDbError) -> bool {
    err.server_message()
        .map(|m| {
            let m = m.to_ascii_lowercase();
            m.contains("concurrent") || m.contains("mvcc") || m.contains("needretry")
        })
        .unwrap_or(false)
}

/// A drop-safe explicit transaction: the guard returned by
/// [`ArcadeDbClient::transaction`]. Commands run through the guard; finishing
/// is explicit (`commit`/`rollback` both consume `self`), and a guard dropped
/// unfinished rolls back in a background task — the server's idle-reaper is
/// the last line of defense.
///
/// NOTE (mirrors [`ArcadeDbClient::begin_transaction`]): ArcadeDB keeps DDL
/// outside transaction scope — use the guard for *record* operations only.
pub struct Transaction<'a> {
    client: &'a ArcadeDbClient,
    database: String,
    id: String,
    finished: bool,
}

impl Transaction<'_> {
    /// The server-assigned transaction id.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Run a command inside the transaction.
    pub async fn execute(&self, language: &str, command: &str) -> Result<ExecuteCommandResponse> {
        self.client
            .execute_in_transaction(&self.database, language, command, &self.id)
            .await
    }

    /// Run a SQL read inside the transaction.
    pub async fn query(&self, query: &str) -> Result<QueryResult> {
        self.client
            .query_in_transaction(&self.database, query, &self.id)
            .await
    }

    /// Commit, making every record operation durable. Consumes the guard.
    pub async fn commit(mut self) -> Result<()> {
        self.finished = true;
        self.client
            .commit_transaction(&self.database, &self.id)
            .await
    }

    /// Roll back, discarding every record operation. Consumes the guard.
    pub async fn rollback(mut self) -> Result<()> {
        self.finished = true;
        self.client
            .rollback_transaction(&self.database, &self.id)
            .await
    }
}

impl Drop for Transaction<'_> {
    fn drop(&mut self) {
        if self.finished {
            return;
        }
        tracing::warn!(tx = %self.id, db = %self.database, "transaction dropped unfinished — rolling back");
        // Best-effort async rollback: requires a live runtime (the caller is
        // async by construction); outside one, the server reaper reclaims it.
        let client = self.client.clone();
        let database = std::mem::take(&mut self.database);
        let id = std::mem::take(&mut self.id);
        let _ = tokio::runtime::Handle::try_current().map(|rt| {
            rt.spawn(async move {
                if let Err(e) = client.rollback_transaction(&database, &id).await {
                    tracing::error!(tx = %id, "drop rollback failed: {e}");
                }
            });
        });
    }
}