findepi commented on code in PR #4537: URL: https://github.com/apache/iceberg/pull/4537#discussion_r857592741
########## core/src/main/java/org/apache/iceberg/stats/StatsReader.java: ########## @@ -0,0 +1,177 @@ +/* + * 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.AbstractMap.SimpleEntry; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Stream; +import org.apache.iceberg.io.InputFile; +import org.apache.iceberg.io.SeekableInputStream; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.io.ByteStreams; + +public class StatsReader implements Closeable { + + private final long fileLength; + private final SeekableInputStream input; + private Optional<Integer> knownFooterSize; + private Optional<FileMetadata> knownFileMetadata = Optional.empty(); + + public StatsReader(InputFile inputFile, Optional<Integer> footerSize) { + this.fileLength = inputFile.getLength(); + this.input = Preconditions.checkNotNull(inputFile, "inputFile is null").newStream(); + this.knownFooterSize = Preconditions.checkNotNull(footerSize, "footerSize is null"); + footerSize.ifPresent(size -> + Preconditions.checkArgument(0 < size && size <= fileLength - StatsFormat.getMagic().length, + "Invalid footer size: %s", size)); + } + + public FileMetadata getFileMetadata() throws IOException { + if (!knownFileMetadata.isPresent()) { + int footerSize = getFooterSize(); + byte[] footer = readInput(fileLength - footerSize, footerSize); + + checkMagic(footer, 0); + checkMagic(footer, footerSize - 4); + + int fileFormatVersion = StatsFormat.readIntegerLittleEndian(footer, footerSize - 8); + Preconditions.checkState( + fileFormatVersion == StatsFormat.CURRENT_FORMAT_VERSION, + "Unsupported format version %s, %s is the latest supported", + fileFormatVersion, + StatsFormat.CURRENT_FORMAT_VERSION); + + int flags = StatsFormat.readIntegerLittleEndian(footer, footerSize - 12); + Preconditions.checkState((flags & ~StatsFormat.SUPPORTED_FLAGS) == 0, "Unsupported flags: %s", flags); + boolean compressed = isFlagSet(flags, StatsFormat.FLAG_COMPRESSED); + + int reserved = StatsFormat.readIntegerLittleEndian(footer, footerSize - 16); Review Comment: As said above, defining the constants doesn't make the code clear, at least not in my eyes. The code is organized after and follows the raw footer layout. That's clearly indicated by the numbers subsequently used in the code (4, 8, 12, 16). Constants wouldn't convey that meaning, and wouldn't make evolving the code easier either. -- 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]
