Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/iceberg/arrow/arrow_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <limits>
#include <mutex>
#include <optional>
#include <vector>

#include <arrow/buffer.h>
#include <arrow/filesystem/localfs.h>
Expand Down Expand Up @@ -568,6 +569,18 @@ Status ArrowFileSystemFileIO::DeleteFile(const std::string& file_location) {
return {};
}

Status ArrowFileSystemFileIO::DeleteFiles(
const std::vector<std::string>& file_locations) {
std::vector<std::string> paths;
paths.reserve(file_locations.size());
for (const auto& file_location : file_locations) {
ICEBERG_ASSIGN_OR_RAISE(auto path, ResolvePath(file_location));
paths.push_back(std::move(path));
}
ICEBERG_ARROW_RETURN_NOT_OK(arrow_fs_->DeleteFiles(paths));
return {};
}

std::unique_ptr<FileIO> ArrowFileSystemFileIO::MakeMockFileIO() {
return std::make_unique<ArrowFileSystemFileIO>(
std::make_shared<::arrow::fs::internal::MockFileSystem>(
Expand Down
4 changes: 4 additions & 0 deletions src/iceberg/arrow/arrow_io_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include <arrow/filesystem/type_fwd.h>
#include <arrow/io/type_fwd.h>
Expand Down Expand Up @@ -77,6 +78,9 @@ class ICEBERG_BUNDLE_EXPORT ArrowFileSystemFileIO : public FileIO {
/// \brief Delete a file at the given location.
Status DeleteFile(const std::string& file_location) override;

/// \brief Delete files at the given locations.
Status DeleteFiles(const std::vector<std::string>& file_locations) override;

/// \brief Get the Arrow file system.
const std::shared_ptr<::arrow::fs::FileSystem>& fs() const { return arrow_fs_; }

Expand Down
7 changes: 7 additions & 0 deletions src/iceberg/file_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ Status FileIO::WriteFile(const std::string& file_location, std::string_view cont
return FinishWithCloseStatus(std::move(status), stream->Close());
}

Status FileIO::DeleteFiles(const std::vector<std::string>& file_locations) {
for (const auto& file_location : file_locations) {
ICEBERG_RETURN_UNEXPECTED(DeleteFile(file_location));
}
return {};
}

} // namespace iceberg
11 changes: 11 additions & 0 deletions src/iceberg/file_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <span>
#include <string>
#include <string_view>
#include <vector>

#include "iceberg/iceberg_export.h"
#include "iceberg/result.h"
Expand Down Expand Up @@ -154,6 +155,16 @@ class ICEBERG_EXPORT FileIO {
virtual Status DeleteFile(const std::string& file_location) {
Comment thread
wgtmac marked this conversation as resolved.
return NotImplemented("DeleteFile not implemented");
}

/// \brief Delete files at the given locations.
///
/// Implementations that can delete multiple files efficiently should override this
/// method. The default implementation deletes files sequentially using DeleteFile
/// and returns the first error encountered.
///
/// \param file_locations The locations of the files to delete.
/// \return void if all deletes succeed, or an error code if any delete fails.
virtual Status DeleteFiles(const std::vector<std::string>& file_locations);
};

} // namespace iceberg
1 change: 1 addition & 0 deletions src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ add_iceberg_test(util_test
data_file_set_test.cc
decimal_test.cc
endian_test.cc
file_io_test.cc
formatter_test.cc
lazy_test.cc
location_util_test.cc
Expand Down
15 changes: 15 additions & 0 deletions src/iceberg/test/arrow_io_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <array>
#include <memory>
#include <string>
#include <vector>

#include <arrow/filesystem/localfs.h>
#include <arrow/result.h>
Expand Down Expand Up @@ -341,6 +342,20 @@ TEST_F(LocalFileIOTest, DeleteFile) {
EXPECT_THAT(del_res, HasErrorMessage("Cannot delete file"));
}

TEST_F(LocalFileIOTest, DeleteFiles) {
auto first_path = CreateNewTempFilePath();
auto second_path = CreateNewTempFilePath();
ASSERT_THAT(file_io_->WriteFile(first_path, "hello"), IsOk());
ASSERT_THAT(file_io_->WriteFile(second_path, "world"), IsOk());

std::vector<std::string> paths = {first_path, second_path};
EXPECT_THAT(file_io_->DeleteFiles(paths), IsOk());

EXPECT_THAT(file_io_->ReadFile(first_path, std::nullopt), IsError(ErrorKind::kIOError));
EXPECT_THAT(file_io_->ReadFile(second_path, std::nullopt),
IsError(ErrorKind::kIOError));
}

void VerifyReadFullyReadsFromAbsolutePosition(const std::shared_ptr<FileIO>& file_io,
const std::string& path) {
ASSERT_THAT(file_io->WriteFile(path, "abcdef"), IsOk());
Expand Down
75 changes: 75 additions & 0 deletions src/iceberg/test/file_io_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 "iceberg/file_io.h"

#include <string>
#include <utility>
#include <vector>

#include <gtest/gtest.h>

#include "iceberg/test/matchers.h"

namespace iceberg {
namespace {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this or its counterpart. If you want to wrap RecordingFileIO in an anonymous namespace, you should shrink the scope instead.

Suggested change
namespace {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I’ll shrink the anonymous namespace scope to cover only the helper RecordingFileIO and keep the tests outside of the iceberg namespace.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not putting all cases in the iceberg namespace so we don't need to use iceberg:: prefix everywhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I moved the test cases into the iceberg namespace and kept RecordingFileIO in an anonymous namespace, so the iceberg:: prefixes are no longer needed.


class RecordingFileIO : public FileIO {
public:
explicit RecordingFileIO(std::string failure_path = "")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name RecordingFileIO is not that obvious about its purpose but this is fine in the test.

: failure_path_(std::move(failure_path)) {}

Status DeleteFile(const std::string& file_location) override {
deleted_paths.push_back(file_location);
if (file_location == failure_path_) {
return IOError("failed to delete {}", file_location);
}
return {};
}

std::vector<std::string> deleted_paths;

private:
std::string failure_path_;
};

} // namespace

TEST(FileIOTest, DeleteFilesFallsBackToDeleteFileForEachPath) {
RecordingFileIO file_io;
std::vector<std::string> paths = {"file-a.avro", "file-b.avro"};

EXPECT_THAT(file_io.DeleteFiles(paths), IsOk());
EXPECT_THAT(file_io.deleted_paths,
::testing::ElementsAre("file-a.avro", "file-b.avro"));
}

TEST(FileIOTest, DeleteFilesReturnsFirstDeleteFileError) {
RecordingFileIO file_io("file-b.avro");
std::vector<std::string> paths = {"file-a.avro", "file-b.avro", "file-c.avro"};

auto status = file_io.DeleteFiles(paths);

EXPECT_THAT(status, IsError(ErrorKind::kIOError));
EXPECT_THAT(status, HasErrorMessage("failed to delete file-b.avro"));
EXPECT_THAT(file_io.deleted_paths,
::testing::ElementsAre("file-a.avro", "file-b.avro"));
}

} // namespace iceberg
1 change: 1 addition & 0 deletions src/iceberg/test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ iceberg_tests = {
'data_file_set_test.cc',
'decimal_test.cc',
'endian_test.cc',
'file_io_test.cc',
'formatter_test.cc',
'lazy_test.cc',
'location_util_test.cc',
Expand Down
18 changes: 7 additions & 11 deletions src/iceberg/update/expire_snapshots.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,22 @@ class FileCleanupStrategy {
return expired;
}

/// \brief Delete a single file
void DeleteFile(const std::string& path) {
/// \brief Delete files at the given locations.
void DeleteFiles(const std::unordered_set<std::string>& paths) {
try {
if (delete_func_) {
delete_func_(path);
for (const auto& path : paths) {
delete_func_(path);
}
} else {
std::ignore = file_io_->DeleteFile(path);
std::vector<std::string> path_list(paths.begin(), paths.end());
std::ignore = file_io_->DeleteFiles(path_list);
}
} catch (...) {
// TODO(shangxinli): add retry
}
}

// TODO(shangxinli): Add bulk deletion
void DeleteFiles(const std::unordered_set<std::string>& paths) {
for (const auto& path : paths) {
DeleteFile(path);
}
}

bool HasAnyStatisticsFiles(const TableMetadata& metadata) const {
return !metadata.statistics.empty() || !metadata.partition_statistics.empty();
}
Expand Down
Loading