-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Junwang Zhao <[email protected]>
- Loading branch information
Showing
6 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |