VRS
A file format for sensor data.
Loading...
Searching...
No Matches
RecordFileReader.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 <array>
20#include <map>
21#include <memory>
22#include <set>
23#include <string>
24#include <thread>
25#include <vector>
26
27#include "FileFormat.h"
28#include "FileHandler.h"
29#include "IndexRecord.h"
30#include "Record.h"
31#include "RecordFormat.h"
32#include "RecordReaders.h"
33#include "Recordable.h"
34#include "StreamId.h"
35
36namespace vrs {
37
38using std::map;
39using std::set;
40using std::string;
41using std::vector;
42
43class DataLayout;
44class StreamPlayer;
45class ProgressLogger;
46
51constexpr const char* kFailFastOnIncompleteIndex = "fail_fast_on_incomplete_index";
52
67 public:
69 RecordFileReader(const RecordFileReader&) = delete;
71
72 virtual ~RecordFileReader();
73
74 RecordFileReader& operator=(const RecordFileReader&) = delete;
75 RecordFileReader& operator=(RecordFileReader&&) = delete;
76
82 bool isVrsFile(const string& filePath);
88 bool isVrsFile(const FileSpec& fileSpec);
89
97 int openFile(const string& filePath, bool autoWriteFixedIndex = false);
104 int openFile(const FileSpec& fileSpec, bool autoWriteFixedIndex = false);
105
108 bool isOpened() const;
109
117 void setStreamPlayer(StreamId streamId, StreamPlayer* streamPlayer);
118
121 int clearStreamPlayers();
122
138 const vector<const IndexRecord::RecordInfo*>& records,
139 bool clearSequence = true);
140
146
152 int readRecord(const IndexRecord::RecordInfo& recordInfo);
153
165 int readRecord(
166 const IndexRecord::RecordInfo& recordInfo,
167 StreamPlayer* streamPlayer,
168 bool setupPlayer = false);
169
173 int readAllRecords();
174
177 vector<std::pair<string, int64_t>> getFileChunks() const;
178
181 int64_t getTotalSourceSize() const;
182
185 int closeFile();
186
192 bool hasIndex() const;
193
196 const set<StreamId>& getStreams() const {
197 return streamIds_;
198 }
199
205 vector<StreamId> getStreams(RecordableTypeId typeId, const string& flavor = {}) const;
206
212 StreamId getStreamForType(RecordableTypeId typeId, uint32_t indexNumber = 0) const;
213
222 StreamId getStreamForName(const string& name) const;
223
230 StreamId
231 getStreamForFlavor(RecordableTypeId typeId, const string& flavor, uint32_t indexNumber = 0) const;
232
241 StreamId getStreamForTag(
242 const string& tagName,
243 const string& tag,
245
247 StreamId getStreamForSerialNumber(const string& streamSerialNumber) const;
248
252 const vector<IndexRecord::RecordInfo>& getIndex() const {
253 return recordIndex_;
254 }
255
259 const vector<const IndexRecord::RecordInfo*>& getIndex(StreamId streamId) const;
260
263 uint32_t getRecordCount() const {
264 return static_cast<uint32_t>(recordIndex_.size());
265 }
266
270 uint32_t getRecordCount(StreamId streamId) const;
271
277 uint32_t getRecordCount(StreamId streamId, Record::Type recordType) const;
278
283 const IndexRecord::RecordInfo* getRecord(uint32_t globalIndex) const;
284
290 const IndexRecord::RecordInfo* getRecord(StreamId streamId, uint32_t indexNumber) const;
291
300 getRecord(StreamId streamId, Record::Type recordType, uint32_t indexNumber) const;
301
307 const IndexRecord::RecordInfo* getLastRecord(StreamId streamId, Record::Type recordType) const;
308
312 const IndexRecord::RecordInfo* getRecordByTime(double timestamp) const;
317 const IndexRecord::RecordInfo* getRecordByTime(Record::Type recordType, double timestamp) const;
322 const IndexRecord::RecordInfo* getRecordByTime(StreamId streamId, double timestamp) const;
329 getRecordByTime(StreamId streamId, Record::Type recordType, double timestamp) const;
330
339 double timestamp,
340 double epsilon,
341 StreamId streamId = {},
342 Record::Type recordType = Record::Type::UNDEFINED) const;
343
347 uint32_t getRecordIndex(const IndexRecord::RecordInfo* record) const;
348
352 uint32_t getRecordStreamIndex(const IndexRecord::RecordInfo* record) const;
353
359 uint32_t getRecordSize(uint32_t recordIndex, bool useBoundaries = true) const;
360
363 double getFirstDataRecordTime() const;
364
367 double getLastDataRecordTime() const;
368
383 bool readFirstConfigurationRecord(StreamId streamId, StreamPlayer* streamPlayer = nullptr);
384
389 bool readFirstConfigurationRecords(StreamPlayer* streamPlayer = nullptr);
390
397 RecordableTypeId typeId,
398 StreamPlayer* streamPlayer = nullptr);
399
402 const map<string, string>& getTags() const {
403 return fileTags_;
404 }
405
409 const string& getTag(const string& name) const {
410 return getTag(fileTags_, name);
411 }
412
417 const StreamTags& getTags(StreamId streamId) const;
418
420 const map<StreamId, StreamTags>& getStreamTags() const {
421 return streamTags_;
422 }
423
428 const string& getTag(StreamId streamId, const string& name) const {
429 return getTag(getTags(streamId).user, name);
430 }
431
436 const string& getOriginalRecordableTypeName(StreamId streamId) const;
437
444 const string& getFlavor(StreamId streamId) const;
445
457 const string& getSerialNumber(StreamId streamId) const;
458
462 string getStreamsSignature() const;
463
469 bool mightContainImages(StreamId streamId) const;
470
476 bool mightContainAudio(StreamId streamId) const;
477
485 bool getRecordFormat(
486 StreamId streamId,
487 Record::Type recordType,
488 uint32_t formatVersion,
489 RecordFormat& outFormat) const;
490
496 uint32_t getRecordFormats(StreamId streamId, RecordFormatMap& outFormats) const;
497 std::unique_ptr<DataLayout> getDataLayout(StreamId streamId, const ContentBlockId& blockId) const;
498
501 void setOpenProgressLogger(ProgressLogger* progressLogger);
502
505 bool setCachingStrategy(CachingStrategy cachingStrategy) {
506 return file_->setCachingStrategy(cachingStrategy);
507 }
508 CachingStrategy getCachingStrategy() const {
509 return file_->getCachingStrategy();
510 }
511
513 bool setStatsCallback(const FileHandler::CacheStatsCallbackFunction& statsCallback) {
514 return file_->setStatsCallback(statsCallback);
515 }
516
522 return file_->purgeCache();
523 }
524
527
533 void setFileHandler(std::unique_ptr<FileHandler> fileHandler);
534
537 std::unique_ptr<FileHandler> getFileHandler() const;
538
545 static int
546 vrsFilePathToFileSpec(const string& filePath, FileSpec& outFileSpec, bool checkLocalFile = false);
547
548 class RecordTypeCounter : public std::array<uint32_t, enumCount<Record::Type>()> {
549 using ParentType = std::array<uint32_t, enumCount<Record::Type>()>;
550
551 public:
552 RecordTypeCounter() : array() {
553 fill(0);
554 }
555 inline uint32_t operator[](Record::Type recordType) const {
556 return ParentType::operator[](static_cast<uint32_t>(recordType));
557 }
558 inline uint32_t& operator[](Record::Type recordType) {
559 return ParentType::operator[](static_cast<uint32_t>(recordType));
560 }
561 uint32_t totalCount() const;
562 };
563
564 void buildRecordBoundaries(bool boundariesAndLimits = false) const;
565
566 private:
567 int doOpenFile(const FileSpec& fileSpec, bool autoWriteFixedIndex, bool checkSignatureOnly);
568 int readFileHeader(const FileSpec& fileSpec, FileFormat::FileHeader& outFileHeader);
569 int readFileDetails(
570 const FileSpec& fileSpec,
571 bool autoWriteFixedIndex,
572 FileFormat::FileHeader& fileHeader);
573 bool readConfigRecords(
574 const set<const IndexRecord::RecordInfo*>& configRecords,
575 StreamPlayer* streamPlayer);
576
577 static const string& getTag(const map<string, string>& tags, const string& name);
578 bool mightContainContentTypeInDataRecord(StreamId streamId, ContentType type) const;
579
580 int64_t getFollowingRecordOffset(uint32_t recordIndex, bool useBoundaries) const;
581 mutable vector<int64_t> recordBoundaries_;
582 mutable map<uint32_t, int64_t> recordLimits_;
583
584 // Members to read an open VRS file
585 std::unique_ptr<FileHandler> file_;
586 UncompressedRecordReader uncompressedRecordReader_;
587 CompressedRecordReader compressedRecordReader_;
588
589 // Source of truth describing the VRS file: must never change while the file is open.
590 set<StreamId> streamIds_;
591 map<StreamId, StreamTags> streamTags_;
592 map<string, string> fileTags_;
593 vector<IndexRecord::RecordInfo> recordIndex_;
594 mutable map<StreamId, RecordTypeCounter> streamRecordCounts_;
595
596 // Pointers to stream players to notify when reading records. These are NOT owned by the class.
597 map<StreamId, StreamPlayer*> streamPlayers_;
598
599 // Misc less critical members, for presentation or optimization
600 unique_ptr<ProgressLogger> defaultProgressLogger_;
601 ProgressLogger* openProgressLogger_;
602 unique_ptr<std::thread> detailsSaveThread_;
603 mutable map<StreamId, vector<const IndexRecord::RecordInfo*>> streamIndex_;
604 // Location of the last record searched for a specific stream & record type
605 // The pair: index of the record for the type (query), index of the record in the stream (result)
606 mutable map<pair<StreamId, Record::Type>, pair<uint32_t, size_t>> lastRequest_;
607 int64_t endOfUserRecordsOffset_{};
608 uint32_t recordHeaderSize_{};
609 bool fileHasAnIndex_{};
610};
611
619const IndexRecord::RecordInfo* getNearestRecordByTime(
620 const std::vector<const IndexRecord::RecordInfo*>& index,
621 double timestamp,
622 double epsilon,
624
625} // namespace vrs
RecordReader specialized to read compressed records. For VRS internal usage only.
Definition RecordReaders.h:115
Helper to identify a particular content block within a file.
Definition RecordFormat.h:634
ProgressLogger class to be notified of some process' progress.
Definition ProgressLogger.h:31
Definition RecordFileReader.h:548
The class to read VRS files.
Definition RecordFileReader.h:66
uint32_t getRecordFormats(StreamId streamId, RecordFormatMap &outFormats) const
Definition RecordFileReader.cpp:972
int readAllRecords()
Definition RecordFileReader.cpp:1076
void setStreamPlayer(StreamId streamId, StreamPlayer *streamPlayer)
Definition RecordFileReader.cpp:106
bool purgeFileCache()
Definition RecordFileReader.h:521
uint32_t getRecordSize(uint32_t recordIndex, bool useBoundaries=true) const
Definition RecordFileReader.cpp:580
void buildRecordBoundaries(bool boundariesAndLimits=false) const
private, for testing only
Definition RecordFileReader.cpp:503
const string & getSerialNumber(StreamId streamId) const
Definition RecordFileReader.cpp:1016
int closeFile()
Definition RecordFileReader.cpp:477
bool prefetchRecordSequence(const vector< const IndexRecord::RecordInfo * > &records, bool clearSequence=true)
Definition RecordFileReader.cpp:591
const string & getTag(StreamId streamId, const string &name) const
Definition RecordFileReader.h:428
bool isVrsFile(const string &filePath)
Definition RecordFileReader.cpp:200
bool isRecordAvailableOrPrefetch(const IndexRecord::RecordInfo &recordInfo)
Definition RecordFileReader.cpp:1098
bool isOpened() const
Definition RecordFileReader.cpp:192
const string & getFlavor(StreamId streamId) const
Definition RecordFileReader.cpp:1012
const map< StreamId, StreamTags > & getStreamTags() const
Get the tags for all the streams at once.
Definition RecordFileReader.h:420
StreamId getStreamForType(RecordableTypeId typeId, uint32_t indexNumber=0) const
Definition RecordFileReader.cpp:624
const IndexRecord::RecordInfo * getRecord(uint32_t globalIndex) const
Definition RecordFileReader.cpp:683
vector< std::pair< string, int64_t > > getFileChunks() const
Definition RecordFileReader.cpp:1090
StreamId getStreamForFlavor(RecordableTypeId typeId, const string &flavor, uint32_t indexNumber=0) const
Definition RecordFileReader.cpp:647
bool getRecordFormat(StreamId streamId, Record::Type recordType, uint32_t formatVersion, RecordFormat &outFormat) const
Definition RecordFileReader.cpp:957
uint32_t getRecordIndex(const IndexRecord::RecordInfo *record) const
Definition RecordFileReader.cpp:849
static int vrsFilePathToFileSpec(const string &filePath, FileSpec &outFileSpec, bool checkLocalFile=false)
Definition RecordFileReader.cpp:123
double getLastDataRecordTime() const
Definition RecordFileReader.cpp:904
double getFirstDataRecordTime() const
Definition RecordFileReader.cpp:895
void setOpenProgressLogger(ProgressLogger *progressLogger)
Definition RecordFileReader.cpp:86
int openFile(const string &filePath, bool autoWriteFixedIndex=false)
Definition RecordFileReader.cpp:117
const IndexRecord::RecordInfo * getLastRecord(StreamId streamId, Record::Type recordType) const
Definition RecordFileReader.cpp:721
bool hasIndex() const
Definition RecordFileReader.cpp:609
bool readFirstConfigurationRecords(StreamPlayer *streamPlayer=nullptr)
Definition RecordFileReader.cpp:937
const vector< IndexRecord::RecordInfo > & getIndex() const
Definition RecordFileReader.h:252
bool mightContainAudio(StreamId streamId) const
Definition RecordFileReader.cpp:1044
bool readFirstConfigurationRecord(StreamId streamId, StreamPlayer *streamPlayer=nullptr)
Definition RecordFileReader.cpp:933
int readRecord(const IndexRecord::RecordInfo &recordInfo)
Definition RecordFileReader.cpp:1130
const string & getTag(const string &name) const
Definition RecordFileReader.h:409
bool setCachingStrategy(CachingStrategy cachingStrategy)
Definition RecordFileReader.h:505
bool setStatsCallback(const FileHandler::CacheStatsCallbackFunction &statsCallback)
Set callback function for cache stats.
Definition RecordFileReader.h:513
bool mightContainImages(StreamId streamId) const
Definition RecordFileReader.cpp:1040
const string & getOriginalRecordableTypeName(StreamId streamId) const
Definition RecordFileReader.cpp:1008
int clearStreamPlayers()
Definition RecordFileReader.cpp:498
StreamId getStreamForSerialNumber(const string &streamSerialNumber) const
Find the stream with the specified stream serial number.
Definition RecordFileReader.cpp:674
void setFileHandler(std::unique_ptr< FileHandler > fileHandler)
Definition RecordFileReader.cpp:96
const map< string, string > & getTags() const
Definition RecordFileReader.h:402
std::unique_ptr< FileHandler > getFileHandler() const
Definition RecordFileReader.cpp:102
const IndexRecord::RecordInfo * getNearestRecordByTime(double timestamp, double epsilon, StreamId streamId={}, Record::Type recordType=Record::Type::UNDEFINED) const
Definition RecordFileReader.cpp:795
bool readFirstConfigurationRecordsForType(RecordableTypeId typeId, StreamPlayer *streamPlayer=nullptr)
Definition RecordFileReader.cpp:945
StreamId getStreamForName(const string &name) const
Definition RecordFileReader.cpp:634
StreamId getStreamForTag(const string &tagName, const string &tag, RecordableTypeId typeId=RecordableTypeId::Undefined) const
Definition RecordFileReader.cpp:661
string getStreamsSignature() const
Definition RecordFileReader.cpp:1020
const set< StreamId > & getStreams() const
Definition RecordFileReader.h:196
int64_t getTotalSourceSize() const
Definition RecordFileReader.cpp:1094
const IndexRecord::RecordInfo * getRecordByTime(double timestamp) const
Definition RecordFileReader.cpp:737
uint32_t getRecordCount() const
Definition RecordFileReader.h:263
uint32_t getRecordStreamIndex(const IndexRecord::RecordInfo *record) const
Definition RecordFileReader.cpp:856
Description of the format of a VRS record as a succession of typed blocks of content.
Definition RecordFormat.h:678
Type
Definition Record.h:88
@ UNDEFINED
don't use.
VRS stream identifier class.
Definition StreamId.h:243
Class designed to receive record data when reading a VRS file.
Definition StreamPlayer.h:53
RecordReader specialized to read uncompressed records. For VRS internal usage only.
Definition RecordReaders.h:107
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:624
CachingStrategy
Caching strategy requests.
Definition FileHandler.h:39
RecordableTypeId
VRS stream type or class identifier enum.
Definition StreamId.h:49
@ Undefined
Value used for default initializations and marking undefined situations.
ContentType
Type of a record's block.
Definition RecordFormat.h:35
constexpr const char * kFailFastOnIncompleteIndex
Definition RecordFileReader.h:51
Every file starts with this header, which may grow but not shrink!
Definition FileFormat.h:89
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37
Helper class to hold the details about a single VRS record in memory.
Definition IndexRecord.h:105
Container for a stream's tags, both user and VRS controlled.
Definition Recordable.h:37