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

com.scalar.db.storage.dynamo.QueryScanner Maven / Gradle / Ivy

Go to download

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

The newest version!
package com.scalar.db.storage.dynamo;

import com.scalar.db.api.Result;
import com.scalar.db.api.Scanner;
import com.scalar.db.common.ScannerIterator;
import com.scalar.db.storage.dynamo.request.PaginatedRequest;
import com.scalar.db.storage.dynamo.request.PaginatedRequestResponse;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

public class QueryScanner implements Scanner {

  private final PaginatedRequest request;
  private final ResultInterpreter resultInterpreter;

  private Iterator> itemsIterator;
  @Nullable private Map lastEvaluatedKey;
  private int totalResultCount;

  private ScannerIterator scannerIterator;

  @SuppressFBWarnings("EI_EXPOSE_REP2")
  public QueryScanner(PaginatedRequest request, ResultInterpreter resultInterpreter) {
    this.request = request;
    this.resultInterpreter = resultInterpreter;

    handleResponse(request.execute());
  }

  @Override
  @Nonnull
  public Optional one() {
    if (!hasNext()) {
      return Optional.empty();
    }

    return Optional.of(resultInterpreter.interpret(itemsIterator.next()));
  }

  private boolean hasNext() {
    if (itemsIterator.hasNext()) {
      return true;
    }
    if (lastEvaluatedKey != null) {
      handleResponse(request.execute(lastEvaluatedKey));
      return itemsIterator.hasNext();
    }
    return false;
  }

  private void handleResponse(PaginatedRequestResponse response) {
    List> items = response.items();
    totalResultCount += items.size();
    itemsIterator = items.iterator();
    if ((request.limit() == null || totalResultCount < request.limit())
        && response.hasLastEvaluatedKey()) {
      lastEvaluatedKey = response.lastEvaluatedKey();
    } else {
      lastEvaluatedKey = null;
    }
  }

  @Override
  @Nonnull
  public List all() {
    List ret = new ArrayList<>();
    while (true) {
      Optional one = one();
      if (!one.isPresent()) {
        break;
      }
      ret.add(one.get());
    }
    return ret;
  }

  @Override
  @Nonnull
  public Iterator iterator() {
    if (scannerIterator == null) {
      scannerIterator = new ScannerIterator(this);
    }
    return scannerIterator;
  }

  @Override
  public void close() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy