Skip to content
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

chore: better code reuse in callback #145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.vector.ipc.ArrowFileReader;
import org.apache.arrow.vector.ipc.ArrowReader;
Expand Down Expand Up @@ -69,13 +70,7 @@ public CompletableFuture<Void> show() {
DataFrames.showDataframe(
runtimePointer,
dataframe,
(String errString) -> {
if (ErrorUtil.containsError(errString)) {
future.completeExceptionally(new RuntimeException(errString));
} else {
future.complete(null);
}
});
new RuntimeExceptionCallback(future));
return future;
}

Expand All @@ -89,13 +84,7 @@ public CompletableFuture<Void> writeParquet(Path path) {
runtimePointer,
dataframe,
path.toAbsolutePath().toString(),
(String errString) -> {
if (ErrorUtil.containsError(errString)) {
future.completeExceptionally(new RuntimeException(errString));
} else {
future.complete(null);
}
});
new RuntimeExceptionCallback(future));
return future;
}

Expand All @@ -109,13 +98,7 @@ public CompletableFuture<Void> writeCsv(Path path) {
runtimePointer,
dataframe,
path.toAbsolutePath().toString(),
(String errString) -> {
if (ErrorUtil.containsError(errString)) {
future.completeExceptionally(new RuntimeException(errString));
} else {
future.complete(null);
}
});
new RuntimeExceptionCallback(future));
return future;
}

Expand All @@ -130,4 +113,21 @@ public TableProvider intoView() {
void doClose(long pointer) {
DataFrames.destroyDataFrame(pointer);
}

private static class RuntimeExceptionCallback implements Consumer<String> {
private final CompletableFuture<?> future;

private RuntimeExceptionCallback(CompletableFuture<?> future) {
this.future = future;
}

@Override
public void accept(String errString) {
if (ErrorUtil.containsError(errString)) {
future.completeExceptionally(new RuntimeException(errString));
} else {
future.complete(null);
}
}
}
}
Loading