21#include <vrs/os/Platform.h>
23#include "WriteFileHandler.h"
33template <
class FileChunk>
36 static const std::string& staticName();
42 std::unique_ptr<FileHandler>
makeNew()
const override;
44 const string& getFileHandlerName()
const override;
53 int create(
const string& newFilePath,
const map<string, string>& options = {})
override;
59 vector<std::pair<string, int64_t>>
getFileChunks()
const override;
66 int setPos(int64_t offset)
override;
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>
73 return read(&
object,
sizeof(
object));
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>
89 return write(&
object,
sizeof(
object));
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>
97 return overwrite(&
object,
sizeof(
object));
107 bool isEof()
const override;
109 int64_t
getPos()
const override;
113 int getChunkRange(int64_t& outChunkOffset, int64_t& outChunkSize)
const override;
115 bool getCurrentChunk(
string& outChunkPath,
size_t& outChunkIndex)
const override;
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>
127 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value,
int> = 0>
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>
139 static int readZstdFile(
const string& path,
void* data,
size_t dataSize);
151 static int writeTextFile(
const string& path,
const string& text);
153 int parseUri(FileSpec& inOutFileSpec,
size_t colonIndex)
const override;
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);
163 map<string, string> options_;
164 std::unique_ptr<vector<FileChunk>> chunks_;
165 FileChunk* currentChunk_{};
166 int filesOpenCount_{};
168 size_t lastRWSize_{};
170 bool readOnly_{
true};
173using DiskFile = DiskFileT<DiskFileChunk>;
175#if (IS_LINUX_PLATFORM() && IS_X86_PLATFORM()) || (IS_WINDOWS_PLATFORM() && IS_VRS_FB_INTERNAL())
176#define VRS_ASYNC_DISKFILE_SUPPORTED() true
178class AsyncDiskFileChunk;
179using AsyncDiskFile = DiskFileT<AsyncDiskFileChunk>;
182#define VRS_ASYNC_DISKFILE_SUPPORTED() false
205 int create(
const string& newFilePath,
const map<string, string>& options = {})
override;
206 int close()
override;
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