VRS
A file format for sensor data.
Loading...
Searching...
No Matches
Record.h
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
17#pragma once
18
19#include <memory>
20#include <vector>
21
22#include <vrs/ForwardDefinitions.h>
23#include <vrs/StreamId.h>
24#include <vrs/VrsExport.h>
25#include <vrs/helpers/EnumTemplates.hpp>
26
27namespace vrs {
28
29using std::unique_ptr;
30using std::vector;
31
32class DataSource;
33class DirectWriteRecordData;
34class Compressor;
35class RecordManager;
36
38enum class CompressionType : uint8_t {
39 None = 0,
40 Lz4,
41 Zstd,
42
43 COUNT
44};
45
82class VRS_API Record final {
83 public:
85 static const double kMaxTimestamp;
86
91 enum class Type : uint8_t {
92 UNDEFINED = 0,
93 STATE = 1,
94 CONFIGURATION = 2,
95 DATA = 3,
96 TAGS = 4,
97 COUNT
98 };
99
102 void recycle();
103
106 void set(
107 double timestamp,
108 Type type,
109 uint32_t formatVersion,
110 const DataSource& data,
111 uint64_t creationOrder);
112
114 void addDirectWriteRecordData(unique_ptr<DirectWriteRecordData>&& directWriteRecordData);
115
119 bool shouldTryToCompress() const;
120
124 uint32_t compressRecord(Compressor& compressor);
125
128 int writeRecord(
129 WriteFileHandler& file,
130 StreamId streamId,
131 uint32_t& inOutRecordSize,
132 Compressor& compressor,
133 uint32_t compressedSize);
134
136 double getTimestamp() const {
137 return timestamp_;
138 }
139
140 uint64_t getCreationOrder() const {
141 return creationOrder_;
142 }
143
145 size_t getSize() const {
146 return usedBufferSize_;
147 }
148
151 return recordType_;
152 }
153
155 static const char* typeName(Type type);
156
158 struct uninitialized_byte final {
159 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, modernize-use-equals-default)
160 uninitialized_byte() {} // do not use '= default' as it will initialize byte!
161 uint8_t byte;
162 };
163
164 private:
165 friend class RecordManager;
166
168 explicit Record(RecordManager& recordManager);
169 ~Record();
170
171 double timestamp_{};
172 Type recordType_{};
173 uint32_t formatVersion_{};
174 vector<uninitialized_byte> buffer_;
175 size_t usedBufferSize_{};
176 uint64_t creationOrder_{};
177 unique_ptr<DirectWriteRecordData> directWriteRecordData_;
178
179 RecordManager& recordManager_;
180};
181
183VRS_API string toString(Record::Type recordType);
184
186template <>
187VRS_API Record::Type toEnum(const string& name);
188
189} // namespace vrs
Helper class to compress data using lz4 or zstd presets.
Definition Compressor.h:83
A class referencing data to be captured in a record at creation.
Definition DataSource.h:153
Essential VRS class holding a record's details and payload in memory during creation.
Definition Record.h:82
Type getRecordType() const
Get the record's record type.
Definition Record.h:150
Type
Definition Record.h:91
double getTimestamp() const
Get the record's timestamp.
Definition Record.h:136
static const double kMaxTimestamp
Maximum timestamp for a record.
Definition Record.h:85
size_t getSize() const
Get the record's payload size, uncompressed.
Definition Record.h:145
VRS internal class to manage the records of a specific Recordable after their creation.
Definition RecordManager.h:39
VRS stream identifier class.
Definition StreamId.h:251
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:43
Definition Compressor.cpp:113
@ None
No compression.
@ UNDEFINED
Unknown/unspecified.
CompressionType
Type of compression. Used in VRS record headers, so never modify the values.
Definition Record.h:38
@ Zstd
zstd compression.
@ Lz4
lz4 compression.
Public for testing.
Definition Record.h:158