RussellSpitzer commented on code in PR #4537: URL: https://github.com/apache/iceberg/pull/4537#discussion_r856350865
########## core/src/main/java/org/apache/iceberg/stats/StatsFormat.java: ########## @@ -0,0 +1,141 @@ +/* + * 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 ByteBuffer decompressFooterPayload(ByteBuffer footer) { + return decompress(StatsCompressionCodec.LZ4, 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 Review Comment: Isn't this required for the footers at the moment? -- 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]
