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/FileDelegator.h>
26#include <vrs/FileSpec.h>
27#include <vrs/helpers/EnumTemplates.hpp>
28#include <vrs/os/CompilerAttributes.h>
29
30namespace vrs {
31
32using std::map;
33using std::string;
34using std::vector;
35
37enum class CachingStrategy {
38 Undefined = 0,
39
40 Passive,
41 Streaming,
46
47 COUNT
48};
49
50string toString(CachingStrategy cachingStrategy);
51
52template <>
53CachingStrategy toEnum<>(const string& name);
54
71class FileHandler : public FileDelegator {
72 public:
74 struct CacheStats {
75 double startTime;
76 double waitTime;
77 size_t blockReadCount;
78 size_t blockMissingCount;
79 size_t blockPendingCount;
80 size_t sequenceSize;
81 };
82
83 using CacheStatsCallbackFunction = std::function<void(const CacheStats& stats)>;
84
85 FileHandler() = default;
86
88 static unique_ptr<FileHandler> makeOpen(const string& filePath);
89 static unique_ptr<FileHandler> makeOpen(const FileSpec& fileSpec);
90
94 virtual unique_ptr<FileHandler> makeNew() const = 0;
95 virtual const string& getFileHandlerName() const = 0;
96 virtual const string& getWriteFileHandlerName() const; // maybe use another FileHandler for writes
97
101 virtual int open(const string& filePath);
105 virtual int openSpec(const FileSpec& fileSpec) = 0;
106
117 int delegateOpen(const FileSpec& fileSpec, unique_ptr<FileHandler>& outNewDelegate) override;
118
121 virtual bool isOpened() const = 0;
122
125 virtual int64_t getTotalSize() const = 0;
128 virtual int close() = 0;
129
133 virtual int skipForward(int64_t offset) = 0;
137 virtual int setPos(int64_t offset) = 0;
141 virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length) {
142 return !isRemoteFileSystem();
143 }
152 virtual int read(void* buffer, size_t length) = 0;
154 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
155 int read(T& object) {
156 return read(&object, sizeof(object));
157 }
160 virtual size_t getLastRWSize() const = 0;
161
164 virtual bool isReadOnly() const;
165
168 virtual vector<std::pair<string, int64_t>> getFileChunks() const = 0;
170 virtual void forgetFurtherChunks(int64_t maxSize) = 0;
171
174 virtual int getLastError() const = 0;
177 virtual bool isEof() const = 0;
180 virtual int64_t getPos() const = 0;
183 virtual int64_t getChunkPos() const = 0;
188 virtual int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const = 0;
189
194 virtual bool setCachingStrategy(CachingStrategy /*cachingStrategy*/) {
195 return false;
196 }
200 return CachingStrategy::Passive; // default, in particular when there is no caching implemented.
201 }
202
215 MAYBE_UNUSED const vector<std::pair<size_t, size_t>>& sequence,
216 MAYBE_UNUSED bool clearSequence = true) {
217 return false;
218 }
219
220 virtual bool setStatsCallback(const CacheStatsCallbackFunction& /* callback */) {
221 return false;
222 }
223
227 virtual bool purgeCache() {
228 return true;
229 }
230
231 bool isFileHandlerMatch(const FileSpec& fileSpec) const;
232
235 virtual bool isRemoteFileSystem() const = 0;
236
238 virtual bool showProgress() const {
239 return isRemoteFileSystem();
240 }
241};
242
245 public:
246 TemporaryCachingStrategy(unique_ptr<FileHandler>& handler, CachingStrategy temporaryStrategy)
247 : handler_{handler},
248 originalPtr_{handler_.get()},
249 originalStrategy_{handler->getCachingStrategy()} {
250 handler_->setCachingStrategy(temporaryStrategy);
251 }
253 // only restore the original strategy if the handler hasn't been changed
254 if (originalPtr_ == handler_.get()) {
255 originalPtr_->setCachingStrategy(originalStrategy_);
256 }
257 }
258
259 private:
260 unique_ptr<FileHandler>& handler_;
261 FileHandler* originalPtr_;
262 CachingStrategy originalStrategy_;
263};
264
265} // 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:71
virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length)
Definition FileHandler.h:141
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:103
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:214
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:155
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:238
virtual unique_ptr< FileHandler > makeNew() const =0
virtual bool isOpened() const =0
virtual CachingStrategy getCachingStrategy() const
Definition FileHandler.h:199
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:85
virtual bool purgeCache()
Definition FileHandler.h:227
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:65
virtual int close()=0
virtual bool setCachingStrategy(CachingStrategy)
Definition FileHandler.h:194
virtual int getLastError() const =0
virtual bool isReadOnly() const
Definition FileHandler.cpp:118
Helper class to temporarily modify a FileHandler's caching strategy.
Definition FileHandler.h:244
Definition AsyncDiskFileChunk.hpp:49
@ Undefined
when not set explicitly
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:74
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37