use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class DynamoAdminTest method createTable_tableMetadataWithPartitionKeyWithNonLastBlobValueGiven_ShouldThrowIllegalArgumentException.
@Test
public void createTable_tableMetadataWithPartitionKeyWithNonLastBlobValueGiven_ShouldThrowIllegalArgumentException() {
// Arrange
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c1").addPartitionKey("c2").addColumn("c1", DataType.BLOB).addColumn("c2", DataType.INT).addColumn("c3", DataType.INT).build();
// Act Assert
assertThatThrownBy(() -> admin.createTable(NAMESPACE, TABLE, metadata)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class DynamoAdminTest method getTableMetadata_ShouldReturnCorrectTableMetadata.
private void getTableMetadata_ShouldReturnCorrectTableMetadata(Optional<String> tableMetadataNamespace) throws ExecutionException {
// Arrange
String metadataNamespaceName = tableMetadataNamespace.orElse(DynamoAdmin.METADATA_NAMESPACE);
Map<String, AttributeValue> expectedKey = new HashMap<>();
expectedKey.put(DynamoAdmin.METADATA_ATTR_TABLE, AttributeValue.builder().s(FULL_TABLE_NAME).build());
GetItemResponse response = mock(GetItemResponse.class);
when(client.getItem(any(GetItemRequest.class))).thenReturn(response);
when(response.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("blob").build()).put("c5", AttributeValue.builder().s("int").build()).put("c6", AttributeValue.builder().s("double").build()).put("c7", AttributeValue.builder().s("float").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()).put(DynamoAdmin.METADATA_ATTR_SECONDARY_INDEX, AttributeValue.builder().ss("c4").build()).build());
if (tableMetadataNamespace.isPresent()) {
when(config.getTableMetadataNamespace()).thenReturn(tableMetadataNamespace);
admin = new DynamoAdmin(client, applicationAutoScalingClient, config);
}
// Act
TableMetadata actual = admin.getTableMetadata(NAMESPACE, TABLE);
// Assert
assertThat(actual).isEqualTo(TableMetadata.newBuilder().addPartitionKey("c1").addClusteringKey("c2", Order.DESC).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());
ArgumentCaptor<GetItemRequest> captor = ArgumentCaptor.forClass(GetItemRequest.class);
verify(client).getItem(captor.capture());
GetItemRequest actualRequest = captor.getValue();
assertThat(actualRequest.tableName()).isEqualTo(metadataNamespaceName + "." + DynamoAdmin.METADATA_TABLE);
assertThat(actualRequest.key()).isEqualTo(expectedKey);
assertThat(actualRequest.consistentRead()).isTrue();
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class DynamoAdminTest method createTable_WhenMetadataTableExists_ShouldCreateOnlyTable.
private void createTable_WhenMetadataTableExists_ShouldCreateOnlyTable(Optional<String> tableMetadataNamespace) throws ExecutionException {
// Arrange
String metadataNamespaceName = tableMetadataNamespace.orElse(DynamoAdmin.METADATA_NAMESPACE);
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c1").addClusteringKey("c2", Order.DESC).addClusteringKey("c3", Order.ASC).addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.INT).addColumn("c5", DataType.BLOB).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c4").build();
DescribeTableResponse describeTableResponse = mock(DescribeTableResponse.class);
when(client.describeTable(any(DescribeTableRequest.class))).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);
if (tableMetadataNamespace.isPresent()) {
when(config.getTableMetadataNamespace()).thenReturn(tableMetadataNamespace);
admin = new DynamoAdmin(client, applicationAutoScalingClient, config);
}
Map<String, String> options = new HashMap<>();
options.put(DynamoAdmin.REQUEST_UNIT, "100");
options.put(DynamoAdmin.NO_SCALING, "true");
options.put(DynamoAdmin.NO_BACKUP, "true");
// Act
admin.createTable(NAMESPACE, TABLE, metadata, options);
// Assert
ArgumentCaptor<CreateTableRequest> createTableRequestCaptor = ArgumentCaptor.forClass(CreateTableRequest.class);
verify(client).createTable(createTableRequestCaptor.capture());
CreateTableRequest actualCreateTableRequest = createTableRequestCaptor.getValue();
List<AttributeDefinition> attributeDefinitions = actualCreateTableRequest.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.N);
assertThat(actualCreateTableRequest.keySchema().size()).isEqualTo(2);
assertThat(actualCreateTableRequest.keySchema().get(0).attributeName()).isEqualTo(DynamoAdmin.PARTITION_KEY);
assertThat(actualCreateTableRequest.keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
assertThat(actualCreateTableRequest.keySchema().get(1).attributeName()).isEqualTo(DynamoAdmin.CLUSTERING_KEY);
assertThat(actualCreateTableRequest.keySchema().get(1).keyType()).isEqualTo(KeyType.RANGE);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().size()).isEqualTo(1);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).indexName()).isEqualTo(FULL_TABLE_NAME + ".global_index.c4");
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().size()).isEqualTo(1);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().get(0).attributeName()).isEqualTo("c4");
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).keySchema().get(0).keyType()).isEqualTo(KeyType.HASH);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).projection().projectionType()).isEqualTo(ProjectionType.ALL);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).provisionedThroughput().readCapacityUnits()).isEqualTo(100);
assertThat(actualCreateTableRequest.globalSecondaryIndexes().get(0).provisionedThroughput().writeCapacityUnits()).isEqualTo(100);
assertThat(actualCreateTableRequest.provisionedThroughput().writeCapacityUnits()).isEqualTo(100);
assertThat(actualCreateTableRequest.provisionedThroughput().readCapacityUnits()).isEqualTo(100);
assertThat(actualCreateTableRequest.tableName()).isEqualTo(FULL_TABLE_NAME);
// for the table 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("int").build());
columns.put("c5", AttributeValue.builder().s("blob").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("DESC").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, never()).registerScalableTarget(any(RegisterScalableTargetRequest.class));
verify(applicationAutoScalingClient, never()).putScalingPolicy(any(PutScalingPolicyRequest.class));
verify(client, never()).updateContinuousBackups(any(UpdateContinuousBackupsRequest.class));
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class DynamoAdminTest method createTable_tableMetadataWithClusteringKeyWithBlobValueGiven_ShouldThrowIllegalArgumentException.
@Test
public void createTable_tableMetadataWithClusteringKeyWithBlobValueGiven_ShouldThrowIllegalArgumentException() {
// Arrange
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c1").addClusteringKey("c2").addClusteringKey("c3").addColumn("c1", DataType.INT).addColumn("c2", DataType.INT).addColumn("c3", DataType.BLOB).addColumn("c4", DataType.TEXT).build();
// Act Assert
assertThatThrownBy(() -> admin.createTable(NAMESPACE, TABLE, metadata)).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class ConsensusCommitAdminTest method createTable_tableMetadataGiven_shouldCreateTransactionalTableProperly.
@Test
public void createTable_tableMetadataGiven_shouldCreateTransactionalTableProperly() throws ExecutionException {
// Arrange
final String ACCOUNT_ID = "account_id";
final String ACCOUNT_TYPE = "account_type";
final String BALANCE = "balance";
TableMetadata tableMetadata = TableMetadata.newBuilder().addColumn(ACCOUNT_ID, DataType.INT).addColumn(ACCOUNT_TYPE, DataType.INT).addColumn(BALANCE, DataType.INT).addPartitionKey(ACCOUNT_ID).addClusteringKey(ACCOUNT_TYPE).build();
TableMetadata expected = TableMetadata.newBuilder().addColumn(ACCOUNT_ID, DataType.INT).addColumn(ACCOUNT_TYPE, DataType.INT).addColumn(BALANCE, DataType.INT).addColumn(Attribute.ID, DataType.TEXT).addColumn(Attribute.STATE, DataType.INT).addColumn(Attribute.VERSION, DataType.INT).addColumn(Attribute.PREPARED_AT, DataType.BIGINT).addColumn(Attribute.COMMITTED_AT, DataType.BIGINT).addColumn(Attribute.BEFORE_PREFIX + BALANCE, DataType.INT).addColumn(Attribute.BEFORE_ID, DataType.TEXT).addColumn(Attribute.BEFORE_STATE, DataType.INT).addColumn(Attribute.BEFORE_VERSION, DataType.INT).addColumn(Attribute.BEFORE_PREPARED_AT, DataType.BIGINT).addColumn(Attribute.BEFORE_COMMITTED_AT, DataType.BIGINT).addPartitionKey(ACCOUNT_ID).addClusteringKey(ACCOUNT_TYPE).build();
// Act
admin.createTable(NAMESPACE, TABLE, tableMetadata);
// Assert
verify(distributedStorageAdmin).createTable(NAMESPACE, TABLE, expected, Collections.emptyMap());
}
Aggregations