Search in sources :

Example 66 with TableMetadata

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

the class JdbcTransactionAdminTest method getTableMetadata_ShouldCallJdbcAdminProperly.

@Test
public void getTableMetadata_ShouldCallJdbcAdminProperly() throws ExecutionException {
    // Arrange
    TableMetadata metadata = TableMetadata.newBuilder().addColumn("c1", DataType.INT).addPartitionKey("c1").build();
    when(jdbcAdmin.getTableMetadata(any(), any())).thenReturn(metadata);
    // Act
    TableMetadata actual = admin.getTableMetadata("ns", "tbl");
    // Assert
    verify(jdbcAdmin).getTableMetadata("ns", "tbl");
    assertThat(actual).isEqualTo(metadata);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Test(org.junit.jupiter.api.Test)

Example 67 with TableMetadata

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

the class JdbcTransactionAdminTest method createTable_ShouldCallJdbcAdminProperly.

@Test
public void createTable_ShouldCallJdbcAdminProperly() throws ExecutionException {
    // Arrange
    TableMetadata metadata = TableMetadata.newBuilder().addColumn("c1", DataType.INT).addPartitionKey("c1").build();
    // Act
    admin.createTable("ns", "tbl", metadata, Collections.emptyMap());
    // Assert
    verify(jdbcAdmin).createTable("ns", "tbl", metadata, Collections.emptyMap());
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) Test(org.junit.jupiter.api.Test)

Example 68 with TableMetadata

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

the class GrpcTransactionAdminTest method getTableMetadata_CalledWithProperArguments_MetadataManagerShouldBeCalledProperly.

@Test
public void getTableMetadata_CalledWithProperArguments_MetadataManagerShouldBeCalledProperly() throws ExecutionException {
    // Arrange
    String namespace = "namespace";
    String table = "table";
    GetTableMetadataResponse response = mock(GetTableMetadataResponse.class);
    when(stub.getTableMetadata(any())).thenReturn(response);
    when(response.hasTableMetadata()).thenReturn(true);
    when(response.getTableMetadata()).thenReturn(com.scalar.db.rpc.TableMetadata.newBuilder().putColumn("col1", com.scalar.db.rpc.DataType.DATA_TYPE_INT).putColumn("col2", com.scalar.db.rpc.DataType.DATA_TYPE_INT).putColumn("col3", com.scalar.db.rpc.DataType.DATA_TYPE_INT).addPartitionKeyName("col1").addClusteringKeyName("col2").putClusteringOrder("col2", com.scalar.db.rpc.Order.ORDER_DESC).build());
    // Act
    TableMetadata tableMetadata = admin.getTableMetadata(namespace, table);
    // Assert
    verify(stub).getTableMetadata(GetTableMetadataRequest.newBuilder().setNamespace(namespace).setTable(table).build());
    assertThat(tableMetadata).isEqualTo(TableMetadata.newBuilder().addColumn("col1", DataType.INT).addColumn("col2", DataType.INT).addColumn("col3", DataType.INT).addPartitionKey("col1").addClusteringKey("col2", Scan.Ordering.Order.DESC).build());
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) GetTableMetadataResponse(com.scalar.db.rpc.GetTableMetadataResponse) Test(org.junit.jupiter.api.Test)

Example 69 with TableMetadata

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

the class TableSchema method buildTableMetadata.

protected TableMetadata buildTableMetadata(JsonObject tableDefinition) throws SchemaLoaderException {
    TableMetadata.Builder tableBuilder = TableMetadata.newBuilder();
    // Add partition keys
    if (!tableDefinition.keySet().contains(PARTITION_KEY)) {
        throw new SchemaLoaderException("Parsing the schema JSON failed. Table must contains partition key");
    }
    JsonArray partitionKeys = tableDefinition.get(PARTITION_KEY).getAsJsonArray();
    traveledKeys.add(PARTITION_KEY);
    for (JsonElement partitionKey : partitionKeys) {
        tableBuilder.addPartitionKey(partitionKey.getAsString());
    }
    // Add clustering keys
    if (tableDefinition.keySet().contains(CLUSTERING_KEY)) {
        JsonArray clusteringKeys = tableDefinition.get(CLUSTERING_KEY).getAsJsonArray();
        traveledKeys.add(CLUSTERING_KEY);
        for (JsonElement clusteringKeyRaw : clusteringKeys) {
            String clusteringKey;
            String order;
            String[] clusteringKeyFull = clusteringKeyRaw.getAsString().split(" ", -1);
            if (clusteringKeyFull.length < 2) {
                clusteringKey = clusteringKeyFull[0];
                tableBuilder.addClusteringKey(clusteringKey);
            } else if (clusteringKeyFull.length == 2 && (clusteringKeyFull[1].equalsIgnoreCase("ASC") || clusteringKeyFull[1].equalsIgnoreCase("DESC"))) {
                clusteringKey = clusteringKeyFull[0];
                order = clusteringKeyFull[1];
                tableBuilder.addClusteringKey(clusteringKey, ORDER_MAP.get(order.toUpperCase()));
            } else {
                throw new SchemaLoaderException("Parsing the schema JSON failed. Invalid clustering keys");
            }
        }
    }
    boolean transaction = false;
    if (tableDefinition.keySet().contains(TRANSACTION)) {
        transaction = tableDefinition.get(TRANSACTION).getAsBoolean();
        traveledKeys.add(TRANSACTION);
    }
    if (transaction) {
        isTransactionalTable = true;
    }
    // Add columns
    if (!tableDefinition.keySet().contains(COLUMNS)) {
        throw new SchemaLoaderException("Parsing the schema JSON failed. Table must contains columns");
    }
    JsonObject columns = tableDefinition.get(COLUMNS).getAsJsonObject();
    traveledKeys.add(COLUMNS);
    for (Entry<String, JsonElement> column : columns.entrySet()) {
        String columnName = column.getKey();
        DataType columnDataType = DATA_MAP_TYPE.get(column.getValue().getAsString().toUpperCase());
        if (columnDataType == null) {
            throw new SchemaLoaderException("Parsing the schema JSON failed. Invalid column type for column " + columnName);
        }
        tableBuilder.addColumn(columnName, columnDataType);
    }
    // Add secondary indexes
    if (tableDefinition.keySet().contains(SECONDARY_INDEX)) {
        JsonArray secondaryIndexes = tableDefinition.get(SECONDARY_INDEX).getAsJsonArray();
        traveledKeys.add(SECONDARY_INDEX);
        for (JsonElement sIdx : secondaryIndexes) {
            tableBuilder.addSecondaryIndex(sIdx.getAsString());
        }
    }
    return tableBuilder.build();
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) DataType(com.scalar.db.io.DataType)

Example 70 with TableMetadata

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

the class DynamoAdminTest method createTable_WhenMetadataTableNotExist_ShouldCreateTableAndMetadataTable.

private void createTable_WhenMetadataTableNotExist_ShouldCreateTableAndMetadataTable(Optional<String> tableMetadataNamespace) throws ExecutionException {
    // Arrange
    String metadataNamespaceName = tableMetadataNamespace.orElse(DynamoAdmin.METADATA_NAMESPACE);
    TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c1").addClusteringKey("c2", Order.ASC).addClusteringKey("c3", Order.ASC).addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.BLOB).addColumn("c5", DataType.INT).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c4").build();
    DescribeTableResponse describeTableResponse = mock(DescribeTableResponse.class);
    when(client.describeTable(DescribeTableRequest.builder().tableName(FULL_TABLE_NAME).build())).thenReturn(describeTableResponse);
    TableDescription tableDescription = mock(TableDescription.class);
    when(describeTableResponse.table()).thenReturn(tableDescription);
    when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
    DescribeContinuousBackupsResponse describeContinuousBackupsResponse = mock(DescribeContinuousBackupsResponse.class);
    when(client.describeContinuousBackups(any(DescribeContinuousBackupsRequest.class))).thenReturn(describeContinuousBackupsResponse);
    ContinuousBackupsDescription continuousBackupsDescription = mock(ContinuousBackupsDescription.class);
    when(describeContinuousBackupsResponse.continuousBackupsDescription()).thenReturn(continuousBackupsDescription);
    when(continuousBackupsDescription.continuousBackupsStatus()).thenReturn(ContinuousBackupsStatus.ENABLED);
    // for the table metadata table
    describeTableResponse = mock(DescribeTableResponse.class);
    tableDescription = mock(TableDescription.class);
    when(describeTableResponse.table()).thenReturn(tableDescription);
    when(tableDescription.tableStatus()).thenReturn(TableStatus.ACTIVE);
    when(client.describeTable(DescribeTableRequest.builder().tableName(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE).build())).thenThrow(ResourceNotFoundException.class).thenReturn(describeTableResponse);
    if (tableMetadataNamespace.isPresent()) {
        when(config.getTableMetadataNamespace()).thenReturn(tableMetadataNamespace);
        admin = new DynamoAdmin(client, applicationAutoScalingClient, config);
    }
    // Act
    admin.createTable(NAMESPACE, TABLE, metadata);
    // Assert
    ArgumentCaptor<CreateTableRequest> createTableRequestCaptor = ArgumentCaptor.forClass(CreateTableRequest.class);
    verify(client, times(2)).createTable(createTableRequestCaptor.capture());
    List<CreateTableRequest> actualCreateTableRequests = createTableRequestCaptor.getAllValues();
    List<AttributeDefinition> attributeDefinitions = actualCreateTableRequests.get(0).attributeDefinitions();
    assertThat(attributeDefinitions.size()).isEqualTo(3);
    assertThat(attributeDefinitions.get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
    assertThat(attributeDefinitions.get(0).attributeType()).isEqualTo(ScalarAttributeType.B);
    assertThat(attributeDefinitions.get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
    assertThat(attributeDefinitions.get(1).attributeType()).isEqualTo(ScalarAttributeType.B);
    assertThat(attributeDefinitions.get(2).attributeName()).isEqualTo("c4");
    assertThat(attributeDefinitions.get(2).attributeType()).isEqualTo(ScalarAttributeType.B);
    assertThat(actualCreateTableRequests.get(0).keySchema().size()).isEqualTo(2);
    assertThat(actualCreateTableRequests.get(0).keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
    assertThat(actualCreateTableRequests.get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
    assertThat(actualCreateTableRequests.get(0).keySchema().get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
    assertThat(actualCreateTableRequests.get(0).keySchema().get(1).keyType()).isEqualTo(KeyType.RANGE);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().size()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).indexName()).isEqualTo(FULL_TABLE_NAME + ".global_index.c4");
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().size()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().get(0).attributeName()).isEqualTo("c4");
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).projection().projectionType()).isEqualTo(ProjectionType.ALL);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(10);
    assertThat(actualCreateTableRequests.get(0).globalSecondaryIndexes().get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(10);
    assertThat(actualCreateTableRequests.get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(10);
    assertThat(actualCreateTableRequests.get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(10);
    assertThat(actualCreateTableRequests.get(0).tableName()).isEqualTo(FULL_TABLE_NAME);
    // for the table metadata table
    attributeDefinitions = actualCreateTableRequests.get(1).attributeDefinitions();
    assertThat(attributeDefinitions.size()).isEqualTo(1);
    assertThat(attributeDefinitions.get(0).attributeName()).isEqualTo(DynamoAdmin.METADATA_ATTR_TABLE);
    assertThat(attributeDefinitions.get(0).attributeType()).isEqualTo(ScalarAttributeType.S);
    assertThat(actualCreateTableRequests.get(1).keySchema().size()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(1).keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.METADATA_ATTR_TABLE);
    assertThat(actualCreateTableRequests.get(1).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
    assertThat(actualCreateTableRequests.get(1).provisionedThroughput().writeCapacityUnits()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(1).provisionedThroughput().readCapacityUnits()).isEqualTo(1);
    assertThat(actualCreateTableRequests.get(1).tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
    ArgumentCaptor<PutItemRequest> putItemRequestCaptor = ArgumentCaptor.forClass(PutItemRequest.class);
    verify(client).putItem(putItemRequestCaptor.capture());
    PutItemRequest actualPutItemRequest = putItemRequestCaptor.getValue();
    assertThat(actualPutItemRequest.tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
    Map<String, AttributeValue> itemValues = new HashMap<>();
    itemValues.put(DynamoAdmin.METADATA_ATTR_TABLE, AttributeValue.builder().s(FULL_TABLE_NAME).build());
    Map<String, AttributeValue> columns = new HashMap<>();
    columns.put("c1", AttributeValue.builder().s("text").build());
    columns.put("c2", AttributeValue.builder().s("bigint").build());
    columns.put("c3", AttributeValue.builder().s("boolean").build());
    columns.put("c4", AttributeValue.builder().s("blob").build());
    columns.put("c5", AttributeValue.builder().s("int").build());
    columns.put("c6", AttributeValue.builder().s("double").build());
    columns.put("c7", AttributeValue.builder().s("float").build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_COLUMNS, AttributeValue.builder().m(columns).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_PARTITION_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c1").build()).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_KEY, AttributeValue.builder().l(AttributeValue.builder().s("c2").build(), AttributeValue.builder().s("c3").build()).build());
    Map<String, AttributeValue> clusteringOrders = new HashMap<>();
    clusteringOrders.put("c2", AttributeValue.builder().s("ASC").build());
    clusteringOrders.put("c3", AttributeValue.builder().s("ASC").build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_CLUSTERING_ORDERS, AttributeValue.builder().m(clusteringOrders).build());
    itemValues.put(DynamoAdmin.METADATA_ATTR_SECONDARY_INDEX, AttributeValue.builder().ss("c4").build());
    assertThat(actualPutItemRequest.item()).isEqualTo(itemValues);
    verify(applicationAutoScalingClient, times(4)).registerScalableTarget(any(RegisterScalableTargetRequest.class));
    verify(applicationAutoScalingClient, times(4)).putScalingPolicy(any(PutScalingPolicyRequest.class));
    ArgumentCaptor<UpdateContinuousBackupsRequest> updateContinuousBackupsRequestCaptor = ArgumentCaptor.forClass(UpdateContinuousBackupsRequest.class);
    verify(client, times(2)).updateContinuousBackups(updateContinuousBackupsRequestCaptor.capture());
    List<UpdateContinuousBackupsRequest> updateContinuousBackupsRequests = updateContinuousBackupsRequestCaptor.getAllValues();
    assertThat(updateContinuousBackupsRequests.size()).isEqualTo(2);
    assertThat(updateContinuousBackupsRequests.get(0).tableName()).isEqualTo(FULL_TABLE_NAME);
    assertThat(updateContinuousBackupsRequests.get(1).tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) ContinuousBackupsDescription(software.amazon.awssdk.services.dynamodb.model.ContinuousBackupsDescription) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) RegisterScalableTargetRequest(software.amazon.awssdk.services.applicationautoscaling.model.RegisterScalableTargetRequest) HashMap(java.util.HashMap) UpdateContinuousBackupsRequest(software.amazon.awssdk.services.dynamodb.model.UpdateContinuousBackupsRequest) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) DescribeTableResponse(software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) DescribeContinuousBackupsRequest(software.amazon.awssdk.services.dynamodb.model.DescribeContinuousBackupsRequest) DescribeContinuousBackupsResponse(software.amazon.awssdk.services.dynamodb.model.DescribeContinuousBackupsResponse) PutScalingPolicyRequest(software.amazon.awssdk.services.applicationautoscaling.model.PutScalingPolicyRequest) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)

Aggregations

TableMetadata (com.scalar.db.api.TableMetadata)101 Test (org.junit.jupiter.api.Test)39 Get (com.scalar.db.api.Get)11 PreparedStatement (java.sql.PreparedStatement)10 ExecutionException (com.scalar.db.exception.storage.ExecutionException)9 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Key (com.scalar.db.io.Key)6 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)6 Mutation (com.scalar.db.api.Mutation)5 Result (com.scalar.db.api.Result)5 GetTableMetadataResponse (com.scalar.db.rpc.GetTableMetadataResponse)5 ResultSet (java.sql.ResultSet)5 Nonnull (javax.annotation.Nonnull)5 HashMap (java.util.HashMap)4 Options (com.datastax.driver.core.schemabuilder.Create.Options)3 KeyspaceOptions (com.datastax.driver.core.schemabuilder.KeyspaceOptions)3 TableOptions (com.datastax.driver.core.schemabuilder.TableOptions)3 ScanAll (com.scalar.db.api.ScanAll)3 NoMutationException (com.scalar.db.exception.storage.NoMutationException)3