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


##########
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);
+        }
+        prev = current;
+      }
+    }
+
+    return sortedRanges;
+  }
+
+  /**
+   * Sort the input ranges by offset; no validation is done.
+   *
+   * @param input input ranges.
+   * @return a new list of the ranges, sorted by offset.
+   */
+  static List<FileRange> sortRangeList(List<FileRange> input) {

Review Comment:
   I feel it would be better to inline this in `sortRanges`.  We're exposing 
this through the interface by making it static and it's more of an 
implementation detail. 



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