VRS
A file format for sensor data.
Loading...
Searching...
No Matches
DiskFile.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 <cstdio>
20
21#include <vrs/WriteFileHandler.h>
22#include <vrs/os/Platform.h>
23
24namespace vrs {
25
26using std::string;
27using std::vector;
28
29class DiskFileChunk;
30
32template <class FileChunk>
34 public:
35 static const std::string& staticName();
36
37 DiskFileT();
38 ~DiskFileT() override;
39
41 std::unique_ptr<FileHandler> makeNew() const override;
42
43 const string& getFileHandlerName() const override;
44
46 int openSpec(const FileSpec& fileSpec) override;
47
49 bool isOpened() const override;
50
52 int create(const string& newFilePath, const map<string, string>& options = {}) override;
54 void forgetFurtherChunks(int64_t fileSize) override;
56 int64_t getTotalSize() const override;
58 vector<std::pair<string, int64_t>> getFileChunks() const override;
60 int close() override;
61
63 int skipForward(int64_t offset) override;
65 int setPos(int64_t offset) override;
66
68 int read(void* buffer, size_t length) override;
70 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
71 int read(T& object) {
72 return read(&object, sizeof(object));
73 }
75 size_t getLastRWSize() const override;
76
78 bool reopenForUpdatesSupported() const override;
80 int reopenForUpdates() override;
82 bool isReadOnly() const override;
84 int write(const void* buffer, size_t length) override;
86 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
87 int write(const T& object) {
88 return write(&object, sizeof(object));
89 }
92 int overwrite(const void* buffer, size_t length) override;
94 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
95 int overwrite(const T& object) {
96 return overwrite(&object, sizeof(object));
97 }
99 int addChunk() override;
101 int truncate() override;
102
104 int getLastError() const override;
106 bool isEof() const override;
108 int64_t getPos() const override;
110 int64_t getChunkPos() const override;
112 int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const override;
114 bool getCurrentChunk(string& outChunkPath, size_t& outChunkIndex) const override;
115
116 bool isRemoteFileSystem() const override;
117
120 static int writeZstdFile(const string& path, const void* data, size_t dataSize);
121 static int writeZstdFile(const string& path, const string& string);
122 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
123 int writeZstdFile(const string& path, const T& object) {
124 return writeZstdFile(path, &object, sizeof(T));
125 }
126 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
127 int writeZstdFile(const string& path, const vector<T>& v) {
128 return writeZstdFile(path, v.data(), v.size() * sizeof(T));
129 }
131 static int readZstdFile(const string& path, vector<char>& outContent);
132 static int readZstdFile(const string& path, string& outString);
133 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
134 int readZstdFile(const string& path, T& object) {
135 return readZstdFile(path, &object, sizeof(T));
136 }
138 static int readZstdFile(const string& path, void* data, size_t dataSize);
139
145 static string readTextFile(const string& path);
150 static int writeTextFile(const string& path, const string& text);
151
152 int parseUri(FileSpec& inOutFileSpec, size_t colonIndex) const override;
153
154 protected:
155 int checkChunks(const vector<string>& chunks);
156 int openChunk(FileChunk* chunk);
157 int closeChunk(FileChunk* chunk);
158 int addChunk(const string& chunkFilePath);
159 bool isLastChunk() const;
160 bool trySetPosInCurrentChunk(int64_t offset);
161
162 map<string, string> options_; // optional options provided in openSpec or createFile
163 std::unique_ptr<vector<FileChunk>> chunks_; // all the chunks, when a VRS file is opened.
164 FileChunk* currentChunk_{}; // always points to the current chunk within chunks_.
165 int filesOpenCount_{};
166
167 size_t lastRWSize_{};
168 int lastError_{};
169 bool readOnly_{true};
170};
171
172using DiskFile = DiskFileT<DiskFileChunk>;
173
174#if (IS_LINUX_PLATFORM() && IS_X86_PLATFORM()) || (IS_WINDOWS_PLATFORM() && IS_VRS_FB_INTERNAL())
175#define VRS_ASYNC_DISKFILE_SUPPORTED() true
176
177class AsyncDiskFileChunk;
178using AsyncDiskFile = DiskFileT<AsyncDiskFileChunk>;
179
180#else
181#define VRS_ASYNC_DISKFILE_SUPPORTED() false
182
183#endif
184
200class AtomicDiskFile : public DiskFile {
201 public:
202 ~AtomicDiskFile() override;
203
204 int create(const string& newFilePath, const map<string, string>& options = {}) override;
205 int close() override;
206
207 void abort();
208
209 private:
210 string finalName_;
211};
212
213} // namespace vrs
Definition DiskFile.h:200
int close() override
Definition DiskFile.cpp:638
int create(const string &newFilePath, const map< string, string > &options={}) override
Definition DiskFile.cpp:633
FileHandler implementation for disk files, with chunked file support.
Definition DiskFile.h:33
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition DiskFile.h:71
int close() override
Close the file.
Definition DiskFile.cpp:69
static int writeTextFile(const string &path, const string &text)
Definition DiskFile.cpp:580
int create(const string &newFilePath, const map< string, string > &options={}) override
Create a new file.
Definition DiskFile.cpp:112
bool isEof() const override
Tell if we are at the end of the last chunk.
Definition DiskFile.cpp:338
static int writeZstdFile(const string &path, const void *data, size_t dataSize)
Definition DiskFile.cpp:494
int write(const void *buffer, size_t length) override
Write to the current chunk, possibly expanding it.
Definition DiskFile.cpp:262
int overwrite(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:95
int openSpec(const FileSpec &fileSpec) override
Open a file in read-only mode.
Definition DiskFile.cpp:92
int64_t getTotalSize() const override
Get the total size of all the chunks considered.
Definition DiskFile.cpp:179
int getLastError() const override
Get the last error code. 0 means no error.
Definition DiskFile.cpp:333
std::unique_ptr< FileHandler > makeNew() const override
Make a new DiskFile object, with a default state.
Definition DiskFile.cpp:59
static string readTextFile(const string &path)
Definition DiskFile.cpp:564
int64_t getChunkPos() const override
Get position in the current chunk.
Definition DiskFile.cpp:457
bool isOpened() const override
Tell if a file is actually open.
Definition DiskFile.cpp:107
int truncate() override
Truncate chunk to the current file position. Use with care.
Definition DiskFile.cpp:313
int reopenForUpdates() override
Switch from read-only to read-write mode.
Definition DiskFile.cpp:240
bool reopenForUpdatesSupported() const override
Tell if modifying files is supported by this FileHandler implementation.
Definition DiskFile.cpp:235
int addChunk() override
Append a new chunk to the current file.
Definition DiskFile.cpp:376
int write(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:87
int read(void *buffer, size_t length) override
Read a number of bytes, in a chunk aware way.
Definition DiskFile.cpp:198
bool getCurrentChunk(string &outChunkPath, size_t &outChunkIndex) const override
Get the path of the current chunk, or an empty string if no chunk is open.
Definition DiskFile.cpp:479
int setPos(int64_t offset) override
Set the file position at an arbitrary position, in a chunk aware way.
Definition DiskFile.cpp:144
int overwrite(const void *buffer, size_t length) override
Definition DiskFile.cpp:279
int64_t getPos() const override
Get the absolute position in the file, in a chunk aware way.
Definition DiskFile.cpp:450
size_t getLastRWSize() const override
Get the number of bytes actually moved by the last read or write operation.
Definition DiskFile.cpp:230
int skipForward(int64_t offset) override
Skip a number of bytes further in the file, in a chunk aware way.
Definition DiskFile.cpp:131
bool isRemoteFileSystem() const override
Definition DiskFile.cpp:489
bool isReadOnly() const override
Find out if the file is in read-only mode.
Definition DiskFile.cpp:257
vector< std::pair< string, int64_t > > getFileChunks() const override
Get the list of chunks, path + size.
Definition DiskFile.cpp:188
static int readZstdFile(const string &path, vector< char > &outContent)
Read a compressed buffer or a string (automatically adjusts the size)
Definition DiskFile.cpp:535
void forgetFurtherChunks(int64_t fileSize) override
Call this method to forget any chunk beyond this file size.
Definition DiskFile.cpp:120
int getChunkRange(int64_t &outChunkOffset, int64_t &outChunkSize) const override
Get range of the current chunk.
Definition DiskFile.cpp:464
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:45
Definition Compressor.cpp:112
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37