Skip to content

Commit

Permalink
Updated CosmosConfig to take database name (#13756)
Browse files Browse the repository at this point in the history
* Updated CosmosConfig to take database name. Removed CosmosClientConfig, replaced with CosmosClientBuilder. Updated readme, and samples with new architecture

* Removed database property from CosmosConfig

* Removed unused imports
  • Loading branch information
kushagraThapar authored Aug 4, 2020
1 parent f0dfee0 commit 8ae28b1
Show file tree
Hide file tree
Showing 29 changed files with 170 additions and 339 deletions.
82 changes: 34 additions & 48 deletions sdk/cosmos/azure-spring-data-cosmos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ For reactive repository support, use `@EnableReactiveCosmosRepositories`
2.2.x supports Response Diagnostics String and Query Metrics.
Set `queryMetricsEnabled` flag to true in application.properties to enable query metrics.
In addition to setting the flag, implement `ResponseDiagnosticsProcessor` to log diagnostics information.
<!-- embedme src/samples/java/com/azure/cosmos/AppConfiguration.java#L24-L87 -->
<!-- embedme src/samples/java/com/azure/cosmos/AppConfiguration.java#L23-L82 -->

```java
@Configuration
Expand All @@ -173,26 +173,22 @@ public class AppConfiguration extends AbstractCosmosConfiguration {
private AzureKeyCredential azureKeyCredential;

@Bean
public CosmosClientConfig getClientConfig() {
public CosmosClientBuilder getCosmosClientBuilder() {
this.azureKeyCredential = new AzureKeyCredential(key);
DirectConnectionConfig directConnectionConfig = new DirectConnectionConfig();
GatewayConnectionConfig gatewayConnectionConfig = new GatewayConnectionConfig();
CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder()
return new CosmosClientBuilder()
.endpoint(uri)
.credential(azureKeyCredential)
.directMode(directConnectionConfig, gatewayConnectionConfig);
return CosmosClientConfig.builder()
.cosmosClientBuilder(cosmosClientBuilder)
.database(getDatabaseName())
.build();
}

@Override
public CosmosConfig cosmosConfig() {
return CosmosConfig.builder()
.enableQueryMetrics(queryMetricsEnabled)
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
.build();
.enableQueryMetrics(queryMetricsEnabled)
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
.build();
}

public void switchToSecondaryKey() {
Expand All @@ -215,29 +211,25 @@ public class AppConfiguration extends AbstractCosmosConfiguration {
}
```
Or if you want to customize your config:
<!-- embedme src/samples/java/com/azure/cosmos/AppConfigurationCodeSnippet.java#L46-L66 -->
<!-- embedme src/samples/java/com/azure/cosmos/AppConfigurationCodeSnippet.java#L45-L61 -->

```java
@Bean
public CosmosClientConfig getClientConfig() {
public CosmosClientBuilder getCosmosClientBuilder() {

DirectConnectionConfig directConnectionConfig = new DirectConnectionConfig();
GatewayConnectionConfig gatewayConnectionConfig = new GatewayConnectionConfig();
CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder()
return new CosmosClientBuilder()
.endpoint(uri)
.directMode(directConnectionConfig, gatewayConnectionConfig);
return CosmosClientConfig.builder()
.cosmosClientBuilder(cosmosClientBuilder)
.database(getDatabaseName())
.build();
}

@Override
public CosmosConfig cosmosConfig() {
return CosmosConfig.builder()
.enableQueryMetrics(queryMetricsEnabled)
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
.build();
.enableQueryMetrics(queryMetricsEnabled)
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
.build();
}
```
By default, `@EnableCosmosRepositories` will scan the current package for any interfaces that extend one of Spring Data's repository interfaces. Using it to annotate your Configuration class to scan a different root package by `@EnableCosmosRepositories(basePackageClass=UserRepository.class)` if your project layout has multiple projects and it's not finding your repositories.
Expand Down Expand Up @@ -413,7 +405,7 @@ You can put different database entities into different packages.
### Setup configuration
The `@EnableReactiveCosmosRepositories` or `@EnableCosmosRepositories` support user-define the cosmos template, use `reactiveCosmosTemplateRef` or `cosmosTemplateRef` to config the name of the `ReactiveCosmosTemplate` or `CosmosTemplate` bean to be used with the repositories detected.
If you have multiple cosmos database accounts, you can define multiple `CosmosAsyncClient`. If the single cosmos account has multiple databases, you can use the same `CosmosAsyncClient` to initialize the cosmos template.
<!-- embedme src/samples/java/com/azure/cosmos/multidatasource/DatabaseConfiguration.java#L35-L138 -->
<!-- embedme src/samples/java/com/azure/cosmos/multidatasource/DatabaseConfiguration.java#L34-L131 -->

```java
@Configuration
Expand Down Expand Up @@ -447,23 +439,17 @@ public class DatabaseConfiguration extends AbstractCosmosConfiguration {
private IsNewAwareAuditingHandler cosmosAuditingHandler;

@Bean
public CosmosClientConfig cosmosClientConfig() {
CosmosClientConfig cosmosClientConfig = CosmosClientConfig.builder()
.cosmosClientBuilder(new CosmosClientBuilder()
.key(primaryProperties.getKey()).endpoint(primaryProperties.getUri()))
.database(primaryProperties.getDatabase())
.build();

return cosmosClientConfig;
public CosmosClientBuilder cosmosClientBuilder() {
return new CosmosClientBuilder()
.key(primaryProperties.getKey())
.endpoint(primaryProperties.getUri());
}

@Bean
public CosmosClientConfig secondaryCosmosClientConfig() {
CosmosClientConfig cosmosClientConfig = CosmosClientConfig.builder()
.cosmosClientBuilder(new CosmosClientBuilder()
.key(secondaryProperties.getKey()).endpoint(secondaryProperties.getUri()))
.database(secondaryProperties.getDatabase())
.build();
return cosmosClientConfig;
public CosmosClientBuilder secondaryCosmosClientBuilder() {
return new CosmosClientBuilder()
.key(secondaryProperties.getKey())
.endpoint(secondaryProperties.getUri());
}
// -------------------------First Cosmos Client for First Cosmos Account---------------------------
@EnableReactiveCosmosRepositories(basePackages = "com.azure.cosmos.multidatasource.primarydatasource.first")
Expand All @@ -480,16 +466,16 @@ public class DatabaseConfiguration extends AbstractCosmosConfiguration {

// -------------------------Second Cosmos Client for Secondary Cosmos Account---------------------------
@Bean("secondaryCosmosAsyncClient")
public CosmosAsyncClient getCosmosAsyncClient(CosmosClientConfig secondaryCosmosClientConfig) {
return CosmosFactory.createCosmosAsyncClient(secondaryCosmosClientConfig);
public CosmosAsyncClient getCosmosAsyncClient(CosmosClientBuilder secondaryCosmosClientBuilder) {
return CosmosFactory.createCosmosAsyncClient(secondaryCosmosClientBuilder);
}

@Bean("secondaryCosmosConfig")
public CosmosConfig getCosmosConfig() {
return CosmosConfig.builder()
.enableQueryMetrics(true)
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
.build();
.enableQueryMetrics(true)
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
.build();
}

@EnableReactiveCosmosRepositories(basePackages = "com.azure.cosmos.multidatasource.secondarydatasource.first", reactiveCosmosTemplateRef = "secondaryReactiveCosmosTemplate")
Expand Down Expand Up @@ -524,26 +510,26 @@ public class DatabaseConfiguration extends AbstractCosmosConfiguration {

In the above example, we have two cosmos account, each account has two databases. For each account, we can use the same Cosmos Client. You can create the `CosmosAsyncClient` like this:

<!-- embedme src/samples/java/com/azure/cosmos/multidatasource/DatabaseConfiguration.java#L98-L101 -->
<!-- embedme src/samples/java/com/azure/cosmos/multidatasource/DatabaseConfiguration.java#L91-L94 -->

```java
@Bean("secondaryCosmosAsyncClient")
public CosmosAsyncClient getCosmosAsyncClient(CosmosClientConfig secondaryCosmosClientConfig) {
return CosmosFactory.createCosmosAsyncClient(secondaryCosmosClientConfig);
public CosmosAsyncClient getCosmosAsyncClient(CosmosClientBuilder secondaryCosmosClientBuilder) {
return CosmosFactory.createCosmosAsyncClient(secondaryCosmosClientBuilder);
}
```

Besides, if you want to define `queryMetricsEnabled` or `ResponseDiagnosticsProcessor` , you can create the `CosmosConfig` for your cosmos template.

<!-- embedme src/samples/java/com/azure/cosmos/multidatasource/DatabaseConfiguration.java#L103-L109-->
<!-- embedme src/samples/java/com/azure/cosmos/multidatasource/DatabaseConfiguration.java#L96-L102-->

```java
@Bean("secondaryCosmosConfig")
public CosmosConfig getCosmosConfig() {
return CosmosConfig.builder()
.enableQueryMetrics(true)
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
.build();
.enableQueryMetrics(true)
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
.build();
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.spring.data.cosmos.common.PropertyLoader;
import com.azure.spring.data.cosmos.config.CosmosClientConfig;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -66,24 +65,22 @@ public String getDatabaseName() {
/**
* Create Cosmos Async Client
*
* @param config CosmosClientConfig
* @param cosmosClientBuilder CosmosClientBuilder
* @return CosmosAsyncClient
*/
public static CosmosAsyncClient createCosmosAsyncClient(CosmosClientConfig config) {
final CosmosClientBuilder cosmosClientBuilder = getCosmosClientBuilderFromConfig(config);
return cosmosClientBuilder.buildAsyncClient();
public static CosmosAsyncClient createCosmosAsyncClient(CosmosClientBuilder cosmosClientBuilder) {
return updateCosmosClientBuilderWithUASuffix(cosmosClientBuilder).buildAsyncClient();
}

private static CosmosClientBuilder getCosmosClientBuilderFromConfig(CosmosClientConfig cosmosClientConfig) {
final CosmosClientBuilder cosmosClientBuilder = cosmosClientConfig.getCosmosClientBuilder();
private static CosmosClientBuilder updateCosmosClientBuilderWithUASuffix(CosmosClientBuilder cosmosClientBuilder) {
cosmosClientBuilder.contentResponseOnWriteEnabled(true);
final String userAgentSuffixValue = getUserAgentSuffixValue(cosmosClientBuilder);
String userAgentSuffix = getUserAgentSuffix();
if (!userAgentSuffixValue.contains(userAgentSuffix)) {
userAgentSuffix += userAgentSuffixValue;
}

return cosmosClientConfig.getCosmosClientBuilder().userAgentSuffix(userAgentSuffix);
return cosmosClientBuilder.userAgentSuffix(userAgentSuffix);
}

private static String getUserAgentSuffixValue(CosmosClientBuilder cosmosClientBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.azure.spring.data.cosmos.config;

import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.spring.data.cosmos.Constants;
import com.azure.spring.data.cosmos.CosmosFactory;
import com.azure.spring.data.cosmos.core.CosmosTemplate;
Expand Down Expand Up @@ -39,23 +40,21 @@ public CosmosFactory cosmosFactory(CosmosAsyncClient cosmosAsyncClient) {
*
* @param cosmosMappingContext cosmosMappingContext
* @return MappingCosmosConverter bean
* @throws ClassNotFoundException if the class type is invalid
*/
@Bean
public MappingCosmosConverter mappingCosmosConverter(CosmosMappingContext cosmosMappingContext)
throws ClassNotFoundException {
public MappingCosmosConverter mappingCosmosConverter(CosmosMappingContext cosmosMappingContext) {
return new MappingCosmosConverter(cosmosMappingContext, objectMapper);
}

/**
* Declare CosmosAsyncClient bean.
*
* @param cosmosClientConfig CosmosClientConfig
* @param cosmosClientBuilder cosmosClientBuilder
* @return CosmosAsyncClient bean
*/
@Bean
public CosmosAsyncClient cosmosAsyncClient(CosmosClientConfig cosmosClientConfig) {
return CosmosFactory.createCosmosAsyncClient(cosmosClientConfig);
public CosmosAsyncClient cosmosAsyncClient(CosmosClientBuilder cosmosClientBuilder) {
return CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder);
}

@Qualifier(Constants.OBJECT_MAPPER_BEAN_NAME)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class CosmosConfig {
* @param queryMetricsEnabled must not be {@literal null}
*/
@ConstructorProperties({"responseDiagnosticsProcessor", "queryMetricsEnabled"})
public CosmosConfig(ResponseDiagnosticsProcessor responseDiagnosticsProcessor, boolean queryMetricsEnabled) {
public CosmosConfig(ResponseDiagnosticsProcessor responseDiagnosticsProcessor,
boolean queryMetricsEnabled) {
this.responseDiagnosticsProcessor = responseDiagnosticsProcessor;
this.queryMetricsEnabled = queryMetricsEnabled;
}
Expand Down Expand Up @@ -63,6 +64,7 @@ public static class CosmosConfigBuilder {
CosmosConfigBuilder() {
}


/**
* Set responseDiagnosticsProcessor
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import com.azure.cosmos.models.SqlParameter;
import com.azure.cosmos.models.SqlQuerySpec;
import com.azure.spring.data.cosmos.core.query.CosmosQuery;
import com.azure.spring.data.cosmos.core.query.Criteria;
import com.azure.spring.data.cosmos.core.query.CriteriaType;
import com.azure.spring.data.cosmos.core.query.CosmosQuery;
import com.azure.spring.data.cosmos.exception.IllegalQueryException;
import org.javatuples.Pair;
import org.springframework.data.domain.Sort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import com.azure.spring.data.cosmos.Constants;
import com.azure.spring.data.cosmos.core.mapping.CosmosPersistentProperty;
import com.azure.spring.data.cosmos.core.query.CosmosQuery;
import com.azure.spring.data.cosmos.core.query.Criteria;
import com.azure.spring.data.cosmos.core.query.CriteriaType;
import com.azure.spring.data.cosmos.core.query.CosmosQuery;
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.context.MappingContext;
Expand Down
Loading

0 comments on commit 8ae28b1

Please sign in to comment.