|
| 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 <arrow/filesystem/localfs.h> |
| 21 | +#include <arrow/io/compressed.h> |
| 22 | +#include <arrow/io/file.h> |
| 23 | +#include <arrow/util/compression.h> |
| 24 | +#include <gtest/gtest.h> |
| 25 | + |
| 26 | +#include "iceberg/arrow/arrow_fs_file_io.h" |
| 27 | +#include "iceberg/file_io.h" |
| 28 | +#include "iceberg/util/gzip_internal.h" |
| 29 | +#include "matchers.h" |
| 30 | +#include "temp_file_test_base.h" |
| 31 | + |
| 32 | +namespace iceberg { |
| 33 | + |
| 34 | +class GZipTest : public TempFileTestBase { |
| 35 | + protected: |
| 36 | + void SetUp() override { |
| 37 | + TempFileTestBase::SetUp(); |
| 38 | + io_ = std::make_shared<iceberg::arrow::ArrowFileSystemFileIO>( |
| 39 | + std::make_shared<::arrow::fs::LocalFileSystem>()); |
| 40 | + temp_filepath_ = CreateNewTempFilePathWithSuffix("test.gz"); |
| 41 | + } |
| 42 | + |
| 43 | + std::shared_ptr<iceberg::FileIO> io_; |
| 44 | + std::string temp_filepath_; |
| 45 | +}; |
| 46 | + |
| 47 | +TEST_F(GZipTest, GZipDecompressedString) { |
| 48 | + auto test_string = GenerateRandomString(1024); |
| 49 | + |
| 50 | +#define ARROW_ASSIGN_OR_THROW(var, expr) \ |
| 51 | + { \ |
| 52 | + auto result = expr; \ |
| 53 | + ASSERT_TRUE(result.ok()); \ |
| 54 | + var = std::move(result.ValueUnsafe()); \ |
| 55 | + } |
| 56 | + std::shared_ptr<::arrow::io::FileOutputStream> out_stream; |
| 57 | + ARROW_ASSIGN_OR_THROW(out_stream, ::arrow::io::FileOutputStream::Open(temp_filepath_)); |
| 58 | + |
| 59 | + std::shared_ptr<::arrow::util::Codec> gzip_codec; |
| 60 | + ARROW_ASSIGN_OR_THROW(gzip_codec, |
| 61 | + ::arrow::util::Codec::Create(::arrow::Compression::GZIP)); |
| 62 | + |
| 63 | + std::shared_ptr<::arrow::io::OutputStream> compressed_stream; |
| 64 | + ARROW_ASSIGN_OR_THROW(compressed_stream, ::arrow::io::CompressedOutputStream::Make( |
| 65 | + gzip_codec.get(), out_stream)); |
| 66 | +#undef ARROW_ASSIGN_OR_THROW |
| 67 | + |
| 68 | + ASSERT_TRUE(compressed_stream->Write(test_string).ok()); |
| 69 | + ASSERT_TRUE(compressed_stream->Flush().ok()); |
| 70 | + ASSERT_TRUE(compressed_stream->Close().ok()); |
| 71 | + |
| 72 | + auto result = io_->ReadFile(temp_filepath_, test_string.size()); |
| 73 | + EXPECT_THAT(result, IsOk()); |
| 74 | + |
| 75 | + auto gzip_decompressor = std::make_unique<GZipDecompressor>(); |
| 76 | + EXPECT_THAT(gzip_decompressor->Init(), IsOk()); |
| 77 | + EXPECT_THAT(result, IsOk()); |
| 78 | + auto read_data = gzip_decompressor->Decompress(result.value()); |
| 79 | + EXPECT_THAT(read_data, IsOk()); |
| 80 | + ASSERT_EQ(read_data.value(), test_string); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace iceberg |
0 commit comments