VRS
A file format for sensor data.
Loading...
Searching...
No Matches
RecordReaders.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 <vrs/DataReference.h>
20#include <vrs/Decompressor.h>
21#include <vrs/ErrorCode.h>
22#include <vrs/VrsExport.h>
23
24namespace vrs {
25
26class FileHandler;
27
30class VRS_API RecordReader {
31 public:
32 virtual ~RecordReader();
33
39 RecordReader* init(FileHandler& file, uint32_t diskSize, uint32_t expandedSize);
40
45 virtual int read(DataReference& destination, uint32_t& outReadSize) = 0;
46
50 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
51 int read(std::vector<T>& buffer) {
52 DataReference bufferDataReference(buffer);
53 uint32_t readSize = 0;
54 int res = read(bufferDataReference, readSize);
55 buffer.resize(readSize / sizeof(T));
56 return res;
57 }
58
64 int read(void* destination, size_t size) {
65 DataReference bufferDataReference(destination, static_cast<uint32_t>(size));
66 uint32_t readSize = 0;
67 const int res = read(bufferDataReference, readSize);
68 if (res != 0) {
69 return res;
70 }
71 return readSize == size ? 0 : READ_ERROR;
72 }
73
75 virtual void finish() {}
76
79 uint32_t getUnreadDiskBytes() const {
80 return remainingDiskBytes_;
81 }
82
85 uint32_t getUnreadBytes() const {
86 return remainingUncompressedSize_;
87 }
88
89 int64_t getFileOffset() const;
90
93 [[nodiscard]] virtual CompressionType getCompressionType() const = 0;
94
95 // for warning/error throttling only
96 const void* getRef() const {
97 return file_;
98 }
99
100 protected:
101 FileHandler* file_;
102 uint32_t remainingDiskBytes_;
103 uint32_t remainingUncompressedSize_;
104};
105
109 public:
110 int read(DataReference& destination, uint32_t& outReadSize) override;
111 CompressionType getCompressionType() const override;
112};
113
116class VRS_API CompressedRecordReader : public RecordReader {
117 public:
118 void initCompressionType(CompressionType compressionType);
119 int read(DataReference& destination, uint32_t& outReadSize) override;
120 void finish() override;
121 CompressionType getCompressionType() const override;
122
123 private:
124 int read(void* dest, uint32_t destSize, uint32_t overallSize, uint32_t& outReadSize);
125
126 Decompressor decompressor_;
127};
128
129} // namespace vrs
RecordReader specialized to read compressed records. For VRS internal usage only.
Definition RecordReaders.h:116
Container of data pointers, to tell where to write data when reading a record.
Definition DataReference.h:61
Decompressor helper class, to decompresses data at a target location.
Definition Decompressor.h:35
Class to abstract VRS file system operations, to enable support for alternate storage methods,...
Definition FileHandler.h:75
Abstract VRS internal helper class to read & (if necessary) uncompress records.
Definition RecordReaders.h:30
uint32_t getUnreadDiskBytes() const
Definition RecordReaders.h:79
int read(void *destination, size_t size)
Definition RecordReaders.h:64
virtual void finish()
Discard any unread data.
Definition RecordReaders.h:75
uint32_t getUnreadBytes() const
Definition RecordReaders.h:85
int read(std::vector< T > &buffer)
Definition RecordReaders.h:51
virtual CompressionType getCompressionType() const =0
virtual int read(DataReference &destination, uint32_t &outReadSize)=0
RecordReader specialized to read uncompressed records. For VRS internal usage only.
Definition RecordReaders.h:108
Definition Compressor.cpp:113
CompressionType
Type of compression. Used in VRS record headers, so never modify the values.
Definition Record.h:38