rdblue commented on code in PR #4537: URL: https://github.com/apache/iceberg/pull/4537#discussion_r871520555
########## core/src/main/java/org/apache/iceberg/stats/StatsReader.java: ########## @@ -0,0 +1,183 @@ +/* + * 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); Review Comment: When you open a stream, you check whether it implements `RangeReadable`. If not, then get the file length and subtract. But there is no need for the extra HEAD request for S3 to get the file length if `readTail` is available. -- 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]
