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
28using std::unique_ptr;
29using std::vector;
30
31class DataSource;
32class DirectWriteRecordData;
33class Compressor;
34class RecordManager;
35
37enum class CompressionType : uint8_t {
38 None = 0,
39 Lz4,
40 Zstd,
41
42 COUNT
43};
44
81class Record final {
82 public:
84 static const double kMaxTimestamp;
85
90 enum class Type : uint8_t {
91 UNDEFINED = 0,
92 STATE = 1,
93 CONFIGURATION = 2,
94 DATA = 3,
95 TAGS = 4,
96 COUNT
97 };
98
101 void recycle();
102
105 void set(
106 double timestamp,
107 Type type,
108 uint32_t formatVersion,
109 const DataSource& data,
110 uint64_t creationOrder);
111
113 void addDirectWriteRecordData(unique_ptr<DirectWriteRecordData>&& directWriteRecordData);
114
118 bool shouldTryToCompress() const;
119
123 uint32_t compressRecord(Compressor& compressor);
124
127 int writeRecord(
128 WriteFileHandler& file,
129 StreamId streamId,
130 uint32_t& inOutRecordSize,
131 Compressor& compressor,
132 uint32_t compressedSize);
133
135 double getTimestamp() const {
136 return timestamp_;
137 }
138
139 uint64_t getCreationOrder() const {
140 return creationOrder_;
141 }
142
144 size_t getSize() const {
145 return usedBufferSize_;
146 }
147
150 return recordType_;
151 }
152
154 static const char* typeName(Type type);
155
157 struct uninitialized_byte final {
158 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, modernize-use-equals-default)
159 uninitialized_byte() {} // do not use '= default' as it will initialize byte!
160 uint8_t byte;
161 };
162
163 private:
164 friend class RecordManager;
165
167 explicit Record(RecordManager& recordManager);
168 ~Record();
169
170 double timestamp_{};
171 Type recordType_{};
172 uint32_t formatVersion_{};
173 vector<uninitialized_byte> buffer_;
174 size_t usedBufferSize_{};
175 uint64_t creationOrder_{};
176 unique_ptr<DirectWriteRecordData> directWriteRecordData_;
177
178 RecordManager& recordManager_;
179};
180
182string toString(Record::Type recordType);
183
185template <>
186Record::Type toEnum(const string& name);
187
188} // 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:151
Essential VRS class holding a record's details and payload in memory during creation.
Definition Record.h:81
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:149
Type
Definition Record.h:90
@ 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
double getTimestamp() const
Get the record's timestamp.
Definition Record.h:135
static const double kMaxTimestamp
Maximum timestamp for a record.
Definition Record.h:84
size_t getSize() const
Get the record's payload size, uncompressed.
Definition Record.h:144
bool shouldTryToCompress() const
Definition Record.cpp:92
void addDirectWriteRecordData(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
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:249
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:42
Definition Compressor.cpp:113
@ None
No compression.
CompressionType
Type of compression. Used in VRS record headers, so never modify the values.
Definition Record.h:37
@ Zstd
zstd compression.
@ Lz4
lz4 compression.
Public for testing.
Definition Record.h:157