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/VrsExport.h>
29#include <vrs/helpers/EnumTemplates.hpp>
30#include <vrs/os/CompilerAttributes.h>
31
32namespace vrs {
33
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
54VRS_API string toString(CachingStrategy cachingStrategy);
55
56template <>
57VRS_API CachingStrategy toEnum<>(const string& name);
58
75class VRS_API 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 }
163 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
164 int read(vector<T>& vec) {
165 return read(vec.data(), vec.size() * sizeof(T));
166 }
169 virtual size_t getLastRWSize() const = 0;
170
173 virtual bool isReadOnly() const;
174
177 virtual vector<std::pair<string, int64_t>> getFileChunks() const = 0;
179 virtual void forgetFurtherChunks(int64_t maxSize) = 0;
180
183 virtual int getLastError() const = 0;
186 virtual bool isEof() const = 0;
189 virtual int64_t getPos() const = 0;
192 virtual int64_t getChunkPos() const = 0;
197 virtual int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const = 0;
198
203 virtual bool setCachingStrategy(CachingStrategy /*cachingStrategy*/) {
204 return false;
205 }
209 return CachingStrategy::Passive; // default, in particular when there is no caching implemented.
210 }
211
224 MAYBE_UNUSED const vector<std::pair<size_t, size_t>>& sequence,
225 MAYBE_UNUSED bool clearSequence = true) {
226 return false;
227 }
228
229 virtual bool setStatsCallback(const CacheStatsCallbackFunction& /* callback */) {
230 return false;
231 }
232
236 virtual bool purgeCache() {
237 return true;
238 }
239
240 bool isFileHandlerMatch(const FileSpec& fileSpec) const;
241
244 virtual bool isRemoteFileSystem() const = 0;
245
247 virtual bool showProgress() const {
248 return isRemoteFileSystem();
249 }
250};
251
254 public:
255 TemporaryCachingStrategy(unique_ptr<FileHandler>& handler, CachingStrategy temporaryStrategy)
256 : handler_{handler},
257 originalPtr_{handler_.get()},
258 originalStrategy_{handler->getCachingStrategy()} {
259 handler_->setCachingStrategy(temporaryStrategy);
260 }
262 // only restore the original strategy if the handler hasn't been changed
263 if (originalPtr_ == handler_.get()) {
264 originalPtr_->setCachingStrategy(originalStrategy_);
265 }
266 }
267
268 private:
269 unique_ptr<FileHandler>& handler_;
270 FileHandler* originalPtr_;
271 CachingStrategy originalStrategy_;
272};
273
274} // namespace vrs
Class to abstract the delegate file open operation for VRS file.
Definition FileDelegator.h:32
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
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:223
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:247
virtual unique_ptr< FileHandler > makeNew() const =0
virtual bool isOpened() const =0
virtual CachingStrategy getCachingStrategy() const
Definition FileHandler.h:208
int read(vector< T > &vec)
Helper to read a vector of trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:164
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 bool purgeCache()
Definition FileHandler.h:236
virtual int close()=0
virtual bool setCachingStrategy(CachingStrategy)
Definition FileHandler.h:203
virtual int getLastError() const =0
Helper class to temporarily modify a FileHandler's caching strategy.
Definition FileHandler.h:253
Definition Compressor.cpp:113
@ 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:42