Skip to content

Adding support to create GSI containers#48480

Draft
mbhaskar wants to merge 1 commit intoAzure:mainfrom
mbhaskar:global-secondary-index
Draft

Adding support to create GSI containers#48480
mbhaskar wants to merge 1 commit intoAzure:mainfrom
mbhaskar:global-secondary-index

Conversation

@mbhaskar
Copy link
Member

Description

Materialized View Support — Change Summary

Overview

This branch adds client-side support for Materialized Views/Global Secondary Index in the Azure Cosmos DB Java SDK.
Materialized views are derived containers built from a source container using a SQL-like projection
query. The changes expose two properties that the service already supports:

Property Direction Description
materializedViewDefinition Read / Write Defines the source container and query for a materialized view container
materializedViews Read-only List of materialized view containers derived from a source container, populated by the service in container.read() responses

Important Changes

CosmosMaterializedViewDefinition.java

Package: com.azure.cosmos.models

Represents the write-side definition of a materialized view container. The source
container Rid should be set when creating a materialized view container.

Example usage:

CosmosMaterializedViewDefinition mvDef = new CosmosMaterializedViewDefinition()
    .setSourceCollectionRid("TughAMEOdUI=")
    .setDefinition("SELECT c.customerId, c.emailAddress FROM c");

containerProperties.setMaterializedViewDefinition(mvDef);
Method Description
getSourceCollectionRid() Returns the _rid of the source container
setSourceCollectionRid(String) Sets the _rid of the source container. Fluent.
getDefinition() Returns the SQL-like projection query
setDefinition(String) Sets the SQL-like projection query. Fluent.

Wire format:

"materializedViewDefinition": {
    "sourceCollectionRid": "TughAMEOdUI=",
    "definition": "SELECT c.customerId, c.emailAddress FROM c"
}

CosmosMaterializedView.java

Package: com.azure.cosmos.models

A read-only model representing a single entry in the materializedViews array returned by the
service when reading a source container. Each entry identifies one derived materialized view
container.

Method Description
getId() The container id
getResourceId() The container _rid

Example Usage:

CosmosContainerResponse response = container.read();
List<CosmosMaterializedView> materializedViews = response.getProperties().getMaterializedViews();
for (CosmosMaterializedView mv : materializedViews) {
    System.out.println("Materialized view container id: " + mv.getId());
    System.out.println("Materialized view container _rid: " + mv.getResourceId());
}

CosmosContainerProperties.java

Two new public methods:

Method Description
setMaterializedViewDefinition(CosmosMaterializedViewDefinition) Sets the materialized view definition. Used when creating a materialized view container. Fluent, returns this. Throws NullPointerException if null is passed.
getMaterializedViewDefinition() Gets the materialized view definition. Returns null if not set.
getMaterializedViews() Read-only. Returns the unmodifiable list of CosmosMaterializedView entries populated by the service when reading a source container. Returns an empty list if not present.

DocumentCollection.java

Internal backing implementation for CosmosContainerProperties:

  • Added cosmosMaterializedViewDefinition field with lazy-load caching
  • getMaterializedViewDefinition() — lazy-loads from the property bag using getObject()
  • setMaterializedViewDefinition() — null-checks, caches the value, and persists to the property bag via set()
  • getMaterializedViews() — reads the server-populated list directly from the property bag via getList(), returns an unmodifiable list

Constants.java

Four new string constants added under a // Materialized View Definition section:

Constant Value
MATERIALIZED_VIEW_DEFINITION "materializedViewDefinition"
MATERIALIZED_VIEW_SOURCE_COLLECTION_RID "sourceCollectionRid"
MATERIALIZED_VIEW_QUERY_DEFINITION "definition"
MATERIALIZED_VIEWS "materializedViews"

Test Coverage (MaterializedViewTest.java)

Unit tests added for the new materialized view properties on CosmosContainerProperties:

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant