Search in sources :

Example 31 with Scan

use of com.scalar.db.api.Scan in project scalardb by scalar-labs.

the class AdminIntegrationTestBase method truncateTable_ShouldTruncateProperly.

@Test
public void truncateTable_ShouldTruncateProperly() throws ExecutionException, IOException {
    // Arrange
    Key partitionKey = new Key(COL_NAME2, "aaa", COL_NAME1, 1);
    Key clusteringKey = new Key(COL_NAME4, 2, COL_NAME3, "bbb");
    storage.put(new Put(partitionKey, clusteringKey).withValue(COL_NAME5, 3).withValue(COL_NAME6, "ccc").withValue(COL_NAME7, 4L).withValue(COL_NAME8, 1.0f).withValue(COL_NAME9, 1.0d).withValue(COL_NAME10, true).withValue(COL_NAME11, "ddd".getBytes(StandardCharsets.UTF_8)).forNamespace(namespace1).forTable(TABLE1));
    // Act
    admin.truncateTable(namespace1, TABLE1);
    // Assert
    Scanner scanner = storage.scan(new Scan(partitionKey).forNamespace(namespace1).forTable(TABLE1));
    assertThat(scanner.all()).isEmpty();
    scanner.close();
}
Also used : Scanner(com.scalar.db.api.Scanner) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) Test(org.junit.Test)

Example 32 with Scan

use of com.scalar.db.api.Scan in project scalardb by scalar-labs.

the class SelectStatementHandler method executeQueryWithIndex.

private List<Map<String, AttributeValue>> executeQueryWithIndex(Selection selection, TableMetadata tableMetadata) {
    DynamoOperation dynamoOperation = new DynamoOperation(selection, tableMetadata);
    Value<?> keyValue = selection.getPartitionKey().get().get(0);
    String column = keyValue.getName();
    String indexTable = dynamoOperation.getGlobalIndexName(column);
    QueryRequest.Builder builder = QueryRequest.builder().tableName(dynamoOperation.getTableName()).indexName(indexTable);
    String expressionColumnName = DynamoOperation.COLUMN_NAME_ALIAS + "0";
    String condition = expressionColumnName + " = " + DynamoOperation.VALUE_ALIAS + "0";
    ValueBinder binder = new ValueBinder(DynamoOperation.VALUE_ALIAS);
    keyValue.accept(binder);
    Map<String, AttributeValue> bindMap = binder.build();
    builder.keyConditionExpression(condition).expressionAttributeValues(bindMap).expressionAttributeNames(ImmutableMap.of(expressionColumnName, column));
    if (!selection.getProjections().isEmpty()) {
        projectionExpression(builder, selection);
    }
    if (selection instanceof Scan) {
        Scan scan = (Scan) selection;
        if (scan.getLimit() > 0) {
            builder.limit(scan.getLimit());
        }
    }
    QueryResponse queryResponse = client.query(builder.build());
    return new ArrayList<>(queryResponse.items());
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse) ArrayList(java.util.ArrayList) Scan(com.scalar.db.api.Scan)

Example 33 with Scan

use of com.scalar.db.api.Scan in project scalardb by scalar-labs.

the class JdbcServiceTest method whenScanExecuted_shouldCallQueryBuilder.

@Test
@SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION")
public void whenScanExecuted_shouldCallQueryBuilder() throws Exception {
    // Arrange
    when(queryBuilder.select(any())).thenReturn(selectQueryBuilder);
    when(selectQueryBuilder.from(any(), any(), any())).thenReturn(selectQueryBuilder);
    when(selectQueryBuilder.where(any(), any(), anyBoolean(), any(), anyBoolean())).thenReturn(selectQueryBuilder);
    when(selectQueryBuilder.orderBy(any())).thenReturn(selectQueryBuilder);
    when(selectQueryBuilder.limit(anyInt())).thenReturn(selectQueryBuilder);
    when(selectQueryBuilder.build()).thenReturn(selectQuery);
    when(connection.prepareStatement(any())).thenReturn(preparedStatement);
    when(preparedStatement.executeQuery()).thenReturn(resultSet);
    when(resultSet.next()).thenReturn(false);
    // Act
    Scan scan = new Scan(new Key("p1", "val")).forNamespace(NAMESPACE).forTable(TABLE);
    jdbcService.scan(scan, connection);
    // Assert
    verify(operationChecker).check(any(Scan.class));
    verify(queryBuilder).select(any());
}
Also used : Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 34 with Scan

