VRS
A file format for sensor data.
Loading...
Searching...
No Matches
WriteFileHandler.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 <vrs/ErrorCode.h>
20#include <vrs/FileHandler.h>
21#include <vrs/os/CompilerAttributes.h>
22
23namespace vrs {
24
43 public:
44 WriteFileHandler() = default;
45
47 static unique_ptr<WriteFileHandler> make(const string& fileHandlerName);
48
52 virtual int create(const FileSpec& spec) {
53 return spec.chunks.empty() ? INVALID_FILE_SPEC : create(spec.chunks.front(), spec.extras);
54 }
55
60 virtual int create(const string& newFilePath, const FileSpec::Extras& options = {}) = 0;
61
66 virtual int createSplitFile(const FileSpec& spec, const string& initialFilePath) {
67 // create the (first) user record chunk
68 if (spec.chunks.size() == 1) {
69 return create(spec.chunks.front() + "_1", spec.extras);
70 } else {
71 return create(initialFilePath, spec.extras);
72 }
73 }
74
79 virtual void addSplitHead(MAYBE_UNUSED FileSpec& inOutSpec) {}
80
83 virtual bool reopenForUpdatesSupported() const = 0;
84
88 virtual int reopenForUpdates() = 0;
89
92 bool isReadOnly() const override = 0;
93
99 virtual int write(const void* buffer, size_t length) = 0;
101 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
102 int write(const T& object) {
103 return write(&object, sizeof(object));
104 }
105
111 virtual int overwrite(const void* buffer, size_t length) = 0;
113 template <typename T, std::enable_if_t<std::is_trivially_copyable<T>::value, int> = 0>
114 int overwrite(const T& object) {
115 return overwrite(&object, sizeof(object));
116 }
117
121 virtual int addChunk() = 0;
124 virtual int truncate() = 0;
126 virtual bool getCurrentChunk(string& outChunkPath, size_t& outChunkIndex) const = 0;
127};
128
129} // namespace vrs
Class to abstract VRS file system operations, to enable support for alternate storage methods,...
Definition FileHandler.h:74
The WriteFileHandler interface adds write operations to the FileHandler interface.
Definition WriteFileHandler.h:42
int write(const T &object)
Helper for trivially copyable objects.
Definition WriteFileHandler.h:102
virtual void addSplitHead(MAYBE_UNUSED FileSpec &inOutSpec)
Definition WriteFileHandler.h:79
virtual bool reopenForUpdatesSupported() const =0
virtual int overwrite(const void *buffer, size_t length)=0
int overwrite(const T &object)
Helper for trivially copyable objects.
Definition WriteFileHandler.h:114
virtual int truncate()=0
bool isReadOnly() const override=0
virtual int reopenForUpdates()=0
virtual int write(const void *buffer, size_t length)=0
virtual int addChunk()=0
static unique_ptr< WriteFileHandler > make(const string &fileHandlerName)
Create a new WriteFileHandler from a name.
Definition FileHandlerFactory.cpp:213
virtual int create(const string &newFilePath, const FileSpec::Extras &options={})=0
virtual int create(const FileSpec &spec)
Definition WriteFileHandler.h:52
virtual bool getCurrentChunk(string &outChunkPath, size_t &outChunkIndex) const =0
Get the path of the current chunk, or an empty string if no chunk is open.
virtual int createSplitFile(const FileSpec &spec, const string &initialFilePath)
Definition WriteFileHandler.h:66
Definition Compressor.cpp:113
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:40
map< string, string, std::less<> > Extras
Definition FileSpec.h:43