use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class DistributedTransactionAdminService method getTableMetadata.
@Override
public void getTableMetadata(GetTableMetadataRequest request, StreamObserver<GetTableMetadataResponse> responseObserver) {
execute(() -> {
TableMetadata tableMetadata = admin.getTableMetadata(request.getNamespace(), request.getTable());
GetTableMetadataResponse.Builder builder = GetTableMetadataResponse.newBuilder();
if (tableMetadata != null) {
builder.setTableMetadata(ProtoUtils.toTableMetadata(tableMetadata));
}
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
}, responseObserver, "get_table_metadata");
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class SchemaOperatorTest method deleteTables_WithTableList_ShouldCallProperMethods.
@Test
public void deleteTables_WithTableList_ShouldCallProperMethods() throws Exception {
// Arrange
List<TableSchema> tableSchemaList = Arrays.asList(tableSchema, tableSchema, tableSchema);
when(tableSchema.getNamespace()).thenReturn("ns");
when(tableSchema.getOptions()).thenReturn(options);
when(tableSchema.isTransactionalTable()).thenReturn(true);
when(tableSchema.getTable()).thenReturn("tb");
TableMetadata tableMetadata = mock(TableMetadata.class);
when(tableSchema.getTableMetadata()).thenReturn(tableMetadata);
when(admin.tableExists("ns", "tb")).thenReturn(true);
// Act
operator.deleteTables(tableSchemaList);
// Assert
verify(admin, times(3)).tableExists("ns", "tb");
verify(admin, times(3)).dropTable("ns", "tb");
verify(admin).dropNamespace("ns", true);
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class SchemaParserTest method parse_ProperSerializedSchemaJsonAndOptionsGiven_ShouldParseCorrectly.
@Test
public void parse_ProperSerializedSchemaJsonAndOptionsGiven_ShouldParseCorrectly() throws SchemaLoaderException {
// Arrange
String serializedSchemaJson = "{" + " \"sample_db.sample_table\": {" + " \"transaction\": false," + " \"partition-key\": [" + " \"c1\"," + " \"c2\"" + " ]," + " \"clustering-key\": [" + " \"c3 ASC\"," + " \"c4 DESC\"" + " ]," + " \"columns\": {" + " \"c1\": \"INT\"," + " \"c2\": \"TEXT\"," + " \"c3\": \"BLOB\"," + " \"c4\": \"FLOAT\"," + " \"c5\": \"BOOLEAN\"," + " \"c6\": \"DOUBLE\"," + " \"c7\": \"BIGINT\"" + " }," + " \"secondary-index\": [" + " \"c5\"," + " \"c6\"" + " ]," + " \"ru\": 5000," + " \"compaction-strategy\": \"LCS\"" + " }," + " \"sample_db.sample_table1\": {" + " \"transaction\": true," + " \"partition-key\": [" + " \"c1\"" + " ]," + " \"clustering-key\": [" + " \"c4\"" + " ]," + " \"columns\": {" + " \"c1\": \"INT\"," + " \"c2\": \"TEXT\"," + " \"c3\": \"INT\"," + " \"c4\": \"INT\"," + " \"c5\": \"BOOLEAN\"" + " }" + " }," + " \"sample_db2.sample_table2\": {" + " \"transaction\": false," + " \"partition-key\": [" + " \"c1\"" + " ]," + " \"clustering-key\": [" + " \"c4\"," + " \"c3\"" + " ]," + " \"columns\": {" + " \"c1\": \"INT\"," + " \"c2\": \"TEXT\"," + " \"c3\": \"INT\"," + " \"c4\": \"INT\"," + " \"c5\": \"BOOLEAN\"" + " }" + " }" + "}";
Map<String, String> option = Collections.emptyMap();
SchemaParser parser = new SchemaParser(serializedSchemaJson, option);
// Act
List<TableSchema> actual = parser.parse();
// Assert
assertThat(actual.size()).isEqualTo(3);
assertThat(actual.get(0).getNamespace()).isEqualTo("sample_db");
assertThat(actual.get(0).getTable()).isEqualTo("sample_table");
assertThat(actual.get(0).isTransactionalTable()).isFalse();
assertThat(actual.get(0).getOptions()).isEqualTo(ImmutableMap.of("ru", "5000", "compaction-strategy", "LCS"));
TableMetadata tableMetadata = actual.get(0).getTableMetadata();
assertThat(tableMetadata.getPartitionKeyNames()).isEqualTo(new LinkedHashSet<>(Arrays.asList("c1", "c2")));
assertThat(tableMetadata.getClusteringKeyNames()).isEqualTo(new LinkedHashSet<>(Arrays.asList("c3", "c4")));
assertThat(tableMetadata.getClusteringOrder("c3")).isEqualTo(Order.ASC);
assertThat(tableMetadata.getClusteringOrder("c4")).isEqualTo(Order.DESC);
assertThat(tableMetadata.getColumnNames()).isEqualTo(new LinkedHashSet<>(Arrays.asList("c1", "c2", "c3", "c4", "c5", "c6", "c7")));
assertThat(tableMetadata.getColumnDataType("c1")).isEqualTo(DataType.INT);
assertThat(tableMetadata.getColumnDataType("c2")).isEqualTo(DataType.TEXT);
assertThat(tableMetadata.getColumnDataType("c3")).isEqualTo(DataType.BLOB);
assertThat(tableMetadata.getColumnDataType("c4")).isEqualTo(DataType.FLOAT);
assertThat(tableMetadata.getColumnDataType("c5")).isEqualTo(DataType.BOOLEAN);
assertThat(tableMetadata.getColumnDataType("c6")).isEqualTo(DataType.DOUBLE);
assertThat(tableMetadata.getColumnDataType("c7")).isEqualTo(DataType.BIGINT);
assertThat(tableMetadata.getSecondaryIndexNames()).isEqualTo(new HashSet<>(Arrays.asList("c5", "c6")));
assertThat(actual.get(1).getNamespace()).isEqualTo("sample_db");
assertThat(actual.get(1).getTable()).isEqualTo("sample_table1");
assertThat(actual.get(1).isTransactionalTable()).isTrue();
assertThat(actual.get(1).getOptions()).isEmpty();
tableMetadata = actual.get(1).getTableMetadata();
assertThat(tableMetadata.getPartitionKeyNames()).isEqualTo(new LinkedHashSet<>(Collections.singletonList("c1")));
assertThat(tableMetadata.getClusteringKeyNames()).isEqualTo(new LinkedHashSet<>(Collections.singletonList("c4")));
assertThat(tableMetadata.getClusteringOrder("c4")).isEqualTo(Order.ASC);
assertThat(tableMetadata.getColumnNames()).isEqualTo(new LinkedHashSet<>(Arrays.asList("c1", "c2", "c3", "c4", "c5")));
assertThat(tableMetadata.getColumnDataType("c1")).isEqualTo(DataType.INT);
assertThat(tableMetadata.getColumnDataType("c2")).isEqualTo(DataType.TEXT);
assertThat(tableMetadata.getColumnDataType("c3")).isEqualTo(DataType.INT);
assertThat(tableMetadata.getColumnDataType("c4")).isEqualTo(DataType.INT);
assertThat(tableMetadata.getColumnDataType("c5")).isEqualTo(DataType.BOOLEAN);
assertThat(tableMetadata.getSecondaryIndexNames()).isEmpty();
assertThat(actual.get(2).getNamespace()).isEqualTo("sample_db2");
assertThat(actual.get(2).getTable()).isEqualTo("sample_table2");
assertThat(actual.get(2).isTransactionalTable()).isFalse();
assertThat(actual.get(2).getOptions()).isEmpty();
tableMetadata = actual.get(2).getTableMetadata();
assertThat(tableMetadata.getPartitionKeyNames()).isEqualTo(new LinkedHashSet<>(Collections.singletonList("c1")));
assertThat(tableMetadata.getClusteringKeyNames()).isEqualTo(new LinkedHashSet<>(Arrays.asList("c4", "c3")));
assertThat(tableMetadata.getClusteringOrder("c4")).isEqualTo(Order.ASC);
assertThat(tableMetadata.getClusteringOrder("c3")).isEqualTo(Order.ASC);
assertThat(tableMetadata.getColumnNames()).isEqualTo(new LinkedHashSet<>(Arrays.asList("c1", "c2", "c3", "c4", "c5")));
assertThat(tableMetadata.getColumnDataType("c1")).isEqualTo(DataType.INT);
assertThat(tableMetadata.getColumnDataType("c2")).isEqualTo(DataType.TEXT);
assertThat(tableMetadata.getColumnDataType("c3")).isEqualTo(DataType.INT);
assertThat(tableMetadata.getColumnDataType("c4")).isEqualTo(DataType.INT);
assertThat(tableMetadata.getColumnDataType("c5")).isEqualTo(DataType.BOOLEAN);
assertThat(tableMetadata.getSecondaryIndexNames()).isEmpty();
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class TableSchemaTest method buildTableMetadata_ProperFormatTableDefinitionGiven_ShouldReturnProperTableMetadata.
@Test
public void buildTableMetadata_ProperFormatTableDefinitionGiven_ShouldReturnProperTableMetadata() throws SchemaLoaderException {
String tableDefinitionJson = "{\"transaction\": false," + "\"partition-key\": [\"c1\"]," + "\"clustering-key\": [\"c3\",\"c4 ASC\",\"c6 DESC\"]," + "\"columns\": {" + " \"c1\": \"INT\"," + " \"c2\": \"TEXT\"," + " \"c3\": \"BLOB\"," + " \"c4\": \"INT\"," + " \"c5\": \"BOOLEAN\"," + " \"c6\": \"INT\"" + "}," + "\"ru\": 5000," + "\"compaction-strategy\": \"LCS\"," + "\"secondary-index\": [\"c2\",\"c4\"]}";
JsonObject tableDefinition = JsonParser.parseString(tableDefinitionJson).getAsJsonObject();
TableMetadata.Builder tableBuilder = TableMetadata.newBuilder();
tableBuilder.addPartitionKey("c1");
tableBuilder.addClusteringKey("c3");
tableBuilder.addClusteringKey("c4");
tableBuilder.addClusteringKey("c6", Order.DESC);
tableBuilder.addSecondaryIndex("c2");
tableBuilder.addSecondaryIndex("c4");
tableBuilder.addColumn("c1", DataType.INT);
tableBuilder.addColumn("c2", DataType.TEXT);
tableBuilder.addColumn("c3", DataType.BLOB);
tableBuilder.addColumn("c4", DataType.INT);
tableBuilder.addColumn("c5", DataType.BOOLEAN);
tableBuilder.addColumn("c6", DataType.INT);
TableMetadata expectedTableMetadata = tableBuilder.build();
// Act
TableMetadata tableMetadata = new TableSchema("ns.tb", tableDefinition, Collections.emptyMap()).getTableMetadata();
// Assert
Assertions.assertThat(tableMetadata).isEqualTo(expectedTableMetadata);
}
use of com.scalar.db.api.TableMetadata in project scalardb by scalar-labs.
the class CosmosAdminTest method createTable_WithoutClusteringKeys_ShouldCreateContainerWithCompositeIndex.
private void createTable_WithoutClusteringKeys_ShouldCreateContainerWithCompositeIndex(Optional<String> tableMetadataDatabase) throws ExecutionException {
// Arrange
String namespace = "ns";
String table = "sample_table";
TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c3").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(2);
assertThat(indexingPolicy.getIncludedPaths().get(0).getPath()).isEqualTo("/concatenatedPartitionKey/?");
assertThat(indexingPolicy.getIncludedPaths().get(1).getPath()).isEqualTo("/values/c4/?");
assertThat(indexingPolicy.getExcludedPaths().size()).isEqualTo(1);
assertThat(indexingPolicy.getExcludedPaths().get(0).getPath()).isEqualTo("/*");
assertThat(indexingPolicy.getCompositeIndexes()).isEmpty();
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.setClusteringOrders(Collections.emptyMap());
cosmosTableMetadata.setClusteringKeyNames(Collections.emptyList());
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);
}
Aggregations