Search in sources :

Example 21 with KeyBytesEncoder

use of com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder in project scalardb by scalar-labs.

the class SelectStatementHandlerTest method handle_ScanOperationWithFullMultipleStartClusteringKeysInclusively_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanOperationWithFullMultipleStartClusteringKeysInclusively_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), 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, ANY_NAME_3, ANY_TEXT_3), metadata.getClusteringOrders()))).build());
    expectedBindMap.put(DynamoOperation.END_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)

Example 22 with KeyBytesEncoder

use of com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder in project scalardb by scalar-labs.

the class SelectStatementHandlerTest method handle_ScanOperationWithFullMultipleStartClusteringKeysExclusively_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanOperationWithFullMultipleStartClusteringKeysExclusively_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);
    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.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)

Example 23 with KeyBytesEncoder

use of com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder in project scalardb by scalar-labs.

the class SelectStatementHandlerTest method handle_ScanOperationWithPartialMultipleClusteringKeysRangeExclusively_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanOperationWithPartialMultipleClusteringKeysRangeExclusively_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).withEnd(new Key(ANY_NAME_2, ANY_TEXT_3), 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), metadata.getClusteringOrders())).get())).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 24 with KeyBytesEncoder

use of com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder in project scalardb by scalar-labs.

the class SelectStatementHandlerTest method handle_ScanOperationWithMultipleOrderings_ShouldCallQueryWithProperRequest.

@Test
public void handle_ScanOperationWithMultipleOrderings_ShouldCallQueryWithProperRequest() {
    // 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)).withOrdering(new Scan.Ordering(ANY_NAME_2, ASC_ORDER)).withOrdering(new Scan.Ordering(ANY_NAME_3, DESC_ORDER)).withLimit(ANY_LIMIT);
    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);
    assertThat(actualRequest.scanIndexForward()).isNull();
    assertThat(actualRequest.limit()).isEqualTo(ANY_LIMIT);
}
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) ByteBuffer(java.nio.ByteBuffer) Scan(com.scalar.db.api.Scan) Key(com.scalar.db.io.Key) Test(org.junit.jupiter.api.Test)

Example 25 with KeyBytesEncoder

use of com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder in project scalardb by scalar-labs.

the class SelectStatementHandlerTest method handle_ScanOperationWithSingleStartClusteringKeyExclusively_ShouldCallQueryItemsWithProperQuery.

@Test
public void handle_ScanOperationWithSingleStartClusteringKeyExclusively_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), 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(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)

Aggregations

Key (com.scalar.db.io.Key)27 KeyBytesEncoder (com.scalar.db.storage.dynamo.bytes.KeyBytesEncoder)27 HashMap (java.util.HashMap)27 Test (org.junit.jupiter.api.Test)27 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)27 Scan (com.scalar.db.api.Scan)26 ByteBuffer (java.nio.ByteBuffer)26 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)26 Get (com.scalar.db.api.Get)1