com.scalar.db.storage.rpc.ScannerImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalardb Show documentation
Show all versions of scalardb Show documentation
A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases
package com.scalar.db.storage.rpc;
import com.scalar.db.api.Result;
import com.scalar.db.api.Scan;
import com.scalar.db.api.Scanner;
import com.scalar.db.api.TableMetadata;
import com.scalar.db.common.ScannerIterator;
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.rpc.DistributedStorageGrpc;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import javax.annotation.concurrent.NotThreadSafe;
@NotThreadSafe
public class ScannerImpl implements Scanner {
private final GrpcScanOnBidirectionalStream stream;
private List results;
public ScannerImpl(
GrpcConfig config,
Scan scan,
DistributedStorageGrpc.DistributedStorageStub stub,
TableMetadata metadata)
throws ExecutionException {
stream = new GrpcScanOnBidirectionalStream(config, stub, metadata);
results = stream.openScanner(scan);
}
@Override
public Optional one() throws ExecutionException {
if (results.isEmpty()) {
return Optional.empty();
}
Result result = results.remove(0);
if (results.isEmpty() && stream.hasMoreResults()) {
results = stream.next();
}
return Optional.of(result);
}
@Override
public List all() throws ExecutionException {
List ret = new ArrayList<>();
while (true) {
Optional result = one();
if (result.isPresent()) {
ret.add(result.get());
} else {
break;
}
}
return ret;
}
@Override
public void close() throws IOException {
try {
stream.closeScanner();
} catch (ExecutionException e) {
throw new IOException("close failed", e);
}
}
@Override
public Iterator iterator() {
return new ScannerIterator(this);
}
}