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
30#include "iceberg/iceberg_export.h"
31#include "iceberg/result.h"
32
33namespace iceberg {
34
36class ICEBERG_EXPORT SeekableInputStream {
37 public:
38 virtual ~SeekableInputStream() = default;
39
41 virtual Result<int64_t> Position() const = 0;
42
44 virtual Status Seek(int64_t position) = 0;
45
47 virtual Result<int64_t> Read(std::span<std::byte> out) = 0;
48
54 virtual Status ReadFully(int64_t position, std::span<std::byte> out) = 0;
55
57 virtual Status Close() = 0;
58};
59
61class ICEBERG_EXPORT PositionOutputStream {
62 public:
63 virtual ~PositionOutputStream() = default;
64
66 virtual Result<int64_t> Position() const = 0;
67
69 virtual Status Write(std::span<const std::byte> data) = 0;
70
72 virtual Status Flush() = 0;
73
75 virtual Status Close() = 0;
76};
77
79class ICEBERG_EXPORT InputFile {
80 public:
81 virtual ~InputFile() = default;
82
84 virtual std::string_view location() const = 0;
85
87 virtual Result<int64_t> Size() const = 0;
88
90 virtual Result<std::unique_ptr<SeekableInputStream>> Open() = 0;
91};
92
94class ICEBERG_EXPORT OutputFile {
95 public:
96 virtual ~OutputFile() = default;
97
99 virtual std::string_view location() const = 0;
100
102 virtual Result<std::unique_ptr<PositionOutputStream>> Create() = 0;
103
105 virtual Result<std::unique_ptr<PositionOutputStream>> CreateOrOverwrite() = 0;
106};
107
115class ICEBERG_EXPORT FileIO {
116 public:
117 FileIO() = default;
118 virtual ~FileIO() = default;
119
121 virtual Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location);
122
127 virtual Result<std::unique_ptr<InputFile>> NewInputFile(std::string file_location,
128 size_t length);
129
131 virtual Result<std::unique_ptr<OutputFile>> NewOutputFile(std::string file_location);
132
140 virtual Result<std::string> ReadFile(const std::string& file_location,
141 std::optional<size_t> length);
142
148 virtual Status WriteFile(const std::string& file_location, std::string_view content);
149
154 virtual Status DeleteFile(const std::string& file_location) {
155 return NotImplemented("DeleteFile not implemented");
156 }
157};
158
159} // namespace iceberg
Pluggable module for reading, writing, and deleting files.
Definition file_io.h:115
virtual Status DeleteFile(const std::string &file_location)
Delete a file at the given location.
Definition file_io.h:154
Handle for opening a readable file.
Definition file_io.h:79
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:94
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:61
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.
Seekable byte stream for reading file contents.
Definition file_io.h:36
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.