Skip to content

Commit 514ded9

Browse files
committed
feat: add file_io and local fs impl
Signed-off-by: Junwang Zhao <[email protected]>
1 parent 9a04468 commit 514ded9

File tree

6 files changed

+241
-2
lines changed

6 files changed

+241
-2
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
set(ICEBERG_SOURCES demo_table.cc)
18+
set(ICEBERG_SOURCES demo_table.cc io/fs_file_io.cc)
1919

2020
add_iceberg_lib(iceberg
2121
SOURCES

src/iceberg/io/file_io.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 <string>
23+
24+
#include "iceberg/iceberg_export.h"
25+
26+
namespace iceberg {
27+
namespace io {
28+
29+
class ICEBERG_EXPORT InputFile {
30+
public:
31+
explicit InputFile(std::string path) : path_(std::move(path)) {}
32+
33+
virtual ~InputFile() = default;
34+
35+
virtual bool exists() const = 0;
36+
37+
const std::string& path() const { return path_; }
38+
39+
protected:
40+
std::string path_;
41+
};
42+
43+
class ICEBERG_EXPORT OutputFile {
44+
public:
45+
explicit OutputFile(std::string path) : path_(std::move(path)) {}
46+
47+
virtual ~OutputFile() = default;
48+
49+
virtual void create() = 0;
50+
51+
const std::string& path() const { return path_; }
52+
53+
protected:
54+
std::string path_;
55+
};
56+
57+
class ICEBERG_EXPORT FileIO {
58+
public:
59+
explicit FileIO(std::string name) : name_(std::move(name)) {}
60+
61+
virtual ~FileIO() = default;
62+
63+
virtual std::shared_ptr<InputFile> newInputFile(const std::string& path) = 0;
64+
virtual std::shared_ptr<OutputFile> newOutputFile(const std::string& path) = 0;
65+
66+
virtual void DeleteFile(const std::string& path) = 0;
67+
void DeleteFile(const InputFile& ifile) { return DeleteFile(ifile.path()); }
68+
void DeleteFile(const OutputFile& ofile) { return DeleteFile(ofile.path()); }
69+
70+
const std::string& name() const { return name_; }
71+
72+
protected:
73+
std::string name_;
74+
};
75+
76+
} // namespace io
77+
} // namespace iceberg

src/iceberg/io/fs_file_io.cc

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 "fs_file_io.h"
21+
22+
#include <fcntl.h>
23+
24+
#include <cassert>
25+
#include <filesystem>
26+
27+
namespace iceberg {
28+
namespace io {
29+
30+
bool FsInputFile::exists() const { return std::filesystem::exists(path()); }
31+
32+
void FsOutputFile::create() {
33+
int fd = open(path().c_str(), O_CREAT | O_WRONLY, 0666);
34+
assert(fd != -1);
35+
// TODO: how to handle this scenario
36+
}
37+
38+
std::shared_ptr<InputFile> FsFileIO::newInputFile(const std::string& path) {
39+
return std::make_shared<FsInputFile>(path);
40+
}
41+
42+
std::shared_ptr<OutputFile> FsFileIO::newOutputFile(const std::string& path) {
43+
return std::make_shared<FsOutputFile>(path);
44+
}
45+
46+
void FsFileIO::DeleteFile(const std::string& path) {
47+
std::error_code ec;
48+
bool ret = std::filesystem::remove(path, ec);
49+
assert(ret);
50+
// TODO: how to handle this scenario
51+
}
52+
53+
} // namespace io
54+
} // namespace iceberg

src/iceberg/io/fs_file_io.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 "file_io.h"
23+
24+
namespace iceberg {
25+
namespace io {
26+
27+
class ICEBERG_EXPORT FsInputFile : public InputFile {
28+
public:
29+
explicit FsInputFile(std::string path) : InputFile(std::move(path)) {}
30+
~FsInputFile() = default;
31+
32+
bool exists() const override;
33+
};
34+
35+
class ICEBERG_EXPORT FsOutputFile : public OutputFile {
36+
public:
37+
explicit FsOutputFile(std::string path) : OutputFile(std::move(path)) {}
38+
~FsOutputFile() = default;
39+
40+
void create() override;
41+
};
42+
43+
class ICEBERG_EXPORT FsFileIO : public FileIO {
44+
public:
45+
FsFileIO() : FileIO("fs") {}
46+
~FsFileIO() = default;
47+
48+
std::shared_ptr<InputFile> newInputFile(const std::string& path) override;
49+
std::shared_ptr<OutputFile> newOutputFile(const std::string& path) override;
50+
51+
void DeleteFile(const std::string& path) override;
52+
};
53+
54+
} // namespace io
55+
} // namespace iceberg

test/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717

1818
add_executable(core_unittest)
19-
target_sources(core_unittest PRIVATE core_unittest.cc)
19+
target_sources(core_unittest PRIVATE core_unittest.cc file_io_test.cc)
2020
target_link_libraries(core_unittest PRIVATE iceberg_static GTest::gtest_main)
2121
target_include_directories(core_unittest PRIVATE "${ICEBERG_INCLUDES}")
2222
add_test(NAME core_unittest COMMAND core_unittest)

test/core/file_io_test.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 <filesystem>
21+
22+
#include <gtest/gtest.h>
23+
24+
#include "iceberg/io/fs_file_io.h"
25+
26+
namespace iceberg {
27+
28+
namespace io {
29+
30+
class FsFileIOTest : public testing::Test {
31+
protected:
32+
void SetUp() override { fs = std::make_shared<FsFileIO>(); }
33+
34+
std::shared_ptr<FileIO> fs;
35+
std::filesystem::path tmpfile = std::filesystem::temp_directory_path() / "123.txt";
36+
};
37+
38+
TEST_F(FsFileIOTest, newOutputFile) {
39+
auto out = fs->newOutputFile(tmpfile);
40+
out->create();
41+
ASSERT_EQ(out->path(), tmpfile.string());
42+
}
43+
44+
TEST_F(FsFileIOTest, newInputFile) {
45+
auto in = fs->newInputFile(tmpfile);
46+
ASSERT_TRUE(in->exists());
47+
ASSERT_EQ(in->path(), tmpfile.string());
48+
fs->DeleteFile(in->path());
49+
}
50+
51+
} // namespace io
52+
53+
} // namespace iceberg

0 commit comments

Comments
 (0)