VRS
A file format for sensor data.
Loading...
Searching...
No Matches
FileHandler.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 <functional>
20#include <map>
21#include <string>
22#include <utility>
23#include <vector>
24
25#include <vrs/ErrorCode.h>
26#include <vrs/FileDelegator.h>
27#include <vrs/FileSpec.h>
28#include <vrs/helpers/EnumTemplates.hpp>
29#include <vrs/os/CompilerAttributes.h>
30
31namespace vrs {
32
33using std::map;
34using std::string;
35using std::vector;
36
38enum class CachingStrategy {
39 Undefined = 0,
40
41 Passive,
42 Streaming,
47
48 COUNT
49};
50
52constexpr const char* kPrefetchCache = "prefetch_cache";
53
54string toString(CachingStrategy cachingStrategy);
55
56template <>
57CachingStrategy toEnum<>(const string& name);
58
75class FileHandler : public FileDelegator {
76 public:
78 struct CacheStats {
79 double startTime;
80 double waitTime;
81 size_t blockReadCount;
82 size_t blockMissingCount;
83 size_t blockPendingCount;
84 size_t sequenceSize;
85 };
86
87 using CacheStatsCallbackFunction = std::function<void(const CacheStats& stats)>;
88
89 FileHandler() = default;
90
92 static unique_ptr<FileHandler> makeOpen(const string& filePath);
93 static unique_ptr<FileHandler> makeOpen(const FileSpec& fileSpec);
94
98 virtual unique_ptr<FileHandler> makeNew() const = 0;
99 virtual const string& getFileHandlerName() const = 0;
100 virtual const string& getWriteFileHandlerName() const; // maybe use another FileHandler for writes
101
105 virtual int open(const string& filePath);
109 virtual int openSpec(const FileSpec& fileSpec) = 0;
110
121 int delegateOpen(const FileSpec& fileSpec, unique_ptr<FileHandler>& outNewDelegate) override;
122
125 virtual bool isOpened() const = 0;
126
129 virtual int64_t getTotalSize() const = 0;
132 virtual int close() = 0;
133
137 virtual int skipForward(int64_t offset) = 0;
141 virtual int setPos(int64_t offset) = 0;
145 virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length) {
146 return !isRemoteFileSystem();
147 }
156 virtual int read(void* buffer, size_t length) = 0;
158 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
159 int read(T& object) {
160 return read(&object, sizeof(object));
161 }
164 virtual size_t getLastRWSize() const = 0;
165
168 virtual bool isReadOnly() const;
169
172 virtual vector<std::pair<string, int64_t>> getFileChunks() const = 0;
174 virtual void forgetFurtherChunks(int64_t maxSize) = 0;
175
178 virtual int getLastError() const = 0;
181 virtual bool isEof() const = 0;
184 virtual int64_t getPos() const = 0;
187 virtual int64_t getChunkPos() const = 0;
192 virtual int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const = 0;
193
198 virtual bool setCachingStrategy(CachingStrategy /*cachingStrategy*/) {
199 return false;
200 }
204 return CachingStrategy::Passive; // default, in particular when there is no caching implemented.
205 }
206
219 MAYBE_UNUSED const vector<std::pair<size_t, size_t>>& sequence,
220 MAYBE_UNUSED bool clearSequence = true) {
221 return false;
222 }
223
224 virtual bool setStatsCallback(const CacheStatsCallbackFunction& /* callback */) {
225 return false;
226 }
227
231 virtual bool purgeCache() {
232 return true;
233 }
234
235 bool isFileHandlerMatch(const FileSpec& fileSpec) const;
236
239 virtual bool isRemoteFileSystem() const = 0;
240
242 virtual bool showProgress() const {
243 return isRemoteFileSystem();
244 }
245};
246
249 public:
250 TemporaryCachingStrategy(unique_ptr<FileHandler>& handler, CachingStrategy temporaryStrategy)
251 : handler_{handler},
252 originalPtr_{handler_.get()},
253 originalStrategy_{handler->getCachingStrategy()} {
254 handler_->setCachingStrategy(temporaryStrategy);
255 }
257 // only restore the original strategy if the handler hasn't been changed
258 if (originalPtr_ == handler_.get()) {
259 originalPtr_->setCachingStrategy(originalStrategy_);
260 }
261 }
262
263 private:
264 unique_ptr<FileHandler>& handler_;
265 FileHandler* originalPtr_;
266 CachingStrategy originalStrategy_;
267};
268
269} // namespace vrs
Class to abstract the delegate file open operation for VRS file.
Definition FileDelegator.h:31
Class to abstract VRS file system operations, to enable support for alternate storage methods,...
Definition FileHandler.h:75
virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length)
Definition FileHandler.h:145
virtual int read(void *buffer, size_t length)=0
virtual int64_t getPos() const =0
virtual int64_t getTotalSize() const =0
int delegateOpen(const FileSpec &fileSpec, unique_ptr< FileHandler > &outNewDelegate) override
Definition FileHandler.cpp:94
virtual int skipForward(int64_t offset)=0
virtual vector< std::pair< string, int64_t > > getFileChunks() const =0
virtual bool isRemoteFileSystem() const =0
virtual bool prefetchReadSequence(MAYBE_UNUSED const vector< std::pair< size_t, size_t > > &sequence, MAYBE_UNUSED bool clearSequence=true)
Definition FileHandler.h:218
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:159
virtual bool isEof() const =0
virtual bool showProgress() const
Tell if the file handler is probably slow, and extra progress information might be useful.
Definition FileHandler.h:242
virtual unique_ptr< FileHandler > makeNew() const =0
virtual bool isOpened() const =0
virtual CachingStrategy getCachingStrategy() const
Definition FileHandler.h:203
virtual int setPos(int64_t offset)=0
virtual int64_t getChunkPos() const =0
virtual int openSpec(const FileSpec &fileSpec)=0
virtual void forgetFurtherChunks(int64_t maxSize)=0
Call this method to forget any chunk beyond this file size.
virtual size_t getLastRWSize() const =0
virtual int getChunkRange(int64_t &outChunkOffset, int64_t &outChunkSize) const =0
virtual int open(const string &filePath)
Definition FileHandler.cpp:76
virtual bool purgeCache()
Definition FileHandler.h:231
static unique_ptr< FileHandler > makeOpen(const string &filePath)
Open a file in read-only mode. Returns an open file handler, or nullptr on error.
Definition FileHandler.cpp:56
virtual int close()=0
virtual bool setCachingStrategy(CachingStrategy)
Definition FileHandler.h:198
virtual int getLastError() const =0
virtual bool isReadOnly() const
Definition FileHandler.cpp:109
Helper class to temporarily modify a FileHandler's caching strategy.
Definition FileHandler.h:248
Definition Compressor.cpp:112
@ Undefined
when not set explicitly
constexpr const char * kPrefetchCache
To enable basic caching for network file handlers.
Definition FileHandler.h:52
CachingStrategy
Caching strategy requests.
Definition FileHandler.h:38
@ ReleaseAfterRead
Same as "Passive" but release used cache blocks immediately after read.
@ Streaming
Automatically download data "forward", using last read-request as a hint.
@ StreamingBackward
Automatically download data "backward", using last read-request as a hint.
@ Passive
(default) Read & cache on-demand (don't prefetch).
Stats for cache.
Definition FileHandler.h:78
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37