use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class CosmosAdminTest method createTable_ShouldCreateContainer.
private void createTable_ShouldCreateContainer(Optional<String> tableMetadataDatabase) throws ExecutionException {
// Arrange
String namespace = "ns";
String table = "sample_table";
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c3").addClusteringKey("c1", Order.DESC).addClusteringKey("c2", 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();
when(client.getDatabase(namespace)).thenReturn(database);
when(database.getContainer(table)).thenReturn(container);
CosmosScripts cosmosScripts = Mockito.mock(CosmosScripts.class);
when(container.getScripts()).thenReturn(cosmosScripts);
// for metadata table
String metadataDatabaseName = tableMetadataDatabase.orElse(CosmosAdmin.METADATA_DATABASE);
CosmosDatabase metadataDatabase = mock(CosmosDatabase.class);
CosmosContainer metadataContainer = mock(CosmosContainer.class);
when(client.getDatabase(metadataDatabaseName)).thenReturn(metadataDatabase);
when(metadataDatabase.getContainer(CosmosAdmin.METADATA_CONTAINER)).thenReturn(metadataContainer);
if (tableMetadataDatabase.isPresent()) {
when(config.getTableMetadataDatabase()).thenReturn(tableMetadataDatabase);
admin = new CosmosAdmin(client, config);
}
// Act
admin.createTable(namespace, table, metadata, Collections.emptyMap());
// Assert
ArgumentCaptor<CosmosContainerProperties> containerPropertiesCaptor = ArgumentCaptor.forClass(CosmosContainerProperties.class);
verify(database).createContainer(containerPropertiesCaptor.capture());
assertThat(containerPropertiesCaptor.getValue().getId()).isEqualTo(table);
// check index related info
IndexingPolicy indexingPolicy = containerPropertiesCaptor.getValue().getIndexingPolicy();
assertThat(indexingPolicy.getIncludedPaths().size()).isEqualTo(1);
assertThat(indexingPolicy.getIncludedPaths().get(0).getPath()).isEqualTo("/values/c4/?");
assertThat(indexingPolicy.getExcludedPaths().size()).isEqualTo(1);
assertThat(indexingPolicy.getExcludedPaths().get(0).getPath()).isEqualTo("/*");
assertThat(indexingPolicy.getCompositeIndexes().size()).isEqualTo(1);
assertThat(indexingPolicy.getCompositeIndexes().get(0).size()).isEqualTo(3);
assertThat(indexingPolicy.getCompositeIndexes().get(0).get(0).getPath()).isEqualTo("/concatenatedPartitionKey");
assertThat(indexingPolicy.getCompositeIndexes().get(0).get(0).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING);
assertThat(indexingPolicy.getCompositeIndexes().get(0).get(1).getPath()).isEqualTo("/clusteringKey/c1");
assertThat(indexingPolicy.getCompositeIndexes().get(0).get(1).getOrder()).isEqualTo(CompositePathSortOrder.DESCENDING);
assertThat(indexingPolicy.getCompositeIndexes().get(0).get(2).getPath()).isEqualTo("/clusteringKey/c2");
assertThat(indexingPolicy.getCompositeIndexes().get(0).get(2).getOrder()).isEqualTo(CompositePathSortOrder.ASCENDING);
verify(cosmosScripts).createStoredProcedure(any(CosmosStoredProcedureProperties.class));
// for metadata table
verify(client).createDatabaseIfNotExists(eq(metadataDatabaseName), refEq(ThroughputProperties.createManualThroughput(Integer.parseInt("400"))));
verify(metadataDatabase).createContainerIfNotExists(containerPropertiesCaptor.capture());
assertThat(containerPropertiesCaptor.getValue().getId()).isEqualTo(CosmosAdmin.METADATA_CONTAINER);
assertThat(containerPropertiesCaptor.getValue().getPartitionKeyDefinition().getPaths()).containsExactly("/id");
CosmosTableMetadata cosmosTableMetadata = new CosmosTableMetadata();
cosmosTableMetadata.setId(getFullTableName(namespace, table));
cosmosTableMetadata.setPartitionKeyNames(Collections.singletonList("c3"));
cosmosTableMetadata.setClusteringKeyNames(Arrays.asList("c1", "c2"));
cosmosTableMetadata.setClusteringOrders(ImmutableMap.of("c1", "DESC", "c2", "ASC"));
cosmosTableMetadata.setColumns(new ImmutableMap.Builder<String, String>().put("c1", "text").put("c2", "bigint").put("c3", "boolean").put("c4", "blob").put("c5", "int").put("c6", "double").put("c7", "float").build());
cosmosTableMetadata.setSecondaryIndexNames(ImmutableSet.of("c4"));
verify(metadataContainer).upsertItem(cosmosTableMetadata);
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class CosmosAdminTest method getTableMetadata_ShouldReturnCorrectTableMetadata.
private void getTableMetadata_ShouldReturnCorrectTableMetadata(Optional<String> tableMetadataDatabase) throws ExecutionException {
// Arrange
String namespace = "ns";
String table = "table";
String fullName = getFullTableName(namespace, table);
String metadataDatabaseName = tableMetadataDatabase.orElse(CosmosAdmin.METADATA_DATABASE);
@SuppressWarnings("unchecked") CosmosItemResponse<CosmosTableMetadata> response = mock(CosmosItemResponse.class);
when(client.getDatabase(metadataDatabaseName)).thenReturn(database);
when(database.getContainer(CosmosAdmin.METADATA_CONTAINER)).thenReturn(container);
when(container.readItem(anyString(), any(PartitionKey.class), ArgumentMatchers.<Class<CosmosTableMetadata>>any())).thenReturn(response);
CosmosTableMetadata cosmosTableMetadata = new CosmosTableMetadata();
cosmosTableMetadata.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
cosmosTableMetadata.setPartitionKeyNames(Collections.singletonList("c1"));
cosmosTableMetadata.setClusteringKeyNames(Collections.emptyList());
cosmosTableMetadata.setSecondaryIndexNames(Collections.emptySet());
when(response.getItem()).thenReturn(cosmosTableMetadata);
if (tableMetadataDatabase.isPresent()) {
when(config.getTableMetadataDatabase()).thenReturn(tableMetadataDatabase);
admin = new CosmosAdmin(client, config);
}
// Act
TableMetadata actual = admin.getTableMetadata(namespace, table);
// Assert
assertThat(actual).isEqualTo(TableMetadata.newBuilder().addColumn("c1", DataType.INT).addColumn("c2", DataType.TEXT).addColumn("c3", DataType.BIGINT).addPartitionKey("c1").build());
verify(client).getDatabase(metadataDatabaseName);
verify(database).getContainer(CosmosAdmin.METADATA_CONTAINER);
verify(container).readItem(fullName, new PartitionKey(fullName), CosmosTableMetadata.class);
verify(response).getItem();
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class QueryBuilderTest method selectQueryWithTableWithoutClusteringKeyTest.
@ParameterizedTest
@EnumSource(RdbEngine.class)
public void selectQueryWithTableWithoutClusteringKeyTest(RdbEngine rdbEngine) throws SQLException {
QueryBuilder queryBuilder = new QueryBuilder(rdbEngine);
TableMetadata tableMetadataWithoutClusteringKey = TableMetadata.newBuilder().addColumn("p1", DataType.TEXT).addColumn("p2", DataType.INT).addColumn("v1", DataType.TEXT).addColumn("v2", DataType.TEXT).addColumn("v3", DataType.TEXT).addPartitionKey("p1").addPartitionKey("p2").build();
SelectQuery query;
PreparedStatement preparedStatement;
preparedStatement = mock(PreparedStatement.class);
query = queryBuilder.select(Arrays.asList("c1", "c2")).from(NAMESPACE, TABLE, tableMetadataWithoutClusteringKey).where(Key.of("p1", "p1Value", "p2", "p2Value"), Optional.empty()).build();
assertThat(query.sql()).isEqualTo(encloseSql("SELECT c1,c2 FROM n1.t1 WHERE p1=? AND p2=?", rdbEngine));
query.bind(preparedStatement);
verify(preparedStatement).setString(1, "p1Value");
verify(preparedStatement).setString(2, "p2Value");
preparedStatement = mock(PreparedStatement.class);
query = queryBuilder.select(Arrays.asList("c1", "c2")).from(NAMESPACE, TABLE, tableMetadataWithoutClusteringKey).where(Key.of("p1", "p1Value", "p2", "p2Value"), Optional.empty(), false, Optional.empty(), false).build();
assertThat(query.sql()).isEqualTo(encloseSql("SELECT c1,c2 FROM n1.t1 WHERE p1=? AND p2=?", rdbEngine));
query.bind(preparedStatement);
verify(preparedStatement).setString(1, "p1Value");
verify(preparedStatement).setString(2, "p2Value");
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class GrpcAdminTest 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());
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class GrpcAdminTest method createTable_CalledWithProperArguments_StubShouldBeCalledProperly.
@Test
public void createTable_CalledWithProperArguments_StubShouldBeCalledProperly() throws ExecutionException {
// Arrange
String namespace = "namespace";
String table = "table";
TableMetadata metadata = TableMetadata.newBuilder().addColumn("col1", DataType.INT).addColumn("col2", DataType.INT).addPartitionKey("col1").build();
Map<String, String> options = Collections.emptyMap();
// Act
admin.createTable(namespace, table, metadata, true, options);
// Assert
verify(stub).createTable(CreateTableRequest.newBuilder().setNamespace(namespace).setTable(table).setTableMetadata(ProtoUtils.toTableMetadata(metadata)).setIfNotExists(true).putAllOptions(options).build());
}
Aggregations