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


##########
core/src/main/java/org/apache/iceberg/stats/StatsReader.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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 java.io.Closeable;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.iceberg.io.InputFile;
+import org.apache.iceberg.io.RangeReadable;
+import org.apache.iceberg.io.SeekableInputStream;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
+import org.apache.iceberg.relocated.com.google.common.io.ByteStreams;
+import org.apache.iceberg.stats.StatsFormat.Flag;
+import org.apache.iceberg.util.Pair;
+
+public class StatsReader implements Closeable {
+  // Must not be modified
+  private static final byte[] MAGIC = StatsFormat.getMagic();
+
+  private final long fileSize;
+  private final SeekableInputStream input;
+  private Integer knownFooterSize;
+  private FileMetadata knownFileMetadata;
+
+  public StatsReader(InputFile inputFile, @Nullable Long fileSize, @Nullable 
Long footerSize) {
+    Preconditions.checkNotNull(inputFile, "inputFile is null");
+    this.fileSize = fileSize == null ? inputFile.getLength() : fileSize;
+    this.input = inputFile.newStream();
+    if (footerSize != null) {
+      Preconditions.checkArgument(0 < footerSize && footerSize <= 
this.fileSize - MAGIC.length,
+          "Invalid footer size: %s", footerSize);
+      this.knownFooterSize = Math.toIntExact(footerSize);
+    }
+  }
+
+  public FileMetadata fileMetadata() throws IOException {
+    if (knownFileMetadata == null) {
+      int footerSize = footerSize();
+      byte[] footer = readInput(fileSize - footerSize, footerSize);
+
+      checkMagic(footer, StatsFormat.FOOTER_MAGIC_OFFSET);
+      int footerStructOffset = footerSize - StatsFormat.FOOTER_STRUCT_LENGTH;
+      checkMagic(footer, footerStructOffset + 
StatsFormat.FOOTER_STRUCT_MAGIC_OFFSET);
+
+      StatsCompressionCodec footerCompression = StatsCompressionCodec.NONE;
+      for (Flag flag : decodeFlags(footer, footerStructOffset)) {
+        switch (flag) {
+          case FOOTER_PAYLOAD_COMPRESSED:
+            footerCompression = StatsFormat.FOOTER_COMPRESSION_CODEC;
+            break;
+          default:
+            throw new IllegalStateException("Unsupported flag: " + flag);
+        }
+      }
+
+      int footerPayloadSize = StatsFormat.readIntegerLittleEndian(
+          footer,
+          footerStructOffset + StatsFormat.FOOTER_STRUCT_PAYLOAD_SIZE_OFFSET);
+      Preconditions.checkState(footerSize == footerPayloadSize + 
StatsFormat.FOOTER_STRUCT_LENGTH + 4 /* magic */,
+          "Unexpected footer payload size value %s for footer size %s", 
footerPayloadSize, footerSize);
+
+      ByteBuffer footerPayload = ByteBuffer.wrap(footer, 4, footerPayloadSize);
+      ByteBuffer footerJson = StatsFormat.decompress(footerCompression, 
footerPayload);
+      this.knownFileMetadata = parseFileMetadata(footerJson);
+    }
+    return knownFileMetadata;
+  }
+
+  private Set<Flag> decodeFlags(byte[] footer, int footerStructOffset) {
+    EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
+    for (int byteNumber = 0; byteNumber < 
StatsFormat.FOOTER_STRUCT_FLAGS_LENGTH; byteNumber++) {
+      int flagByte =
+          Byte.toUnsignedInt(footer[footerStructOffset + 
StatsFormat.FOOTER_STRUCT_FLAGS_OFFSET + byteNumber]);
+      int bitNumber = 0;
+      while (flagByte != 0) {
+        if ((flagByte & 0x1) != 0) {
+          Flag flag = Flag.fromBit(byteNumber, bitNumber);
+          Preconditions.checkState(flag != null, "Unknown flag byte %s and bit 
%s set", byteNumber, bitNumber);
+          flags.add(flag);
+        }
+        flagByte = flagByte >> 1;
+        bitNumber++;
+      }
+    }
+    return flags;
+  }
+
+  public Iterable<Pair<BlobMetadata, ByteBuffer>> readAll(List<BlobMetadata> 
blobs) {
+    if (blobs.isEmpty()) {
+      return ImmutableList.of();
+    }
+
+    // TODO inspect blob offsets and coalesce read regions close to each other
+
+    return () -> blobs.stream()
+        .sorted(Comparator.comparingLong(BlobMetadata::offset))
+        .map((BlobMetadata blobMetadata) -> {
+          try {
+            input.seek(blobMetadata.offset());
+            byte[] bytes = new byte[Math.toIntExact(blobMetadata.length())];
+            ByteStreams.readFully(input, bytes);
+            ByteBuffer rawData = ByteBuffer.wrap(bytes);
+            StatsCompressionCodec codec = 
StatsCompressionCodec.forName(blobMetadata.compressionCodec());
+            ByteBuffer data = StatsFormat.decompress(codec, rawData);
+            return Pair.of(blobMetadata, data);
+          } catch (IOException e) {
+            throw new UncheckedIOException(e);
+          }
+        })
+        .iterator();
+  }
+
+  private static void checkMagic(byte[] data, int offset) {
+    byte[] read = Arrays.copyOfRange(data, offset, offset + MAGIC.length);
+    if (!Arrays.equals(read, MAGIC)) {
+      throw new IllegalStateException(String.format(
+          "Invalid file: expected magic at offset %s: %s, but got %s",
+          offset, Arrays.toString(MAGIC), Arrays.toString(read)));
+    }
+  }
+
+  private int footerSize() throws IOException {
+    if (knownFooterSize == null) {
+      if (fileSize < StatsFormat.FOOTER_STRUCT_LENGTH) {

Review Comment:
   done



-- 
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