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
53constexpr const char* kPrefetchCache = "prefetch_cache";
54
55string toString(CachingStrategy cachingStrategy);
56
57template <>
58CachingStrategy toEnum<>(const string& name);
59
76class FileHandler : public FileDelegator {
77 public:
79 struct CacheStats {
80 double startTime;
81 double waitTime;
82 size_t blockReadCount;
83 size_t blockMissingCount;
84 size_t blockPendingCount;
85 size_t sequenceSize;
86 };
87
88 using CacheStatsCallbackFunction = std::function<void(const CacheStats& stats)>;
89
90 FileHandler() = default;
91
93 static unique_ptr<FileHandler> makeOpen(const string& filePath);
94 static unique_ptr<FileHandler> makeOpen(const FileSpec& fileSpec);
95
99 virtual unique_ptr<FileHandler> makeNew() const = 0;
100 virtual const string& getFileHandlerName() const = 0;
101 virtual const string& getWriteFileHandlerName() const; // maybe use another FileHandler for writes
102
106 virtual int open(const string& filePath);
110 virtual int openSpec(const FileSpec& fileSpec) = 0;
111
122 int delegateOpen(const FileSpec& fileSpec, unique_ptr<FileHandler>& outNewDelegate) override;
123
126 virtual bool isOpened() const = 0;
127
130 virtual int64_t getTotalSize() const = 0;
133 virtual int close() = 0;
134
138 virtual int skipForward(int64_t offset) = 0;
142 virtual int setPos(int64_t offset) = 0;
146 virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length) {
147 return !isRemoteFileSystem();
148 }
157 virtual int read(void* buffer, size_t length) = 0;
159 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
160 int read(T& object) {
161 return read(&object, sizeof(object));
162 }
165 virtual size_t getLastRWSize() const = 0;
166
169 virtual bool isReadOnly() const;
170
173 virtual vector<std::pair<string, int64_t>> getFileChunks() const = 0;
175 virtual void forgetFurtherChunks(int64_t maxSize) = 0;
176
179 virtual int getLastError() const = 0;
182 virtual bool isEof() const = 0;
185 virtual int64_t getPos() const = 0;
188 virtual int64_t getChunkPos() const = 0;
193 virtual int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const = 0;
194
199 virtual bool setCachingStrategy(CachingStrategy /*cachingStrategy*/) {
200 return false;
201 }
205 return CachingStrategy::Passive; // default, in particular when there is no caching implemented.
206 }
207
220 MAYBE_UNUSED const vector<std::pair<size_t, size_t>>& sequence,
221 MAYBE_UNUSED bool clearSequence = true) {
222 return false;
223 }
224
225 virtual bool setStatsCallback(const CacheStatsCallbackFunction& /* callback */) {
226 return false;
227 }
228
232 virtual bool purgeCache() {
233 return true;
234 }
235
236 bool isFileHandlerMatch(const FileSpec& fileSpec) const;
237
240 virtual bool isRemoteFileSystem() const = 0;
241
243 virtual bool showProgress() const {
244 return isRemoteFileSystem();
245 }
246};
247
250 public:
251 TemporaryCachingStrategy(unique_ptr<FileHandler>& handler, CachingStrategy temporaryStrategy)
252 : handler_{handler},
253 originalPtr_{handler_.get()},
254 originalStrategy_{handler->getCachingStrategy()} {
255 handler_->setCachingStrategy(temporaryStrategy);
256 }
258 // only restore the original strategy if the handler hasn't been changed
259 if (originalPtr_ == handler_.get()) {
260 originalPtr_->setCachingStrategy(originalStrategy_);
261 }
262 }
263
264 private:
265 unique_ptr<FileHandler>& handler_;
266 FileHandler* originalPtr_;
267 CachingStrategy originalStrategy_;
268};
269
270} // 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:76
virtual bool isAvailableOrPrefetch(MAYBE_UNUSED size_t length)
Definition FileHandler.h:146
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:219
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition FileHandler.h:160
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:243
virtual unique_ptr< FileHandler > makeNew() const =0
virtual bool isOpened() const =0
virtual CachingStrategy getCachingStrategy() const
Definition FileHandler.h:204
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:232
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:199
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:249
Definition Compressor.cpp:113
@ Undefined
when not set explicitly
constexpr const char * kPrefetchCache
To enable basic caching for network file handlers.
Definition FileHandler.h:53
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:79
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37