diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Constants.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Constants.java index 75b63c8a956d..c0b1cd422ae3 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Constants.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Constants.java @@ -156,6 +156,12 @@ public static final class Properties { public static final String VECTOR_INDEXING_SEARCH_LIST_SIZE = "indexingSearchListSize"; public static final String VECTOR_INDEX_SHARD_KEYS = "vectorIndexShardKeys"; + // Materialized View Definition + public static final String MATERIALIZED_VIEW_DEFINITION = "materializedViewDefinition"; + public static final String MATERIALIZED_VIEW_SOURCE_COLLECTION_RID = "sourceCollectionRid"; + public static final String MATERIALIZED_VIEW_QUERY_DEFINITION = "definition"; + public static final String MATERIALIZED_VIEWS = "materializedViews"; + // Unique index. public static final String UNIQUE_KEY_POLICY = "uniqueKeyPolicy"; public static final String UNIQUE_KEYS = "uniqueKeys"; diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DocumentCollection.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DocumentCollection.java index 50d903ebfa7a..9aa1ae2b096b 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DocumentCollection.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DocumentCollection.java @@ -9,6 +9,8 @@ import com.azure.cosmos.models.ClientEncryptionPolicy; import com.azure.cosmos.models.ComputedProperty; import com.azure.cosmos.models.ConflictResolutionPolicy; +import com.azure.cosmos.models.CosmosMaterializedViewDefinition; +import com.azure.cosmos.models.CosmosMaterializedView; import com.azure.cosmos.models.CosmosFullTextPolicy; import com.azure.cosmos.models.CosmosVectorEmbeddingPolicy; import com.azure.cosmos.models.IndexingPolicy; @@ -24,6 +26,7 @@ import java.io.ObjectOutputStream; import java.util.Collection; import java.util.Collections; +import java.util.List; import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull; @@ -45,6 +48,7 @@ public final class DocumentCollection extends Resource { private ClientEncryptionPolicy clientEncryptionPolicyInternal; private CosmosVectorEmbeddingPolicy cosmosVectorEmbeddingPolicy; private CosmosFullTextPolicy cosmosFullTextPolicy; + private CosmosMaterializedViewDefinition cosmosMaterializedViewDefinition; /** * Constructor. @@ -469,6 +473,48 @@ public void setFullTextPolicy(CosmosFullTextPolicy value) { this.set(Constants.Properties.FULL_TEXT_POLICY, value); } + /** + * Gets the materialized view definition for this container in the Azure Cosmos DB service. + * + * @return the CosmosMaterializedViewDefinition + */ + public CosmosMaterializedViewDefinition getMaterializedViewDefinition() { + if (this.cosmosMaterializedViewDefinition == null) { + if (super.has(Constants.Properties.MATERIALIZED_VIEW_DEFINITION)) { + this.cosmosMaterializedViewDefinition = super.getObject( + Constants.Properties.MATERIALIZED_VIEW_DEFINITION, + CosmosMaterializedViewDefinition.class); + } + } + return this.cosmosMaterializedViewDefinition; + } + + /** + * Sets the materialized view definition for this container in the Azure Cosmos DB service. + * + * @param value the CosmosMaterializedViewDefinition + */ + public void setMaterializedViewDefinition(CosmosMaterializedViewDefinition value) { + checkNotNull(value, "cosmosMaterializedViewDefinition cannot be null"); + this.cosmosMaterializedViewDefinition = value; + this.set(Constants.Properties.MATERIALIZED_VIEW_DEFINITION, value); + } + + /** + * Gets the read-only list of materialized views derived from this container. + * This property is populated only when reading a container response from the Azure Cosmos DB service. + * + * @return the list of {@link CosmosMaterializedView}, or an empty list if none are present. + */ + public List getMaterializedViews() { + List results = + super.getList(Constants.Properties.MATERIALIZED_VIEWS, CosmosMaterializedView.class); + if (results == null) { + return Collections.emptyList(); + } + return Collections.unmodifiableList(results); + } + public void populatePropertyBag() { super.populatePropertyBag(); if (this.indexingPolicy == null) { diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java index c06d8a6bc1ed..9410cae88fd1 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/JsonSerializable.java @@ -13,6 +13,7 @@ import com.azure.cosmos.models.ChangeFeedPolicy; import com.azure.cosmos.models.CompositePath; import com.azure.cosmos.models.ConflictResolutionPolicy; +import com.azure.cosmos.models.CosmosMaterializedViewDefinition; import com.azure.cosmos.models.ExcludedPath; import com.azure.cosmos.models.IncludedPath; import com.azure.cosmos.models.IndexingPolicy; @@ -815,7 +816,8 @@ static boolean containsJsonSerializable(Class c) { || SqlParameter.class.equals(c) || SqlQuerySpec.class.equals(c) || UniqueKey.class.equals(c) - || UniqueKeyPolicy.class.equals(c); + || UniqueKeyPolicy.class.equals(c) + || CosmosMaterializedViewDefinition.class.equals(c); } @Override diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosContainerProperties.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosContainerProperties.java index f24579861b6b..98e0a1174470 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosContainerProperties.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosContainerProperties.java @@ -393,6 +393,51 @@ public CosmosContainerProperties setFullTextPolicy(CosmosFullTextPolicy value) { return this; } + /** + * Gets the materialized view definition for this container in the Azure Cosmos DB service. + * A materialized view is derived from a source container and is defined by a SQL-like query. + * + * @return the CosmosMaterializedViewDefinition + */ + public CosmosMaterializedViewDefinition getMaterializedViewDefinition() { + return this.documentCollection.getMaterializedViewDefinition(); + } + + /** + * Sets the materialized view definition for this container in the Azure Cosmos DB service. + * A materialized view is derived from a source container and is defined by a SQL-like query. + *

+ * Example: + *

{@code
+     * CosmosMaterializedViewDefinition mvDef = new CosmosMaterializedViewDefinition()
+     *     .setSourceCollectionId("gsi-src")
+     *     .setDefinition("SELECT c.customerId, c.emailAddress FROM c");
+     * containerProperties.setMaterializedViewDefinition(mvDef);
+     * }
+ * + * @param value the CosmosMaterializedViewDefinition to be used. + * @return the CosmosContainerProperties. + */ + public CosmosContainerProperties setMaterializedViewDefinition(CosmosMaterializedViewDefinition value) { + this.documentCollection.setMaterializedViewDefinition(value); + return this; + } + + /** + * Gets the read-only list of materialized view containers derived from this container. + * This property is populated only when reading a container response from the Azure Cosmos DB service. + *

+ * Example JSON representation in the response: + *

{@code
+     * "materializedViews": [{ "id": "gsi_testcontainer1", "_rid": "TughAMEOdUI=" }]
+     * }
+ * + * @return the list of {@link CosmosMaterializedView}, or an empty list if none are present. + */ + public List getMaterializedViews() { + return this.documentCollection.getMaterializedViews(); + } + Resource getResource() { return this.documentCollection; } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMaterializedView.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMaterializedView.java new file mode 100644 index 000000000000..7aa9ef6ac89f --- /dev/null +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMaterializedView.java @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.cosmos.models; + +import com.azure.cosmos.implementation.Constants; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Represents an entry in the read-only {@code materializedViews} list returned from the Azure Cosmos DB service + * when reading a container. Each entry identifies a materialized view container derived from the source container. + *

+ * Example JSON representation: + *

{@code
+ * "materializedViews": [{ "id": "gsi_testcontainer1", "_rid": "TughAMEOdUI=" }]
+ * }
+ */ +public final class CosmosMaterializedView { + + @JsonProperty(Constants.Properties.ID) + private String id; + + @JsonProperty(Constants.Properties.R_ID) + private String resourceId; + + /** + * Constructor + */ + public CosmosMaterializedView() { + } + + /** + * Gets the id of the materialized view container. + * + * @return the id of the materialized view container. + */ + public String getId() { + return id; + } + + /** + * Gets the resource id (_rid) of the materialized view container. + * + * @return the resource id of the materialized view container. + */ + public String getResourceId() { + return resourceId; + } + + @Override + public String toString() { + return "CosmosMaterializedViewResult{" + + "id='" + id + '\'' + + ", resourceId='" + resourceId + '\'' + + '}'; + } +} diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMaterializedViewDefinition.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMaterializedViewDefinition.java new file mode 100644 index 000000000000..3b19992f682d --- /dev/null +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMaterializedViewDefinition.java @@ -0,0 +1,89 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.cosmos.models; + +import com.azure.cosmos.implementation.Constants; +import com.azure.cosmos.implementation.JsonSerializable; +import com.fasterxml.jackson.databind.node.ObjectNode; + +/** + * Represents the materialized view definition for a container in the Azure Cosmos DB service. + * A materialized view is derived from a source container and is defined by a SQL-like query. + *

+ * Example: + *

{@code
+ * "materializedViewDefinition": {
+ *     "sourceCollectionId": "gsi-src",
+ *     "definition": "SELECT c.customerId, c.emailAddress FROM c"
+ * }
+ * }
+ */ +public final class CosmosMaterializedViewDefinition { + + private JsonSerializable jsonSerializable; + + /** + * Constructor + */ + public CosmosMaterializedViewDefinition() { + this.jsonSerializable = new JsonSerializable(); + } + + /** + * Constructor. + * + * @param objectNode the {@link ObjectNode} that represents the materialized view definition. + */ + CosmosMaterializedViewDefinition(ObjectNode objectNode) { + this.jsonSerializable = new JsonSerializable(objectNode); + } + + /** + * Gets the source collection id for the materialized view. + * + * @return the source collection id. + */ + public String getSourceCollectionRid() { + return this.jsonSerializable.getString(Constants.Properties.MATERIALIZED_VIEW_SOURCE_COLLECTION_RID); + } + + /** + * Sets the source collection id for the materialized view. + * + * @param sourceCollectionRid the source collection id. + * @return CosmosMaterializedViewDefinition + */ + public CosmosMaterializedViewDefinition setSourceCollectionRid(String sourceCollectionRid) { + this.jsonSerializable.set(Constants.Properties.MATERIALIZED_VIEW_SOURCE_COLLECTION_RID, sourceCollectionRid); + return this; + } + + /** + * Gets the query definition for the materialized view. + * + * @return the query definition. + */ + public String getDefinition() { + return this.jsonSerializable.getString(Constants.Properties.MATERIALIZED_VIEW_QUERY_DEFINITION); + } + + /** + * Sets the query definition for the materialized view. + * + * @param definition the query definition (e.g. {@code "SELECT c.customerId, c.emailAddress FROM c"}). + * @return CosmosMaterializedViewDefinition + */ + public CosmosMaterializedViewDefinition setDefinition(String definition) { + this.jsonSerializable.set(Constants.Properties.MATERIALIZED_VIEW_QUERY_DEFINITION, definition); + return this; + } + + void populatePropertyBag() { + this.jsonSerializable.populatePropertyBag(); + } + + JsonSerializable getJsonSerializable() { + return this.jsonSerializable; + } +} diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java index e698e0664c41..423a2b31a62b 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/ModelBridgeInternal.java @@ -451,6 +451,8 @@ public static void populatePropertyBag(T t) { ((UniqueKey) t).populatePropertyBag(); } else if (t instanceof UniqueKeyPolicy) { ((UniqueKeyPolicy) t).populatePropertyBag(); + } else if (t instanceof CosmosMaterializedViewDefinition) { + ((CosmosMaterializedViewDefinition) t).populatePropertyBag(); } else { throw new IllegalArgumentException("populatePropertyBag method does not exists in class " + t.getClass()); } @@ -488,6 +490,8 @@ public static JsonSerializable getJsonSerializable(T t) { return ((UniqueKey) t).getJsonSerializable(); } else if (t instanceof UniqueKeyPolicy) { return ((UniqueKeyPolicy) t).getJsonSerializable(); + } else if (t instanceof CosmosMaterializedViewDefinition) { + return ((CosmosMaterializedViewDefinition) t).getJsonSerializable(); } else { throw new IllegalArgumentException("getJsonSerializable method does not exists in class " + t.getClass()); }