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::string;
34using std::vector;
35
37enum class CachingStrategy {
38 Undefined = 0,
39
40 Passive,
41 Streaming,
46
47 COUNT
48};
49
51constexpr const char* kPrefetchCache = "prefetch_cache";
52
53string toString(CachingStrategy cachingStrategy);
54
55template <>
56CachingStrategy toEnum<>(const string& name);
57
74class FileHandler : public FileDelegator {
75 public:
77 struct CacheStats {
78 double startTime;
79 double waitTime;
80 size_t blockReadCount;
81 size_t blockMissingCount;
82 size_t blockPendingCount;
83 size_t sequenceSize;
84 };
85
86 using CacheStatsCallbackFunction = std::function<void(const CacheStats& stats)>;
87
88 FileHandler() = default;
89
91 static unique_ptr<FileHandler> makeOpen(const string& filePath);
92 static unique_ptr<FileHandler> makeOpen(const FileSpec& fileSpec);
93
97 virtual unique_ptr<FileHandler> makeNew() const = 0;
98 virtual const string& getFileHandlerName() const = 0;
99 virtual const string& getWriteFileHandlerName() const; // maybe use another FileHandler for writes
100
104 virtual int open(const string& filePath);
108 virtual int openSpec(const FileSpec& fileSpec) = 0;
109
120 int delegateOpen(const FileSpec& fileSpec, unique_ptr<FileHandler>& outNewDelegate) override;
121
124 virtual bool isOpened() const = 0;
125
128 virtual int64_t getTotalSize() const = 0;
131 virtual int close() = 0;
132
136 virtual int skipForward(int64_t offset) = 0;
140 virtual int setPos(int64_t offset) = 0;
144 virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length) {
145 return !isRemoteFileSystem();
146 }
155 virtual int read(void* buffer, size_t length) = 0;
157 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
158 int read(T& object) {
159 return read(&object, sizeof(object));
160 }
162 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
163 int read(vector<T>& vec) {
164 return read(vec.data(), vec.size() * sizeof(T));
165 }
168 virtual size_t getLastRWSize() const = 0;
169
172 virtual bool isReadOnly() const;
173
176 virtual vector<std::pair<string, int64_t>> getFileChunks() const = 0;
178 virtual void forgetFurtherChunks(int64_t maxSize) = 0;
179
182 virtual int getLastError() const = 0;
185 virtual bool isEof() const = 0;
188 virtual int64_t getPos() const = 0;
191 virtual int64_t getChunkPos() const = 0;
196 virtual int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const = 0;
197
202 virtual bool setCachingStrategy(CachingStrategy /*cachingStrategy*/) {
203 return false;
204 }
208 return CachingStrategy::Passive; // default, in particular when there is no caching implemented.
209 }
210
223 MAYBE_UNUSED const vector<std::pair<size_t, size_t>>& sequence,
224 MAYBE_UNUSED bool clearSequence = true) {
225 return false;
226 }
227
228 virtual bool setStatsCallback(const CacheStatsCallbackFunction& /* callback */) {
229 return false;
230 }
231
235 virtual bool purgeCache() {
236 return true;
237 }
238
239 bool isFileHandlerMatch(const FileSpec& fileSpec) const;
240
243 virtual bool isRemoteFileSystem() const = 0;
244
246 virtual bool showProgress() const {
247 return isRemoteFileSystem();
248 }
249};
250
253 public:
254 TemporaryCachingStrategy(unique_ptr<FileHandler>& handler, CachingStrategy temporaryStrategy)
255 : handler_{handler},
256 originalPtr_{handler_.get()},
257 originalStrategy_{handler->getCachingStrategy()} {
258 handler_->setCachingStrategy(temporaryStrategy);
259 }
261 // only restore the original strategy if the handler hasn't been changed
262 if (originalPtr_ == handler_.get()) {
263 originalPtr_->setCachingStrategy(originalStrategy_);
264 }
265 }
266
267 private:
268 unique_ptr<FileHandler>& handler_;
269 FileHandler* originalPtr_;
270 CachingStrategy originalStrategy_;
271};
272
273} // 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:74
virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length)
Definition FileHandler.h:144
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:222
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:158
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:246
virtual unique_ptr< FileHandler > makeNew() const =0
virtual bool isOpened() const =0
virtual CachingStrategy getCachingStrategy() const
Definition FileHandler.h:207
int read(vector< T > &vec)
Helper to read a vector of trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:163
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:235
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:202
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:252
Definition Compressor.cpp:113
@ Undefined
when not set explicitly
constexpr const char * kPrefetchCache
To enable basic caching for network file handlers.
Definition FileHandler.h:51
CachingStrategy
Caching strategy requests.
Definition FileHandler.h:37
@ 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:77
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:40