Search in sources :

Example 1 with GlobalSecondaryIndexDescription

use of software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription in project para by Erudika.

the class AWSDynamoUtils method queryGSI.

private static QueryResponse queryGSI(String appid, Pager p) {
    Pager pager = (p != null) ? p : new Pager();
    GlobalSecondaryIndexDescription index = getSharedGlobalIndex();
    QueryRequest.Builder query = QueryRequest.builder().limit(pager.getLimit()).keyConditionExpression(Config._APPID + " = :aid").expressionAttributeValues(Collections.singletonMap(":aid", AttributeValue.builder().s(appid).build()));
    if (!StringUtils.isBlank(pager.getLastKey())) {
        // See https://stackoverflow.com/questions/40988397/42735813#42735813
        Map<String, AttributeValue> startKey = new HashMap<>(3);
        // HASH/PARTITION KEY
        startKey.put(Config._APPID, AttributeValue.builder().s(appid).build());
        // RANGE/SORT KEY
        startKey.put(Config._ID, AttributeValue.builder().s(pager.getLastKey()).build());
        // TABLE PRIMARY KEY
        startKey.put(Config._KEY, AttributeValue.builder().s(getKeyForAppid(pager.getLastKey(), appid)).build());
        query.exclusiveStartKey(startKey);
    }
    return index != null ? getClient().query(query.indexName(index.indexName()).tableName(getTableNameForAppid(SHARED_TABLE)).build()) : null;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) HashMap(java.util.HashMap) Pager(com.erudika.para.core.utils.Pager) GlobalSecondaryIndexDescription(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription)

Example 2 with GlobalSecondaryIndexDescription

use of software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription in project scalardb by scalar-labs.

the class DynamoAdminTest method createIndex_ShouldCreateIndexProperly.

@Test
public void createIndex_ShouldCreateIndexProperly() throws ExecutionException {
    // Arrange
    GetItemResponse getItemResponse = mock(GetItemResponse.class);
    when(client.getItem(any(GetItemRequest.class))).thenReturn(getItemResponse);
    when(getItemResponse.item()).thenReturn(ImmutableMap.<String, AttributeValue>builder().put(DynamoAdmin.METADATA_ATTR_TABLE, AttributeValue.builder().s(FULL_TABLE_NAME).build()).put(DynamoAdmin.METADATA_ATTR_COLUMNS, AttributeValue.builder().m(ImmutableMap.<String, AttributeValue>builder().put("c1", AttributeValue.builder().s("text").build()).put("c2", AttributeValue.builder().s("bigint").build()).put("c3", AttributeValue.builder().s("boolean").build()).put("c4", AttributeValue.builder().s("int").build()).build()).build()).put(DynamoAdmin.METADATA_ATTR_PARTITION_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c1").build()).build()).put(DynamoAdmin.METADATA_ATTR_CLUSTERING_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c2").build(), AttributeValue.builder().s("c3").build()).build()).put(DynamoAdmin.METADATA_ATTR_CLUSTERING_ORDERS, AttributeValue.builder().m(ImmutableMap.<String, AttributeValue>builder().put("c2", AttributeValue.builder().s("DESC").build()).put("c3", AttributeValue.builder().s("ASC").build()).build()).build()).build());
    DescribeTableResponse describeTableResponse = mock(DescribeTableResponse.class);
    when(client.describeTable(any(DescribeTableRequest.class))).thenReturn(describeTableResponse);
    TableDescription tableDescription = mock(TableDescription.class);
    when(describeTableResponse.table()).thenReturn(tableDescription);
    GlobalSecondaryIndexDescription globalSecondaryIndexDescription = mock(GlobalSecondaryIndexDescription.class);
    when(tableDescription.globalSecondaryIndexes()).thenReturn(Collections.singletonList(globalSecondaryIndexDescription));
    String indexName = getFullTableName(NAMESPACE, TABLE) + ".global_index.c4";
    when(globalSecondaryIndexDescription.indexName()).thenReturn(indexName);
    when(globalSecondaryIndexDescription.indexStatus()).thenReturn(IndexStatus.ACTIVE);
    // Act
    admin.createIndex(NAMESPACE, TABLE, "c4", Collections.emptyMap());
    // Assert
    verify(client).updateTable(any(UpdateTableRequest.class));
    verify(applicationAutoScalingClient, times(2)).putScalingPolicy(any(PutScalingPolicyRequest.class));
    verify(applicationAutoScalingClient, times(2)).registerScalableTarget(any(RegisterScalableTargetRequest.class));
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) RegisterScalableTargetRequest(software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest) UpdateTableRequest(software.amazon.awssdk.services.dynamodb.model.UpdateTableRequest) PutScalingPolicyRequest(software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) GlobalSecondaryIndexDescription(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription) GetItemResponse(software.amazon.awssdk.services.dynamodb.model.GetItemResponse) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) Test(org.junit.jupiter.api.Test)

