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
47enum class CachingStrategy {
48 Undefined = 0,
49
50 Passive,
54 Streaming,
69
70 COUNT
71};
72
74constexpr const char* kPrefetchCache = "prefetch_cache";
75
76VRS_API string toString(CachingStrategy cachingStrategy);
77
78template <>
79VRS_API CachingStrategy toEnum<>(const string& name);
80
97class VRS_API FileHandler : public FileDelegator {
98 public:
100 struct CacheStats {
101 double startTime;
102 double waitTime;
103 size_t blockReadCount;
104 size_t blockMissingCount;
105 size_t blockPendingCount;
106 size_t sequenceSize;
107 };
108
109 using CacheStatsCallbackFunction = std::function<void(const CacheStats& stats)>;
110
111 FileHandler() = default;
112
114 static unique_ptr<FileHandler> makeOpen(const string& filePath);
115 static unique_ptr<FileHandler> makeOpen(const FileSpec& fileSpec);
116
120 virtual unique_ptr<FileHandler> makeNew() const = 0;
121 virtual const string& getFileHandlerName() const = 0;
122 virtual const string& getWriteFileHandlerName() const; // maybe use another FileHandler for writes
123
127 virtual int open(const string& filePath);
131 virtual int openSpec(const FileSpec& fileSpec) = 0;
132
143 int delegateOpen(const FileSpec& fileSpec, unique_ptr<FileHandler>& outNewDelegate) override;
144
147 virtual bool isOpened() const = 0;
148
151 virtual int64_t getTotalSize() const = 0;
154 virtual int close() = 0;
155
159 virtual int skipForward(int64_t offset) = 0;
163 virtual int setPos(int64_t offset) = 0;
167 virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length) {
168 return !isRemoteFileSystem();
169 }
178 virtual int read(void* buffer, size_t length) = 0;
180 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
181 int read(T& object) {
182 return read(&object, sizeof(object));
183 }
185 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
186 int read(vector<T>& vec) {
187 return read(vec.data(), vec.size() * sizeof(T));
188 }
191 virtual size_t getLastRWSize() const = 0;
192
195 virtual bool isReadOnly() const;
196
199 virtual vector<std::pair<string, int64_t>> getFileChunks() const = 0;
201 virtual void forgetFurtherChunks(int64_t maxSize) = 0;
202
205 virtual int getLastError() const = 0;
208 virtual bool isEof() const = 0;
211 virtual int64_t getPos() const = 0;
214 virtual int64_t getChunkPos() const = 0;
219 virtual int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const = 0;
220
225 virtual bool setCachingStrategy(CachingStrategy /*cachingStrategy*/) {
226 return false;
227 }
231 return CachingStrategy::Passive; // default, in particular when there is no caching implemented.
232 }
233
246 MAYBE_UNUSED const vector<std::pair<size_t, size_t>>& sequence,
247 MAYBE_UNUSED bool clearSequence = true) {
248 return false;
249 }
250
251 virtual bool setStatsCallback(const CacheStatsCallbackFunction& /* callback */) {
252 return false;
253 }
254
258 virtual bool purgeCache() {
259 return true;
260 }
261
262 bool isFileHandlerMatch(const FileSpec& fileSpec) const;
263
266 virtual bool isRemoteFileSystem() const = 0;
267
269 virtual bool showProgress() const {
270 return isRemoteFileSystem();
271 }
272};
273
276 public:
277 TemporaryCachingStrategy(unique_ptr<FileHandler>& handler, CachingStrategy temporaryStrategy)
278 : handler_{handler},
279 originalPtr_{handler_.get()},
280 originalStrategy_{handler->getCachingStrategy()} {
281 handler_->setCachingStrategy(temporaryStrategy);
282 }
284 // only restore the original strategy if the handler hasn't been changed
285 if (originalPtr_ == handler_.get()) {
286 originalPtr_->setCachingStrategy(originalStrategy_);
287 }
288 }
289
290 private:
291 unique_ptr<FileHandler>& handler_;
292 FileHandler* originalPtr_;
293 CachingStrategy originalStrategy_;
294};
295
296} // 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:97
virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length)
Definition FileHandler.h:167
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:245
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:181
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:269
virtual unique_ptr< FileHandler > makeNew() const =0
virtual bool isOpened() const =0
virtual CachingStrategy getCachingStrategy() const
Definition FileHandler.h:230
int read(vector< T > &vec)
Helper to read a vector of trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:186
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:258
virtual int close()=0
virtual bool setCachingStrategy(CachingStrategy)
Definition FileHandler.h:225
virtual int getLastError() const =0
Helper class to temporarily modify a FileHandler's caching strategy.
Definition FileHandler.h:275
Definition Compressor.cpp:113
@ Undefined
when not set explicitly
constexpr const char * kPrefetchCache
To enable basic caching for network file handlers.
Definition FileHandler.h:74
CachingStrategy
Definition FileHandler.h:47
Stats for cache.
Definition FileHandler.h:100
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:42