Search in sources :

Example 71 with Scan

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

the class SelectStatementHandlerTest method handle_ScanOperationWithSingleClusteringKeyRangeInclusively_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanOperationWithSingleClusteringKeyRangeInclusively_ShouldCallQueryItemsWithProperQuery() {
    // Arrange
    when(client.query(any(QueryRequest.class))).thenReturn(queryResponse);
    when(queryResponse.items()).thenReturn(Collections.singletonList(new HashMap<>()));
    Scan scan = prepareScan().withStart(new Key(ANY_NAME_2, ANY_TEXT_2), true).withEnd(new Key(ANY_NAME_2, ANY_TEXT_3), true);
    String expectedCondition = DynamoOperation.PARTITION_KEY + " = " + DynamoOperation.PARTITION_KEY_ALIAS + " AND " + DynamoOperation.CLUSTERING_KEY + " BETWEEN " + DynamoOperation.START_CLUSTERING_KEY_ALIAS + " AND " + DynamoOperation.END_CLUSTERING_KEY_ALIAS;
    DynamoOperation dynamoOperation = new DynamoOperation(scan, metadata);
    ByteBuffer partitionKey = dynamoOperation.getConcatenatedPartitionKey();
    Map<String, AttributeValue> expectedBindMap = new HashMap<>();
    expectedBindMap.put(DynamoOperation.PARTITION_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(partitionKey)).build());
    expectedBindMap.put(DynamoOperation.START_CLUSTERING_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(new KeyBytesEncoder().encode(new Key(ANY_NAME_2, ANY_TEXT_2), metadata.getClusteringOrders()))).build());
    expectedBindMap.put(DynamoOperation.END_CLUSTERING_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(new KeyBytesEncoder().encode(new Key(ANY_NAME_2, ANY_TEXT_3), metadata.getClusteringOrders()))).build());
    // Act Assert
    assertThatCode(() -> handler.handle(scan)).doesNotThrowAnyException();
    // Assert
    ArgumentCaptor<QueryRequest> captor = ArgumentCaptor.forClass(QueryRequest.class);
    verify(client).query(captor.capture());
    QueryRequest actualRequest = captor.getValue();
    assertThat(actualRequest.keyConditionExpression()).isEqualTo(expectedCondition);
    assertThat(actualRequest.expressionAttributeValues()).isEqualTo(expectedBindMap);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) KeyBytesEncoder(com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder) HashMap(java.util.HashMap) Scan(com.scalar.db.api.Scan) ByteBuffer(java.nio.ByteBuffer) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 72 with Scan

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

the class SelectStatementHandlerTest method handle_ScanOperationWithFullMultipleClusteringKeysRangeExclusively_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanOperationWithFullMultipleClusteringKeysRangeExclusively_ShouldCallQueryItemsWithProperQuery() {
    // Arrange
    when(metadata.getPartitionKeyNames()).thenReturn(new LinkedHashSet<>(Collections.singletonList(ANY_NAME_1)));
    when(metadata.getClusteringKeyNames()).thenReturn(new LinkedHashSet<>(Arrays.asList(ANY_NAME_2, ANY_NAME_3)));
    when(metadata.getClusteringOrder(any())).thenReturn(Order.ASC);
    when(metadata.getClusteringOrders()).thenReturn(ImmutableMap.of(ANY_NAME_2, Order.ASC, ANY_NAME_3, Order.ASC));
    when(client.query(any(QueryRequest.class))).thenReturn(queryResponse);
    when(queryResponse.items()).thenReturn(Collections.singletonList(new HashMap<>()));
    Scan scan = prepareScan().withStart(new Key(ANY_NAME_2, ANY_TEXT_2, ANY_NAME_3, ANY_TEXT_3), false).withEnd(new Key(ANY_NAME_2, ANY_TEXT_2, ANY_NAME_3, ANY_TEXT_4), false);
    String expectedCondition = DynamoOperation.PARTITION_KEY + " = " + DynamoOperation.PARTITION_KEY_ALIAS + " AND " + DynamoOperation.CLUSTERING_KEY + " BETWEEN " + DynamoOperation.START_CLUSTERING_KEY_ALIAS + " AND " + DynamoOperation.END_CLUSTERING_KEY_ALIAS;
    DynamoOperation dynamoOperation = new DynamoOperation(scan, metadata);
    ByteBuffer partitionKey = dynamoOperation.getConcatenatedPartitionKey();
    Map<String, AttributeValue> expectedBindMap = new HashMap<>();
    expectedBindMap.put(DynamoOperation.PARTITION_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(partitionKey)).build());
    expectedBindMap.put(DynamoOperation.START_CLUSTERING_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(BytesUtils.getClosestNextBytes(new KeyBytesEncoder().encode(new Key(ANY_NAME_2, ANY_TEXT_2, ANY_NAME_3, ANY_TEXT_3), metadata.getClusteringOrders())).get())).build());
    expectedBindMap.put(DynamoOperation.END_CLUSTERING_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(BytesUtils.getClosestPreviousBytes(new KeyBytesEncoder().encode(new Key(ANY_NAME_2, ANY_TEXT_2, ANY_NAME_3, ANY_TEXT_4), metadata.getClusteringOrders())).get())).build());
    // Act Assert
    assertThatCode(() -> handler.handle(scan)).doesNotThrowAnyException();
    // Assert
    ArgumentCaptor<QueryRequest> captor = ArgumentCaptor.forClass(QueryRequest.class);
    verify(client).query(captor.capture());
    QueryRequest actualRequest = captor.getValue();
    assertThat(actualRequest.keyConditionExpression()).isEqualTo(expectedCondition);
    assertThat(actualRequest.expressionAttributeValues()).isEqualTo(expectedBindMap);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) KeyBytesEncoder(com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder) HashMap(java.util.HashMap) Scan(com.scalar.db.api.Scan) ByteBuffer(java.nio.ByteBuffer) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 73 with Scan

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