use of com.scalar.db.api.Scan in project scalardb by scalar-labs.

the class StorageWithReservedKeywordIntegrationTestBase method delete_WithReservedKeywordAndDeleteWithPartitionKeyAndClusteringKeyGiven_ShouldDeleteSingleRecordProperly.

@Test
public void delete_WithReservedKeywordAndDeleteWithPartitionKeyAndClusteringKeyGiven_ShouldDeleteSingleRecordProperly() throws IOException, ExecutionException {
    // Arrange
    populateRecords();
    int pKey = 0;
    int cKey = 0;
    Key partitionKey = new Key(columnName1, pKey);
    // Act
    Delete delete = prepareDelete(pKey, cKey);
    assertThatCode(() -> storage.delete(delete)).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(new Scan(partitionKey));
    assertThat(results.size()).isEqualTo(2);
    assertThat(results.get(0).getValue(columnName1).isPresent()).isTrue();
    assertThat(results.get(0).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(0).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(0).getValue(columnName4).get().getAsInt()).isEqualTo(cKey + 1);
    assertThat(results.get(1).getValue(columnName1).isPresent()).isTrue();
    assertThat(results.get(1).getValue(columnName1).get().getAsInt()).isEqualTo(0);
    assertThat(results.get(1).getValue(columnName4).isPresent()).isTrue();
    assertThat(results.get(1).getValue(columnName4).get().getAsInt()).isEqualTo(cKey + 2);
}
Also used : Delete(com.scalar.db.api.Delete) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 35 with Scan

use of com.scalar.db.api.Scan in project scalardb by scalar-labs.

the class StorageWithReservedKeywordIntegrationTestBase method delete_WithReservedKeywordAndMultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProperly.

@Test
public void delete_WithReservedKeywordAndMultipleDeleteWithDifferentConditionsGiven_ShouldDeleteProperly() throws IOException, ExecutionException {
    // Arrange
    List<Put> puts = preparePuts();
    List<Delete> deletes = prepareDeletes();
    storage.mutate(Arrays.asList(puts.get(0), puts.get(1), puts.get(2)));
    deletes.get(0).withCondition(new DeleteIfExists());
    deletes.get(1).withCondition(new DeleteIf(new ConditionalExpression(columnName2, new TextValue("1"), ConditionalExpression.Operator.EQ)));
    // Act
    assertThatCode(() -> storage.delete(Arrays.asList(deletes.get(0), deletes.get(1), deletes.get(2)))).doesNotThrowAnyException();
    // Assert
    List<Result> results = scanAll(new Scan(new Key(columnName1, 0)));
    assertThat(results.size()).isEqualTo(0);
}
Also used : Delete(com.scalar.db.api.Delete) TextValue(com.scalar.db.io.TextValue) ConditionalExpression(com.scalar.db.api.ConditionalExpression) Scan(com.scalar.db.api.Scan) Put(com.scalar.db.api.Put) DeleteIfExists(com.scalar.db.api.DeleteIfExists) DeleteIf(com.scalar.db.api.DeleteIf) Key(com.scalar.db.io.Key) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Aggregations

Scan (com.scalar.db.api.Scan)241 Key (com.scalar.db.io.Key)137 Test (org.junit.jupiter.api.Test)125 Result (com.scalar.db.api.Result)105 Test (org.junit.Test)72 Put (com.scalar.db.api.Put)37 HashMap (java.util.HashMap)32 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)32 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)31 ByteBuffer (java.nio.ByteBuffer)27 KeyBytesEncoder (com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder)26 Scanner (com.scalar.db.api.Scanner)17 CosmosQueryRequestOptions (com.azure.cosmos.models.CosmosQueryRequestOptions)14 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)13 PartitionKey (com.azure.cosmos.models.PartitionKey)11 ArrayList (java.util.ArrayList)11 Delete (com.scalar.db.api.Delete)9 Value (com.scalar.db.io.Value)8 ConditionalExpression (com.scalar.db.api.ConditionalExpression)7