iceberg-cpp
Loading...
Searching...
No Matches
file_io.h
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
22#include <cstddef>
23#include <cstdint>
24#include <memory>
25#include <optional>
26#include <span>
27#include <string>
28#include <string_view>
29#include <vector>
30
31#include "iceberg/iceberg_export.h"
32#include "iceberg/result.h"
33#include "iceberg/storage_credential.h"
34
35namespace iceberg {
36
37class SupportsStorageCredentials;
38
40class ICEBERG_EXPORT SeekableInputStream {
41 public:
42 virtual ~SeekableInputStream() = default;
43
45 virtual Result<int64_t> Position() const = 0;
46
48 virtual Status Seek(int64_t position) = 0;
49
51 virtual Result<int64_t> Read(std::span<std::byte> out) = 0;
52
58 virtual Status ReadFully(int64_t position, std::span<std::byte> out) = 0;
59
61 virtual Status Close() = 0;
62};
63
65class ICEBERG_EXPORT PositionOutputStream {
66 public:
67 virtual ~PositionOutputStream() = default;
68
70 virtual Result<int64_t> Position() const = 0;
71
76 virtual Result<int64_t> StoredLength() const { return Position(); }
77
79 virtual Status Write(std::span<const std::byte> data) = 0;
80
82 virtual Status Flush() = 0;
83
85 virtual Status Close() = 0;
86};
87
89class ICEBERG_EXPORT InputFile {
90 public:
91 virtual ~InputFile() = default;
92
94 virtual std::string_view location() const = 0;
95
97 virtual Result<int64_t> Size() const = 0;
98
100 virtual Result<std::unique_ptr<SeekableInputStream>> Open() = 0;
101};
102
104class ICEBERG_EXPORT OutputFile {
105 public:
106 virtual ~OutputFile() = default;
107
109 virtual std::string_view location() const = 0;
110
112 virtual Result<std::unique_ptr<PositionOutputStream>> Create() = 0;
113
115 virtual Result<std::unique_ptr<PositionOutputStream>> CreateOrOverwrite() = 0;
116};
117
125class ICEBERG_EXPORT FileIO {
126 public:
127 FileIO() = default;
128 virtual ~FileIO() = default;
129
131 virtual Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location);
132
137 virtual Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location,
138 size_t length);
139
141 virtual Result<std::unique_ptr<OutputFile>> NewOutputFile(std::string file_location);
142
150 virtual Result<std::string> ReadFile(const std::string& file_location,
151 std::optional<size_t> length);
152
158 virtual Status WriteFile(const std::string& file_location, std::string_view content);
159
164 virtual Status DeleteFile(const std::string& file_location) {
165 return NotImplemented("DeleteFile not implemented");
166 }
167
176 virtual Status DeleteFiles(const std::vector<std::string>& file_locations);
177
180};
181
185class ICEBERG_EXPORT SupportsStorageCredentials {
186 public:
187 virtual ~SupportsStorageCredentials() = default;
188
190 virtual Status SetStorageCredentials(
191 const std::vector<StorageCredential>& storage_credentials) = 0;
192
194 virtual const std::vector<StorageCredential>& credentials() const = 0;
195};
196
197} // namespace iceberg
API for deleting data files from a table.
Definition delete_files.h:44
Pluggable module for reading, writing, and deleting files.
Definition file_io.h:125
virtual SupportsStorageCredentials * AsSupportsStorageCredentials()
Return storage-credential support when implemented by this FileIO.
Definition file_io.h:179
virtual Status DeleteFile(const std::string &file_location)
Delete a file at the given location.
Definition file_io.h:164
Handle for opening a readable file.
Definition file_io.h:89
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:104
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:65
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:76
Seekable byte stream for reading file contents.
Definition file_io.h:40
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:185
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.