Search in sources :

Example 1 with CosmosScripts

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);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CosmosScripts(com.azure.cosmos.CosmosScripts) ImmutableMap(com.google.common.collect.ImmutableMap) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Example 2 with CosmosScripts

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);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CosmosScripts(com.azure.cosmos.CosmosScripts) ImmutableMap(com.google.common.collect.ImmutableMap) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Example 3 with CosmosScripts

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);
    }
}
Also used : Scanner(java.util.Scanner) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) CosmosStoredProcedureResponse(com.azure.cosmos.models.CosmosStoredProcedureResponse) CosmosScripts(com.azure.cosmos.CosmosScripts)

Aggregations

CosmosScripts (com.azure.cosmos.CosmosScripts)3 CosmosStoredProcedureProperties (com.azure.cosmos.models.CosmosStoredProcedureProperties)3 CosmosContainer (com.azure.cosmos.CosmosContainer)2 CosmosDatabase (com.azure.cosmos.CosmosDatabase)2 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)2 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 TableMetadata (com.scalar.db.api.TableMetadata)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 CosmosStoredProcedureResponse (com.azure.cosmos.models.CosmosStoredProcedureResponse)1 Scanner (java.util.Scanner)1