com.scalar.db.storage.dynamo.GetItemScanner 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.dynamo;
import com.scalar.db.api.Result;
import com.scalar.db.api.Scanner;
import com.scalar.db.common.ScannerIterator;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
@NotThreadSafe
public final class GetItemScanner implements Scanner {
private final Map item;
private final ResultInterpreter resultInterpreter;
private boolean hasNext;
private ScannerIterator scannerIterator;
@SuppressFBWarnings("EI_EXPOSE_REP2")
public GetItemScanner(
DynamoDbClient client, GetItemRequest request, ResultInterpreter resultInterpreter) {
GetItemResponse response = client.getItem(request);
if (response.hasItem()) {
item = response.item();
hasNext = true;
} else {
item = null;
hasNext = false;
}
this.resultInterpreter = resultInterpreter;
}
@Override
@Nonnull
public Optional one() {
if (!hasNext) {
return Optional.empty();
}
Result result = resultInterpreter.interpret(item);
hasNext = false;
return Optional.of(result);
}
@Override
@Nonnull
public List all() {
if (!hasNext) {
return Collections.emptyList();
}
Result result = resultInterpreter.interpret(item);
hasNext = false;
return Collections.singletonList(result);
}
@Override
@Nonnull
public Iterator iterator() {
if (scannerIterator == null) {
scannerIterator = new ScannerIterator(this);
}
return scannerIterator;
}
@Override
public void close() {}
}