types.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "errors.hpp"
4 #include <cstddef>
5 #include <functional>
6 #include <limits>
7 #include <memory>
8 #include <string>
9 #include <unordered_map>
10 #include <vector>
11 
12 namespace mcap {
13 
14 #define MCAP_LIBRARY_VERSION "0.1.2"
15 
16 using SchemaId = uint16_t;
17 using ChannelId = uint16_t;
18 using Timestamp = uint64_t;
19 using ByteOffset = uint64_t;
20 using KeyValueMap = std::unordered_map<std::string, std::string>;
21 using ByteArray = std::vector<std::byte>;
22 using ProblemCallback = std::function<void(const Status&)>;
23 
24 constexpr char SpecVersion = '0';
26 constexpr uint8_t Magic[] = {137, 77, 67, 65, 80, SpecVersion, 13, 10}; // "\x89MCAP0\r\n"
27 constexpr uint64_t DefaultChunkSize = 1024 * 768;
28 constexpr ByteOffset EndOffset = std::numeric_limits<ByteOffset>::max();
29 constexpr Timestamp MaxTime = std::numeric_limits<Timestamp>::max();
30 
34 enum struct Compression {
35  None,
36  Lz4,
37  Zstd,
38 };
39 
45 enum struct CompressionLevel {
46  Fastest,
47  Fast,
48  Default,
49  Slow,
50  Slowest,
51 };
52 
56 enum struct OpCode : uint8_t {
57  Header = 0x01,
58  Footer = 0x02,
59  Schema = 0x03,
60  Channel = 0x04,
61  Message = 0x05,
62  Chunk = 0x06,
63  MessageIndex = 0x07,
64  ChunkIndex = 0x08,
65  Attachment = 0x09,
66  AttachmentIndex = 0x0A,
67  Statistics = 0x0B,
68  Metadata = 0x0C,
69  MetadataIndex = 0x0D,
70  SummaryOffset = 0x0E,
71  DataEnd = 0x0F,
72 };
73 
77 constexpr std::string_view OpCodeString(OpCode opcode);
78 
83 struct Record {
85  uint64_t dataSize;
86  std::byte* data;
87 
88  uint64_t recordSize() const {
89  return sizeof(opcode) + sizeof(dataSize) + dataSize;
90  }
91 };
92 
99 struct Header {
100  std::string profile;
101  std::string library;
102 };
103 
111 struct Footer {
114  uint32_t summaryCrc;
115 
116  Footer() = default;
117  Footer(ByteOffset summaryStart, ByteOffset summaryOffsetStart)
118  : summaryStart(summaryStart)
119  , summaryOffsetStart(summaryOffsetStart)
120  , summaryCrc(0) {}
121 };
122 
128 struct Schema {
130  std::string name;
131  std::string encoding;
133 
134  Schema() = default;
135 
136  Schema(const std::string_view name, const std::string_view encoding, const std::string_view data)
137  : name(name)
138  , encoding(encoding)
139  , data{reinterpret_cast<const std::byte*>(data.data()),
140  reinterpret_cast<const std::byte*>(data.data() + data.size())} {}
141 
142  Schema(const std::string_view name, const std::string_view encoding, const ByteArray& data)
143  : name(name)
144  , encoding(encoding)
145  , data{data} {}
146 };
147 
155 struct Channel {
157  std::string topic;
158  std::string messageEncoding;
161 
162  Channel() = default;
163 
164  Channel(const std::string_view topic, const std::string_view messageEncoding, SchemaId schemaId,
165  const KeyValueMap& metadata = {})
166  : topic(topic)
167  , messageEncoding(messageEncoding)
168  , schemaId(schemaId)
169  , metadata(metadata) {}
170 };
171 
172 using SchemaPtr = std::shared_ptr<Schema>;
173 using ChannelPtr = std::shared_ptr<Channel>;
174 
178 struct Message {
184  uint32_t sequence;
198  uint64_t dataSize;
204  const std::byte* data = nullptr;
205 };
206 
211 struct Chunk {
215  uint32_t uncompressedCrc;
216  std::string compression;
218  const std::byte* records = nullptr;
219 };
220 
225 struct MessageIndex {
227  std::vector<std::pair<Timestamp, ByteOffset>> records;
228 };
229 
235 struct ChunkIndex {
240  std::unordered_map<ChannelId, ByteOffset> messageIndexOffsets;
242  std::string compression;
245 };
246 
252 struct Attachment {
255  std::string name;
256  std::string contentType;
257  uint64_t dataSize;
258  const std::byte* data = nullptr;
259  uint32_t crc;
260 };
261 
271  uint64_t dataSize;
272  std::string name;
273  std::string contentType;
274 
275  AttachmentIndex() = default;
276  AttachmentIndex(const Attachment& attachment, ByteOffset fileOffset)
277  : offset(fileOffset)
278  , length(9 +
279  /* name */ 4 + attachment.name.size() +
280  /* log_time */ 8 +
281  /* create_time */ 8 +
282  /* content_type */ 4 + attachment.contentType.size() +
283  /* data */ 8 + attachment.dataSize +
284  /* crc */ 4)
285  , logTime(attachment.logTime)
286  , createTime(attachment.createTime)
287  , dataSize(attachment.dataSize)
288  , name(attachment.name)
289  , contentType(attachment.contentType) {}
290 };
291 
296 struct Statistics {
297  uint64_t messageCount;
298  uint16_t schemaCount;
299  uint32_t channelCount;
300  uint32_t attachmentCount;
301  uint32_t metadataCount;
302  uint32_t chunkCount;
305  std::unordered_map<ChannelId, uint64_t> channelMessageCounts;
306 };
307 
312 struct Metadata {
313  std::string name;
315 };
316 
322  uint64_t offset;
323  uint64_t length;
324  std::string name;
325 
326  MetadataIndex() = default;
327  MetadataIndex(const Metadata& metadata, ByteOffset fileOffset);
328 };
329 
340 };
341 
346 struct DataEnd {
347  uint32_t dataSectionCrc;
348 };
349 
356 struct MessageView {
357  const Message& message;
360 
361  MessageView(const Message& message, const ChannelPtr channel, const SchemaPtr schema)
362  : message(message)
363  , channel(channel)
364  , schema(schema) {}
365 };
366 
367 } // namespace mcap
368 
369 #ifdef MCAP_IMPLEMENTATION
370 #include "types.inl"
371 #endif
Timestamp messageStartTime
Definition: types.hpp:236
uint64_t messageCount
Definition: types.hpp:297
Compression
Supported MCAP compression algorithms.
Definition: types.hpp:34
OpCode opcode
Definition: types.hpp:84
uint64_t offset
Definition: types.hpp:322
uint32_t metadataCount
Definition: types.hpp:301
An Attachment is an arbitrary file embedded in an MCAP file, including a name, content-type, timestamps, and optional CRC. Attachment records are written in the Data section, outside of Chunks.
Definition: types.hpp:252
std::function< void(const Status &)> ProblemCallback
Definition: types.hpp:22
ByteOffset summaryOffsetStart
Definition: types.hpp:113
ByteOffset groupStart
Definition: types.hpp:338
uint16_t SchemaId
Definition: types.hpp:16
Timestamp messageEndTime
Definition: types.hpp:213
Timestamp messageEndTime
Definition: types.hpp:237
std::string contentType
Definition: types.hpp:256
uint32_t summaryCrc
Definition: types.hpp:114
The Statistics record is found in the Summary section, providing counts and timestamp ranges for the ...
Definition: types.hpp:296
constexpr ByteOffset EndOffset
Definition: types.hpp:28
Describes a Channel that messages are written to. A Channel represents a single connection from a pub...
Definition: types.hpp:155
ByteOffset uncompressedSize
Definition: types.hpp:244
AttachmentIndex(const Attachment &attachment, ByteOffset fileOffset)
Definition: types.hpp:276
uint16_t schemaCount
Definition: types.hpp:298
Timestamp messageStartTime
Definition: types.hpp:303
uint16_t ChannelId
Definition: types.hpp:17
Timestamp createTime
Definition: types.hpp:270
uint64_t length
Definition: types.hpp:323
uint64_t dataSize
Definition: types.hpp:257
std::byte * data
Definition: types.hpp:86
uint32_t dataSectionCrc
Definition: types.hpp:347
ByteOffset messageIndexLength
Definition: types.hpp:241
uint32_t uncompressedCrc
Definition: types.hpp:215
uint32_t channelCount
Definition: types.hpp:299
std::unordered_map< ChannelId, uint64_t > channelMessageCounts
Definition: types.hpp:305
Timestamp createTime
Definition: types.hpp:254
std::vector< std::pair< Timestamp, ByteOffset > > records
Definition: types.hpp:227
constexpr Timestamp MaxTime
Definition: types.hpp:29
uint64_t dataSize
Definition: types.hpp:85
std::string topic
Definition: types.hpp:157
ByteOffset chunkLength
Definition: types.hpp:239
std::string compression
Definition: types.hpp:216
ByteArray data
Definition: types.hpp:132
uint64_t Timestamp
Definition: types.hpp:18
ByteOffset groupLength
Definition: types.hpp:339
uint32_t sequence
An optional sequence number. If non-zero, sequence numbers should be unique per channel and increasin...
Definition: types.hpp:184
SchemaId schemaId
Definition: types.hpp:159
An collection of Schemas, Channels, and Messages that supports compression and indexing.
Definition: types.hpp:211
basic_string_view< char > string_view
Definition: core.h:522
uint64_t recordSize() const
Definition: types.hpp:88
Chunk Index records are found in the Summary section, providing summary information for a single Chun...
Definition: types.hpp:235
Metdata Index records are found in the Summary section, providing summary information for a single Me...
Definition: types.hpp:321
Attachment Index records are found in the Summary section, providing summary information for a single...
Definition: types.hpp:266
Returned when iterating over Messages in a file, MessageView contains a reference to one Message...
Definition: types.hpp:356
ChannelId id
Definition: types.hpp:156
const ChannelPtr channel
Definition: types.hpp:358
uint32_t chunkCount
Definition: types.hpp:302
std::string messageEncoding
Definition: types.hpp:158
The final record in an MCAP file (before the trailing magic byte sequence). Contains byte offsets fro...
Definition: types.hpp:111
std::string contentType
Definition: types.hpp:273
#define MCAP_LIBRARY_VERSION
Definition: types.hpp:14
Timestamp logTime
Definition: types.hpp:253
const SchemaPtr schema
Definition: types.hpp:359
Schema(const std::string_view name, const std::string_view encoding, const ByteArray &data)
Definition: types.hpp:142
ByteOffset summaryStart
Definition: types.hpp:112
std::shared_ptr< Schema > SchemaPtr
Definition: types.hpp:172
ByteOffset uncompressedSize
Definition: types.hpp:214
OpCode groupOpCode
Definition: types.hpp:337
Timestamp messageStartTime
Definition: types.hpp:212
uint32_t crc
Definition: types.hpp:259
std::string profile
Definition: types.hpp:100
uint64_t ByteOffset
Definition: types.hpp:19
ChannelId channelId
Definition: types.hpp:226
std::string name
Definition: types.hpp:255
constexpr uint8_t Magic[]
Definition: types.hpp:26
Timestamp logTime
Definition: types.hpp:269
Summary Offset records are found in the Summary Offset section. Records in the Summary section are gr...
Definition: types.hpp:336
std::string library
Definition: types.hpp:101
std::shared_ptr< Channel > ChannelPtr
Definition: types.hpp:173
Schema(const std::string_view name, const std::string_view encoding, const std::string_view data)
Definition: types.hpp:136
Timestamp publishTime
Nanosecond timestamp when this message was initially published. If not available, this should be set ...
Definition: types.hpp:194
The final record in the Data section, signaling the end of Data and beginning of Summary. Optionally contains a CRC of the entire Data section.
Definition: types.hpp:346
constexpr uint64_t DefaultChunkSize
Definition: types.hpp:27
uint32_t attachmentCount
Definition: types.hpp:300
std::string compression
Definition: types.hpp:242
constexpr std::string_view OpCodeString(OpCode opcode)
Get the string representation of an OpCode.
Holds a named map of key/value strings containing arbitrary user data. Metadata records are found in ...
Definition: types.hpp:312
KeyValueMap metadata
Definition: types.hpp:160
KeyValueMap metadata
Definition: types.hpp:314
MessageView(const Message &message, const ChannelPtr channel, const SchemaPtr schema)
Definition: types.hpp:361
ByteOffset chunkStartOffset
Definition: types.hpp:238
OpCode
MCAP record types.
Definition: types.hpp:56
A single Message published to a Channel.
Definition: types.hpp:178
std::unordered_map< ChannelId, ByteOffset > messageIndexOffsets
Definition: types.hpp:240
constexpr char SpecVersion
Definition: types.hpp:24
constexpr char LibraryVersion[]
Definition: types.hpp:25
ChannelId channelId
Definition: types.hpp:179
Appears at the beginning of every MCAP file (after the magic byte sequence) and contains the recordin...
Definition: types.hpp:99
ByteOffset compressedSize
Definition: types.hpp:217
ByteOffset length
Definition: types.hpp:268
Timestamp logTime
Nanosecond timestamp when this message was recorded or received for recording.
Definition: types.hpp:189
const Message & message
Definition: types.hpp:357
Channel(const std::string_view topic, const std::string_view messageEncoding, SchemaId schemaId, const KeyValueMap &metadata={})
Definition: types.hpp:164
std::string name
Definition: types.hpp:313
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1485
CompressionLevel
Compression level to use when compression is enabled. Slower generally produces smaller files...
Definition: types.hpp:45
std::string name
Definition: types.hpp:130
uint64_t dataSize
Size of the message payload in bytes, pointed to via data.
Definition: types.hpp:198
std::string encoding
Definition: types.hpp:131
std::unordered_map< std::string, std::string > KeyValueMap
Definition: types.hpp:20
std::string name
Definition: types.hpp:324
Describes a schema used for message encoding and decoding and/or describing the shape of messages...
Definition: types.hpp:128
A list of timestamps to byte offsets for a single Channel. This record appears after each Chunk...
Definition: types.hpp:225
SchemaId id
Definition: types.hpp:129
Timestamp messageEndTime
Definition: types.hpp:304
std::string name
Definition: types.hpp:272
std::vector< std::byte > ByteArray
Definition: types.hpp:21
ByteOffset compressedSize
Definition: types.hpp:243
Definition: format.h:895
A generic Type-Length-Value record using a uint8 type and uint64 length. This is the generic form of ...
Definition: types.hpp:83
ByteOffset offset
Definition: types.hpp:267
Footer(ByteOffset summaryStart, ByteOffset summaryOffsetStart)
Definition: types.hpp:117


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:12:53