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/os/Platform.h>
22
23#include "WriteFileHandler.h"
24
25namespace vrs {
26
27using std::string;
28using std::vector;
29
30class DiskFileChunk;
31
33template <class FileChunk>
35 public:
36 static const std::string& staticName();
37
38 DiskFileT();
39 ~DiskFileT() override;
40
42 std::unique_ptr<FileHandler> makeNew() const override;
43
44 const string& getFileHandlerName() const override;
45
47 int openSpec(const FileSpec& fileSpec) override;
48
50 bool isOpened() const override;
51
53 int create(const string& newFilePath, const map<string, string>& options = {}) override;
55 void forgetFurtherChunks(int64_t fileSize) override;
57 int64_t getTotalSize() const override;
59 vector<std::pair<string, int64_t>> getFileChunks() const override;
61 int close() override;
62
64 int skipForward(int64_t offset) override;
66 int setPos(int64_t offset) override;
67
69 int read(void* buffer, size_t length) override;
71 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
72 int read(T& object) {
73 return read(&object, sizeof(object));
74 }
76 size_t getLastRWSize() const override;
77
79 bool reopenForUpdatesSupported() const override;
81 int reopenForUpdates() override;
83 bool isReadOnly() const override;
85 int write(const void* buffer, size_t length) override;
87 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
88 int write(const T& object) {
89 return write(&object, sizeof(object));
90 }
93 int overwrite(const void* buffer, size_t length) override;
95 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
96 int overwrite(const T& object) {
97 return overwrite(&object, sizeof(object));
98 }
100 int addChunk() override;
102 int truncate() override;
103
105 int getLastError() const override;
107 bool isEof() const override;
109 int64_t getPos() const override;
111 int64_t getChunkPos() const override;
113 int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize) const override;
115 bool getCurrentChunk(string& outChunkPath, size_t& outChunkIndex) const override;
116
117 bool isRemoteFileSystem() const override;
118
121 static int writeZstdFile(const string& path, const void* data, size_t dataSize);
122 static int writeZstdFile(const string& path, const string& string);
123 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
124 int writeZstdFile(const string& path, const T& object) {
125 return writeZstdFile(path, &object, sizeof(T));
126 }
127 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
128 int writeZstdFile(const string& path, const vector<T>& v) {
129 return writeZstdFile(path, v.data(), v.size() * sizeof(T));
130 }
132 static int readZstdFile(const string& path, vector<char>& outContent);
133 static int readZstdFile(const string& path, string& outString);
134 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
135 int readZstdFile(const string& path, T& object) {
136 return readZstdFile(path, &object, sizeof(T));
137 }
139 static int readZstdFile(const string& path, void* data, size_t dataSize);
140
146 static string readTextFile(const string& path);
151 static int writeTextFile(const string& path, const string& text);
152
153 int parseUri(FileSpec& inOutFileSpec, size_t colonIndex) const override;
154
155 protected:
156 int checkChunks(const vector<string>& chunks);
157 int openChunk(FileChunk* chunk);
158 int closeChunk(FileChunk* chunk);
159 int addChunk(const string& chunkFilePath);
160 bool isLastChunk() const;
161 bool trySetPosInCurrentChunk(int64_t offset);
162
163 map<string, string> options_; // optional options provided in openSpec or createFile
164 std::unique_ptr<vector<FileChunk>> chunks_; // all the chunks, when a VRS file is opened.
165 FileChunk* currentChunk_{}; // always points to the current chunk within chunks_.
166 int filesOpenCount_{};
167
168 size_t lastRWSize_{};
169 int lastError_{};
170 bool readOnly_{true};
171};
172
173using DiskFile = DiskFileT<DiskFileChunk>;
174
175#if (IS_LINUX_PLATFORM() && IS_X86_PLATFORM()) || (IS_WINDOWS_PLATFORM() && IS_VRS_FB_INTERNAL())
176#define VRS_ASYNC_DISKFILE_SUPPORTED() true
177
178class AsyncDiskFileChunk;
179using AsyncDiskFile = DiskFileT<AsyncDiskFileChunk>;
180
181#else
182#define VRS_ASYNC_DISKFILE_SUPPORTED() false
183
184#endif
185
201class AtomicDiskFile : public DiskFile {
202 public:
203 ~AtomicDiskFile() override;
204
205 int create(const string& newFilePath, const map<string, string>& options = {}) override;
206 int close() override;
207
208 void abort();
209
210 private:
211 string finalName_;
212};
213
214} // namespace vrs
Definition DiskFile.h:201
int close() override
Definition DiskFile.cpp:639
int create(const string &newFilePath, const map< string, string > &options={}) override
Definition DiskFile.cpp:634
FileHandler implementation for disk files, with chunked file support.
Definition DiskFile.h:34
int read(T &object)
Helper to read trivially copyable objects, in a chunk aware way.
Definition DiskFile.h:72
int close() override
Close the file.
Definition DiskFile.cpp:70
static int writeTextFile(const string &path, const string &text)
Definition DiskFile.cpp:581
int create(const string &newFilePath, const map< string, string > &options={}) override
Create a new file.
Definition DiskFile.cpp:113
bool isEof() const override
Tell if we are at the end of the last chunk.
Definition DiskFile.cpp:339
static int writeZstdFile(const string &path, const void *data, size_t dataSize)
Definition DiskFile.cpp:495
int write(const void *buffer, size_t length) override
Write to the current chunk, possibly expanding it.
Definition DiskFile.cpp:263
int overwrite(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:96
int openSpec(const FileSpec &fileSpec) override
Open a file in read-only mode.
Definition DiskFile.cpp:93
int64_t getTotalSize() const override
Get the total size of all the chunks considered.
Definition DiskFile.cpp:180
int getLastError() const override
Get the last error code. 0 means no error.
Definition DiskFile.cpp:334
std::unique_ptr< FileHandler > makeNew() const override
Make a new DiskFile object, with a default state.
Definition DiskFile.cpp:60
static string readTextFile(const string &path)
Definition DiskFile.cpp:565
int64_t getChunkPos() const override
Get position in the current chunk.
Definition DiskFile.cpp:458
bool isOpened() const override
Tell if a file is actually open.
Definition DiskFile.cpp:108
int truncate() override
Truncate chunk to the current file position. Use with care.
Definition DiskFile.cpp:314
int reopenForUpdates() override
Switch from read-only to read-write mode.
Definition DiskFile.cpp:241
bool reopenForUpdatesSupported() const override
Tell if modifying files is supported by this FileHandler implementation.
Definition DiskFile.cpp:236
int addChunk() override
Append a new chunk to the current file.
Definition DiskFile.cpp:377
int write(const T &object)
Helper for trivially copyable objects.
Definition DiskFile.h:88
int read(void *buffer, size_t length) override
Read a number of bytes, in a chunk aware way.
Definition DiskFile.cpp:199
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:480
int setPos(int64_t offset) override
Set the file position at an arbitrary position, in a chunk aware way.
Definition DiskFile.cpp:145
int overwrite(const void *buffer, size_t length) override
Definition DiskFile.cpp:280
int64_t getPos() const override
Get the absolute position in the file, in a chunk aware way.
Definition DiskFile.cpp:451
size_t getLastRWSize() const override
Get the number of bytes actually moved by the last read or write operation.
Definition DiskFile.cpp:231
int skipForward(int64_t offset) override
Skip a number of bytes further in the file, in a chunk aware way.
Definition DiskFile.cpp:132
bool isRemoteFileSystem() const override
Definition DiskFile.cpp:490
bool isReadOnly() const override
Find out if the file is in read-only mode.
Definition DiskFile.cpp:258
vector< std::pair< string, int64_t > > getFileChunks() const override
Get the list of chunks, path + size.
Definition DiskFile.cpp:189
static int readZstdFile(const string &path, vector< char > &outContent)
Read a compressed buffer or a string (automatically adjusts the size)
Definition DiskFile.cpp:536
void forgetFurtherChunks(int64_t fileSize) override
Call this method to forget any chunk beyond this file size.
Definition DiskFile.cpp:121
int getChunkRange(int64_t &outChunkOffset, int64_t &outChunkSize) const override
Get range of the current chunk.
Definition DiskFile.cpp:465
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:45
Definition AsyncDiskFileChunk.hpp:49
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37