findepi commented on code in PR #4537:
URL: https://github.com/apache/iceberg/pull/4537#discussion_r871096061


##########
core/src/main/java/org/apache/iceberg/stats/StatsFormat.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.
+ */
+
+package org.apache.iceberg.stats;
+
+import io.airlift.compress.Compressor;
+import io.airlift.compress.zstd.ZstdCompressor;
+import io.airlift.compress.zstd.ZstdDecompressor;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.ByteBuffers;
+
+final class StatsFormat {
+  private StatsFormat() {
+  }
+
+  static final int CURRENT_FORMAT_VERSION = 1;
+
+  static final int MAGIC_AS_NUMBER_LE = new 
BigInteger(swapBytes(getMagic())).intValueExact();
+
+  static final int FLAG_COMPRESSED = 0b1;
+  static final int SUPPORTED_FLAGS = FLAG_COMPRESSED;
+
+  private static final StatsCompressionCodec FOOTER_COMPRESSION_CODEC = 
StatsCompressionCodec.LZ4;
+
+  static byte[] getMagic() {
+    return new byte[] {0x50, 0x46, 0x49, 0x53};
+  }
+
+  static void writeIntegerLittleEndian(OutputStream outputStream, int value) 
throws IOException {
+    outputStream.write(0xFF & value);
+    outputStream.write(0xFF & value >> 8);
+    outputStream.write(0xFF & value >> 16);
+    outputStream.write(0xFF & value >> 24);
+  }
+
+  static int readIntegerLittleEndian(byte[] data, int offset) {
+    return Byte.toUnsignedInt(data[offset]) |
+        (Byte.toUnsignedInt(data[offset + 1]) << 8) |
+        (Byte.toUnsignedInt(data[offset + 2]) << 16) |
+        (Byte.toUnsignedInt(data[offset + 3]) << 24);
+  }
+
+  static ByteBuffer compressFooterPayload(ByteBuffer payload) {
+    return compress(FOOTER_COMPRESSION_CODEC, payload);
+  }
+
+  static ByteBuffer decompressFooterPayload(ByteBuffer footer) {
+    return decompress(FOOTER_COMPRESSION_CODEC, footer);
+  }
+
+  static ByteBuffer compressBlob(StatsCompressionCodec codec, ByteBuffer data) 
{
+    return compress(codec, data);
+  }
+
+  static ByteBuffer decompressBlob(StatsCompressionCodec codec, ByteBuffer 
data) {
+    return decompress(codec, data);
+  }
+
+  private static ByteBuffer compress(StatsCompressionCodec codec, ByteBuffer 
input) {
+    Compressor compressor = getCompressor(codec);
+    ByteBuffer output = 
ByteBuffer.allocate(compressor.maxCompressedLength(input.remaining()));
+    compressor.compress(input, output);
+    output.flip();
+    return output;
+  }
+
+  private static Compressor getCompressor(StatsCompressionCodec codec) {
+    switch (codec) {
+      case LZ4:
+        // TODO currently not supported
+        break;
+      case ZSTD:
+        return new ZstdCompressor();
+    }
+    throw new UnsupportedOperationException("Unsupported codec: " + codec);
+  }
+
+  private static ByteBuffer decompress(StatsCompressionCodec codec, ByteBuffer 
input) {
+    switch (codec) {
+      case LZ4:
+        // TODO requires LZ4 frame decompressor, e.g. 
https://github.com/airlift/aircompressor/pull/142
+        break;
+
+      case ZSTD: {
+        byte[] inputBytes;
+        int inputOffset;
+        int inputLength;
+        if (input.hasArray()) {
+          inputBytes = input.array();
+          inputOffset = input.arrayOffset();
+          inputLength = input.remaining();
+        } else {
+          // TODO implement ZstdDecompressor.getDecompressedSize for 
ByteBuffer to avoid copying
+          inputBytes = ByteBuffers.toByteArray(input);
+          inputOffset = 0;
+          inputLength = inputBytes.length;
+        }
+        byte[] decompressed =

Review Comment:
   > Style: Iceberg adds empty lines between control flow blocks and the 
following statements.
   
   added
   
   
   
   > I guess it doesn't matter since we use the byte array path anyway.
   
   i don't understand



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to