A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
22#ifndef A11_DATA_TYPES_H_
23#define A11_DATA_TYPES_H_
24
25#include <optional>
26#include <string>
27#include <string_view>
28#include <variant>
29#include <vector>
30
31#include <absl/base/nullability.h>
32#include <absl/container/flat_hash_map.h>
33#include <absl/status/status.h>
34#include <absl/status/statusor.h>
35#include <absl/time/time.h>
36
37namespace a11::data {
38
40using Bytes = std::string;
42using ByteMap = absl::flat_hash_map<std::string, Bytes>;
43
49absl::Status ValidateName(std::string_view name);
50
59 std::string mimetype;
60 std::optional<absl::Time> timestamp{};
62
64 [[nodiscard]] size_t ApproxBytes() const;
66 [[nodiscard]] std::string DebugString() const;
68 absl::Status Validate() const;
70 absl::StatusOr<std::string> GetAttribute(std::string_view key) const;
72 absl::Status SetAttribute(std::string key, std::string value);
73
75 absl::StatusOr<Bytes> ToMsgpack() const;
77 static absl::StatusOr<ChunkMetadata> FromMsgpack(std::string_view bytes);
78
79 friend bool operator==(const ChunkMetadata&, const ChunkMetadata&) = default;
80
81 template <typename Sink>
82 friend void AbslStringify(Sink& sink, const ChunkMetadata& value) {
83 sink.Append(value.DebugString());
84 }
85};
86
95struct Chunk {
96 std::optional<ChunkMetadata> metadata{};
97 std::string ref{};
99
101 [[nodiscard]] size_t ApproxBytes() const;
103 [[nodiscard]] std::string DebugString() const;
105 [[nodiscard]] std::string GetMimetype() const;
107 [[nodiscard]] bool IsEmpty() const;
109 [[nodiscard]] bool IsNull() const;
111 absl::Status Validate() const;
112
114 absl::StatusOr<Bytes> ToMsgpack() const;
116 static absl::StatusOr<Chunk> FromMsgpack(std::string_view bytes);
117
118 friend bool operator==(const Chunk&, const Chunk&) = default;
119
120 template <typename Sink>
121 friend void AbslStringify(Sink& sink, const Chunk& value) {
122 sink.Append(value.DebugString());
123 }
124};
125
132struct NodeRef {
133 std::string id;
134 std::uint32_t offset = 0;
135 // 2^32 is a valid length for a full logical node and therefore requires a
136 // wider representation than offset and sequence numbers.
137 std::optional<std::uint64_t> length;
138
140 [[nodiscard]] size_t ApproxBytes() const;
142 [[nodiscard]] std::string DebugString() const;
144 absl::Status Validate() const;
145
147 absl::StatusOr<Bytes> ToMsgpack() const;
149 static absl::StatusOr<NodeRef> FromMsgpack(std::string_view bytes);
150
151 friend bool operator==(const NodeRef&, const NodeRef&) = default;
152
153 template <typename Sink>
154 friend void AbslStringify(Sink& sink, const NodeRef& value) {
155 sink.Append(value.DebugString());
156 }
157};
158
168 std::string id;
169 std::variant<Chunk, NodeRef> data = Chunk{};
170 std::optional<std::uint32_t> seq;
171 bool continued = false;
172
174 [[nodiscard]] size_t ApproxBytes() const;
176 [[nodiscard]] std::string DebugString() const;
178 absl::Status Validate() const;
180 absl::StatusOr<Chunk* absl_nonnull> GetChunk();
182 absl::StatusOr<const Chunk* absl_nonnull> GetChunk() const;
184 absl::StatusOr<NodeRef* absl_nonnull> GetNodeRef();
186 absl::StatusOr<const NodeRef* absl_nonnull> GetNodeRef() const;
187
189 absl::StatusOr<Bytes> ToMsgpack() const;
191 static absl::StatusOr<NodeFragment> FromMsgpack(std::string_view bytes);
192
193 friend bool operator==(const NodeFragment&, const NodeFragment&) = default;
194
195 template <typename Sink>
196 friend void AbslStringify(Sink& sink, const NodeFragment& value) {
197 sink.Append(value.DebugString());
198 }
199};
200
207struct Port {
208 std::string name;
209 std::string id;
210
212 [[nodiscard]] size_t ApproxBytes() const;
214 [[nodiscard]] std::string DebugString() const;
216 absl::Status Validate() const;
217
219 absl::StatusOr<Bytes> ToMsgpack() const;
221 static absl::StatusOr<Port> FromMsgpack(std::string_view bytes);
222
223 friend bool operator==(const Port&, const Port&) = default;
224
225 template <typename Sink>
226 friend void AbslStringify(Sink& sink, const Port& value) {
227 sink.Append(value.DebugString());
228 }
229};
230
240 std::string id;
241 std::string name;
242 std::vector<Port> inputs{};
243 std::vector<Port> outputs{};
245
247 [[nodiscard]] size_t ApproxBytes() const;
249 [[nodiscard]] std::string DebugString() const;
251 absl::Status Validate() const;
252
254 absl::StatusOr<Bytes> ToMsgpack() const;
256 static absl::StatusOr<ActionMessage> FromMsgpack(std::string_view bytes);
257
258 friend bool operator==(const ActionMessage&, const ActionMessage&) = default;
259
260 template <typename Sink>
261 friend void AbslStringify(Sink& sink, const ActionMessage& value) {
262 sink.Append(value.DebugString());
263 }
264};
265
276 static constexpr std::uint32_t kVersion = 1;
277
278 std::vector<NodeFragment> node_fragments{};
279 std::vector<ActionMessage> actions{};
281
283 [[nodiscard]] size_t ApproxBytes() const;
285 [[nodiscard]] std::string DebugString() const;
287 absl::Status Validate() const;
288
290 absl::StatusOr<Bytes> ToMsgpack() const;
292 static absl::StatusOr<WireMessage> FromMsgpack(std::string_view bytes);
293
294 friend bool operator==(const WireMessage&, const WireMessage&) = default;
295
296 template <typename Sink>
297 friend void AbslStringify(Sink& sink, const WireMessage& value) {
298 sink.Append(value.DebugString());
299 }
300};
301
303bool IsHalfCloseMessage(const WireMessage& message);
305WireMessage MakeHalfCloseMessage(ByteMap trailers = {});
306
313size_t EmptyWireMessageSize();
314
315} // namespace a11::data
316
317#endif // A11_DATA_TYPES_H_
Definition json.cc:22
std::string Bytes
Raw byte payload; an alias of std::string.
Definition types.h:40
size_t EmptyWireMessageSize()
The encoded size of an empty WireMessage.
Definition types.cc:1036
bool IsHalfCloseMessage(const WireMessage &message)
Whether message is a transport half-close signal.
Definition types.cc:1028
absl::Status ValidateName(std::string_view name)
Validates an A11 identifier (node/port/action name).
Definition types.cc:163
WireMessage MakeHalfCloseMessage(ByteMap trailers)
Builds a half-close WireMessage carrying optional trailers.
Definition types.cc:1032
absl::flat_hash_map< std::string, Bytes > ByteMap
String-keyed map of byte values (headers, attributes, etc.).
Definition types.h:42
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30
The wire description of an action invocation.
Definition types.h:239
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:686
std::vector< Port > outputs
Output port -> node bindings.
Definition types.h:243
static absl::StatusOr< ActionMessage > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into an ActionMessage.
Definition types.cc:762
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this message as MessagePack bytes.
Definition types.cc:725
std::string id
Unique id of this action instance.
Definition types.h:240
friend bool operator==(const ActionMessage &, const ActionMessage &)=default
std::vector< Port > inputs
Input port -> node bindings.
Definition types.h:242
absl::Status Validate() const
Validate the invocation id, action name, ports, and headers.
Definition types.cc:694
std::string name
Registered action name.
Definition types.h:241
ByteMap headers
Per-call headers.
Definition types.h:244
size_t ApproxBytes() const
Estimate memory/wire weight for session and transport limits.
Definition types.cc:675
friend void AbslStringify(Sink &sink, const ActionMessage &value)
Definition types.h:261
Descriptive metadata attached to a a11::data::Chunk.
Definition types.h:58
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this metadata as MessagePack bytes.
Definition types.cc:220
std::string mimetype
Media type of the chunk payload.
Definition types.h:59
std::optional< absl::Time > timestamp
Optional creation timestamp.
Definition types.h:60
absl::Status SetAttribute(std::string key, std::string value)
Sets attribute key to value.
Definition types.cc:212
friend void AbslStringify(Sink &sink, const ChunkMetadata &value)
Definition types.h:82
absl::StatusOr< std::string > GetAttribute(std::string_view key) const
Returns attribute key, or a NotFound error when absent.
Definition types.cc:203
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:186
size_t ApproxBytes() const
Estimate memory/wire weight for bounded-buffer accounting.
Definition types.cc:179
absl::Status Validate() const
Validate metadata before it crosses a store or transport boundary.
Definition types.cc:193
ByteMap attributes
Free-form key/value attributes.
Definition types.h:61
static absl::StatusOr< ChunkMetadata > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a ChunkMetadata.
Definition types.cc:239
friend bool operator==(const ChunkMetadata &, const ChunkMetadata &)=default
A unit of data: bytes plus optional descriptive metadata.
Definition types.h:95
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this chunk as MessagePack bytes.
Definition types.cc:314
std::optional< ChunkMetadata > metadata
Optional payload metadata.
Definition types.h:96
bool IsEmpty() const
Whether the chunk carries neither data nor a reference.
Definition types.cc:299
friend bool operator==(const Chunk &, const Chunk &)=default
absl::Status Validate() const
Validate that payload, reference, and metadata fields are consistent.
Definition types.cc:307
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:291
Bytes data
Inline byte payload.
Definition types.h:98
size_t ApproxBytes() const
Estimate memory/wire weight for bounded-buffer accounting.
Definition types.cc:286
friend void AbslStringify(Sink &sink, const Chunk &value)
Definition types.h:121
static absl::StatusOr< Chunk > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a Chunk.
Definition types.cc:338
std::string GetMimetype() const
Returns the metadata mimetype, or empty when unset.
Definition types.cc:295
bool IsNull() const
Whether the chunk represents an explicit null value.
Definition types.cc:303
std::string ref
Node id this chunk references, if not inline.
Definition types.h:97
One piece of a node's stream: an inline chunk or a node reference.
Definition types.h:167
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this fragment as MessagePack bytes.
Definition types.cc:512
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:469
size_t ApproxBytes() const
Estimate memory/wire weight for session and transport limits.
Definition types.cc:463
absl::StatusOr< Chunk *absl_nonnull > GetChunk()
Returns the held Chunk, or an error when it holds a NodeRef.
Definition types.cc:488
friend bool operator==(const NodeFragment &, const NodeFragment &)=default
absl::StatusOr< NodeRef *absl_nonnull > GetNodeRef()
Returns the held NodeRef, or an error when it holds a Chunk.
Definition types.cc:500
friend void AbslStringify(Sink &sink, const NodeFragment &value)
Definition types.h:196
std::variant< Chunk, NodeRef > data
Inline chunk or reference.
Definition types.h:169
std::string id
Id of the node this fragment belongs to.
Definition types.h:168
absl::Status Validate() const
Validate the node id, payload, sequence, and continuation marker.
Definition types.cc:479
bool continued
Whether further fragments follow this one.
Definition types.h:171
std::optional< std::uint32_t > seq
Ordering sequence number.
Definition types.h:170
static absl::StatusOr< NodeFragment > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a NodeFragment.
Definition types.cc:541
A reference to a (slice of a) logical node, in lieu of inline data.
Definition types.h:132
absl::Status Validate() const
Validate the referenced node id and requested slice.
Definition types.cc:391
std::optional< std::uint64_t > length
Optional length of the window.
Definition types.h:137
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:384
friend bool operator==(const NodeRef &, const NodeRef &)=default
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this reference as MessagePack bytes.
Definition types.cc:402
friend void AbslStringify(Sink &sink, const NodeRef &value)
Definition types.h:154
static absl::StatusOr< NodeRef > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a NodeRef.
Definition types.cc:420
std::uint32_t offset
Byte offset into the referenced node.
Definition types.h:134
size_t ApproxBytes() const
Estimate memory/wire weight for bounded-buffer accounting.
Definition types.cc:380
std::string id
Id of the referenced node.
Definition types.h:133
Binds an action's port name to the concrete node id serving it.
Definition types.h:207
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this port as MessagePack bytes.
Definition types.cc:637
size_t ApproxBytes() const
Estimate memory/wire weight for session and transport limits.
Definition types.cc:617
std::string name
Schema-defined port name.
Definition types.h:208
static absl::StatusOr< Port > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a Port.
Definition types.cc:651
friend void AbslStringify(Sink &sink, const Port &value)
Definition types.h:226
friend bool operator==(const Port &, const Port &)=default
absl::Status Validate() const
Validate the port name and node id used to bind an action.
Definition types.cc:627
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:621
std::string id
Id of the node backing this port.
Definition types.h:209
The top-level frame exchanged between two A11 endpoints.
Definition types.h:275
friend void AbslStringify(Sink &sink, const WireMessage &value)
Definition types.h:297
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:843
absl::Status Validate() const
Validate every contained fragment, action, and header.
Definition types.cc:906
ByteMap headers
Message-level headers.
Definition types.h:280
size_t ApproxBytes() const
Estimate memory/wire weight before admitting this message to a buffer.
Definition types.cc:831
std::vector< ActionMessage > actions
Action invocations.
Definition types.h:279
static absl::StatusOr< WireMessage > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a WireMessage.
Definition types.cc:961
static constexpr std::uint32_t kVersion
Wire format version.
Definition types.h:276
friend bool operator==(const WireMessage &, const WireMessage &)=default
std::vector< NodeFragment > node_fragments
Streamed data fragments.
Definition types.h:278
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this message as MessagePack bytes.
Definition types.cc:927