Search in sources :

Example 6 with Scanner

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);
            }
        }
    }
}
Also used : Scanner(com.scalar.db.api.Scanner) IOException(java.io.IOException) Result(com.scalar.db.api.Result) Nonnull(javax.annotation.Nonnull)

Example 7 with Scanner

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);
}
Also used : Scanner(com.scalar.db.api.Scanner) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Result(com.scalar.db.api.Result) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with Scanner

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();
}
Also used : Scanner(com.scalar.db.api.Scanner) Get(com.scalar.db.api.Get) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) Result(com.scalar.db.api.Result) Test(org.junit.jupiter.api.Test)

Example 9 with Scanner

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();
}
Also used : Scanner(com.scalar.db.api.Scanner) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 10 with Scanner

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);
}
Also used : Scanner(com.scalar.db.api.Scanner) DistributedStorage(com.scalar.db.api.DistributedStorage) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Aggregations

Scanner (com.scalar.db.api.Scanner)20 Scan (com.scalar.db.api.Scan)17 Result (com.scalar.db.api.Result)14 Key (com.scalar.db.io.Key)14 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 DistributedStorage (com.scalar.db.api.DistributedStorage)6 Put (com.scalar.db.api.Put)6 Test (org.junit.jupiter.api.Test)6 IOException (java.io.IOException)3 Get (com.scalar.db.api.Get)2 List (java.util.List)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Ordering (com.scalar.db.api.Scan.Ordering)1 ResultImpl (com.scalar.db.common.ResultImpl)1 IntValue (com.scalar.db.io.IntValue)1 TextValue (com.scalar.db.io.TextValue)1 ParallelExecutorTask (com.scalar.db.transaction.consensuscommit.ParallelExecutor.ParallelExecutorTask)1 ResultImpl (com.scalar.db.util.ResultImpl)1 HashMap (java.util.HashMap)1