-
Notifications
You must be signed in to change notification settings - Fork 3k
Add RangeReadable interface for range base FileIO reads #4608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
83b35b0
Add RangeReadable interface for range base FileIO reads
danielcweeks c01cfb9
Checkstyle
danielcweeks 4ae673c
Style
danielcweeks b05b968
Checkstyle
danielcweeks 19c376d
Add readTail
danielcweeks 895a77a
Checkstyle
danielcweeks 298c17e
Relocate IOUtil to core
danielcweeks 961e789
Fix readFully
danielcweeks 1f92f22
Update readTail to return actual size read before EOF
danielcweeks 36032b1
Cleanup comments
danielcweeks ec6c81d
Checkstyle
danielcweeks 8596020
Replace readFully implementation with one from Parquet.
rdblue a532db3
Update LICENSE.
rdblue 10452be
Merge pull request #1 from rdblue/s3fileio-range-reads
danielcweeks 3960e5b
Checkstyle
danielcweeks 51c292e
Remove trailing whitespace.
rdblue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
api/src/main/java/org/apache/iceberg/io/RangeReadable.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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.Closeable; | ||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * {@code RangeReadable} is an interface that allows for implementations | ||
| * of {@link InputFile} streams to perform positional, range-based reads, which | ||
| * are more efficient than unbounded reads in many cloud provider object stores. | ||
| * | ||
| * Thread safety is not a requirement of the interface and is left to the | ||
| * implementation. | ||
| * | ||
| * If the implementation is also a {@link SeekableInputStream}, the position | ||
| * of the stream is not required to be updated based on the positional reads | ||
| * performed by this interface. Usage of {@link SeekableInputStream} should | ||
| * always seek to the appropriate position for {@link java.io.InputStream} | ||
| * based reads. | ||
| * | ||
| */ | ||
| public interface RangeReadable extends Closeable { | ||
|
|
||
| /** | ||
| * Fill the provided buffer with the contents of the input source starting | ||
| * at {@code position} for the given {@code offset} and {@code length}. | ||
| * | ||
| * @param position start position of the read | ||
| * @param buffer target buffer to copy data | ||
| * @param offset offset in the buffer to copy the data | ||
| * @param length size of the read | ||
| */ | ||
| void readFully(long position, byte[] buffer, int offset, int length) throws IOException; | ||
|
|
||
| /** | ||
| * Fill the entire buffer with the contents of the input source starting | ||
| * at {@code position}. | ||
| * | ||
| * @param position start position of the read | ||
| * @param buffer target buffer to copy data | ||
| */ | ||
| default void readFully(long position, byte[] buffer) throws IOException { | ||
| readFully(position, buffer, 0, buffer.length); | ||
| } | ||
|
|
||
| /** | ||
| * Read the last {@code length} bytes from the file. | ||
| * | ||
| * @param buffer the buffer to write data into | ||
| * @param offset the offset in the buffer to start writing | ||
| * @param length the number of bytes from the end of the object to read | ||
| * @return the actual number of bytes read | ||
| * @throws IOException if an error occurs while reading | ||
| */ | ||
| int readTail(byte [] buffer, int offset, int length) throws IOException; | ||
|
|
||
| /** | ||
| * Read the full size of the buffer from the end of the file. | ||
| * | ||
| * @param buffer the buffer to write data into | ||
| * @return the actual number of bytes read | ||
| * @throws IOException if an error occurs while reading | ||
| */ | ||
| default int readTail(byte [] buffer) throws IOException { | ||
| return readTail(buffer, 0, buffer.length); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| public class IOUtil { | ||
| // not meant to be instantiated | ||
| private IOUtil() { | ||
| } | ||
|
|
||
| /** | ||
| * Reads into a buffer from a stream, making multiple read calls if necessary. | ||
| * | ||
| * @param stream an InputStream to read from | ||
| * @param bytes a buffer to write into | ||
| * @param offset starting offset in the buffer for the data | ||
| * @param length length of bytes to copy from the input stream to the buffer | ||
| * @throws EOFException if the end of the stream is reached before reading length bytes | ||
| * @throws IOException if there is an error while reading | ||
| */ | ||
| public static void readFully(InputStream stream, byte[] bytes, int offset, int length) throws IOException { | ||
| int bytesRead = readRemaining(stream, bytes, offset, length); | ||
| if (bytesRead < length) { | ||
| throw new EOFException( | ||
| "Reached the end of stream with " + (length - bytesRead) + " bytes left to read"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Reads into a buffer from a stream, making multiple read calls if necessary | ||
| * returning the number of bytes read until end of stream. | ||
| * | ||
| * @param stream an InputStream to read from | ||
| * @param bytes a buffer to write into | ||
| * @param offset starting offset in the buffer for the data | ||
| * @param length length of bytes to copy from the input stream to the buffer | ||
| * @return the number of bytes read | ||
| * @throws IOException if there is an error while reading | ||
| */ | ||
| public static int readRemaining(InputStream stream, byte[] bytes, int offset, int length) throws IOException { | ||
| int pos = offset; | ||
| int remaining = length; | ||
| while (remaining > 0) { | ||
| int bytesRead = stream.read(bytes, pos, remaining); | ||
| if (bytesRead < 0) { | ||
| break; | ||
| } | ||
|
|
||
| remaining -= bytesRead; | ||
| pos += bytesRead; | ||
| } | ||
|
|
||
| return length - remaining; | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
core/src/test/java/org/apache/iceberg/io/MockInputStream.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * 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.ByteArrayInputStream; | ||
|
|
||
| class MockInputStream extends ByteArrayInputStream { | ||
|
|
||
| static final byte[] TEST_ARRAY = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | ||
|
|
||
| private int[] lengths; | ||
| private int current = 0; | ||
|
|
||
| MockInputStream(int... actualReadLengths) { | ||
| super(TEST_ARRAY); | ||
| this.lengths = actualReadLengths; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized int read(byte[] b, int off, int len) { | ||
| if (current < lengths.length) { | ||
| if (len <= lengths[current]) { | ||
| // when len == lengths[current], the next read will by 0 bytes | ||
| int bytesRead = super.read(b, off, len); | ||
| lengths[current] -= bytesRead; | ||
| return bytesRead; | ||
| } else { | ||
| int bytesRead = super.read(b, off, lengths[current]); | ||
| current += 1; | ||
| return bytesRead; | ||
| } | ||
| } else { | ||
| return super.read(b, off, len); | ||
| } | ||
| } | ||
|
|
||
| public long getPos() { | ||
| return this.pos; | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.