-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Update spring data cosmos to track2 cosmos SDK v4 dependency #13229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
918038e
e7bc706
89abdd8
b96615d
f660fb0
a68586a
2a95f0d
80a7203
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,29 +2,29 @@ | |
| // Licensed under the MIT License. | ||
| package com.azure.spring.data.cosmos; | ||
|
|
||
| import com.azure.data.cosmos.IndexingMode; | ||
| import com.azure.cosmos.models.IndexingMode; | ||
|
|
||
| /** | ||
| * Constants class of CosmosDB properties | ||
| */ | ||
| public final class Constants { | ||
|
|
||
| public static final String DEFAULT_COLLECTION_NAME = ""; | ||
| public static final boolean DEFAULT_INDEXINGPOLICY_AUTOMATIC = true; | ||
| public static final IndexingMode DEFAULT_INDEXINGPOLICY_MODE = IndexingMode.CONSISTENT; | ||
| public static final String DEFAULT_CONTAINER_NAME = ""; | ||
| public static final boolean DEFAULT_INDEXING_POLICY_AUTOMATIC = true; | ||
| public static final IndexingMode DEFAULT_INDEXING_POLICY_MODE = IndexingMode.CONSISTENT; | ||
| public static final String DEFAULT_REPOSITORY_IMPLEMENT_POSTFIX = "Impl"; | ||
| public static final int DEFAULT_TIME_TO_LIVE = -1; // Indicates never expire | ||
| public static final boolean DEFAULT_AUTO_CREATE_CONTAINER = true; | ||
|
|
||
| public static final String ID_PROPERTY_NAME = "id"; | ||
|
|
||
| public static final String COSMOSDB_MODULE_NAME = "cosmosdb"; | ||
| public static final String COSMOSDB_MODULE_PREFIX = "cosmosdb"; | ||
| public static final String COSMOS_MODULE_NAME = "cosmosDb"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the brand name is Cosmos not CosmosDB.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I am pushing an update for these. thanks. |
||
| public static final String COSMOS_MODULE_PREFIX = "cosmosDb"; | ||
| public static final String COSMOS_MAPPING_CONTEXT = "cosmosMappingContext"; | ||
|
|
||
| public static final String USER_AGENT_SUFFIX = "spring-data/"; | ||
|
|
||
| public static final String OBJECTMAPPER_BEAN_NAME = "cosmosdbObjectMapper"; | ||
| public static final String OBJECT_MAPPER_BEAN_NAME = "cosmosDbObjectMapper"; | ||
|
|
||
| public static final String ISO_8601_COMPATIBLE_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss:SSSXXX"; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.spring.data.cosmos; | ||
|
|
||
| import com.azure.cosmos.CosmosAsyncClient; | ||
| import com.azure.cosmos.CosmosClient; | ||
| import com.azure.cosmos.CosmosClientBuilder; | ||
| import com.azure.spring.data.cosmos.common.MacAddress; | ||
| import com.azure.spring.data.cosmos.common.PropertyLoader; | ||
| import com.azure.spring.data.cosmos.common.TelemetrySender; | ||
| import com.azure.spring.data.cosmos.config.CosmosDBConfig; | ||
| import org.apache.commons.lang3.reflect.FieldUtils; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.lang.NonNull; | ||
| import org.springframework.util.Assert; | ||
|
|
||
| import javax.annotation.PostConstruct; | ||
| import java.lang.reflect.Field; | ||
|
|
||
| /** | ||
| * Factory class for CosmosDb to create client | ||
| */ | ||
| public class CosmosDBFactory { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a public class? I wonder if it should be CosmosDBFactory or CosmosFactory. the same comment applies to all other public surface area, should we go with CosmosDB or Cosmos on the public surface. our brand name is Cosmos
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, changing all CosmosDB to Cosmos in next commit on this PR. |
||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(CosmosDBFactory.class); | ||
|
|
||
| private final CosmosDBConfig config; | ||
|
|
||
| private static final boolean IS_TELEMETRY_ALLOWED = | ||
| PropertyLoader.isApplicationTelemetryAllowed(); | ||
|
|
||
| private static final String USER_AGENT_SUFFIX = | ||
| Constants.USER_AGENT_SUFFIX + PropertyLoader.getProjectVersion(); | ||
|
|
||
| private String getUserAgentSuffix() { | ||
| String suffix = ";" + USER_AGENT_SUFFIX; | ||
|
|
||
| if (IS_TELEMETRY_ALLOWED || config.isAllowTelemetry()) { | ||
| suffix += ";" + MacAddress.getHashMac(); | ||
| } | ||
|
|
||
| return suffix; | ||
| } | ||
|
|
||
| /** | ||
| * Validate config and initialization | ||
| * | ||
| * @param cosmosDBConfig cosmosDBConfig | ||
| */ | ||
| public CosmosDBFactory(@NonNull CosmosDBConfig cosmosDBConfig) { | ||
| validateConfig(cosmosDBConfig); | ||
|
|
||
| this.config = cosmosDBConfig; | ||
| } | ||
|
|
||
| /** | ||
| * To create a CosmosAsyncClient | ||
| * | ||
| * @return CosmosClient | ||
| */ | ||
| public CosmosAsyncClient getCosmosAsyncClient() { | ||
| final CosmosClientBuilder cosmosClientBuilderFromConfig = | ||
| getCosmosClientBuilderFromConfig(config); | ||
| return cosmosClientBuilderFromConfig.buildAsyncClient(); | ||
| } | ||
|
|
||
| /** | ||
| * To create a CosmosClient | ||
| * | ||
| * @return CosmosSyncClient | ||
| */ | ||
| public CosmosClient getCosmosSyncClient() { | ||
| final CosmosClientBuilder cosmosClientBuilderFromConfig = | ||
| getCosmosClientBuilderFromConfig(config); | ||
| return cosmosClientBuilderFromConfig.buildClient(); | ||
| } | ||
|
|
||
| private CosmosClientBuilder getCosmosClientBuilderFromConfig(CosmosDBConfig cosmosDBConfig) { | ||
| final CosmosClientBuilder cosmosClientBuilder = cosmosDBConfig.getCosmosClientBuilder(); | ||
| cosmosClientBuilder.contentResponseOnWriteEnabled(true); | ||
| final String userAgentSuffixValue = getUserAgentSuffixValue(cosmosClientBuilder); | ||
| final String userAgentSuffix = getUserAgentSuffix() + userAgentSuffixValue; | ||
|
|
||
| return cosmosDBConfig.getCosmosClientBuilder().userAgentSuffix(userAgentSuffix); | ||
| } | ||
|
|
||
| private String getUserAgentSuffixValue(CosmosClientBuilder cosmosClientBuilder) { | ||
| final Field userAgentSuffix = FieldUtils.getDeclaredField(CosmosClientBuilder.class, | ||
| "userAgentSuffix", true); | ||
| try { | ||
| return (String)userAgentSuffix.get(cosmosClientBuilder); | ||
| } catch (IllegalAccessException e) { | ||
| logger.error("Error occurred while getting userAgentSuffix from CosmosClientBuilder", | ||
| e); | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| private void validateConfig(@NonNull CosmosDBConfig config) { | ||
| Assert.hasText(config.getDatabase(), "cosmosDb database should have text!"); | ||
| } | ||
|
|
||
| @PostConstruct | ||
| private void sendTelemetry() { | ||
| // If any one of them is enabled, send telemetry data | ||
| if (IS_TELEMETRY_ALLOWED || config.isAllowTelemetry()) { | ||
| final TelemetrySender sender = new TelemetrySender(); | ||
|
|
||
| sender.send(this.getClass().getSimpleName()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * To get config object of cosmosDb | ||
| * | ||
| * @return CosmosDBConfig | ||
| */ | ||
| public CosmosDBConfig getConfig() { | ||
| return config; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't need com.google.code.findbugs:jsr305 in the bannedDependency includes. This particular entry was removed from this file as part of the same PR that fixed in the inheritance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure @JimSuplizio - will get rid of it from bannedDependencies includes.