io.trino.cli.OutputHandler Maven / Gradle / Ivy
/*
* Licensed 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 io.trino.cli;
import io.airlift.units.Duration;
import io.trino.client.StatementClient;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static io.airlift.units.Duration.nanosSince;
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
public final class OutputHandler
implements Closeable
{
private static final int MAX_QUEUED_ROWS = 50_000;
private static final int MAX_BUFFERED_ROWS = 10_000;
private static final Duration MAX_BUFFER_TIME = new Duration(3, SECONDS);
private static final List> END_TOKEN = new ArrayList<>(0);
private final AtomicBoolean closed = new AtomicBoolean();
private final OutputPrinter printer;
public OutputHandler(OutputPrinter printer)
{
this.printer = requireNonNull(printer, "printer is null");
}
@Override
public void close()
throws IOException
{
if (!closed.getAndSet(true)) {
printer.finish();
}
}
public void processRows(StatementClient client)
throws IOException
{
BlockingQueue> rowQueue = new ArrayBlockingQueue<>(MAX_QUEUED_ROWS);
CompletableFuture readerFuture = CompletableFuture.runAsync(() -> {
while (client.isRunning()) {
Iterable> data = client.currentData().getData();
if (data != null) {
for (List