use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class Dynamo method get.
@Override
@Nonnull
public Optional<Result> get(Get get) throws ExecutionException {
get = copyAndSetTargetToIfNot(get);
operationChecker.check(get);
Scanner scanner = null;
try {
scanner = selectStatementHandler.handle(get);
Optional<Result> ret = scanner.one();
if (scanner.one().isPresent()) {
throw new IllegalArgumentException("please use scan() for non-exact match selection");
}
return ret;
} finally {
if (scanner != null) {
try {
scanner.close();
} catch (IOException e) {
LOGGER.warn("failed to close the scanner", e);
}
}
}
}
use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class CrudHandler method scan.
public List<Result> scan(Scan scan) throws CrudException {
List<String> originalProjections = new ArrayList<>(scan.getProjections());
List<Result> results = new ArrayList<>();
Optional<List<Snapshot.Key>> keysInSnapshot = snapshot.get(scan);
if (keysInSnapshot.isPresent()) {
for (Snapshot.Key key : keysInSnapshot.get()) {
snapshot.get(key).ifPresent(results::add);
}
return createScanResults(scan, originalProjections, results);
}
List<Snapshot.Key> keys = new ArrayList<>();
Scanner scanner = null;
try {
scanner = getFromStorage(scan);
for (Result r : scanner) {
TransactionResult result = new TransactionResult(r);
if (!result.isCommitted()) {
throw new UncommittedRecordException(result, "the record needs recovery");
}
Snapshot.Key key = new Snapshot.Key(scan, r);
if (!snapshot.containsKeyInReadSet(key)) {
snapshot.put(key, Optional.of(result));
}
keys.add(key);
snapshot.get(key).ifPresent(results::add);
}
} finally {
if (scanner != null) {
try {
scanner.close();
} catch (IOException e) {
LOGGER.warn("failed to close the scanner", e);
}
}
}
snapshot.put(scan, keys);
return createScanResults(scan, originalProjections, results);
}
use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class SelectStatementHandlerTest method handle_GetOperationNoItemReturned_ShouldReturnEmptyScanner.
@Test
public void handle_GetOperationNoItemReturned_ShouldReturnEmptyScanner() throws Exception {
// Arrange
when(client.getItem(any(GetItemRequest.class))).thenReturn(getResponse);
when(getResponse.hasItem()).thenReturn(false);
Get get = prepareGet();
// Act Assert
List<Result> actual;
try (Scanner scanner = handler.handle(get)) {
actual = scanner.all();
}
// Assert
assertThat(actual).isEmpty();
}
use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class JdbcDatabaseTest method whenScanOperationExecutedAndScannerClosed_shouldCallJdbcService.
@Test
public void whenScanOperationExecutedAndScannerClosed_shouldCallJdbcService() throws Exception {
// Arrange
when(jdbcService.getScanner(any(), any())).thenReturn(new ScannerImpl(resultInterpreter, connection, preparedStatement, resultSet));
// Act
Scan scan = new Scan(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
Scanner scanner = jdbcDatabase.scan(scan);
scanner.close();
// Assert
verify(jdbcService).getScanner(any(), any());
verify(connection).close();
}
use of com.scalar.db.api.Scanner in project scalardb by scalar-labs.
the class SnapshotTest method toSerializableWithExtraRead_ScanSetUpdated_ShouldProcessWithoutExceptions.
@Test
public void toSerializableWithExtraRead_ScanSetUpdated_ShouldProcessWithoutExceptions() throws ExecutionException {
// Arrange
snapshot = prepareSnapshot(Isolation.SERIALIZABLE, SerializableStrategy.EXTRA_READ);
Scan scan = prepareScan();
Put put = preparePut();
TransactionResult txResult = prepareResult(ANY_ID);
Snapshot.Key key = new Snapshot.Key(scan, txResult);
snapshot.put(key, Optional.of(txResult));
snapshot.put(scan, Collections.singletonList(key));
snapshot.put(new Snapshot.Key(put), put);
DistributedStorage storage = mock(DistributedStorage.class);
TransactionResult changedTxResult = prepareResult(ANY_ID + "x");
Scanner scanner = mock(Scanner.class);
when(scanner.iterator()).thenReturn(Collections.singletonList((Result) changedTxResult).iterator());
Scan scanWithProjections = prepareScan().withProjections(Arrays.asList(Attribute.ID, Attribute.VERSION, ANY_NAME_1, ANY_NAME_2));
when(storage.scan(scanWithProjections)).thenReturn(scanner);
// Act Assert
assertThatThrownBy(() -> snapshot.toSerializableWithExtraRead(storage)).isInstanceOf(CommitConflictException.class);
// Assert
verify(storage).scan(scanWithProjections);
}
Aggregations