-
Notifications
You must be signed in to change notification settings - Fork 3k
Use memory-backed streams in tests #4534
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
RussellSpitzer
merged 1 commit into
apache:master
from
findepi:findepi/use-memory-backed-streams-in-tests-3b5a04
Apr 21, 2022
Merged
Changes from all commits
Commits
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
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
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
131 changes: 131 additions & 0 deletions
131
core/src/test/java/org/apache/iceberg/io/InMemoryInputFile.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,131 @@ | ||
| /* | ||
| * 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; | ||
| import java.io.IOException; | ||
| import java.util.UUID; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
|
|
||
| public class InMemoryInputFile implements InputFile { | ||
|
|
||
| private final String location; | ||
| private final byte[] contents; | ||
|
|
||
| public InMemoryInputFile(byte[] contents) { | ||
| this("memory:" + UUID.randomUUID(), contents); | ||
| } | ||
|
|
||
| public InMemoryInputFile(String location, byte[] contents) { | ||
| Preconditions.checkNotNull(location, "location is null"); | ||
| Preconditions.checkNotNull(contents, "contents is null"); | ||
| this.location = location; | ||
| this.contents = contents.clone(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getLength() { | ||
| return contents.length; | ||
| } | ||
|
|
||
| @Override | ||
| public SeekableInputStream newStream() { | ||
| return new InMemorySeekableInputStream(contents); | ||
| } | ||
|
|
||
| @Override | ||
| public String location() { | ||
| return location; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean exists() { | ||
| return true; | ||
| } | ||
findepi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private static class InMemorySeekableInputStream extends SeekableInputStream { | ||
|
|
||
| private final int length; | ||
| private final ByteArrayInputStream delegate; | ||
|
|
||
| InMemorySeekableInputStream(byte[] contents) { | ||
| this.length = contents.length; | ||
| this.delegate = new ByteArrayInputStream(contents); | ||
| } | ||
|
|
||
| @Override | ||
| public long getPos() throws IOException { | ||
| return length - delegate.available(); | ||
| } | ||
|
|
||
| @Override | ||
| public void seek(long newPos) throws IOException { | ||
| delegate.reset(); // resets to a marked position | ||
| Preconditions.checkState(delegate.skip(newPos) == newPos, | ||
| "Invalid position %s within stream of length %s", newPos, length); | ||
| } | ||
|
|
||
| @Override | ||
| public int read() { | ||
| return delegate.read(); | ||
| } | ||
|
|
||
| @Override | ||
| public int read(byte[] b) throws IOException { | ||
| return delegate.read(b); | ||
| } | ||
|
|
||
| @Override | ||
| public int read(byte[] b, int off, int len) { | ||
| return delegate.read(b, off, len); | ||
| } | ||
|
|
||
| @Override | ||
| public long skip(long n) { | ||
| return delegate.skip(n); | ||
| } | ||
|
|
||
| @Override | ||
| public int available() { | ||
| return delegate.available(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean markSupported() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void mark(int readAheadLimit) { | ||
| // The delegate's mark is used to implement seek | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| delegate.reset(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| delegate.close(); | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a side note: Typically we use
ByteBuffers instead of rawbyte[], so that we can get views over slices etc for free and help avoid additional allocations in many situations.ByteBuffers also have the advantage of presenting views over type-specific byte arrays (for
char,int,float, etc). And these views are indexed in terms of the type-specific size of the values, which is easier to reason about and also have bulk get and set methods.This is test code, but if we were to ever move this to be code used in the project, we'd almost certainly want to use
ByteBufferinstead of the rawbyte[]throughout where possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For internal representation
byte[]is preferred, since the i canByteArray{Output,Input}Stream}.For constructor -- whatever it's easier to provide by the caller. I think we eventually will accept both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'm good with that. If this were ported outside of test, I just wanted to make sure you were aware that request might come up (though it might not depending on the situation) 🙂