yingcai-cy commented on code in PR #108:
URL: https://github.com/apache/iceberg-cpp/pull/108#discussion_r2103886619


##########
src/iceberg/table_metadata.cc:
##########
@@ -153,14 +154,70 @@ Result<MetadataFileCodecType> 
TableMetadataUtil::CodecFromFileName(
   return MetadataFileCodecType::kNone;
 }
 
+class GZipDecompressor {
+ public:
+  GZipDecompressor() : initialized_(false) {}
+
+  ~GZipDecompressor() {
+    if (initialized_) {
+      inflateEnd(&stream_);
+    }
+  }
+
+  Status Init() {
+    int ret = inflateInit2(&stream_, 15 + 32);
+    if (ret != Z_OK) {
+      return IOError("inflateInit2 failed, result:{}", ret);
+    }
+    initialized_ = true;
+    return {};
+  }
+
+  Result<std::string> Decompress(const std::string& compressed_data) {
+    if (compressed_data.empty()) {
+      return {};
+    }
+    if (!initialized_) {
+      ICEBERG_RETURN_UNEXPECTED(Init());
+    }
+    stream_.avail_in = static_cast<uInt>(compressed_data.size());
+    stream_.next_in = 
reinterpret_cast<Bytef*>(const_cast<char*>(compressed_data.data()));
+
+    // TODO(xiao.dong) magic buffer 16k, can we get a estimated size from 
compressed data?
+    std::vector<char> outBuffer(32 * 1024);
+    std::string result;
+    int ret = 0;
+    do {
+      outBuffer.resize(outBuffer.size());
+      stream_.avail_out = static_cast<uInt>(outBuffer.size());
+      stream_.next_out = reinterpret_cast<Bytef*>(outBuffer.data());
+      ret = inflate(&stream_, Z_NO_FLUSH);
+      if (ret != Z_OK && ret != Z_STREAM_END) {
+        return IOError("inflate failed, result:{}", ret);
+      }
+      result.append(outBuffer.data(), outBuffer.size() - stream_.avail_out);
+    } while (ret != Z_STREAM_END);
+    return result;
+  }
+
+ private:
+  bool initialized_ = false;
+  z_stream stream_;
+};
+
 Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read(
     FileIO& io, const std::string& location, std::optional<size_t> length) {
   ICEBERG_ASSIGN_OR_RAISE(auto codec_type, CodecFromFileName(location));
+
+  ICEBERG_ASSIGN_OR_RAISE(auto content, io.ReadFile(location, length));
   if (codec_type == MetadataFileCodecType::kGzip) {
-    return NotImplemented("Reading gzip-compressed metadata files is not 
supported yet");
+    auto gzip_decompressor = std::make_unique<GZipDecompressor>();

Review Comment:
   I think using functions like gzopen can simplify this code a bit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to