Skip to content

Commit ab43cab

Browse files
committed
feat: add file_io and local fs impl
add reader and writer interfaces Signed-off-by: Junwang Zhao <[email protected]>
1 parent cd82335 commit ab43cab

File tree

12 files changed

+812
-0
lines changed

12 files changed

+812
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ option(ICEBERG_BUILD_SHARED "Build shared library" OFF)
3838
option(ICEBERG_BUILD_TESTS "Build tests" ON)
3939
option(ICEBERG_ARROW "Build Arrow" ON)
4040
option(ICEBERG_AVRO "Build Avro" ON)
41+
option(ICEBERG_IO "Build IO" ON)
4142

4243
include(GNUInstallDirs)
4344
include(FetchContent)

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ install(FILES ${CMAKE_CURRENT_BINARY_DIR}/iceberg_export.h
3131
add_subdirectory(arrow)
3232
add_subdirectory(avro)
3333
add_subdirectory(puffin)
34+
add_subdirectory(io)
3435

3536
iceberg_install_cmake_package(Iceberg iceberg_targets)

src/iceberg/file_io.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 <memory>
23+
#include <string>
24+
#include <unordered_map>
25+
26+
#include "iceberg/iceberg_export.h"
27+
28+
namespace iceberg {
29+
30+
// Forward declarations
31+
class Reader;
32+
class Writer;
33+
34+
/// \brief An interface used to read input files using Reader and AsyncReader
35+
class ICEBERG_EXPORT InputFile {
36+
public:
37+
explicit InputFile(std::string location) : location_(std::move(location)) {}
38+
39+
virtual ~InputFile() = default;
40+
41+
/// \brief Checks whether the file exists.
42+
virtual bool exists() const = 0;
43+
/// \brief Returns the total length of the file, in bytes.
44+
virtual int64_t getLength() const = 0;
45+
46+
/// \brief Get a Reader instance to read bytes from the file.
47+
virtual std::unique_ptr<Reader> newReader() = 0;
48+
49+
/// \brief Get the file location
50+
const std::string& location() const { return location_; }
51+
52+
protected:
53+
std::string location_;
54+
};
55+
56+
/// \brief An interface used to write output files using Writer and AsyncWriter
57+
class ICEBERG_EXPORT OutputFile {
58+
public:
59+
explicit OutputFile(std::string location) : location_(std::move(location)) {}
60+
61+
virtual ~OutputFile() = default;
62+
63+
/// \brief Create the file.
64+
///
65+
/// If the file exists, or an error will be thrown.
66+
virtual void create() = 0;
67+
68+
/// \brief Get a Writer instance to write bytes to the file.
69+
virtual std::unique_ptr<Writer> newWriter() = 0;
70+
71+
/// \brief Get the file location
72+
const std::string& location() const { return location_; }
73+
74+
/// \brief Return an InputFile for the location of this OutputFile.
75+
virtual std::shared_ptr<InputFile> toInputFile() const = 0;
76+
77+
protected:
78+
std::string location_;
79+
};
80+
81+
/// \brief Pluggable module for reading, writing, and deleting files.
82+
///
83+
/// Both table metadata files and data files can be written and read by this module.
84+
class ICEBERG_EXPORT FileIO {
85+
public:
86+
explicit FileIO(std::string name) : name_(std::move(name)) {}
87+
88+
virtual ~FileIO() = default;
89+
90+
/// \brief Get an InputFile
91+
///
92+
/// Get a InputFile instance to read bytes from the file at the given location.
93+
virtual std::shared_ptr<InputFile> newInputFile(const std::string& location) = 0;
94+
95+
/// \brief Get an OutputFile
96+
///
97+
/// Get a OutputFile instance to write bytes to the file at the given location.
98+
virtual std::shared_ptr<OutputFile> newOutputFile(const std::string& location) = 0;
99+
100+
/// \brief Delete file
101+
///
102+
/// Delete the file at the given location.
103+
virtual void DeleteFile(const std::string& location) = 0;
104+
void DeleteFile(const InputFile& ifile) { return DeleteFile(ifile.location()); }
105+
void DeleteFile(const OutputFile& ofile) { return DeleteFile(ofile.location()); }
106+
107+
const std::string& name() const { return name_; }
108+
109+
protected:
110+
std::string name_;
111+
};
112+
113+
} // namespace iceberg

src/iceberg/io/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
if(NOT ICEBERG_IO)
19+
return()
20+
endif()
21+
22+
set(ICEBERG_IO_SOURCES fs_file_io.cc)
23+
set(ICEBERG_IO_INCLUDES "${ICEBERG_INCLUDES}")
24+
25+
add_iceberg_lib(iceberg_io
26+
SOURCES
27+
${ICEBERG_IO_SOURCES}
28+
PRIVATE_INCLUDES
29+
${ICEBERG_IO_INCLUDES})
30+
31+
iceberg_install_all_headers(iceberg/io)

src/iceberg/io/fs_file_io.cc

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
#include "iceberg/io/fs_file_io.h"
21+
22+
#include <fcntl.h>
23+
24+
#include <cassert>
25+
#include <filesystem>
26+
#include <format>
27+
28+
#include <sys/stat.h>
29+
30+
#include "iceberg/exception.h"
31+
32+
namespace iceberg::io {
33+
34+
bool FsInputFile::exists() const { return std::filesystem::exists(location()); }
35+
36+
int64_t FsInputFile::getLength() const {
37+
struct stat stat_buffer;
38+
if (stat(location().c_str(), &stat_buffer) != 0) {
39+
throw IcebergError(std::format(
40+
"Failed to get file length. File does not exist or is inaccessible: {}",
41+
location_));
42+
}
43+
return stat_buffer.st_size;
44+
}
45+
46+
void FsOutputFile::create() {
47+
// Check if the file already exists
48+
std::ifstream existing_file(location_);
49+
bool file_exists = existing_file.good();
50+
existing_file.close();
51+
52+
if (file_exists) {
53+
throw IcebergError(std::format("File already exists: {}", location_));
54+
}
55+
56+
// Create or overwrite the file by opening it in truncating mode
57+
std::ofstream new_file(location_, std::ios::binary | std::ios::out | std::ios::trunc);
58+
if (!new_file.is_open()) {
59+
throw IcebergError(std::format("Failed to create or overwrite file: {}", location_));
60+
}
61+
new_file.close();
62+
}
63+
64+
std::shared_ptr<InputFile> FsFileIO::newInputFile(const std::string& location) {
65+
// Check if the file exists
66+
if (!fileExists(location)) {
67+
throw IcebergError(std::format("InputFile does not exist: {}", location));
68+
}
69+
70+
// Create and return an FsInputFile instance
71+
return std::make_shared<FsInputFile>(location);
72+
}
73+
74+
std::shared_ptr<OutputFile> FsFileIO::newOutputFile(const std::string& location) {
75+
return std::make_shared<FsOutputFile>(location);
76+
}
77+
78+
void FsFileIO::DeleteFile(const std::string& location) {
79+
// Check if the file exists
80+
if (!fileExists(location)) {
81+
throw IcebergError(std::format("InputFile does not exist: {}", location));
82+
}
83+
std::error_code ec;
84+
if (std::filesystem::remove(location, ec) == false) {
85+
throw IcebergError(
86+
std::format("Failed to delete file: {}, error code: {}", location, ec.message()));
87+
}
88+
}
89+
90+
} // namespace iceberg::io

src/iceberg/io/fs_file_io.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 "iceberg/file_io.h"
23+
#include "iceberg/io/fs_file_reader.h"
24+
#include "iceberg/io/fs_file_writer.h"
25+
26+
namespace iceberg::io {
27+
28+
class ICEBERG_EXPORT FsInputFile : public InputFile {
29+
public:
30+
explicit FsInputFile(std::string location) : InputFile(std::move(location)) {}
31+
~FsInputFile() = default;
32+
33+
/// \brief Checks whether the file exists.
34+
bool exists() const override;
35+
36+
/// \brief Returns the total length of the file, in bytes.
37+
int64_t getLength() const override;
38+
39+
/// \brief Get a Reader instance to read bytes from the file.
40+
std::unique_ptr<Reader> newReader() override {
41+
return std::make_unique<FsFileReader>(location_);
42+
}
43+
};
44+
45+
class ICEBERG_EXPORT FsOutputFile : public OutputFile {
46+
public:
47+
explicit FsOutputFile(std::string location) : OutputFile(std::move(location)) {}
48+
~FsOutputFile() = default;
49+
50+
/// \brief Create the file, optionally overwriting if it exists.
51+
///
52+
/// If the file already exists, an exception is thrown.
53+
void create() override;
54+
55+
/// \brief Get a Writer instance to write bytes to the file.
56+
///
57+
/// Returns a unique pointer to a `FsFileWriter`.
58+
std::unique_ptr<Writer> newWriter() override {
59+
return std::make_unique<FsFileWriter>(location_);
60+
}
61+
62+
/// \brief Return an InputFile for the location of this OutputFile.
63+
///
64+
/// Creates an FsInputFile for reading the file pointed by this OutputFile's location.
65+
std::shared_ptr<InputFile> toInputFile() const override {
66+
return std::make_shared<FsInputFile>(location_);
67+
}
68+
};
69+
70+
/// \brief A concrete implementation of FileIO for file system.
71+
class ICEBERG_EXPORT FsFileIO : public FileIO {
72+
public:
73+
explicit FsFileIO(const std::string& name) : FileIO(name) {}
74+
75+
~FsFileIO() override = default;
76+
77+
/// \brief Get an InputFile
78+
///
79+
/// Returns a shared pointer to an FsInputFile instance, representing the file at
80+
/// `location`.
81+
std::shared_ptr<InputFile> newInputFile(const std::string& location) override;
82+
83+
/// \brief Get an OutputFile
84+
///
85+
/// Returns a shared pointer to an FsOutputFile instance, representing the file at
86+
/// `location`.
87+
std::shared_ptr<OutputFile> newOutputFile(const std::string& location) override;
88+
89+
/// \brief Delete file
90+
///
91+
/// Deletes the file at the given location using file system operations.
92+
void DeleteFile(const std::string& location) override;
93+
94+
private:
95+
/// \brief Check if a file exists
96+
bool fileExists(const std::string& location) {
97+
std::ifstream file(location);
98+
return file.good();
99+
}
100+
};
101+
102+
} // namespace iceberg::io

0 commit comments

Comments
 (0)