VRS
A file format for sensor data.
Loading...
Searching...
No Matches
MultiRecordFileReader.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#ifdef GTEST_BUILD
20#include <gtest/gtest.h>
21class GTEST_TEST_CLASS_NAME_(MultiRecordFileReaderTest, multiFile);
22#endif
23
24#include <string>
25#include <vector>
26
27#include <vrs/IndexRecord.h>
28#include <vrs/RecordFileReader.h>
29#include <vrs/VrsExport.h>
30
31namespace vrs {
32
33using std::pair;
34using std::unique_ptr;
35
40class VRS_API MultiRecordFileReader {
41 public:
47
51 virtual ~MultiRecordFileReader();
52
53 MultiRecordFileReader& operator=(const MultiRecordFileReader&) = delete;
54 MultiRecordFileReader& operator=(MultiRecordFileReader&&) = delete;
55
65 int open(const vector<string>& paths);
66
72 int open(const vector<FileSpec>& fileSpecs);
73
79 int open(const string& path) {
80 return open(vector<string>{path});
81 }
82
88 int open(const FileSpec& fileSpec) {
89 return open(vector<FileSpec>{fileSpec});
90 }
91
98 void setRelatedFileTags(vector<string>&& tags);
99
104 int close();
105
110 const set<UniqueStreamId>& getStreams() const;
111
114 bool isOpened() const {
115 return isOpened_;
116 }
117
120 uint32_t getRecordCount() const;
121
125 uint32_t getRecordCount(UniqueStreamId uniqueStreamId) const;
126
132 uint32_t getRecordCount(UniqueStreamId uniqueStreamId, Record::Type recordType) const;
133
138 const StreamTags& getTags(UniqueStreamId uniqueStreamId) const;
139
145 const string& getTag(const string& name) const {
146 return getTag(fileTags_, name);
147 }
148
153 const string& getTag(UniqueStreamId uniqueStreamId, const string& name) const {
154 return getTag(getTags(uniqueStreamId).user, name);
155 }
156
162 vector<pair<string, int64_t>> getFileChunks() const;
163
170 const string& getFlavor(UniqueStreamId uniqueStreamId) const;
171
175 const string& getSerialNumber(UniqueStreamId uniqueStreamId) const;
176
182 vector<UniqueStreamId> getStreams(RecordableTypeId typeId, const string& flavor = {}) const;
183
192 UniqueStreamId getStreamForName(const string& name) const;
193
204 UniqueStreamId getStreamForTag(
205 const string& tagName,
206 const string& tag,
207 RecordableTypeId typeId = RecordableTypeId::Undefined) const;
208
213 UniqueStreamId getStreamForSerialNumber(const string& serialNumber) const;
214
220 bool mightContainImages(UniqueStreamId uniqueStreamId) const;
221
227 bool mightContainAudio(UniqueStreamId uniqueStreamId) const;
228
233 uint32_t getRecordIndex(const IndexRecord::RecordInfo* record) const;
234
238 const IndexRecord::RecordInfo* getRecord(uint32_t globalIndex) const;
239
243 uint32_t getRecordSize(uint32_t globalIndex) const;
244
251 const IndexRecord::RecordInfo* getRecord(UniqueStreamId streamId, uint32_t indexNumber) const;
252
260 const IndexRecord::RecordInfo*
261 getRecord(UniqueStreamId streamId, Record::Type recordType, uint32_t indexNumber) const;
262
268 const IndexRecord::RecordInfo* getLastRecord(UniqueStreamId streamId, Record::Type recordType)
269 const;
270
274 const vector<const IndexRecord::RecordInfo*>& getIndex(UniqueStreamId streamId) const;
275
280 const string& getOriginalRecordableTypeName(UniqueStreamId streamId) const {
281 return getTag(getTags(streamId).vrs, Recordable::getOriginalNameTagName());
282 }
283
291 void setStreamPlayer(UniqueStreamId streamId, StreamPlayer* streamPlayer);
292
298 uint32_t getRecordFormats(UniqueStreamId streamId, RecordFormatMap& outFormats) const;
299
305 int readRecord(const IndexRecord::RecordInfo& recordInfo);
306
316 int readRecord(const IndexRecord::RecordInfo& recordInfo, StreamPlayer* player, bool setupPlayer);
317
324 bool setCachingStrategy(CachingStrategy cachingStrategy);
325
328 CachingStrategy getCachingStrategy() const;
329
344 bool prefetchRecordSequence(
345 const vector<const IndexRecord::RecordInfo*>& records,
346 bool clearSequence = true);
347
352 bool isRecordAvailableOrPrefetch(const IndexRecord::RecordInfo& record) const;
353
358 bool purgeFileCache();
359
362 const map<string, string>& getTags() const {
363 return fileTags_;
364 }
365
371 const IndexRecord::RecordInfo* getFirstRecord(Record::Type recordType) const;
372
378 const IndexRecord::RecordInfo* getLastRecord(Record::Type recordType) const;
379
383 const IndexRecord::RecordInfo* getRecordByTime(double timestamp) const;
384
389 const IndexRecord::RecordInfo* getRecordByTime(UniqueStreamId streamId, double timestamp) const;
390
397 getRecordByTime(StreamId streamId, Record::Type recordType, double timestamp) const;
398
406 const IndexRecord::RecordInfo* getNearestRecordByTime(
407 double timestamp,
408 double epsilon,
409 StreamId streamId = {},
410 Record::Type recordType = Record::Type::UNDEFINED) const;
411
415 unique_ptr<FileHandler> getFileHandler() const;
416
423 UniqueStreamId getUniqueStreamId(const IndexRecord::RecordInfo* record) const;
424
427 int64_t getTotalSourceSize() const;
428
446 bool readFirstConfigurationRecord(
447 UniqueStreamId uniqueStreamId,
448 StreamPlayer* streamPlayer = nullptr);
449
467 bool readFirstConfigurationRecords(StreamPlayer* streamPlayer = nullptr);
468
477 bool readFirstConfigurationRecordsForType(
478 RecordableTypeId typeId,
479 StreamPlayer* streamPlayer = nullptr);
480
482 const vector<unique_ptr<RecordFileReader>>& getReaders() const {
483 return readers_;
484 }
485
486 static unique_ptr<RecordFileReader> reduceToRecordFileReader_TEMPORARY_API_TO_DELETE(
487 MultiRecordFileReader& multiRecordFileReader);
488
489 private:
490 using StreamIdToUniqueIdMap = map<StreamId, UniqueStreamId>;
491 using StreamIdReaderPair = pair<StreamId, RecordFileReader*>;
492
495 bool hasSingleFile() const {
496 return readers_.size() == 1;
497 }
498
503 bool areFilesRelated() const;
504
505 void initializeUniqueStreamIds();
506
508 void createConsolidatedIndex();
509
510 void initializeFileTags();
511
513 UniqueStreamId generateUniqueStreamId(StreamId duplicateStreamId) const;
514
515 const StreamIdReaderPair* getStreamIdReaderPair(UniqueStreamId uniqueStreamId) const;
516
517 static const string& getTag(const map<string, string>& tags, const string& name);
518
521 RecordFileReader* getReader(const IndexRecord::RecordInfo* record) const;
522
526 UniqueStreamId getUniqueStreamIdInternal(const IndexRecord::RecordInfo* record) const;
527 UniqueStreamId getUniqueStreamIdInternal(const RecordFileReader* reader, StreamId streamId) const;
528
529 bool timeLessThan(const IndexRecord::RecordInfo* lhs, const IndexRecord::RecordInfo* rhs) const;
530
531 class RecordComparatorGT {
532 public:
533 explicit RecordComparatorGT(const MultiRecordFileReader& parent) : parent_(parent) {}
534
535 bool operator()(const IndexRecord::RecordInfo* lhs, const IndexRecord::RecordInfo* rhs) const {
536 return parent_.timeLessThan(rhs, lhs);
537 }
538
539 private:
540 const MultiRecordFileReader& parent_;
541 };
542
543 bool isOpened_{false};
545 vector<unique_ptr<RecordFileReader>> readers_;
547 unique_ptr<vector<const IndexRecord::RecordInfo*>> recordIndex_;
550 set<UniqueStreamId> uniqueStreamIds_;
551 map<const RecordFileReader*, StreamIdToUniqueIdMap> readerStreamIdToUniqueMap_;
552 map<UniqueStreamId, StreamIdReaderPair> uniqueToStreamIdReaderPairMap_;
554 vector<string> filePaths_;
555 const RecordComparatorGT recordComparatorGT_{*this};
556 map<string, string> fileTags_;
558 vector<string> relatedFileTags_;
559
560#ifdef GTEST_BUILD
561 FRIEND_TEST(::MultiRecordFileReaderTest, multiFile);
562#endif
563};
564
565} // namespace vrs
Facilitates reading multiple VRS files simultaneously. Records are sorted by timestamps across all th...
Definition MultiRecordFileReader.h:40
const string & getOriginalRecordableTypeName(UniqueStreamId streamId) const
Definition MultiRecordFileReader.h:280
bool isOpened() const
Definition MultiRecordFileReader.h:114
int open(const string &path)
Definition MultiRecordFileReader.h:79
int open(const FileSpec &fileSpec)
Definition MultiRecordFileReader.h:88
const string & getTag(const string &name) const
Definition MultiRecordFileReader.h:145
const string & getTag(UniqueStreamId uniqueStreamId, const string &name) const
Definition MultiRecordFileReader.h:153
const map< string, string > & getTags() const
Definition MultiRecordFileReader.h:362
const vector< unique_ptr< RecordFileReader > > & getReaders() const
Get the list of RecordFileReader objects used to read all the streams.
Definition MultiRecordFileReader.h:482
Type
Definition Record.h:91
VRS stream identifier class.
Definition StreamId.h:251
Class designed to receive record data when reading a VRS file.
Definition StreamPlayer.h:54
Definition Compressor.cpp:113
map< pair< Record::Type, uint32_t >, RecordFormat > RecordFormatMap
Map a pair of record type/format version to a record format, for a particular stream.
Definition RecordFormat.h:634
CachingStrategy
Caching strategy requests.
Definition FileHandler.h:38
RecordableTypeId
VRS stream type or class identifier enum.
Definition StreamId.h:51
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:42
Helper class to hold the details about a single VRS record in memory.
Definition IndexRecord.h:110
Container for a stream's tags, both user and VRS controlled.
Definition Recordable.h:38