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 <vector>
33
35#include "iceberg/result.h"
37
38namespace iceberg {
39
40class SupportsStorageCredentials;
41
43class ICEBERG_EXPORT SeekableInputStream {
44 public:
45 virtual ~SeekableInputStream() = default;
46
48 virtual Result<int64_t> Position() const = 0;
49
51 virtual Status Seek(int64_t position) = 0;
52
54 virtual Result<int64_t> Read(std::span<std::byte> out) = 0;
55
61 virtual Status ReadFully(int64_t position, std::span<std::byte> out) = 0;
62
64 virtual Status Close() = 0;
65};
66
68class ICEBERG_EXPORT PositionOutputStream {
69 public:
70 virtual ~PositionOutputStream() = default;
71
73 virtual Result<int64_t> Position() const = 0;
74
79 virtual Result<int64_t> StoredLength() const { return Position(); }
80
82 virtual Status Write(std::span<const std::byte> data) = 0;
83
85 virtual Status Flush() = 0;
86
88 virtual Status Close() = 0;
89};
90
92class ICEBERG_EXPORT InputFile {
93 public:
94 virtual ~InputFile() = default;
95
97 virtual std::string_view location() const = 0;
98
100 virtual Result<int64_t> Size() const = 0;
101
104};
105
107class ICEBERG_EXPORT OutputFile {
108 public:
109 virtual ~OutputFile() = default;
110
112 virtual std::string_view location() const = 0;
113
116
119};
120
128class ICEBERG_EXPORT FileIO {
129 public:
130 FileIO() = default;
131 virtual ~FileIO() = default;
132
134 virtual Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location);
135
140 virtual Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location,
141 size_t length);
142
144 virtual Result<std::unique_ptr<OutputFile>> NewOutputFile(std::string file_location);
145
153 virtual Result<std::string> ReadFile(const std::string& file_location,
154 std::optional<size_t> length);
155
161 virtual Status WriteFile(const std::string& file_location, std::string_view content);
162
167 virtual Status DeleteFile(const std::string& file_location) {
168 return NotImplemented("DeleteFile not implemented");
169 }
170
179 virtual Status DeleteFiles(const std::vector<std::string>& file_locations);
180
183};
184
188class ICEBERG_EXPORT SupportsStorageCredentials {
189 public:
190 virtual ~SupportsStorageCredentials() = default;
191
193 virtual Status SetStorageCredentials(
194 const std::vector<StorageCredential>& storage_credentials) = 0;
195
197 virtual const std::vector<StorageCredential>& credentials() const = 0;
198};
199
200} // namespace iceberg
Pluggable module for reading, writing, and deleting files.
Definition file_io.h:128
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 SupportsStorageCredentials * AsSupportsStorageCredentials()
Return storage-credential support when implemented by this FileIO.
Definition file_io.h:182
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 DeleteFiles(const std::vector< std::string > &file_locations)
Delete files at the given locations.
virtual Status DeleteFile(const std::string &file_location)
Delete a file at the given location.
Definition file_io.h:167
Handle for opening a readable file.
Definition file_io.h:92
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:107
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:68
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:79
Seekable byte stream for reading file contents.
Definition file_io.h:43
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:188
virtual Status SetStorageCredentials(const std::vector< StorageCredential > &storage_credentials)=0
Install vended storage credentials.
virtual const std::vector< StorageCredential > & credentials() const =0
Return currently installed storage credentials.
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:33
std::expected< T, E > Result
Result alias.
Definition result.h:88
Define Result, Status, and error helpers.
Define storage credential metadata.