Example 3 with GlobalSecondaryIndexDescription

use of software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription in project scalardb by scalar-labs.

the class DynamoAdmin method waitForIndexDeletion.

private void waitForIndexDeletion(String namespace, String table, String columnName) throws ExecutionException {
    try {
        String indexName = getGlobalIndexName(namespace, table, columnName);
        while (true) {
            Uninterruptibles.sleepUninterruptibly(WAITING_DURATION_SECS, TimeUnit.SECONDS);
            DescribeTableResponse response = client.describeTable(DescribeTableRequest.builder().tableName(getFullTableName(namespace, table)).build());
            boolean deleted = true;
            for (GlobalSecondaryIndexDescription globalSecondaryIndex : response.table().globalSecondaryIndexes()) {
                if (globalSecondaryIndex.indexName().equals(indexName)) {
                    deleted = false;
                    break;
                }
            }
            if (deleted) {
                break;
            }
        }
    } catch (Exception e) {
        throw new ExecutionException("waiting for the secondary index deletion failed", e);
    }
}
Also used : DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) GlobalSecondaryIndexDescription(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription) ExecutionException(com.scalar.db.exception.storage.ExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) ObjectNotFoundException(software.amazon.awssdk.services.applicationautoscaling.model.ObjectNotFoundException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 4 with GlobalSecondaryIndexDescription

use of software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription in project scalardb by scalar-labs.

the class DynamoAdmin method waitForIndexCreation.

private void waitForIndexCreation(String namespace, String table, String columnName) throws ExecutionException {
    try {
        String indexName = getGlobalIndexName(namespace, table, columnName);
        while (true) {
            Uninterruptibles.sleepUninterruptibly(WAITING_DURATION_SECS, TimeUnit.SECONDS);
            DescribeTableResponse response = client.describeTable(DescribeTableRequest.builder().tableName(getFullTableName(namespace, table)).build());
            for (GlobalSecondaryIndexDescription globalSecondaryIndex : response.table().globalSecondaryIndexes()) {
                if (globalSecondaryIndex.indexName().equals(indexName)) {
                    if (globalSecondaryIndex.indexStatus() == IndexStatus.ACTIVE) {
                        return;
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new ExecutionException("waiting for the secondary index creation failed", e);
    }
}
Also used : DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) GlobalSecondaryIndexDescription(software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription) ExecutionException(com.scalar.db.exception.storage.ExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) ObjectNotFoundException(software.amazon.awssdk.services.applicationautoscaling.model.ObjectNotFoundException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Aggregations

GlobalSecondaryIndexDescription (software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndexDescription)4 DescribeTableResponse (software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse)3 ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 ObjectNotFoundException (software.amazon.awssdk.services.applicationautoscaling.model.ObjectNotFoundException)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)2 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)2 Pager (com.erudika.para.core.utils.Pager)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1 PutScalingPolicyRequest (software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest)1 RegisterScalableTargetRequest (software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest)1 DescribeTableRequest (software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest)1 GetItemRequest (software.amazon.awssdk.services.dynamodb.model.GetItemRequest)1 GetItemResponse (software.amazon.awssdk.services.dynamodb.model.GetItemResponse)1 QueryRequest (software.amazon.awssdk.services.dynamodb.model.QueryRequest)1 TableDescription (software.amazon.awssdk.services.dynamodb.model.TableDescription)1 UpdateTableRequest (software.amazon.awssdk.services.dynamodb.model.UpdateTableRequest)1