pvary commented on code in PR #13997:
URL: https://github.com/apache/iceberg/pull/13997#discussion_r2371707258


##########
api/src/main/java/org/apache/iceberg/io/FileRange.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.io;
+
+import java.io.EOFException;
+import java.nio.ByteBuffer;
+import java.util.concurrent.CompletableFuture;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+
+public class FileRange {
+  private final CompletableFuture<ByteBuffer> byteBuffer;
+  private final long offset;
+  private final int length;
+
+  public FileRange(CompletableFuture<ByteBuffer> byteBuffer, long offset, int 
length)
+      throws EOFException {
+    Preconditions.checkNotNull(offset, "offset is null");
+    Preconditions.checkNotNull(length, "length is null");
+    Preconditions.checkArgument(length() >= 0, "length %s is negative ", 
length);
+    if (offset < 0) {
+      throw new EOFException("position is negative in range: " + offset);
+    }

Review Comment:
   nit: newline



##########
api/src/main/java/org/apache/iceberg/io/RangeReadable.java:
##########
@@ -77,4 +83,69 @@ default void readFully(long position, byte[] buffer) throws 
IOException {
   default int readTail(byte[] buffer) throws IOException {
     return readTail(buffer, 0, buffer.length);
   }
+
+  /**
+   * Read fully a list of file ranges asynchronously from this file. As a 
result of the call, each
+   * range will have FileRange.setData(CompletableFuture) called with a future 
that when complete
+   * will have a ByteBuffer with the data from the file's range.
+   *
+   * <p>The position returned by getPos() after readVectored() is undefined.
+   *
+   * <p>If a file is changed while the readVectored() operation is in 
progress, the output is
+   * undefined. Some ranges may have old data, some may have new and some may 
have both.
+   *
+   * <p>While a readVectored() operation is in progress, normal read api calls 
may block.
+   *
+   * @param ranges the byte ranges to read
+   * @param allocate the function to allocate ByteBuffer
+   * @throws IOException any IOE.
+   * @throws IllegalArgumentException if the any of ranges are invalid, or 
they overlap.
+   */
+  default void readVectored(List<FileRange> ranges, IntFunction<ByteBuffer> 
allocate)
+      throws IOException {
+    List<FileRange> validatedRanges = sortRanges(ranges);
+    for (FileRange range : validatedRanges) {
+      ByteBuffer buffer = allocate.apply(range.length());
+      readFully(range.offset(), buffer.array());
+      range.byteBuffer().complete(buffer);
+    }
+  }
+
+  static List<FileRange> sortRanges(final List<FileRange> input) {
+    Preconditions.checkNotNull(input, "Null input list");
+
+    final List<FileRange> sortedRanges;
+
+    // 2 because the input size can be 0/1, and then we want to skip sorting.
+    if (input.size() < 2) {
+      sortedRanges = input;
+    } else {
+      sortedRanges = sortRangeList(input);
+      FileRange prev = null;
+      for (final FileRange current : sortedRanges) {
+        if (prev != null) {
+          Preconditions.checkArgument(
+              current.offset() >= prev.offset() + prev.length(),
+              "Overlapping ranges %s and %s",
+              prev,
+              current);
+        }

Review Comment:
   nit: newline



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