the class SelectStatementHandlerTest method handle_ScanOperationWithSingleStartClusteringKeyInclusively_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanOperationWithSingleStartClusteringKeyInclusively_ShouldCallQueryItemsWithProperQuery() {
    // Arrange
    when(client.query(any(QueryRequest.class))).thenReturn(queryResponse);
    when(queryResponse.items()).thenReturn(Collections.singletonList(new HashMap<>()));
    Scan scan = prepareScan().withStart(new Key(ANY_NAME_2, ANY_TEXT_2), true);
    String expectedCondition = DynamoOperation.PARTITION_KEY + " = " + DynamoOperation.PARTITION_KEY_ALIAS + " AND " + DynamoOperation.CLUSTERING_KEY + " >= " + DynamoOperation.START_CLUSTERING_KEY_ALIAS;
    DynamoOperation dynamoOperation = new DynamoOperation(scan, metadata);
    ByteBuffer partitionKey = dynamoOperation.getConcatenatedPartitionKey();
    Map<String, AttributeValue> expectedBindMap = new HashMap<>();
    expectedBindMap.put(DynamoOperation.PARTITION_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(partitionKey)).build());
    expectedBindMap.put(DynamoOperation.START_CLUSTERING_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(new KeyBytesEncoder().encode(new Key(ANY_NAME_2, ANY_TEXT_2), metadata.getClusteringOrders()))).build());
    // Act Assert
    assertThatCode(() -> handler.handle(scan)).doesNotThrowAnyException();
    // Assert
    ArgumentCaptor<QueryRequest> captor = ArgumentCaptor.forClass(QueryRequest.class);
    verify(client).query(captor.capture());
    QueryRequest actualRequest = captor.getValue();
    assertThat(actualRequest.keyConditionExpression()).isEqualTo(expectedCondition);
    assertThat(actualRequest.expressionAttributeValues()).isEqualTo(expectedBindMap);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) KeyBytesEncoder(com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder) HashMap(java.util.HashMap) Scan(com.scalar.db.api.Scan) ByteBuffer(java.nio.ByteBuffer) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 74 with Scan

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

the class SelectStatementHandlerTest method handle_ScanOperationWithIndexGiven_WithProjections_ShouldCallQuery.

