Search in sources :

Example 1 with IndexingPolicy

use of com.azure.cosmos.models.IndexingPolicy in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SalesOrder method MigrateJavaSDKv4IndexingAsync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/migrate-java-v4-sdk
 * Indexing
 */
/**
 * Indexing
 */
public static void MigrateJavaSDKv4IndexingAsync() {
    String containerName = "family_container";
    String partition_key = "/pk";
    CosmosAsyncDatabase database = null;
    // <MigrateIndexingAsync>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // Custom indexing policy
    IndexingPolicy indexingPolicy = new IndexingPolicy();
    indexingPolicy.setIndexingMode(IndexingMode.CONSISTENT);
    // Included paths
    List<IncludedPath> includedPaths = new ArrayList<>();
    includedPaths.add(new IncludedPath("/*"));
    indexingPolicy.setIncludedPaths(includedPaths);
    // Excluded paths
    List<ExcludedPath> excludedPaths = new ArrayList<>();
    excludedPaths.add(new ExcludedPath("/name/*"));
    indexingPolicy.setExcludedPaths(excludedPaths);
    containerProperties.setIndexingPolicy(indexingPolicy);
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    database.createContainerIfNotExists(containerProperties, throughputProperties);
    CosmosAsyncContainer containerIfNotExists = database.getContainer(containerName);
// </MigrateIndexingAsync>
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ArrayList(java.util.ArrayList) IncludedPath(com.azure.cosmos.models.IncludedPath) ExcludedPath(com.azure.cosmos.models.ExcludedPath) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Example 2 with IndexingPolicy

use of com.azure.cosmos.models.IndexingPolicy in project scalardb by scalar-labs.

the class CosmosAdmin method computeContainerProperties.

private CosmosContainerProperties computeContainerProperties(String table, TableMetadata metadata) {
    IndexingPolicy indexingPolicy = new IndexingPolicy();
    List<IncludedPath> paths = new ArrayList<>();
    if (metadata.getClusteringKeyNames().isEmpty()) {
        paths.add(new IncludedPath(PARTITION_KEY_PATH + "/?"));
    } else {
        // Add a composite index when we have clustering keys
        List<CompositePath> compositePaths = new ArrayList<>();
        // Add concatenated partition key to the composite path first
        CompositePath partitionKeyCompositePath = new CompositePath();
        partitionKeyCompositePath.setPath(PARTITION_KEY_PATH);
        partitionKeyCompositePath.setOrder(CompositePathSortOrder.ASCENDING);
        compositePaths.add(partitionKeyCompositePath);
        // Then, add clustering keys to the composite path
        metadata.getClusteringKeyNames().forEach(c -> {
            CompositePath compositePath = new CompositePath();
            compositePath.setPath(CLUSTERING_KEY_PATH_PREFIX + c);
            compositePath.setOrder(metadata.getClusteringOrder(c) == Order.ASC ? CompositePathSortOrder.ASCENDING : CompositePathSortOrder.DESCENDING);
            compositePaths.add(compositePath);
        });
        indexingPolicy.setCompositeIndexes(Collections.singletonList(compositePaths));
    }
    paths.addAll(metadata.getSecondaryIndexNames().stream().map(index -> new IncludedPath(SECONDARY_INDEX_KEY_PATH_PREFIX + index + "/?")).collect(Collectors.toList()));
    if (!paths.isEmpty()) {
        indexingPolicy.setIncludedPaths(paths);
    }
    indexingPolicy.setExcludedPaths(Collections.singletonList(new ExcludedPath(EXCLUDED_PATH)));
    return new CosmosContainerProperties(table, PARTITION_KEY_PATH).setIndexingPolicy(indexingPolicy);
}
Also used : CompositePath(com.azure.cosmos.models.CompositePath) ArrayList(java.util.ArrayList) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) IncludedPath(com.azure.cosmos.models.IncludedPath) ExcludedPath(com.azure.cosmos.models.ExcludedPath) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Example 3 with IndexingPolicy

use of com.azure.cosmos.models.IndexingPolicy 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 4 with IndexingPolicy

use of com.azure.cosmos.models.IndexingPolicy 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 5 with IndexingPolicy

use of com.azure.cosmos.models.IndexingPolicy 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);
}
Also used : CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) PartitionKey(com.azure.cosmos.models.PartitionKey) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) Test(org.junit.jupiter.api.Test)

Aggregations

IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)10 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)9 ExcludedPath (com.azure.cosmos.models.ExcludedPath)5 IncludedPath (com.azure.cosmos.models.IncludedPath)5 ArrayList (java.util.ArrayList)5 CosmosContainer (com.azure.cosmos.CosmosContainer)4 CosmosDatabase (com.azure.cosmos.CosmosDatabase)4 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 PartitionKey (com.azure.cosmos.models.PartitionKey)3 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)3 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)2 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)2 CosmosScripts (com.azure.cosmos.CosmosScripts)2 CompositePath (com.azure.cosmos.models.CompositePath)2 CosmosStoredProcedureProperties (com.azure.cosmos.models.CosmosStoredProcedureProperties)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 TableMetadata (com.scalar.db.api.TableMetadata)2 Test (org.junit.jupiter.api.Test)2 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)1