iceberg-cpp
Loading...
Searching...
No Matches
file_io.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
24
25#include <cstddef>
26#include <cstdint>
27#include <memory>
28#include <optional>
29#include <span>
30#include <string>
31#include <string_view>
32#include <unordered_map>
33#include <utility>
34#include <vector>
35
37#include "iceberg/result.h"
39
40namespace iceberg {
41
42class SupportsStorageCredentials;
43
45class ICEBERG_EXPORT SeekableInputStream {
46 public:
47 virtual ~SeekableInputStream() = default;
48
50 virtual Result<int64_t> Position() const = 0;
51
53 virtual Status Seek(int64_t position) = 0;
54
56 virtual Result<int64_t> Read(std::span<std::byte> out) = 0;
57
63 virtual Status ReadFully(int64_t position, std::span<std::byte> out) = 0;
64
66 virtual Status Close() = 0;
67};
68
70class ICEBERG_EXPORT PositionOutputStream {
71 public:
72 virtual ~PositionOutputStream() = default;
73
75 virtual Result<int64_t> Position() const = 0;
76
81 virtual Result<int64_t> StoredLength() const { return Position(); }
82
84 virtual Status Write(std::span<const std::byte> data) = 0;
85
87 virtual Status Flush() = 0;
88
90 virtual Status Close() = 0;
91};
92
94class ICEBERG_EXPORT InputFile {
95 public:
96 virtual ~InputFile() = default;
97
99 virtual std::string_view location() const = 0;
100
102 virtual Result<int64_t> Size() const = 0;
103
106};
107
109class ICEBERG_EXPORT OutputFile {
110 public:
111 virtual ~OutputFile() = default;
112
114 virtual std::string_view location() const = 0;
115
118
121};
122
130class ICEBERG_EXPORT FileIO {
131 public:
132 FileIO() = default;
133 virtual ~FileIO() = default;
134
136 virtual Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location);
137
142 virtual Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location,
143 size_t length);
144
146 virtual Result<std::unique_ptr<OutputFile>> NewOutputFile(std::string file_location);
147
155 virtual Result<std::string> ReadFile(const std::string& file_location,
156 std::optional<size_t> length);
157
163 virtual Status WriteFile(const std::string& file_location, std::string_view content);
164
169 virtual Status DeleteFile(const std::string& /*file_location*/) {
170 return NotImplemented("DeleteFile not implemented");
171 }
172
181 virtual Status DeleteFiles(const std::vector<std::string>& file_locations);
182
185
190 virtual const std::unordered_map<std::string, std::string>& properties() const {
191 static const std::unordered_map<std::string, std::string> kEmpty;
192 return kEmpty;
193 }
194};
195
199class ICEBERG_EXPORT SupportsStorageCredentials {
200 public:
201 virtual ~SupportsStorageCredentials() = default;
202
205 const std::vector<StorageCredential>& storage_credentials) = 0;
206
211 virtual std::vector<StorageCredential> credentials() const = 0;
212
217 const std::vector<StorageCredential>& storage_credentials,
218 std::shared_ptr<StorageCredentialProvider> provider) {
219 if (provider) {
220 return NotSupported("Credential refresh is not supported");
221 }
222 return SetStorageCredentials(storage_credentials);
223 }
224};
225
226} // namespace iceberg
Pluggable module for reading, writing, and deleting files.
Definition file_io.h:130
virtual Result< std::unique_ptr< InputFile > > NewInputFile(std::string file_location, size_t length)
Create an input file handle for the given location with a known length.
virtual Result< std::unique_ptr< OutputFile > > NewOutputFile(std::string file_location)
Create an output file handle for the given location.
virtual const std::unordered_map< std::string, std::string > & properties() const
Return the configuration used to create this FileIO.
Definition file_io.h:190
virtual SupportsStorageCredentials * AsSupportsStorageCredentials()
Return storage-credential support when implemented by this FileIO.
Definition file_io.h:184
virtual Status WriteFile(const std::string &file_location, std::string_view content)
Write the given content to the file at the given location.
virtual Result< std::unique_ptr< InputFile > > NewInputFile(std::string file_location)
Create an input file handle for the given location.
virtual Result< std::string > ReadFile(const std::string &file_location, std::optional< size_t > length)
Read the content of the file at the given location.
virtual Status DeleteFile(const std::string &)
Delete a file at the given location.
Definition file_io.h:169
virtual Status DeleteFiles(const std::vector< std::string > &file_locations)
Delete files at the given locations.
Handle for opening a readable file.
Definition file_io.h:94
virtual std::string_view location() const =0
File location represented by this handle.
virtual Result< std::unique_ptr< SeekableInputStream > > Open()=0
Open a new independent input stream.
virtual Result< int64_t > Size() const =0
Return the total file size in bytes.
Handle for creating a writable file.
Definition file_io.h:109
virtual Result< std::unique_ptr< PositionOutputStream > > CreateOrOverwrite()=0
Create a new output stream, replacing any existing file.
virtual std::string_view location() const =0
File location represented by this handle.
virtual Result< std::unique_ptr< PositionOutputStream > > Create()=0
Create a new output stream and fail if the file already exists.
Positioned byte stream for writing file contents.
Definition file_io.h:70
virtual Result< int64_t > Position() const =0
Return the current write position.
virtual Status Write(std::span< const std::byte > data)=0
Write all bytes in data at the current position.
virtual Status Flush()=0
Flush buffered data to the underlying store.
virtual Status Close()=0
Close the stream. Implementations should allow repeated Close calls.
virtual Result< int64_t > StoredLength() const
Return the current stored length of the output.
Definition file_io.h:81
Seekable byte stream for reading file contents.
Definition file_io.h:45
virtual Result< int64_t > Position() const =0
Return the current read position.
virtual Result< int64_t > Read(std::span< std::byte > out)=0
Read up to out.size() bytes from the current position.
virtual Status Seek(int64_t position)=0
Seek to an absolute byte position.
virtual Status Close()=0
Close the stream. Implementations should allow repeated Close calls.
virtual Status ReadFully(int64_t position, std::span< std::byte > out)=0
Read exactly out.size() bytes from an absolute position.
Mix-in for FileIO implementations that route object paths to per-prefix file systems built from vende...
Definition file_io.h:199
virtual Status SetStorageCredentials(const std::vector< StorageCredential > &storage_credentials)=0
Install vended storage credentials.
virtual std::vector< StorageCredential > credentials() const =0
Return the storage credentials this FileIO holds.
virtual Status InitializeStorageCredentials(const std::vector< StorageCredential > &storage_credentials, std::shared_ptr< StorageCredentialProvider > provider)
Install initial credentials and their refresh source before first use.
Definition file_io.h:216
Definition expected.h:1199
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
Define Result, Status, and error helpers.
Define storage credential metadata.