@Test
public void handle_ScanOperationWithIndexGiven_WithProjections_ShouldCallQuery() {
    // Arrange
    when(client.query(any(QueryRequest.class))).thenReturn(queryResponse);
    when(queryResponse.items()).thenReturn(Collections.singletonList(new HashMap<>()));
    Key indexKey = new Key(ANY_NAME_3, ANY_TEXT_3);
    Scan scan = new Scan(indexKey).withProjection(ANY_NAME_1).withProjection(ANY_NAME_2).forNamespace(ANY_NAMESPACE_NAME).forTable(ANY_TABLE_NAME);
    String expectedKeyCondition = DynamoOperation.COLUMN_NAME_ALIAS + "0 = " + DynamoOperation.VALUE_ALIAS + "0";
    Map<String, AttributeValue> expectedBindMap = new HashMap<>();
    expectedBindMap.put(DynamoOperation.VALUE_ALIAS + "0", AttributeValue.builder().s(ANY_TEXT_3).build());
    Map<String, String> expectedExpressionAttributeNames = new HashMap<>();
    expectedExpressionAttributeNames.put(DynamoOperation.COLUMN_NAME_ALIAS + "0", ANY_NAME_3);
    expectedExpressionAttributeNames.put(DynamoOperation.COLUMN_NAME_ALIAS + "1", ANY_NAME_1);
    expectedExpressionAttributeNames.put(DynamoOperation.COLUMN_NAME_ALIAS + "2", ANY_NAME_2);
    // Act Assert
    assertThatCode(() -> handler.handle(scan)).doesNotThrowAnyException();
    // Assert
    ArgumentCaptor<QueryRequest> captor = ArgumentCaptor.forClass(QueryRequest.class);
    verify(client).query(captor.capture());
    QueryRequest actualRequest = captor.getValue();
    assertThat(actualRequest.keyConditionExpression()).isEqualTo(expectedKeyCondition);
    assertThat(actualRequest.expressionAttributeValues()).isEqualTo(expectedBindMap);
    assertThat(actualRequest.expressionAttributeNames()).isEqualTo(expectedExpressionAttributeNames);
    assertThat(actualRequest.projectionExpression()).isEqualTo(DynamoOperation.COLUMN_NAME_ALIAS + "1," + DynamoOperation.COLUMN_NAME_ALIAS + "2");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) HashMap(java.util.HashMap) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 75 with Scan

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

the class SelectStatementHandlerTest method handle_ScanOperationWithPartialMultipleStartClusteringKeysExclusively_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanOperationWithPartialMultipleStartClusteringKeysExclusively_ShouldCallQueryItemsWithProperQuery() {
    // Arrange
    when(metadata.getPartitionKeyNames()).thenReturn(new LinkedHashSet<>(Collections.singletonList(ANY_NAME_1)));
    when(metadata.getClusteringKeyNames()).thenReturn(new LinkedHashSet<>(Arrays.asList(ANY_NAME_2, ANY_NAME_3)));
    when(metadata.getClusteringOrder(any())).thenReturn(Order.ASC);
    when(metadata.getClusteringOrders()).thenReturn(ImmutableMap.of(ANY_NAME_2, Order.ASC, ANY_NAME_3, Order.ASC));
    when(client.query(any(QueryRequest.class))).thenReturn(queryResponse);
    when(queryResponse.items()).thenReturn(Collections.singletonList(new HashMap<>()));
    Scan scan = prepareScan().withStart(new Key(ANY_NAME_2, ANY_TEXT_2), false);
    String expectedCondition = DynamoOperation.PARTITION_KEY + " = " + DynamoOperation.PARTITION_KEY_ALIAS + " AND " + DynamoOperation.CLUSTERING_KEY + " >= " + DynamoOperation.START_CLUSTERING_KEY_ALIAS;
    DynamoOperation dynamoOperation = new DynamoOperation(scan, metadata);
    ByteBuffer partitionKey = dynamoOperation.getConcatenatedPartitionKey();
    Map<String, AttributeValue> expectedBindMap = new HashMap<>();
    expectedBindMap.put(DynamoOperation.PARTITION_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(partitionKey)).build());
    expectedBindMap.put(DynamoOperation.START_CLUSTERING_KEY_ALIAS, AttributeValue.builder().b(SdkBytes.fromByteBuffer(BytesUtils.getClosestNextBytes(new KeyBytesEncoder().encode(new Key(ANY_NAME_2, ANY_TEXT_2), metadata.getClusteringOrders())).get())).build());
    // Act Assert
    assertThatCode(() -> handler.handle(scan)).doesNotThrowAnyException();
    // Assert
    ArgumentCaptor<QueryRequest> captor = ArgumentCaptor.forClass(QueryRequest.class);
    verify(client).query(captor.capture());
    QueryRequest actualRequest = captor.getValue();
    assertThat(actualRequest.keyConditionExpression()).isEqualTo(expectedCondition);
    assertThat(actualRequest.expressionAttributeValues()).isEqualTo(expectedBindMap);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) KeyBytesEncoder(com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder) HashMap(java.util.HashMap) Scan(com.scalar.db.api.Scan) ByteBuffer(java.nio.ByteBuffer) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.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