Skip to content

Commit

Permalink
feat: add file_io and local fs impl
Browse files Browse the repository at this point in the history
Signed-off-by: Junwang Zhao <[email protected]>
  • Loading branch information
zhjwpku committed Jan 15, 2025
1 parent 9a04468 commit 514ded9
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

set(ICEBERG_SOURCES demo_table.cc)
set(ICEBERG_SOURCES demo_table.cc io/fs_file_io.cc)

add_iceberg_lib(iceberg
SOURCES
Expand Down
77 changes: 77 additions & 0 deletions src/iceberg/io/file_io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#pragma once

#include <string>

#include "iceberg/iceberg_export.h"

namespace iceberg {
namespace io {

class ICEBERG_EXPORT InputFile {
public:
explicit InputFile(std::string path) : path_(std::move(path)) {}

virtual ~InputFile() = default;

virtual bool exists() const = 0;

const std::string& path() const { return path_; }

protected:
std::string path_;
};

class ICEBERG_EXPORT OutputFile {
public:
explicit OutputFile(std::string path) : path_(std::move(path)) {}

virtual ~OutputFile() = default;

virtual void create() = 0;

const std::string& path() const { return path_; }

protected:
std::string path_;
};

class ICEBERG_EXPORT FileIO {
public:
explicit FileIO(std::string name) : name_(std::move(name)) {}

virtual ~FileIO() = default;

virtual std::shared_ptr<InputFile> newInputFile(const std::string& path) = 0;
virtual std::shared_ptr<OutputFile> newOutputFile(const std::string& path) = 0;

virtual void DeleteFile(const std::string& path) = 0;
void DeleteFile(const InputFile& ifile) { return DeleteFile(ifile.path()); }
void DeleteFile(const OutputFile& ofile) { return DeleteFile(ofile.path()); }

const std::string& name() const { return name_; }

protected:
std::string name_;
};

} // namespace io
} // namespace iceberg
54 changes: 54 additions & 0 deletions src/iceberg/io/fs_file_io.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "fs_file_io.h"

#include <fcntl.h>

#include <cassert>
#include <filesystem>

namespace iceberg {
namespace io {

bool FsInputFile::exists() const { return std::filesystem::exists(path()); }

void FsOutputFile::create() {
int fd = open(path().c_str(), O_CREAT | O_WRONLY, 0666);
assert(fd != -1);
// TODO: how to handle this scenario
}

std::shared_ptr<InputFile> FsFileIO::newInputFile(const std::string& path) {
return std::make_shared<FsInputFile>(path);
}

std::shared_ptr<OutputFile> FsFileIO::newOutputFile(const std::string& path) {
return std::make_shared<FsOutputFile>(path);
}

void FsFileIO::DeleteFile(const std::string& path) {
std::error_code ec;
bool ret = std::filesystem::remove(path, ec);
assert(ret);
// TODO: how to handle this scenario
}

} // namespace io
} // namespace iceberg
55 changes: 55 additions & 0 deletions src/iceberg/io/fs_file_io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#pragma once

#include "file_io.h"

namespace iceberg {
namespace io {

class ICEBERG_EXPORT FsInputFile : public InputFile {
public:
explicit FsInputFile(std::string path) : InputFile(std::move(path)) {}
~FsInputFile() = default;

bool exists() const override;
};

class ICEBERG_EXPORT FsOutputFile : public OutputFile {
public:
explicit FsOutputFile(std::string path) : OutputFile(std::move(path)) {}
~FsOutputFile() = default;

void create() override;
};

class ICEBERG_EXPORT FsFileIO : public FileIO {
public:
FsFileIO() : FileIO("fs") {}
~FsFileIO() = default;

std::shared_ptr<InputFile> newInputFile(const std::string& path) override;
std::shared_ptr<OutputFile> newOutputFile(const std::string& path) override;

void DeleteFile(const std::string& path) override;
};

} // namespace io
} // namespace iceberg
2 changes: 1 addition & 1 deletion test/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# under the License.

add_executable(core_unittest)
target_sources(core_unittest PRIVATE core_unittest.cc)
target_sources(core_unittest PRIVATE core_unittest.cc file_io_test.cc)
target_link_libraries(core_unittest PRIVATE iceberg_static GTest::gtest_main)
target_include_directories(core_unittest PRIVATE "${ICEBERG_INCLUDES}")
add_test(NAME core_unittest COMMAND core_unittest)
53 changes: 53 additions & 0 deletions test/core/file_io_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <filesystem>

#include <gtest/gtest.h>

#include "iceberg/io/fs_file_io.h"

namespace iceberg {

namespace io {

class FsFileIOTest : public testing::Test {
protected:
void SetUp() override { fs = std::make_shared<FsFileIO>(); }

std::shared_ptr<FileIO> fs;
std::filesystem::path tmpfile = std::filesystem::temp_directory_path() / "123.txt";
};

TEST_F(FsFileIOTest, newOutputFile) {
auto out = fs->newOutputFile(tmpfile);
out->create();
ASSERT_EQ(out->path(), tmpfile.string());
}

TEST_F(FsFileIOTest, newInputFile) {
auto in = fs->newInputFile(tmpfile);
ASSERT_TRUE(in->exists());
ASSERT_EQ(in->path(), tmpfile.string());
fs->DeleteFile(in->path());
}

} // namespace io

} // namespace iceberg

0 comments on commit 514ded9

Please sign in to comment.