use of com.azure.cosmos.CosmosScripts 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);
}
use of com.azure.cosmos.CosmosScripts 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.CosmosScripts in project DataSpaceConnector by eclipse-dataspaceconnector.
the class CosmosTransferProcessStoreIntegrationTest method uploadStoredProcedure.
private static void uploadStoredProcedure(CosmosContainer container, String name) {
var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name + ".js");
if (is == null) {
throw new AssertionError("The input stream referring to the " + name + " file cannot be null!");
}
Scanner s = new Scanner(is).useDelimiter("\\A");
String body = s.hasNext() ? s.next() : "";
CosmosStoredProcedureProperties props = new CosmosStoredProcedureProperties(name, body);
CosmosScripts scripts = container.getScripts();
if (scripts.readAllStoredProcedures().stream().noneMatch(sp -> sp.getId().equals(name))) {
CosmosStoredProcedureResponse storedProcedure = scripts.createStoredProcedure(props);
}
}
Aggregations