Skip to content

Commit 8ae28b1

Browse files
Updated CosmosConfig to take database name (#13756)
* 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
1 parent f0dfee0 commit 8ae28b1

29 files changed

+170
-339
lines changed

sdk/cosmos/azure-spring-data-cosmos/README.md

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ For reactive repository support, use `@EnableReactiveCosmosRepositories`
146146
2.2.x supports Response Diagnostics String and Query Metrics.
147147
Set `queryMetricsEnabled` flag to true in application.properties to enable query metrics.
148148
In addition to setting the flag, implement `ResponseDiagnosticsProcessor` to log diagnostics information.
149-
<!-- embedme src/samples/java/com/azure/cosmos/AppConfiguration.java#L24-L87 -->
149+
<!-- embedme src/samples/java/com/azure/cosmos/AppConfiguration.java#L23-L82 -->
150150

151151
```java
152152
@Configuration
@@ -173,26 +173,22 @@ public class AppConfiguration extends AbstractCosmosConfiguration {
173173
private AzureKeyCredential azureKeyCredential;
174174

175175
@Bean
176-
public CosmosClientConfig getClientConfig() {
176+
public CosmosClientBuilder getCosmosClientBuilder() {
177177
this.azureKeyCredential = new AzureKeyCredential(key);
178178
DirectConnectionConfig directConnectionConfig = new DirectConnectionConfig();
179179
GatewayConnectionConfig gatewayConnectionConfig = new GatewayConnectionConfig();
180-
CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder()
180+
return new CosmosClientBuilder()
181181
.endpoint(uri)
182182
.credential(azureKeyCredential)
183183
.directMode(directConnectionConfig, gatewayConnectionConfig);
184-
return CosmosClientConfig.builder()
185-
.cosmosClientBuilder(cosmosClientBuilder)
186-
.database(getDatabaseName())
187-
.build();
188184
}
189185

190186
@Override
191187
public CosmosConfig cosmosConfig() {
192188
return CosmosConfig.builder()
193-
.enableQueryMetrics(queryMetricsEnabled)
194-
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
195-
.build();
189+
.enableQueryMetrics(queryMetricsEnabled)
190+
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
191+
.build();
196192
}
197193

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

220216
```java
221217
@Bean
222-
public CosmosClientConfig getClientConfig() {
218+
public CosmosClientBuilder getCosmosClientBuilder() {
223219

224220
DirectConnectionConfig directConnectionConfig = new DirectConnectionConfig();
225221
GatewayConnectionConfig gatewayConnectionConfig = new GatewayConnectionConfig();
226-
CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder()
222+
return new CosmosClientBuilder()
227223
.endpoint(uri)
228224
.directMode(directConnectionConfig, gatewayConnectionConfig);
229-
return CosmosClientConfig.builder()
230-
.cosmosClientBuilder(cosmosClientBuilder)
231-
.database(getDatabaseName())
232-
.build();
233225
}
234226

235227
@Override
236228
public CosmosConfig cosmosConfig() {
237229
return CosmosConfig.builder()
238-
.enableQueryMetrics(queryMetricsEnabled)
239-
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
240-
.build();
230+
.enableQueryMetrics(queryMetricsEnabled)
231+
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
232+
.build();
241233
}
242234
```
243235
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.
@@ -413,7 +405,7 @@ You can put different database entities into different packages.
413405
### Setup configuration
414406
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.
415407
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.
416-
<!-- embedme src/samples/java/com/azure/cosmos/multidatasource/DatabaseConfiguration.java#L35-L138 -->
408+
<!-- embedme src/samples/java/com/azure/cosmos/multidatasource/DatabaseConfiguration.java#L34-L131 -->
417409

418410
```java
419411
@Configuration
@@ -447,23 +439,17 @@ public class DatabaseConfiguration extends AbstractCosmosConfiguration {
447439
private IsNewAwareAuditingHandler cosmosAuditingHandler;
448440

449441
@Bean
450-
public CosmosClientConfig cosmosClientConfig() {
451-
CosmosClientConfig cosmosClientConfig = CosmosClientConfig.builder()
452-
.cosmosClientBuilder(new CosmosClientBuilder()
453-
.key(primaryProperties.getKey()).endpoint(primaryProperties.getUri()))
454-
.database(primaryProperties.getDatabase())
455-
.build();
456-
457-
return cosmosClientConfig;
442+
public CosmosClientBuilder cosmosClientBuilder() {
443+
return new CosmosClientBuilder()
444+
.key(primaryProperties.getKey())
445+
.endpoint(primaryProperties.getUri());
458446
}
447+
459448
@Bean
460-
public CosmosClientConfig secondaryCosmosClientConfig() {
461-
CosmosClientConfig cosmosClientConfig = CosmosClientConfig.builder()
462-
.cosmosClientBuilder(new CosmosClientBuilder()
463-
.key(secondaryProperties.getKey()).endpoint(secondaryProperties.getUri()))
464-
.database(secondaryProperties.getDatabase())
465-
.build();
466-
return cosmosClientConfig;
449+
public CosmosClientBuilder secondaryCosmosClientBuilder() {
450+
return new CosmosClientBuilder()
451+
.key(secondaryProperties.getKey())
452+
.endpoint(secondaryProperties.getUri());
467453
}
468454
// -------------------------First Cosmos Client for First Cosmos Account---------------------------
469455
@EnableReactiveCosmosRepositories(basePackages = "com.azure.cosmos.multidatasource.primarydatasource.first")
@@ -480,16 +466,16 @@ public class DatabaseConfiguration extends AbstractCosmosConfiguration {
480466

481467
// -------------------------Second Cosmos Client for Secondary Cosmos Account---------------------------
482468
@Bean("secondaryCosmosAsyncClient")
483-
public CosmosAsyncClient getCosmosAsyncClient(CosmosClientConfig secondaryCosmosClientConfig) {
484-
return CosmosFactory.createCosmosAsyncClient(secondaryCosmosClientConfig);
469+
public CosmosAsyncClient getCosmosAsyncClient(CosmosClientBuilder secondaryCosmosClientBuilder) {
470+
return CosmosFactory.createCosmosAsyncClient(secondaryCosmosClientBuilder);
485471
}
486472

487473
@Bean("secondaryCosmosConfig")
488474
public CosmosConfig getCosmosConfig() {
489475
return CosmosConfig.builder()
490-
.enableQueryMetrics(true)
491-
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
492-
.build();
476+
.enableQueryMetrics(true)
477+
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
478+
.build();
493479
}
494480

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

525511
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:
526512

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

529515
```java
530516
@Bean("secondaryCosmosAsyncClient")
531-
public CosmosAsyncClient getCosmosAsyncClient(CosmosClientConfig secondaryCosmosClientConfig) {
532-
return CosmosFactory.createCosmosAsyncClient(secondaryCosmosClientConfig);
517+
public CosmosAsyncClient getCosmosAsyncClient(CosmosClientBuilder secondaryCosmosClientBuilder) {
518+
return CosmosFactory.createCosmosAsyncClient(secondaryCosmosClientBuilder);
533519
}
534520
```
535521

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

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

540526
```java
541527
@Bean("secondaryCosmosConfig")
542528
public CosmosConfig getCosmosConfig() {
543529
return CosmosConfig.builder()
544-
.enableQueryMetrics(true)
545-
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
546-
.build();
530+
.enableQueryMetrics(true)
531+
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
532+
.build();
547533
}
548534
```
549535

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/CosmosFactory.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.azure.cosmos.CosmosAsyncClient;
77
import com.azure.cosmos.CosmosClientBuilder;
88
import com.azure.spring.data.cosmos.common.PropertyLoader;
9-
import com.azure.spring.data.cosmos.config.CosmosClientConfig;
109
import org.apache.commons.lang3.reflect.FieldUtils;
1110
import org.slf4j.Logger;
1211
import org.slf4j.LoggerFactory;
@@ -66,24 +65,22 @@ public String getDatabaseName() {
6665
/**
6766
* Create Cosmos Async Client
6867
*
69-
* @param config CosmosClientConfig
68+
* @param cosmosClientBuilder CosmosClientBuilder
7069
* @return CosmosAsyncClient
7170
*/
72-
public static CosmosAsyncClient createCosmosAsyncClient(CosmosClientConfig config) {
73-
final CosmosClientBuilder cosmosClientBuilder = getCosmosClientBuilderFromConfig(config);
74-
return cosmosClientBuilder.buildAsyncClient();
71+
public static CosmosAsyncClient createCosmosAsyncClient(CosmosClientBuilder cosmosClientBuilder) {
72+
return updateCosmosClientBuilderWithUASuffix(cosmosClientBuilder).buildAsyncClient();
7573
}
7674

77-
private static CosmosClientBuilder getCosmosClientBuilderFromConfig(CosmosClientConfig cosmosClientConfig) {
78-
final CosmosClientBuilder cosmosClientBuilder = cosmosClientConfig.getCosmosClientBuilder();
75+
private static CosmosClientBuilder updateCosmosClientBuilderWithUASuffix(CosmosClientBuilder cosmosClientBuilder) {
7976
cosmosClientBuilder.contentResponseOnWriteEnabled(true);
8077
final String userAgentSuffixValue = getUserAgentSuffixValue(cosmosClientBuilder);
8178
String userAgentSuffix = getUserAgentSuffix();
8279
if (!userAgentSuffixValue.contains(userAgentSuffix)) {
8380
userAgentSuffix += userAgentSuffixValue;
8481
}
8582

86-
return cosmosClientConfig.getCosmosClientBuilder().userAgentSuffix(userAgentSuffix);
83+
return cosmosClientBuilder.userAgentSuffix(userAgentSuffix);
8784
}
8885

8986
private static String getUserAgentSuffixValue(CosmosClientBuilder cosmosClientBuilder) {

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/config/AbstractCosmosConfiguration.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.azure.spring.data.cosmos.config;
55

66
import com.azure.cosmos.CosmosAsyncClient;
7+
import com.azure.cosmos.CosmosClientBuilder;
78
import com.azure.spring.data.cosmos.Constants;
89
import com.azure.spring.data.cosmos.CosmosFactory;
910
import com.azure.spring.data.cosmos.core.CosmosTemplate;
@@ -39,23 +40,21 @@ public CosmosFactory cosmosFactory(CosmosAsyncClient cosmosAsyncClient) {
3940
*
4041
* @param cosmosMappingContext cosmosMappingContext
4142
* @return MappingCosmosConverter bean
42-
* @throws ClassNotFoundException if the class type is invalid
4343
*/
4444
@Bean
45-
public MappingCosmosConverter mappingCosmosConverter(CosmosMappingContext cosmosMappingContext)
46-
throws ClassNotFoundException {
45+
public MappingCosmosConverter mappingCosmosConverter(CosmosMappingContext cosmosMappingContext) {
4746
return new MappingCosmosConverter(cosmosMappingContext, objectMapper);
4847
}
4948

5049
/**
5150
* Declare CosmosAsyncClient bean.
5251
*
53-
* @param cosmosClientConfig CosmosClientConfig
52+
* @param cosmosClientBuilder cosmosClientBuilder
5453
* @return CosmosAsyncClient bean
5554
*/
5655
@Bean
57-
public CosmosAsyncClient cosmosAsyncClient(CosmosClientConfig cosmosClientConfig) {
58-
return CosmosFactory.createCosmosAsyncClient(cosmosClientConfig);
56+
public CosmosAsyncClient cosmosAsyncClient(CosmosClientBuilder cosmosClientBuilder) {
57+
return CosmosFactory.createCosmosAsyncClient(cosmosClientBuilder);
5958
}
6059

6160
@Qualifier(Constants.OBJECT_MAPPER_BEAN_NAME)

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/config/CosmosClientConfig.java

Lines changed: 0 additions & 105 deletions
This file was deleted.

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/config/CosmosConfig.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public class CosmosConfig {
2222
* @param queryMetricsEnabled must not be {@literal null}
2323
*/
2424
@ConstructorProperties({"responseDiagnosticsProcessor", "queryMetricsEnabled"})
25-
public CosmosConfig(ResponseDiagnosticsProcessor responseDiagnosticsProcessor, boolean queryMetricsEnabled) {
25+
public CosmosConfig(ResponseDiagnosticsProcessor responseDiagnosticsProcessor,
26+
boolean queryMetricsEnabled) {
2627
this.responseDiagnosticsProcessor = responseDiagnosticsProcessor;
2728
this.queryMetricsEnabled = queryMetricsEnabled;
2829
}
@@ -63,6 +64,7 @@ public static class CosmosConfigBuilder {
6364
CosmosConfigBuilder() {
6465
}
6566

67+
6668
/**
6769
* Set responseDiagnosticsProcessor
6870
*

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/generator/AbstractQueryGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import com.azure.cosmos.models.SqlParameter;
66
import com.azure.cosmos.models.SqlQuerySpec;
7+
import com.azure.spring.data.cosmos.core.query.CosmosQuery;
78
import com.azure.spring.data.cosmos.core.query.Criteria;
89
import com.azure.spring.data.cosmos.core.query.CriteriaType;
9-
import com.azure.spring.data.cosmos.core.query.CosmosQuery;
1010
import com.azure.spring.data.cosmos.exception.IllegalQueryException;
1111
import org.javatuples.Pair;
1212
import org.springframework.data.domain.Sort;

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/repository/query/CosmosQueryCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import com.azure.spring.data.cosmos.Constants;
66
import com.azure.spring.data.cosmos.core.mapping.CosmosPersistentProperty;
7+
import com.azure.spring.data.cosmos.core.query.CosmosQuery;
78
import com.azure.spring.data.cosmos.core.query.Criteria;
89
import com.azure.spring.data.cosmos.core.query.CriteriaType;
9-
import com.azure.spring.data.cosmos.core.query.CosmosQuery;
1010
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation;
1111
import org.springframework.data.domain.Sort;
1212
import org.springframework.data.mapping.context.MappingContext;

0 commit comments

Comments
 (0)