use of com.azure.cosmos.models.CosmosContainerProperties 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.azure.cosmos.models.CosmosContainerProperties in project scalardb by scalar-labs.
the class CosmosAdminTest method createIndex_ShouldCreateIndexProperly.
@Test
public void createIndex_ShouldCreateIndexProperly() throws ExecutionException {
// Arrange
String namespace = "ns";
String table = "sample_table";
when(client.getDatabase(namespace)).thenReturn(database);
CosmosContainerResponse response = mock(CosmosContainerResponse.class);
when(database.createContainerIfNotExists(table, "/concatenatedPartitionKey")).thenReturn(response);
CosmosContainerProperties properties = mock(CosmosContainerProperties.class);
when(response.getProperties()).thenReturn(properties);
when(database.getContainer(table)).thenReturn(container);
// for metadata table
CosmosDatabase metadataDatabase = mock(CosmosDatabase.class);
CosmosContainer metadataContainer = mock(CosmosContainer.class);
when(client.getDatabase(CosmosAdmin.METADATA_DATABASE)).thenReturn(metadataDatabase);
when(metadataDatabase.getContainer(CosmosAdmin.METADATA_CONTAINER)).thenReturn(metadataContainer);
@SuppressWarnings("unchecked") CosmosItemResponse<CosmosTableMetadata> itemResponse = mock(CosmosItemResponse.class);
when(metadataContainer.readItem(anyString(), any(PartitionKey.class), ArgumentMatchers.<Class<CosmosTableMetadata>>any())).thenReturn(itemResponse);
CosmosTableMetadata cosmosTableMetadata = new CosmosTableMetadata();
cosmosTableMetadata.setId(getFullTableName(namespace, table));
cosmosTableMetadata.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
cosmosTableMetadata.setPartitionKeyNames(Collections.singletonList("c1"));
cosmosTableMetadata.setClusteringKeyNames(Collections.emptyList());
cosmosTableMetadata.setClusteringOrders(Collections.emptyMap());
cosmosTableMetadata.setSecondaryIndexNames(ImmutableSet.of("c2"));
when(itemResponse.getItem()).thenReturn(cosmosTableMetadata);
// Act
admin.createIndex(namespace, table, "c3");
// Assert
verify(database).createContainerIfNotExists(table, "/concatenatedPartitionKey");
ArgumentCaptor<IndexingPolicy> indexingPolicyCaptor = ArgumentCaptor.forClass(IndexingPolicy.class);
verify(properties).setIndexingPolicy(indexingPolicyCaptor.capture());
IndexingPolicy indexingPolicy = indexingPolicyCaptor.getValue();
assertThat(indexingPolicy.getIncludedPaths().size()).isEqualTo(3);
assertThat(indexingPolicy.getIncludedPaths().get(0).getPath()).isEqualTo("/concatenatedPartitionKey/?");
assertThat(indexingPolicy.getIncludedPaths().get(1).getPath()).isEqualTo("/values/c3/?");
assertThat(indexingPolicy.getIncludedPaths().get(2).getPath()).isEqualTo("/values/c2/?");
assertThat(indexingPolicy.getExcludedPaths().size()).isEqualTo(1);
assertThat(indexingPolicy.getExcludedPaths().get(0).getPath()).isEqualTo("/*");
assertThat(indexingPolicy.getCompositeIndexes()).isEmpty();
verify(container).replace(properties);
// for metadata table
CosmosTableMetadata expected = new CosmosTableMetadata();
expected.setId(getFullTableName(namespace, table));
expected.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
expected.setPartitionKeyNames(Collections.singletonList("c1"));
expected.setClusteringKeyNames(Collections.emptyList());
expected.setClusteringOrders(Collections.emptyMap());
expected.setSecondaryIndexNames(ImmutableSet.of("c2", "c3"));
verify(metadataContainer).upsertItem(expected);
}
use of com.azure.cosmos.models.CosmosContainerProperties in project scalardb by scalar-labs.
the class CosmosAdminTest method dropIndex_ShouldDropIndexProperly.
@Test
public void dropIndex_ShouldDropIndexProperly() throws ExecutionException {
// Arrange
String namespace = "ns";
String table = "sample_table";
when(client.getDatabase(namespace)).thenReturn(database);
CosmosContainerResponse response = mock(CosmosContainerResponse.class);
when(database.createContainerIfNotExists(table, "/concatenatedPartitionKey")).thenReturn(response);
CosmosContainerProperties properties = mock(CosmosContainerProperties.class);
when(response.getProperties()).thenReturn(properties);
when(database.getContainer(table)).thenReturn(container);
// for metadata table
CosmosDatabase metadataDatabase = mock(CosmosDatabase.class);
CosmosContainer metadataContainer = mock(CosmosContainer.class);
when(client.getDatabase(CosmosAdmin.METADATA_DATABASE)).thenReturn(metadataDatabase);
when(metadataDatabase.getContainer(CosmosAdmin.METADATA_CONTAINER)).thenReturn(metadataContainer);
@SuppressWarnings("unchecked") CosmosItemResponse<CosmosTableMetadata> itemResponse = mock(CosmosItemResponse.class);
when(metadataContainer.readItem(anyString(), any(PartitionKey.class), ArgumentMatchers.<Class<CosmosTableMetadata>>any())).thenReturn(itemResponse);
CosmosTableMetadata cosmosTableMetadata = new CosmosTableMetadata();
cosmosTableMetadata.setId(getFullTableName(namespace, table));
cosmosTableMetadata.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
cosmosTableMetadata.setPartitionKeyNames(Collections.singletonList("c1"));
cosmosTableMetadata.setClusteringKeyNames(Collections.emptyList());
cosmosTableMetadata.setClusteringOrders(Collections.emptyMap());
cosmosTableMetadata.setSecondaryIndexNames(ImmutableSet.of("c2", "c3"));
when(itemResponse.getItem()).thenReturn(cosmosTableMetadata);
// Act
admin.dropIndex(namespace, table, "c2");
// Assert
verify(database).createContainerIfNotExists(table, "/concatenatedPartitionKey");
ArgumentCaptor<IndexingPolicy> indexingPolicyCaptor = ArgumentCaptor.forClass(IndexingPolicy.class);
verify(properties).setIndexingPolicy(indexingPolicyCaptor.capture());
IndexingPolicy indexingPolicy = indexingPolicyCaptor.getValue();
assertThat(indexingPolicy.getIncludedPaths().size()).isEqualTo(2);
assertThat(indexingPolicy.getIncludedPaths().get(0).getPath()).isEqualTo("/concatenatedPartitionKey/?");
assertThat(indexingPolicy.getIncludedPaths().get(1).getPath()).isEqualTo("/values/c3/?");
assertThat(indexingPolicy.getExcludedPaths().size()).isEqualTo(1);
assertThat(indexingPolicy.getExcludedPaths().get(0).getPath()).isEqualTo("/*");
assertThat(indexingPolicy.getCompositeIndexes()).isEmpty();
verify(container).replace(properties);
// for metadata table
CosmosTableMetadata expected = new CosmosTableMetadata();
expected.setId(getFullTableName(namespace, table));
expected.setColumns(ImmutableMap.of("c1", "int", "c2", "text", "c3", "bigint"));
expected.setPartitionKeyNames(Collections.singletonList("c1"));
expected.setClusteringKeyNames(Collections.emptyList());
expected.setClusteringOrders(Collections.emptyMap());
expected.setSecondaryIndexNames(ImmutableSet.of("c3"));
verify(metadataContainer).upsertItem(expected);
}
use of com.azure.cosmos.models.CosmosContainerProperties in project scalardb by scalar-labs.
the class CosmosAdmin method createContainer.
private void createContainer(String database, String table, TableMetadata metadata) throws ExecutionException {
if (!databaseExists(database)) {
throw new ExecutionException("the database does not exists");
}
CosmosDatabase cosmosDatabase = client.getDatabase(database);
CosmosContainerProperties properties = computeContainerProperties(table, metadata);
cosmosDatabase.createContainer(properties);
CosmosStoredProcedureProperties storedProcedureProperties = computeContainerStoredProcedureProperties();
cosmosDatabase.getContainer(table).getScripts().createStoredProcedure(storedProcedureProperties);
}
use of com.azure.cosmos.models.CosmosContainerProperties in project scalardb by scalar-labs.
the class CosmosAdmin method updateIndexingPolicy.
private void updateIndexingPolicy(String databaseName, String containerName, TableMetadata newTableMetadata) throws ExecutionException {
CosmosDatabase database = client.getDatabase(databaseName);
try {
// get the existing container properties
CosmosContainerResponse response = database.createContainerIfNotExists(containerName, PARTITION_KEY_PATH);
CosmosContainerProperties properties = response.getProperties();
// set the new index policy to the container properties
properties.setIndexingPolicy(computeIndexingPolicy(newTableMetadata));
// update the container properties
database.getContainer(containerName).replace(properties);
} catch (RuntimeException e) {
throw new ExecutionException("updating the indexing policy failed", e);
}
}
Aggregations