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;
70 RecordFileReader& operator=(const RecordFileReader&) = delete;
71
72 virtual ~RecordFileReader();
73
79 bool isVrsFile(const string& filePath);
85 bool isVrsFile(const FileSpec& fileSpec);
86
94 int openFile(const string& filePath, bool autoWriteFixedIndex = false);
101 int openFile(const FileSpec& fileSpec, bool autoWriteFixedIndex = false);
102
105 bool isOpened() const;
106
114 void setStreamPlayer(StreamId streamId, StreamPlayer* streamPlayer);
115
118 int clearStreamPlayers();
119
135 const vector<const IndexRecord::RecordInfo*>& records,
136 bool clearSequence = true);
137
143
149 int readRecord(const IndexRecord::RecordInfo& recordInfo);
150
162 int readRecord(
163 const IndexRecord::RecordInfo& recordInfo,
164 StreamPlayer* streamPlayer,
165 bool setupPlayer = false);
166
170 int readAllRecords();
171
174 vector<std::pair<string, int64_t>> getFileChunks() const;
175
178 int64_t getTotalSourceSize() const;
179
182 int closeFile();
183
189 bool hasIndex() const;
190
193 const set<StreamId>& getStreams() const {
194 return streamIds_;
195 }
196
202 vector<StreamId> getStreams(RecordableTypeId typeId, const string& flavor = {}) const;
203
209 StreamId getStreamForType(RecordableTypeId typeId, uint32_t indexNumber = 0) const;
210
219 StreamId getStreamForName(const string& name) const;
220
227 StreamId
228 getStreamForFlavor(RecordableTypeId typeId, const string& flavor, uint32_t indexNumber = 0) const;
229
238 StreamId getStreamForTag(
239 const string& tagName,
240 const string& tag,
242
244 StreamId getStreamForSerialNumber(const string& streamSerialNumber) const;
245
249 const vector<IndexRecord::RecordInfo>& getIndex() const {
250 return recordIndex_;
251 }
252
256 const vector<const IndexRecord::RecordInfo*>& getIndex(StreamId streamId) const;
257
260 uint32_t getRecordCount() const {
261 return static_cast<uint32_t>(recordIndex_.size());
262 }
263
267 uint32_t getRecordCount(StreamId streamId) const;
268
274 uint32_t getRecordCount(StreamId streamId, Record::Type recordType) const;
275
280 const IndexRecord::RecordInfo* getRecord(uint32_t globalIndex) const;
281
287 const IndexRecord::RecordInfo* getRecord(StreamId streamId, uint32_t indexNumber) const;
288
297 getRecord(StreamId streamId, Record::Type recordType, uint32_t indexNumber) const;
298
304 const IndexRecord::RecordInfo* getLastRecord(StreamId streamId, Record::Type recordType) const;
305
309 const IndexRecord::RecordInfo* getRecordByTime(double timestamp) const;
314 const IndexRecord::RecordInfo* getRecordByTime(Record::Type recordType, double timestamp) const;
319 const IndexRecord::RecordInfo* getRecordByTime(StreamId streamId, double timestamp) const;
326 getRecordByTime(StreamId streamId, Record::Type recordType, double timestamp) const;
327
336 double timestamp,
337 double epsilon,
338 StreamId streamId = {},
339 Record::Type recordType = Record::Type::UNDEFINED) const;
340
344 uint32_t getRecordIndex(const IndexRecord::RecordInfo* record) const;
345
349 uint32_t getRecordStreamIndex(const IndexRecord::RecordInfo* record) const;
350
356 uint32_t getRecordSize(uint32_t recordIndex, bool useBoundaries = true) const;
357
360 double getFirstDataRecordTime() const;
361
364 double getLastDataRecordTime() const;
365
380 bool readFirstConfigurationRecord(StreamId streamId, StreamPlayer* streamPlayer = nullptr);
381
386 bool readFirstConfigurationRecords(StreamPlayer* streamPlayer = nullptr);
387
394 RecordableTypeId typeId,
395 StreamPlayer* streamPlayer = nullptr);
396
399 const map<string, string>& getTags() const {
400 return fileTags_;
401 }
402
406 const string& getTag(const string& name) const {
407 return getTag(fileTags_, name);
408 }
409
414 const StreamTags& getTags(StreamId streamId) const;
415
417 const map<StreamId, StreamTags>& getStreamTags() const {
418 return streamTags_;
419 }
420
425 const string& getTag(StreamId streamId, const string& name) const {
426 return getTag(getTags(streamId).user, name);
427 }
428
433 const string& getOriginalRecordableTypeName(StreamId streamId) const;
434
441 const string& getFlavor(StreamId streamId) const;
442
454 const string& getSerialNumber(StreamId streamId) const;
455
459 string getStreamsSignature() const;
460
466 bool mightContainImages(StreamId streamId) const;
467
473 bool mightContainAudio(StreamId streamId) const;
474
482 bool getRecordFormat(
483 StreamId streamId,
484 Record::Type recordType,
485 uint32_t formatVersion,
486 RecordFormat& outFormat) const;
487
493 uint32_t getRecordFormats(StreamId streamId, RecordFormatMap& outFormats) const;
494 std::unique_ptr<DataLayout> getDataLayout(StreamId streamId, const ContentBlockId& blockId) const;
495
498 void setOpenProgressLogger(ProgressLogger* progressLogger);
499
502 bool setCachingStrategy(CachingStrategy cachingStrategy) {
503 return file_->setCachingStrategy(cachingStrategy);
504 }
505 CachingStrategy getCachingStrategy() const {
506 return file_->getCachingStrategy();
507 }
508
510 bool setStatsCallback(const FileHandler::CacheStatsCallbackFunction& statsCallback) {
511 return file_->setStatsCallback(statsCallback);
512 }
513
519 return file_->purgeCache();
520 }
521
524
530 void setFileHandler(std::unique_ptr<FileHandler> fileHandler);
531
534 std::unique_ptr<FileHandler> getFileHandler() const;
535
542 static int
543 vrsFilePathToFileSpec(const string& filePath, FileSpec& outFileSpec, bool checkLocalFile = false);
544
545 class RecordTypeCounter : public std::array<uint32_t, enumCount<Record::Type>()> {
546 using ParentType = std::array<uint32_t, enumCount<Record::Type>()>;
547
548 public:
550 fill(0);
551 }
552 inline uint32_t operator[](Record::Type recordType) const {
553 return ParentType::operator[](static_cast<uint32_t>(recordType));
554 }
555 inline uint32_t& operator[](Record::Type recordType) {
556 return ParentType::operator[](static_cast<uint32_t>(recordType));
557 }
558 uint32_t totalCount() const;
559 };
560
561 void buildRecordBoundaries(bool boundariesAndLimits = false) const;
562
563 private:
564 int doOpenFile(const FileSpec& fileSpec, bool autoWriteFixedIndex, bool checkSignatureOnly);
565 int readFileHeader(const FileSpec& fileSpec, FileFormat::FileHeader& outFileHeader);
566 int readFileDetails(
567 const FileSpec& fileSpec,
568 bool autoWriteFixedIndex,
569 FileFormat::FileHeader& fileHeader);
570 bool readConfigRecords(
571 const set<const IndexRecord::RecordInfo*>& configRecords,
572 StreamPlayer* streamPlayer);
573
574 static const string& getTag(const map<string, string>& tags, const string& name);
575 bool mightContainContentTypeInDataRecord(StreamId streamId, ContentType type) const;
576
577 int64_t getFollowingRecordOffset(uint32_t recordIndex, bool useBoundaries) const;
578 mutable vector<int64_t> recordBoundaries_;
579 mutable map<uint32_t, int64_t> recordLimits_;
580
581 // Members to read an open VRS file
582 std::unique_ptr<FileHandler> file_;
583 UncompressedRecordReader uncompressedRecordReader_;
584 CompressedRecordReader compressedRecordReader_;
585
586 // Source of truth describing the VRS file: must never change while the file is open.
587 set<StreamId> streamIds_;
588 map<StreamId, StreamTags> streamTags_;
589 map<string, string> fileTags_;
590 vector<IndexRecord::RecordInfo> recordIndex_;
591 mutable map<StreamId, RecordTypeCounter> streamRecordCounts_;
592
593 // Pointers to stream players to notify when reading records. These are NOT owned by the class.
594 map<StreamId, StreamPlayer*> streamPlayers_;
595
596 // Misc less critical members, for presentation or optimization
597 unique_ptr<ProgressLogger> defaultProgressLogger_;
598 ProgressLogger* openProgressLogger_;
599 unique_ptr<std::thread> detailsSaveThread_;
600 mutable map<StreamId, vector<const IndexRecord::RecordInfo*>> streamIndex_;
601 // Location of the last record searched for a specific stream & record type
602 // The pair: index of the record for the type (query), index of the record in the stream (result)
603 mutable map<pair<StreamId, Record::Type>, pair<uint32_t, size_t>> lastRequest_;
604 int64_t endOfUserRecordsOffset_{};
605 uint32_t recordHeaderSize_{};
606 bool fileHasAnIndex_{};
607};
608
616const IndexRecord::RecordInfo* getNearestRecordByTime(
617 const std::vector<const IndexRecord::RecordInfo*>& index,
618 double timestamp,
619 double epsilon,
621
622} // 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:600
ProgressLogger class to be notified of some process' progress.
Definition ProgressLogger.h:31
Definition RecordFileReader.h:545
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:518
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:425
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:417
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:249
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:406
bool setCachingStrategy(CachingStrategy cachingStrategy)
Definition RecordFileReader.h:502
bool setStatsCallback(const FileHandler::CacheStatsCallbackFunction &statsCallback)
Set callback function for cache stats.
Definition RecordFileReader.h:510
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:399
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:193
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:260
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:644
Type
Definition Record.h:88
@ UNDEFINED
don't use.
VRS stream identifier class.
Definition StreamId.h:242
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 AsyncDiskFileChunk.hpp:49
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:590
CachingStrategy
Caching strategy requests.
Definition FileHandler.h:37
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