Search in sources :

Example 6 with IndexingPolicy

use of com.azure.cosmos.models.IndexingPolicy 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);
}
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)

Example 7 with IndexingPolicy

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

the class CosmosAdmin method computeIndexingPolicy.

private IndexingPolicy computeIndexingPolicy(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 indexingPolicy;
}
Also used : CompositePath(com.azure.cosmos.models.CompositePath) ArrayList(java.util.ArrayList) IncludedPath(com.azure.cosmos.models.IncludedPath) ExcludedPath(com.azure.cosmos.models.ExcludedPath) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Example 8 with IndexingPolicy

use of com.azure.cosmos.models.IndexingPolicy in project cas by apereo.

the class CosmosDbObjectFactory method createContainer.

/**
 * Create container.
 *
 * @param name         the name
 * @param partitionKey the partition key
 */
public void createContainer(final String name, final String partitionKey) {
    val database = client.getDatabase(properties.getDatabase());
    LOGGER.debug("Creating CosmosDb container [{}]", name);
    val containerProperties = new CosmosContainerProperties(name, '/' + partitionKey);
    containerProperties.setIndexingPolicy(new IndexingPolicy().setIndexingMode(IndexingMode.valueOf(properties.getIndexingMode())));
    val response = database.createContainerIfNotExists(containerProperties);
    LOGGER.debug("Created CosmosDb container [{}]", response.getProperties().getId());
}
Also used : lombok.val(lombok.val) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Example 9 with IndexingPolicy

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

the class SampleIndexManagementAsync method createContainerIfNotExistsWithSpecifiedIndex.

private void createContainerIfNotExistsWithSpecifiedIndex() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    // <CreateContainerIfNotExists>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // <CustomIndexingPolicy>
    IndexingPolicy indexingPolicy = new IndexingPolicy();
    // To turn indexing off set IndexingMode.NONE
    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);
    // Spatial indices - if you need them, here is how to set them up:
    /*
        List<SpatialSpec> spatialIndexes = new ArrayList<SpatialSpec>();
        List<SpatialType> collectionOfSpatialTypes = new ArrayList<SpatialType>();

        SpatialSpec spec = new SpatialSpec();
        spec.setPath("/locations/*");
        collectionOfSpatialTypes.add(SpatialType.Point);
        spec.setSpatialTypes(collectionOfSpatialTypes);
        spatialIndexes.add(spec);

        indexingPolicy.setSpatialIndexes(spatialIndexes);
         */
    // Composite indices - if you need them, here is how to set them up:
    /*
        List<List<CompositePath>> compositeIndexes = new ArrayList<>();
        List<CompositePath> compositePaths = new ArrayList<>();

        CompositePath nameCompositePath = new CompositePath();
        nameCompositePath.setPath("/name");
        nameCompositePath.setOrder(CompositePathSortOrder.ASCENDING);

        CompositePath ageCompositePath = new CompositePath();
        ageCompositePath.setPath("/age");
        ageCompositePath.setOrder(CompositePathSortOrder.DESCENDING);

        compositePaths.add(ageCompositePath);
        compositePaths.add(nameCompositePath);

        compositeIndexes.add(compositePaths);
        indexingPolicy.setCompositeIndexes(compositeIndexes);
         */
    containerProperties.setIndexingPolicy(indexingPolicy);
    // </CustomIndexingPolicy>
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    Mono<CosmosContainerResponse> containerIfNotExists = database.createContainerIfNotExists(containerProperties, throughputProperties);
    // Create container with 400 RU/s
    containerIfNotExists.flatMap(containerResponse -> {
        container = database.getContainer(containerResponse.getProperties().getId());
        logger.info("Checking container " + container.getId() + " completed!\n");
        return Mono.empty();
    }).block();
// </CreateContainerIfNotExists>
}
Also used : PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) ExcludedPath(com.azure.cosmos.models.ExcludedPath) ArrayList(java.util.ArrayList) Families(com.azure.cosmos.examples.common.Families) Family(com.azure.cosmos.examples.common.Family) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) Duration(java.time.Duration) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosException(com.azure.cosmos.CosmosException) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy) Logger(org.slf4j.Logger) IncludedPath(com.azure.cosmos.models.IncludedPath) IndexingMode(com.azure.cosmos.models.IndexingMode) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) 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) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 10 with IndexingPolicy

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

the class SampleIndexManagement method createContainerIfNotExistsWithSpecifiedIndex.

private void createContainerIfNotExistsWithSpecifiedIndex() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // <CustomIndexingPolicy>
    IndexingPolicy indexingPolicy = new IndexingPolicy();
    // To turn indexing off set IndexingMode.NONE
    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);
    // Spatial indices - if you need them, here is how to set them up:
    /*
        List<SpatialSpec> spatialIndexes = new ArrayList<SpatialSpec>();
        List<SpatialType> collectionOfSpatialTypes = new ArrayList<SpatialType>();

        SpatialSpec spec = new SpatialSpec();
        spec.setPath("/locations/*");
        collectionOfSpatialTypes.add(SpatialType.Point);
        spec.setSpatialTypes(collectionOfSpatialTypes);
        spatialIndexes.add(spec);

        indexingPolicy.setSpatialIndexes(spatialIndexes);
         */
    // Composite indices - if you need them, here is how to set them up:
    /*
        List<List<CompositePath>> compositeIndexes = new ArrayList<>();
        List<CompositePath> compositePaths = new ArrayList<>();

        CompositePath nameCompositePath = new CompositePath();
        nameCompositePath.setPath("/name");
        nameCompositePath.setOrder(CompositePathSortOrder.ASCENDING);

        CompositePath ageCompositePath = new CompositePath();
        ageCompositePath.setPath("/age");
        ageCompositePath.setOrder(CompositePathSortOrder.DESCENDING);

        compositePaths.add(ageCompositePath);
        compositePaths.add(nameCompositePath);

        compositeIndexes.add(compositePaths);
        indexingPolicy.setCompositeIndexes(compositeIndexes);
         */
    containerProperties.setIndexingPolicy(indexingPolicy);
    // </CustomIndexingPolicy>
    // Create container with 400 RU/s
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    container = database.getContainer(containerResponse.getProperties().getId());
    logger.info("Checking container " + container.getId() + " completed!\n");
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) 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) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

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