findepi commented on code in PR #4537: URL: https://github.com/apache/iceberg/pull/4537#discussion_r853939944
########## core/src/main/java/org/apache/iceberg/stats/StatsFormat.java: ########## @@ -0,0 +1,122 @@ +/* + * 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; + +final class StatsFormat { + private StatsFormat() { + } + + static final int CURRENT_FORMAT_VERSION = 1; + + static final int MAGIC_AS_NUMBER_LE = new BigInteger(swap(getMagic())).intValueExact(); + + static final int SUPPORTED_FLAGS = 0b1; + static final int FLAG_COMPRESSED = 0b1; + + 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(StatsCompressionCodec.LZ4, payload); + } + + static byte[] decompressFooterPayload(byte[] footer, int offset, int length) { + return decompress(StatsCompressionCodec.LZ4, footer, offset, length); + } + + static ByteBuffer compressBlob(StatsCompressionCodec codec, ByteBuffer data) { + return compress(codec, data); + } + + static byte[] decompressBlob(StatsCompressionCodec codec, byte[] data, int dataOffset, int dataLength) { + return decompress(codec, data, dataOffset, dataLength); + } + + 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 byte[] decompress(StatsCompressionCodec codec, byte[] input, int inputOffset, int inputLength) { + switch (codec) { + case LZ4: { + // TODO requires LZ4 frame decompressor, e.g. https://github.com/airlift/aircompressor/pull/142 + throw new UnsupportedOperationException("LZ4 is not supported yet"); Review Comment: thanks for the tip -- 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]
