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/helpers/EnumTemplates.hpp>
25
26namespace vrs {
27
28class DataSource;
29class DirectWriteRecordData;
30class Compressor;
31class RecordManager;
32
34enum class CompressionType : uint8_t {
35 None = 0,
36 Lz4,
37 Zstd,
38
39 COUNT
40};
41
78class Record final {
79 public:
81 static const double kMaxTimestamp;
82
87 enum class Type : uint8_t {
88 UNDEFINED = 0,
89 STATE = 1,
90 CONFIGURATION = 2,
91 DATA = 3,
92 TAGS = 4,
93 COUNT
94 };
95
98 void recycle();
99
102 void set(
103 double timestamp,
104 Type type,
105 uint32_t formatVersion,
106 const DataSource& data,
107 uint64_t creationOrder);
108
110 void addDirectWriteRecordData(std::unique_ptr<DirectWriteRecordData>&& directWriteRecordData);
111
115 bool shouldTryToCompress() const;
116
120 uint32_t compressRecord(Compressor& compressor);
121
124 int writeRecord(
125 WriteFileHandler& file,
126 StreamId streamId,
127 uint32_t& inOutRecordSize,
128 Compressor& compressor,
129 uint32_t compressedSize);
130
132 double getTimestamp() const {
133 return timestamp_;
134 }
135
136 uint64_t getCreationOrder() const {
137 return creationOrder_;
138 }
139
141 size_t getSize() const {
142 return usedBufferSize_;
143 }
144
147 return recordType_;
148 }
149
151 static const char* typeName(Type type);
152
154 struct uninitialized_byte final {
155 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, modernize-use-equals-default)
156 uninitialized_byte() {} // do not use '= default' as it will initialize byte!
157 uint8_t byte;
158 };
159
160 private:
161 friend class RecordManager;
162
164 explicit Record(RecordManager& recordManager);
165 ~Record();
166
167 double timestamp_{};
168 Type recordType_{};
169 uint32_t formatVersion_{};
170 std::vector<uninitialized_byte> buffer_;
171 size_t usedBufferSize_{};
172 uint64_t creationOrder_{};
173 std::unique_ptr<DirectWriteRecordData> directWriteRecordData_;
174
175 RecordManager& recordManager_;
176};
177
179string toString(Record::Type recordType);
180
182template <>
183Record::Type toEnum(const string& name);
184
185} // namespace vrs
Helper class to compress data using lz4 or zstd presets.
Definition Compressor.h:82
A class referencing data to be captured in a record at creation.
Definition DataSource.h:152
Essential VRS class holding a record's details and payload in memory during creation.
Definition Record.h:78
static const char * typeName(Type type)
Get a record type as a text string.
Definition Record.cpp:150
int writeRecord(WriteFileHandler &file, StreamId streamId, uint32_t &inOutRecordSize, Compressor &compressor, uint32_t compressedSize)
Definition Record.cpp:105
Type getRecordType() const
Get the record's record type.
Definition Record.h:146
Type
Definition Record.h:87
@ UNDEFINED
don't use.
@ STATE
device or algorithm state information.
@ TAGS
tags record (VRS internal type).
@ COUNT
Count of enum values.
@ CONFIGURATION
device or algorithm configuration.
@ DATA
device or algorithm data.
uint32_t compressRecord(Compressor &compressor)
Definition Record.cpp:97
void addDirectWriteRecordData(std::unique_ptr< DirectWriteRecordData > &&directWriteRecordData)
Add some data to write directly at the end of the record when it's written to disk.
Definition Record.cpp:87
double getTimestamp() const
Get the record's timestamp.
Definition Record.h:132
static const double kMaxTimestamp
Maximum timestamp for a record.
Definition Record.h:81
size_t getSize() const
Get the record's payload size, uncompressed.
Definition Record.h:141
bool shouldTryToCompress() const
Definition Record.cpp:92
void recycle()
Definition Record.cpp:58
VRS internal class to manage the records of a specific Recordable after their creation.
Definition RecordManager.h:38
VRS stream identifier class.
Definition StreamId.h:245
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:45
Definition Compressor.cpp:112
@ None
No compression.
CompressionType
Type of compression. Used in VRS record headers, so never modify the values.
Definition Record.h:34
@ Zstd
zstd compression.
@ Lz4
lz4 compression.
Public for testing.
Definition Record.h:154