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/helpers/EnumTemplates.hpp>
23
24#include "ForwardDefinitions.h"
25#include "StreamId.h"
26
27namespace vrs {
28
29class DataSource;
30class DirectWriteRecordData;
31class Compressor;
32class RecordManager;
33
35enum class CompressionType : uint8_t {
36 None = 0,
37 Lz4,
38 Zstd,
39
40 COUNT
41};
42
79class Record final {
80 public:
82 static const double kMaxTimestamp;
83
88 enum class Type : uint8_t {
89 UNDEFINED = 0,
90 STATE = 1,
91 CONFIGURATION = 2,
92 DATA = 3,
93 TAGS = 4,
94 COUNT
95 };
96
99 void recycle();
100
103 void set(
104 double timestamp,
105 Type type,
106 uint32_t formatVersion,
107 const DataSource& data,
108 uint64_t creationOrder);
109
111 void addDirectWriteRecordData(std::unique_ptr<DirectWriteRecordData>&& directWriteRecordData);
112
116 bool shouldTryToCompress() const;
117
121 uint32_t compressRecord(Compressor& compressor);
122
125 int writeRecord(
126 WriteFileHandler& file,
127 StreamId streamId,
128 uint32_t& inOutRecordSize,
129 Compressor& compressor,
130 uint32_t compressedSize);
131
133 double getTimestamp() const {
134 return timestamp_;
135 }
136
137 uint64_t getCreationOrder() const {
138 return creationOrder_;
139 }
140
142 size_t getSize() const {
143 return usedBufferSize_;
144 }
145
148 return recordType_;
149 }
150
152 static const char* typeName(Type type);
153
155 struct uninitialized_byte final {
156 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, modernize-use-equals-default)
157 uninitialized_byte() {} // do not use '= default' as it will initialize byte!
158 uint8_t byte;
159 };
160
161 private:
162 friend class RecordManager;
163
165 explicit Record(RecordManager& recordManager);
166 ~Record();
167
168 double timestamp_{};
169 Type recordType_{};
170 uint32_t formatVersion_{};
171 std::vector<uninitialized_byte> buffer_;
172 size_t usedBufferSize_{};
173 uint64_t creationOrder_{};
174 std::unique_ptr<DirectWriteRecordData> directWriteRecordData_;
175
176 RecordManager& recordManager_;
177};
178
180string toString(Record::Type recordType);
181
183template <>
184Record::Type toEnum(const string& name);
185
186} // 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:79
static const char * typeName(Type type)
Get a record type as a text string.
Definition Record.cpp:158
int writeRecord(WriteFileHandler &file, StreamId streamId, uint32_t &inOutRecordSize, Compressor &compressor, uint32_t compressedSize)
Definition Record.cpp:113
Type getRecordType() const
Get the record's record type.
Definition Record.h:147
Type
Definition Record.h:88
@ 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:105
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:95
double getTimestamp() const
Get the record's timestamp.
Definition Record.h:133
static const double kMaxTimestamp
Maximum timestamp for a record.
Definition Record.h:82
size_t getSize() const
Get the record's payload size, uncompressed.
Definition Record.h:142
bool shouldTryToCompress() const
Definition Record.cpp:100
void recycle()
Definition Record.cpp:66
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:242
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:45
Definition AsyncDiskFileChunk.hpp:49
@ None
No compression.
CompressionType
Type of compression. Used in VRS record headers, so never modify the values.
Definition Record.h:35
@ Zstd
zstd compression.
@ Lz4
lz4 compression.
CachingStrategy toEnum(const string &name)
Convert a record type name into an enum value.
Definition FileHandler.cpp:131
Public for testing.
Definition Record.h:155