All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.storage.rpc.ScannerImpl Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

There is a newer version: 3.14.0
Show newest version
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);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy