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/helpers/EnumTemplates.hpp>
26#include <vrs/os/CompilerAttributes.h>
27
28#include "ErrorCode.h"
29#include "FileDelegator.h"
30#include "FileSpec.h"
31
32namespace vrs {
33
34using std::map;
35using std::string;
36using std::vector;
37
39enum class CachingStrategy {
40 Undefined = 0,
41
42 Passive,
43 Streaming,
48
49 COUNT
50};
51
52string toString(CachingStrategy cachingStrategy);
53
54template <>
55CachingStrategy toEnum<>(const string& name);
56
73class FileHandler : public FileDelegator {
74 public:
76 struct CacheStats {
77 double startTime;
78 double waitTime;
79 size_t blockReadCount;
80 size_t blockMissingCount;
81 size_t blockPendingCount;
82 size_t sequenceSize;
83 };
84
85 using CacheStatsCallbackFunction = std::function<void(const CacheStats& stats)>;
86
87 FileHandler() = default;
88
90 static unique_ptr<FileHandler> makeOpen(const string& filePath);
91 static unique_ptr<FileHandler> makeOpen(const FileSpec& fileSpec);
92
96 virtual unique_ptr<FileHandler> makeNew() const = 0;
97 virtual const string& getFileHandlerName() const = 0;
98 virtual const string& getWriteFileHandlerName() const; // maybe use another FileHandler for writes
99
103 virtual int open(const string& filePath);
107 virtual int openSpec(const FileSpec& fileSpec) = 0;
108
119 int delegateOpen(const FileSpec& fileSpec, unique_ptr<FileHandler>& outNewDelegate) override;
120
123 virtual bool isOpened() const = 0;
124
127 virtual int64_t getTotalSize() const = 0;
130 virtual int close() = 0;
131
135 virtual int skipForward(int64_t offset) = 0;
139 virtual int setPos(int64_t offset) = 0;
143 virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length) {
144 return !isRemoteFileSystem();
145 }
154 virtual int read(void* buffer, size_t length) = 0;
156 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
157 int read(T& object) {
158 return read(&object, sizeof(object));
159 }
162 virtual size_t getLastRWSize() const = 0;
163
166 virtual bool isReadOnly() const;
167
170 virtual vector<std::pair<string, int64_t>> getFileChunks() const = 0;
172 virtual void forgetFurtherChunks(int64_t maxSize) = 0;
173
176 virtual int getLastError() const = 0;
179 virtual bool isEof() const = 0;
182 virtual int64_t getPos() const = 0;
185 virtual int64_t getChunkPos() const = 0;
190 virtual int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const = 0;
191
196 virtual bool setCachingStrategy(CachingStrategy /*cachingStrategy*/) {
197 return false;
198 }
202 return CachingStrategy::Passive; // default, in particular when there is no caching implemented.
203 }
204
217 MAYBE_UNUSED const vector<std::pair<size_t, size_t>>& sequence,
218 MAYBE_UNUSED bool clearSequence = true) {
219 return false;
220 }
221
222 virtual bool setStatsCallback(const CacheStatsCallbackFunction& /* callback */) {
223 return false;
224 }
225
229 virtual bool purgeCache() {
230 return true;
231 }
232
233 bool isFileHandlerMatch(const FileSpec& fileSpec) const;
234
237 virtual bool isRemoteFileSystem() const = 0;
238
240 virtual bool showProgress() const {
241 return isRemoteFileSystem();
242 }
243};
244
247 public:
248 TemporaryCachingStrategy(unique_ptr<FileHandler>& handler, CachingStrategy temporaryStrategy)
249 : handler_{handler},
250 originalPtr_{handler_.get()},
251 originalStrategy_{handler->getCachingStrategy()} {
252 handler_->setCachingStrategy(temporaryStrategy);
253 }
255 // only restore the original strategy if the handler hasn't been changed
256 if (originalPtr_ == handler_.get()) {
257 originalPtr_->setCachingStrategy(originalStrategy_);
258 }
259 }
260
261 private:
262 unique_ptr<FileHandler>& handler_;
263 FileHandler* originalPtr_;
264 CachingStrategy originalStrategy_;
265};
266
267} // 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:73
virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length)
Definition FileHandler.h:143
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:95
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:216
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:157
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:240
virtual unique_ptr< FileHandler > makeNew() const =0
virtual bool isOpened() const =0
virtual CachingStrategy getCachingStrategy() const
Definition FileHandler.h:201
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:77
virtual bool purgeCache()
Definition FileHandler.h:229
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:57
virtual int close()=0
virtual bool setCachingStrategy(CachingStrategy)
Definition FileHandler.h:196
virtual int getLastError() const =0
virtual bool isReadOnly() const
Definition FileHandler.cpp:110
Helper class to temporarily modify a FileHandler's caching strategy.
Definition FileHandler.h:246
Definition Compressor.cpp:113
@ Undefined
when not set explicitly
CachingStrategy
Caching strategy requests.
Definition FileHandler.h:39
@ 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:76
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37