A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
36#ifndef A11_DATA_TYPES_H_
37#define A11_DATA_TYPES_H_
38
39#include <optional>
40#include <string>
41#include <string_view>
42#include <variant>
43#include <vector>
44
45#include <absl/base/nullability.h>
46#include <absl/container/flat_hash_map.h>
47#include <absl/status/status.h>
48#include <absl/status/statusor.h>
49#include <absl/time/time.h>
50
51namespace a11::data {
52
53class MsgpackWriter;
54
56using Bytes = std::string;
58using ByteMap = absl::flat_hash_map<std::string, Bytes>;
59
65absl::Status ValidateName(std::string_view name);
66
75 std::string mimetype = {};
76 std::optional<absl::Time> timestamp{};
78
80 [[nodiscard]] size_t ApproxBytes() const;
82 [[nodiscard]] std::string DebugString() const;
84 absl::Status Validate() const;
86 absl::StatusOr<std::string> GetAttribute(std::string_view key) const;
88 absl::Status SetAttribute(std::string key, std::string value);
89
91 absl::StatusOr<Bytes> ToMsgpack() const;
105 absl::Status ToMsgpackInto(MsgpackWriter* absl_nonnull writer) const;
107 static absl::StatusOr<ChunkMetadata> FromMsgpack(std::string_view bytes);
108
109 friend bool operator==(const ChunkMetadata&, const ChunkMetadata&) = default;
110
111 template <typename Sink>
112 friend void AbslStringify(Sink& sink, const ChunkMetadata& value) {
113 sink.Append(value.DebugString());
114 }
115};
116
147 public:
148 virtual ~ChunkObject() = default;
149
151 [[nodiscard]] virtual std::string_view tag() const = 0;
153 [[nodiscard]] virtual std::string_view mimetype() const = 0;
155 [[nodiscard]] virtual absl::StatusOr<Bytes> Encode() const = 0;
157 [[nodiscard]] virtual size_t ApproxBytes() const = 0;
158
166 [[nodiscard]] virtual const void* absl_nonnull address() const = 0;
167};
168
185struct Chunk {
186 std::optional<ChunkMetadata> metadata{};
187 std::string ref{};
196 std::shared_ptr<const ChunkObject> object{};
197
199 [[nodiscard]] size_t ApproxBytes() const;
201 [[nodiscard]] std::string DebugString() const;
203 [[nodiscard]] std::string GetMimetype() const;
205 [[nodiscard]] bool IsEmpty() const;
207 [[nodiscard]] bool IsNull() const;
209 absl::Status Validate() const;
210
212 [[nodiscard]] bool HasObject() const { return object != nullptr; }
213
224 absl::Status Materialize();
225
227 absl::StatusOr<Bytes> ToMsgpack() const;
241 absl::Status ToMsgpackInto(MsgpackWriter* absl_nonnull writer) const;
243 static absl::StatusOr<Chunk> FromMsgpack(std::string_view bytes);
244
245 friend bool operator==(const Chunk&, const Chunk&) = default;
246
247 template <typename Sink>
248 friend void AbslStringify(Sink& sink, const Chunk& value) {
249 sink.Append(value.DebugString());
250 }
251};
252
254inline constexpr std::string_view kStatusMimetype = "application/x-a11-status";
263inline constexpr std::string_view kCloseAttribute = "a11-close";
264
271absl::StatusOr<Chunk> MakeStatusChunk(const absl::Status& status,
272 bool closing = false);
274bool IsStatusChunk(const Chunk& chunk);
276bool IsCloseStatusChunk(const Chunk& chunk);
278absl::StatusOr<absl::Status> StatusFromStatusChunk(const Chunk& chunk);
279
286struct NodeRef {
287 std::string id;
288 std::uint32_t offset = 0;
289 // 2^32 is a valid length for a full logical node and therefore requires a
290 // wider representation than offset and sequence numbers.
291 std::optional<std::uint64_t> length;
292
294 [[nodiscard]] size_t ApproxBytes() const;
296 [[nodiscard]] std::string DebugString() const;
298 absl::Status Validate() const;
299
301 absl::StatusOr<Bytes> ToMsgpack() const;
315 absl::Status ToMsgpackInto(MsgpackWriter* absl_nonnull writer) const;
317 static absl::StatusOr<NodeRef> FromMsgpack(std::string_view bytes);
318
319 friend bool operator==(const NodeRef&, const NodeRef&) = default;
320
321 template <typename Sink>
322 friend void AbslStringify(Sink& sink, const NodeRef& value) {
323 sink.Append(value.DebugString());
324 }
325};
326
336 std::string id = {};
337 std::variant<Chunk, NodeRef> data = Chunk{};
338 std::optional<std::uint32_t> seq = {};
339 bool continued = false;
340
342 [[nodiscard]] size_t ApproxBytes() const;
344 [[nodiscard]] std::string DebugString() const;
346 absl::Status Validate() const;
348 absl::StatusOr<Chunk* absl_nonnull> GetChunk();
350 absl::StatusOr<const Chunk* absl_nonnull> GetChunk() const;
352 absl::StatusOr<NodeRef* absl_nonnull> GetNodeRef();
354 absl::StatusOr<const NodeRef* absl_nonnull> GetNodeRef() const;
355
357 absl::StatusOr<Bytes> ToMsgpack() const;
371 absl::Status ToMsgpackInto(MsgpackWriter* absl_nonnull writer) const;
373 static absl::StatusOr<NodeFragment> FromMsgpack(std::string_view bytes);
374
375 friend bool operator==(const NodeFragment&, const NodeFragment&) = default;
376
377 template <typename Sink>
378 friend void AbslStringify(Sink& sink, const NodeFragment& value) {
379 sink.Append(value.DebugString());
380 }
381};
382
389struct Port {
390 std::string name;
391 std::string id;
392
394 [[nodiscard]] size_t ApproxBytes() const;
396 [[nodiscard]] std::string DebugString() const;
398 absl::Status Validate() const;
399
401 absl::StatusOr<Bytes> ToMsgpack() const;
415 absl::Status ToMsgpackInto(MsgpackWriter* absl_nonnull writer) const;
417 static absl::StatusOr<Port> FromMsgpack(std::string_view bytes);
418
419 friend bool operator==(const Port&, const Port&) = default;
420
421 template <typename Sink>
422 friend void AbslStringify(Sink& sink, const Port& value) {
423 sink.Append(value.DebugString());
424 }
425};
426
436 std::string id;
437 std::string name;
438 std::vector<Port> inputs{};
439 std::vector<Port> outputs{};
441
443 [[nodiscard]] size_t ApproxBytes() const;
445 [[nodiscard]] std::string DebugString() const;
447 absl::Status Validate() const;
448
450 absl::StatusOr<Bytes> ToMsgpack() const;
464 absl::Status ToMsgpackInto(MsgpackWriter* absl_nonnull writer) const;
466 static absl::StatusOr<ActionMessage> FromMsgpack(std::string_view bytes);
467
468 friend bool operator==(const ActionMessage&, const ActionMessage&) = default;
469
470 template <typename Sink>
471 friend void AbslStringify(Sink& sink, const ActionMessage& value) {
472 sink.Append(value.DebugString());
473 }
474};
475
486 static constexpr std::uint32_t kVersion = 1;
487
488 std::vector<NodeFragment> node_fragments{};
489 std::vector<ActionMessage> actions{};
491
493 [[nodiscard]] size_t ApproxBytes() const;
495 [[nodiscard]] std::string DebugString() const;
497 absl::Status Validate() const;
498
500 absl::StatusOr<Bytes> ToMsgpack() const;
514 absl::Status ToMsgpackInto(MsgpackWriter* absl_nonnull writer) const;
516 static absl::StatusOr<WireMessage> FromMsgpack(std::string_view bytes);
517
518 friend bool operator==(const WireMessage&, const WireMessage&) = default;
519
520 template <typename Sink>
521 friend void AbslStringify(Sink& sink, const WireMessage& value) {
522 sink.Append(value.DebugString());
523 }
524};
525
527bool IsHalfCloseMessage(const WireMessage& message);
529WireMessage MakeHalfCloseMessage(ByteMap trailers = {});
530
537size_t EmptyWireMessageSize();
538
539} // namespace a11::data
540
541#endif // A11_DATA_TYPES_H_
An in-process value a chunk may carry instead of its encoded bytes.
Definition types.h:146
virtual std::string_view mimetype() const =0
The mimetype the value would be encoded as.
virtual const void *absl_nonnull address() const =0
The address of the held value.
virtual absl::StatusOr< Bytes > Encode() const =0
Encodes the value, for whoever needs bytes after all.
virtual size_t ApproxBytes() const =0
A size estimate that does not encode anything.
virtual std::string_view tag() const =0
The serialisation tag of the held type.
virtual ~ChunkObject()=default
Incrementally encode the fields of one A11 wire record.
Definition msgpack.h:44
std::string value
Definition discover.cc:114
std::string key
Definition discover.cc:782
std::string name
The name and its colon, which travel together because they always do.
Definition format.cc:49
Definition json.cc:35
std::string Bytes
Raw byte payload; an alias of std::string.
Definition types.h:56
constexpr std::string_view kCloseAttribute
Metadata attribute marking a status chunk as a closure marker.
Definition types.h:263
absl::StatusOr< Chunk > MakeStatusChunk(const absl::Status &status, bool closing)
Encodes status as a chunk of mimetype kStatusMimetype.
Definition types.cc:418
size_t EmptyWireMessageSize()
The encoded size of an empty WireMessage.
Definition types.cc:996
bool IsHalfCloseMessage(const WireMessage &message)
Whether message is a transport half-close signal.
Definition types.cc:988
absl::Status ValidateName(std::string_view name)
Validates an A11 identifier (node/port/action name).
Definition types.cc:189
bool IsStatusChunk(const Chunk &chunk)
Whether chunk carries a packed status.
Definition types.cc:429
constexpr std::string_view kStatusMimetype
Mimetype marking a chunk whose payload is a packed status.
Definition types.h:254
WireMessage MakeHalfCloseMessage(ByteMap trailers)
Builds a half-close WireMessage carrying optional trailers.
Definition types.cc:992
absl::StatusOr< absl::Status > StatusFromStatusChunk(const Chunk &chunk)
Decodes a status previously encoded by MakeStatusChunk.
Definition types.cc:438
bool IsCloseStatusChunk(const Chunk &chunk)
Whether chunk is a status chunk marking write-half closure.
Definition types.cc:433
absl::flat_hash_map< std::string, Bytes > ByteMap
String-keyed map of byte values (headers, attributes, etc.).
Definition types.h:58
The wire description of an action invocation.
Definition types.h:435
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:713
std::vector< Port > outputs
Output port -> node bindings.
Definition types.h:439
static absl::StatusOr< ActionMessage > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into an ActionMessage.
Definition types.cc:766
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this message as MessagePack bytes.
Definition types.cc:741
std::string id
Unique id of this action instance.
Definition types.h:436
friend bool operator==(const ActionMessage &, const ActionMessage &)=default
std::vector< Port > inputs
Input port -> node bindings.
Definition types.h:438
absl::Status Validate() const
Validate the invocation id, action name, ports, and headers.
Definition types.cc:721
std::string name
Registered action name.
Definition types.h:437
ByteMap headers
Per-call headers.
Definition types.h:440
absl::Status ToMsgpackInto(MsgpackWriter *absl_nonnull writer) const
Append this record's fields to an already-open writer.
Definition types.cc:751
size_t ApproxBytes() const
Estimate memory/wire weight for session and transport limits.
Definition types.cc:699
friend void AbslStringify(Sink &sink, const ActionMessage &value)
Definition types.h:471
Descriptive metadata attached to a a11::data::Chunk.
Definition types.h:74
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this metadata as MessagePack bytes.
Definition types.cc:255
std::string mimetype
Media type of the chunk payload.
Definition types.h:75
std::optional< absl::Time > timestamp
Optional creation timestamp.
Definition types.h:76
absl::Status SetAttribute(std::string key, std::string value)
Sets attribute key to value.
Definition types.cc:249
absl::Status ToMsgpackInto(MsgpackWriter *absl_nonnull writer) const
Append this record's fields to an already-open writer.
Definition types.cc:265
friend void AbslStringify(Sink &sink, const ChunkMetadata &value)
Definition types.h:112
absl::StatusOr< std::string > GetAttribute(std::string_view key) const
Returns attribute key, or a NotFound error when absent.
Definition types.cc:240
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:225
size_t ApproxBytes() const
Estimate memory/wire weight for bounded-buffer accounting.
Definition types.cc:217
absl::Status Validate() const
Validate metadata before it crosses a store or transport boundary.
Definition types.cc:232
ByteMap attributes
Free-form key/value attributes.
Definition types.h:77
static absl::StatusOr< ChunkMetadata > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a ChunkMetadata.
Definition types.cc:277
friend bool operator==(const ChunkMetadata &, const ChunkMetadata &)=default
A unit of data: bytes plus optional descriptive metadata.
Definition types.h:185
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this chunk as MessagePack bytes.
Definition types.cc:366
std::optional< ChunkMetadata > metadata
Optional payload metadata.
Definition types.h:186
bool IsEmpty() const
Whether the chunk carries no data, reference or object.
Definition types.cc:324
friend bool operator==(const Chunk &, const Chunk &)=default
bool HasObject() const
Whether this chunk is carrying a value rather than bytes.
Definition types.h:212
absl::Status Validate() const
Validate that payload, reference, and metadata fields are consistent.
Definition types.cc:335
absl::Status ToMsgpackInto(MsgpackWriter *absl_nonnull writer) const
Append this record's fields to an already-open writer.
Definition types.cc:376
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:316
Bytes data
Inline byte payload.
Definition types.h:188
size_t ApproxBytes() const
Estimate memory/wire weight for bounded-buffer accounting.
Definition types.cc:307
friend void AbslStringify(Sink &sink, const Chunk &value)
Definition types.h:248
static absl::StatusOr< Chunk > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a Chunk.
Definition types.cc:391
std::string GetMimetype() const
Returns the metadata mimetype, or empty when unset.
Definition types.cc:320
bool IsNull() const
Whether the chunk represents an explicit null value.
Definition types.cc:331
absl::Status Materialize()
Produces data from object, if it has not been produced yet.
Definition types.cc:346
std::string ref
Node id this chunk references, if not inline.
Definition types.h:187
One piece of a node's stream: an inline chunk or a node reference.
Definition types.h:335
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this fragment as MessagePack bytes.
Definition types.cc:572
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:527
size_t ApproxBytes() const
Estimate memory/wire weight for session and transport limits.
Definition types.cc:521
absl::StatusOr< Chunk *absl_nonnull > GetChunk()
Returns the held Chunk, or an error when it holds a NodeRef.
Definition types.cc:544
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:558
friend void AbslStringify(Sink &sink, const NodeFragment &value)
Definition types.h:378
std::variant< Chunk, NodeRef > data
Inline chunk or reference.
Definition types.h:337
absl::Status ToMsgpackInto(MsgpackWriter *absl_nonnull writer) const
Append this record's fields to an already-open writer.
Definition types.cc:582
absl::Status Validate() const
Validate the node id, payload, sequence, and continuation marker.
Definition types.cc:537
bool continued
Whether further fragments follow this one.
Definition types.h:339
std::optional< std::uint32_t > seq
Ordering sequence number.
Definition types.h:338
static absl::StatusOr< NodeFragment > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a NodeFragment.
Definition types.cc:601
A reference to a (slice of a) logical node, in lieu of inline data.
Definition types.h:286
absl::Status Validate() const
Validate the referenced node id and requested slice.
Definition types.cc:463
std::optional< std::uint64_t > length
Optional length of the window.
Definition types.h:291
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:456
friend bool operator==(const NodeRef &, const NodeRef &)=default
absl::Status ToMsgpackInto(MsgpackWriter *absl_nonnull writer) const
Append this record's fields to an already-open writer.
Definition types.cc:482
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this reference as MessagePack bytes.
Definition types.cc:472
friend void AbslStringify(Sink &sink, const NodeRef &value)
Definition types.h:322
static absl::StatusOr< NodeRef > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a NodeRef.
Definition types.cc:493
std::uint32_t offset
Byte offset into the referenced node.
Definition types.h:288
size_t ApproxBytes() const
Estimate memory/wire weight for bounded-buffer accounting.
Definition types.cc:452
std::string id
Id of the referenced node.
Definition types.h:287
Binds an action's port name to the concrete node id serving it.
Definition types.h:389
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this port as MessagePack bytes.
Definition types.cc:670
size_t ApproxBytes() const
Estimate memory/wire weight for session and transport limits.
Definition types.cc:653
std::string name
Schema-defined port name.
Definition types.h:390
absl::Status ToMsgpackInto(MsgpackWriter *absl_nonnull writer) const
Append this record's fields to an already-open writer.
Definition types.cc:680
static absl::StatusOr< Port > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a Port.
Definition types.cc:686
friend void AbslStringify(Sink &sink, const Port &value)
Definition types.h:422
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:663
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:657
std::string id
Id of the node backing this port.
Definition types.h:391
The top-level frame exchanged between two A11 endpoints.
Definition types.h:485
friend void AbslStringify(Sink &sink, const WireMessage &value)
Definition types.h:521
std::string DebugString() const
Return a concise representation suitable for logs and diagnostics.
Definition types.cc:824
absl::Status Validate() const
Validate every contained fragment, action, and header.
Definition types.cc:891
ByteMap headers
Message-level headers.
Definition types.h:490
size_t ApproxBytes() const
Estimate memory/wire weight before admitting this message to a buffer.
Definition types.cc:810
absl::Status ToMsgpackInto(MsgpackWriter *absl_nonnull writer) const
Append this record's fields to an already-open writer.
Definition types.cc:915
std::vector< ActionMessage > actions
Action invocations.
Definition types.h:489
static absl::StatusOr< WireMessage > FromMsgpack(std::string_view bytes)
Decodes MessagePack bytes into a WireMessage.
Definition types.cc:934
static constexpr std::uint32_t kVersion
Wire format version.
Definition types.h:486
friend bool operator==(const WireMessage &, const WireMessage &)=default
std::vector< NodeFragment > node_fragments
Streamed data fragments.
Definition types.h:488
absl::StatusOr< Bytes > ToMsgpack() const
Encodes this message as MessagePack bytes.
Definition types.cc:905