Skip to main content

laser_wire/
batch.rs

1use serde::{Deserialize, Serialize};
2
3/// The mixed-operation batch request
4/// ([`AGDX_BATCH_CODE`](crate::codes::AGDX_BATCH_CODE)): up to
5/// [`MAX_BATCH_OPS`](crate::limits::MAX_BATCH_OPS) managed requests in one
6/// round trip, each carrying its own command code and encoded payload. The
7/// batch amortizes the round trip and nothing else: items execute one after
8/// another in input order, so a later item sees an earlier one's effect, each
9/// yields its own result, and a failed item fails alone (explicitly NOT a
10/// transaction). A nested batch (an item whose code is the batch code) is
11/// rejected.
12#[derive(Clone, Debug, Serialize, Deserialize)]
13#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
14pub struct BatchRequest {
15    /// Op version, [`BATCH_OP_VERSION`](crate::codes::BATCH_OP_VERSION).
16    pub v: u32,
17    /// The managed requests, executed in order.
18    pub ops: Vec<BatchItem>,
19}
20
21/// One managed request inside a [`BatchRequest`]: the command code it would
22/// have been sent under on its own, and its encoded request bytes verbatim.
23#[derive(Clone, Debug, Serialize, Deserialize)]
24#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
25pub struct BatchItem {
26    /// The managed command code (e.g. `AGDX_KV_GET_CODE`).
27    pub code: u32,
28    /// The op's own encoded request frame, exactly what a standalone send
29    /// would carry.
30    #[cfg_attr(
31        feature = "schemars",
32        schemars(schema_with = "crate::encoding::bytes_schema")
33    )]
34    #[serde(with = "crate::encoding::bin_bytes")]
35    pub payload: Vec<u8>,
36}
37
38/// The batch reply: each op's own reply bytes, in request order, exactly what
39/// a standalone round trip would have returned (including a typed error
40/// reply for an item that failed, so the caller decodes each slot with the
41/// item's own reply type).
42#[derive(Clone, Debug, Serialize, Deserialize)]
43#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
44pub struct BatchReply {
45    /// Per-op reply frames, index-aligned with the request's `ops`.
46    #[cfg_attr(
47        feature = "schemars",
48        schemars(schema_with = "crate::encoding::vec_bytes_schema")
49    )]
50    #[serde(with = "crate::encoding::vec_bin_bytes")]
51    pub results: Vec<Vec<u8>>,
52}
53
54#[cfg(all(test, feature = "cbor"))]
55mod tests {
56    use super::*;
57    use crate::framing::{decode_named, encode_named};
58
59    #[test]
60    fn given_a_batch_when_round_tripped_then_should_preserve_items_and_results() {
61        let request = BatchRequest {
62            v: crate::codes::BATCH_OP_VERSION,
63            ops: vec![
64                BatchItem {
65                    code: crate::codes::AGDX_KV_GET_CODE,
66                    payload: b"\xa1av\x01".to_vec(),
67                },
68                BatchItem {
69                    code: crate::codes::AGDX_QUERY_CODE,
70                    payload: Vec::new(),
71                },
72            ],
73        };
74        let decoded: BatchRequest =
75            decode_named(&encode_named(&request).expect("encodes")).expect("decodes");
76        assert_eq!(decoded.ops.len(), 2);
77        assert_eq!(decoded.ops[0].code, crate::codes::AGDX_KV_GET_CODE);
78        assert_eq!(decoded.ops[0].payload, request.ops[0].payload);
79
80        let reply = BatchReply {
81            results: vec![b"ok".to_vec(), Vec::new()],
82        };
83        let decoded: BatchReply =
84            decode_named(&encode_named(&reply).expect("encodes")).expect("decodes");
85        assert_eq!(decoded.results, reply.results);
86    }
87}