Skip to content
This repository was archived by the owner on Aug 29, 2024. It is now read-only.

Commit d028e00

Browse files
Full Reactive Repository Support, Rebranding DocumentDb to CosmosDb (#434)
* Addind reactive repository support * deleteAll fix * Fixing deleteAll issue Rebranding DocumentDB types to Cosmos Removing docdb dependency Upgrading SDK to 3.2 Changing perf tests to use 3.2 sync api Other refactoring * Upgraded pom.xml to use new spring RC versions. Made other changes to support new versions * Updated pom.xml to milestone version * Removing unused imports * Renaming bean method * Renamed documentdb to cosmosdb * updated pom.xml with correct file name * Added new APIs for find by id with partitionkey * Updated cosmos factory to take in connection policy and consistency level in account while creating cosmos client * Removed and implemented some of the TODOs from spring feedback * Updated readme to reflect CosmosRepository * Updated Reactive Cosmos Entity MetaData to be used in Reactive Cosmos Query pipeline * Updated sample to full reactive support. Updated error handlers for reactive cosmos template to throw CosmosDbAccessException
1 parent 6c20721 commit d028e00

File tree

132 files changed

+2182
-1590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+2182
-1590
lines changed

.codacy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
exclude_paths:
22
- 'src/test/java/com/microsoft/azure/spring/data/cosmosdb/domain/Person.java'
3-
- 'src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/DocumentDbEntityInformationUnitTest.java'
3+
- 'src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/CosmosEntityInformationUnitTest.java'

README.md

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Please refer to [sample project here](./samplecode).
2929
This repository supports both Spring Data 1.x and 2.x. Please see [this document](https://github.com/Microsoft/spring-data-cosmosdb/wiki/Spring-Data-dependency-version-management) about more details and corresponding branch mapping.
3030

3131
## Feature List
32-
- Spring Data CRUDRepository basic CRUD functionality
32+
- Spring Data ReactiveCrudRepository CrudRepository basic CRUD functionality
3333
- save
3434
- findAll
3535
- findOne by Id
@@ -73,7 +73,7 @@ If you are using Maven, add the following dependency.
7373
<dependency>
7474
<groupId>com.microsoft.azure</groupId>
7575
<artifactId>spring-data-cosmosdb</artifactId>
76-
<version>2.1.7</version>
76+
<version>2.2.0.M1</version>
7777
</dependency>
7878
```
7979

@@ -83,10 +83,17 @@ Setup configuration class.
8383
CosmosKeyCredential feature provides capability to rotate keys on the fly. You can switch keys using switchToSecondaryKey().
8484
For more information on this, see the Sample Application code.
8585

86+
### Sync and Reactive Repository support
87+
2.2.x supports both sync and reactive repository support.
88+
89+
Use `@EnableCosmosRepositories` to enable sync repository support.
90+
91+
For reactive repository support, use `@EnableReactiveCosmosRepositories`
92+
8693
```java
8794
@Configuration
88-
@EnableDocumentDbRepositories
89-
public class AppConfiguration extends AbstractDocumentDbConfiguration {
95+
@EnableCosmosRepositories
96+
public class AppConfiguration extends AbstractCosmosConfiguration {
9097

9198
@Value("${azure.cosmosdb.uri}")
9299
private String uri;
@@ -102,9 +109,9 @@ public class AppConfiguration extends AbstractDocumentDbConfiguration {
102109

103110
private CosmosKeyCredential cosmosKeyCredential;
104111

105-
public DocumentDBConfig getConfig() {
112+
public CosmosDBConfig getConfig() {
106113
this.cosmosKeyCredential = new CosmosKeyCredential(key);
107-
return DocumentDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
114+
return CosmosDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
108115
}
109116

110117
public void switchToSecondaryKey() {
@@ -114,19 +121,19 @@ public class AppConfiguration extends AbstractDocumentDbConfiguration {
114121
```
115122
Or if you want to customize your config:
116123
```java
117-
public DocumentDBConfig getConfig() {
124+
public CosmosDBConfig getConfig() {
118125
this.cosmosKeyCredential = new CosmosKeyCredential(key);
119-
DocumentDBConfig dbConfig = DocumentDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
120-
dbConfig.getConnectionPolicy().setConnectionMode(ConnectionMode.DirectHttps);
121-
dbConfig.getConnectionPolicy().setMaxPoolSize(1000);
122-
return dbConfig;
126+
CosmosDBConfig cosmosDbConfig = CosmosDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
127+
cosmosDbConfig.getConnectionPolicy().setConnectionMode(ConnectionMode.DIRECT);
128+
cosmosDbConfig.getConnectionPolicy().setMaxPoolSize(1000);
129+
return cosmosDbConfig;
123130
}
124131
```
125-
By default, `@EnableDocumentDbRepositories` 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 type if your project layout has multiple projects and it's not finding your repositories.
132+
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 type if your project layout has multiple projects and it's not finding your repositories.
126133
```java
127134
@Configuration
128-
@EnableDocumentDbRepositories(basePackageClass=UserRepository.class)
129-
public class AppConfiguration extends AbstractDocumentDbConfiguration {
135+
@EnableCosmosRepositories(basePackageClass=UserRepository.class)
136+
public class AppConfiguration extends AbstractCosmosConfiguration {
130137
// configuration code
131138
}
132139
```
@@ -140,6 +147,7 @@ Define a simple entity as Document in Azure Cosmos DB.
140147
public class User {
141148
private String id;
142149
private String firstName;
150+
143151
@PartitionKey
144152
private String lastName;
145153

@@ -178,14 +186,14 @@ public class User {
178186
```
179187

180188
### Create repositories
181-
Extends DocumentDbRepository interface, which provides Spring Data repository support.
189+
Extends CosmosRepository interface, which provides Spring Data repository support.
182190

183191
```java
184-
import DocumentDbRepository;
192+
import CosmosRepository;
185193
import org.springframework.stereotype.Repository;
186194

187195
@Repository
188-
public interface UserRepository extends DocumentDbRepository<User, String> {
196+
public interface UserRepository extends CosmosRepository<User, String> {
189197
List<User> findByFirstName(String firstName);
190198
}
191199
```
@@ -232,7 +240,7 @@ public class SampleApplication implements CommandLineRunner {
232240
}
233241
}
234242
```
235-
Autowired UserRepository interface, then can do save, delete and find operations. Spring Data Azure Cosmos DB uses the DocumentTemplate to execute the queries behind *find*, *save* methods. You can use the template yourself for more complex queries.
243+
Autowired UserRepository interface, then can do save, delete and find operations. Spring Data Azure Cosmos DB uses the CosmosTemplate to execute the queries behind *find*, *save* methods. You can use the template yourself for more complex queries.
236244

237245
## Snapshots
238246
[![Nexus OSS](https://img.shields.io/nexus/snapshots/https/oss.sonatype.org/com.microsoft.azure/spring-data-cosmosdb.svg)](https://oss.sonatype.org/content/repositories/snapshots/com/microsoft/azure/spring-data-cosmosdb/)

pom.xml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.microsoft.azure</groupId>
88
<artifactId>spring-data-cosmosdb</artifactId>
9-
<version>2.1.8-SNAPSHOT</version>
9+
<version>2.2.0.M1</version>
1010

1111
<name>Spring Data for Azure Cosmos DB SQL API</name>
1212
<description>Spring Data for Azure Cosmos DB SQL API</description>
@@ -45,8 +45,8 @@
4545
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4646
<maven.build.timestamp.format>MM-dd-HH-mm-ss</maven.build.timestamp.format>
4747

48-
<spring.springframework.version>5.1.9.RELEASE</spring.springframework.version>
49-
<spring.data.version>2.1.10.RELEASE</spring.data.version>
48+
<spring.springframework.version>5.2.0.RC2</spring.springframework.version>
49+
<spring.data.version>2.2.0.RC3</spring.data.version>
5050
<fasterxml.jackson.version>2.9.5</fasterxml.jackson.version>
5151

5252
<mockito.core.version>2.8.9</mockito.core.version>
@@ -58,15 +58,22 @@
5858
<gson.version>2.8.4</gson.version>
5959
<project.reactor.test.version>3.2.5.RELEASE</project.reactor.test.version>
6060

61-
<azure.documentdb.version>1.16.2</azure.documentdb.version>
62-
<azure.cosmos.version>3.1.0</azure.cosmos.version>
61+
<azure.cosmos.version>3.2.0</azure.cosmos.version>
6362
<azure.test.resourcegroup>spring-data-cosmosdb-test</azure.test.resourcegroup>
6463
<azure.test.dbname>testdb-${maven.build.timestamp}</azure.test.dbname>
6564
<skip.integration.tests>true</skip.integration.tests>
6665
<test.on.azure>false</test.on.azure>
6766
<test.on.emualator>false</test.on.emualator>
6867
</properties>
6968

69+
<repositories>
70+
<repository>
71+
<id>spring-milestone</id>
72+
<name>Spring Milestones</name>
73+
<url>http://repo.spring.io/libs-milestone</url>
74+
</repository>
75+
</repositories>
76+
7077
<dependencies>
7178
<dependency>
7279
<groupId>org.springframework</groupId>
@@ -115,13 +122,6 @@
115122
<artifactId>spring-expression</artifactId>
116123
<version>${spring.springframework.version}</version>
117124
</dependency>
118-
119-
<dependency>
120-
<groupId>com.microsoft.azure</groupId>
121-
<artifactId>azure-documentdb</artifactId>
122-
<version>${azure.documentdb.version}</version>
123-
</dependency>
124-
125125
<dependency>
126126
<groupId>com.microsoft.azure</groupId>
127127
<artifactId>azure-cosmos</artifactId>
@@ -143,7 +143,11 @@
143143
<artifactId>jackson-datatype-jsr310</artifactId>
144144
<version>${fasterxml.jackson.version}</version>
145145
</dependency>
146-
146+
<dependency>
147+
<groupId>org.json</groupId>
148+
<artifactId>json</artifactId>
149+
<version>20140107</version>
150+
</dependency>
147151
<dependency>
148152
<groupId>org.projectlombok</groupId>
149153
<artifactId>lombok</artifactId>
@@ -232,7 +236,7 @@
232236
<failOnError>false</failOnError>
233237
<sourceFileExcludes>
234238
<exclude>
235-
com/microsoft/azure/spring/data/cosmosdb/documentdb/core/mapping/BasicDocumentDbPersistentProperty.java
239+
com/microsoft/azure/spring/data/cosmosdb/documentdb/core/mapping/BasicCosmosPersistentProperty.java
236240
</exclude>
237241
</sourceFileExcludes>
238242
</configuration>

samplecode/example/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.microsoft.azure</groupId>
77
<artifactId>spring-data-cosmosdb-samples</artifactId>
8-
<version>2.1.8-SNAPSHOT</version>
8+
<version>2.2.0.M1</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>com.microsoft.azure</groupId>
2222
<artifactId>spring-data-cosmosdb</artifactId>
23-
<version>2.1.8-SNAPSHOT</version>
23+
<version>2.2.0.M1</version>
2424
</dependency>
2525

2626
<dependency>

samplecode/example/src/main/java/example/springdata/cosmosdb/Application.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
*/
1616
package example.springdata.cosmosdb;
1717

18-
import com.microsoft.azure.spring.data.cosmosdb.core.query.DocumentDbPageRequest;
18+
import org.assertj.core.util.Lists;
1919
import org.springframework.beans.factory.annotation.Autowired;
2020
import org.springframework.boot.CommandLineRunner;
2121
import org.springframework.boot.SpringApplication;
2222
import org.springframework.boot.autoconfigure.SpringBootApplication;
23-
import org.springframework.data.domain.Page;
24-
import org.springframework.data.domain.Pageable;
2523
import org.springframework.data.domain.Sort;
2624

2725
import javax.annotation.PostConstruct;
@@ -67,40 +65,28 @@ public static void main(String... args) {
6765

6866
@Override
6967
public void run(String... args) throws Exception {
70-
printList(this.repository.findByEmailOrName(this.user_1.getEmail(), this.user_1.getName()));
68+
printList(this.repository.findByEmailOrName(this.user_1.getEmail(),
69+
this.user_1.getName()).collectList().block());
7170

72-
printList(this.repository.findByCount(COUNT, Sort.by(new Sort.Order(Sort.Direction.ASC, "count"))));
71+
printList(this.repository.findByCount(COUNT,
72+
Sort.by(new Sort.Order(Sort.Direction.ASC, "count"))).collectList().block());
7373

74-
printList(this.repository.findByNameIn(Arrays.asList(this.user_1.getName(), "fake-name")));
75-
76-
queryByPageable();
77-
}
78-
79-
private void queryByPageable() {
80-
final int pageSize = 2;
81-
final Pageable pageable = new DocumentDbPageRequest(0, pageSize, null);
82-
final Page<User> page = this.repository.findByAddress(address, pageable);
83-
System.out.println("***** Printing Page 1 *****");
84-
printList(page.getContent());
85-
86-
final Page<User> nextPage = this.repository.findByAddress(address, page.getPageable());
87-
System.out.println("***** Printing Page 2 *****");
88-
printList(nextPage.getContent());
74+
printList(this.repository.findByNameIn(Arrays.asList(this.user_1.getName(),
75+
"fake-name")).collectList().block());
8976
}
9077

9178
@PostConstruct
9279
public void setup() {
93-
this.repository.save(user_1);
94-
this.repository.save(user_2);
95-
this.repository.save(user_3);
80+
this.repository.save(user_1).block();
81+
this.repository.saveAll(Lists.newArrayList(user_2, user_3)).collectList().block();
9682
}
9783

9884
@PreDestroy
9985
public void cleanup() {
100-
this.repository.deleteAll();
86+
this.repository.deleteAll().block();
10187
}
10288

10389
private void printList(List<User> users) {
104-
users.forEach(user -> System.out.println(user));
90+
users.forEach(System.out::println);
10591
}
10692
}

samplecode/example/src/main/java/example/springdata/cosmosdb/DocumentDbProperties.java renamed to samplecode/example/src/main/java/example/springdata/cosmosdb/CosmosDbProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@Getter
2323
@Setter
2424
@ConfigurationProperties(prefix = "azure.cosmosdb")
25-
public class DocumentDbProperties {
25+
public class CosmosDbProperties {
2626

2727
private String uri;
2828

samplecode/example/src/main/java/example/springdata/cosmosdb/User.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package example.springdata.cosmosdb;
1818

1919
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.Document;
20+
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey;
2021
import lombok.AllArgsConstructor;
2122
import lombok.Getter;
2223
import lombok.NoArgsConstructor;
@@ -37,6 +38,7 @@ public class User {
3738

3839
private String email;
3940

41+
@PartitionKey
4042
private String name;
4143

4244
private Long count;

samplecode/example/src/main/java/example/springdata/cosmosdb/UserRepository.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,28 @@
1616

1717
package example.springdata.cosmosdb;
1818

19-
import com.microsoft.azure.spring.data.cosmosdb.repository.DocumentDbRepository;
20-
import org.springframework.data.domain.Page;
21-
import org.springframework.data.domain.Pageable;
19+
import com.microsoft.azure.spring.data.cosmosdb.repository.ReactiveCosmosRepository;
2220
import org.springframework.data.domain.Sort;
2321
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
2422
import org.springframework.stereotype.Repository;
23+
import reactor.core.publisher.Flux;
2524

2625
import java.util.Collection;
27-
import java.util.List;
2826

2927
@Repository
3028
@RepositoryRestResource(collectionResourceRel = "user", path = "user")
31-
public interface UserRepository extends DocumentDbRepository<User, String> {
29+
public interface UserRepository extends ReactiveCosmosRepository<User, String> {
3230

33-
List<User> findByName(String firstName);
31+
Flux<User> findByName(String firstName);
3432

35-
List<User> findByEmailAndAddress(String email, Address address);
33+
Flux<User> findByEmailAndAddress(String email, Address address);
3634

37-
List<User> findByEmailOrName(String email, String Name);
35+
Flux<User> findByEmailOrName(String email, String name);
3836

39-
List<User> findByCount(Long count, Sort sort);
37+
Flux<User> findByCount(Long count, Sort sort);
4038

41-
List<User> findByNameIn(Collection<String> names);
39+
Flux<User> findByNameIn(Collection<String> names);
4240

43-
Page<User> findByAddress(Address address, Pageable pageable);
41+
Flux<User> findByAddress(Address address);
4442
}
4543

samplecode/example/src/main/java/example/springdata/cosmosdb/UserRepositoryConfiguration.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
package example.springdata.cosmosdb;
1717

1818
import com.azure.data.cosmos.CosmosKeyCredential;
19-
import com.microsoft.azure.spring.data.cosmosdb.config.AbstractDocumentDbConfiguration;
20-
import com.microsoft.azure.spring.data.cosmosdb.config.DocumentDBConfig;
21-
import com.microsoft.azure.spring.data.cosmosdb.repository.config.EnableDocumentDbRepositories;
19+
import com.microsoft.azure.spring.data.cosmosdb.config.AbstractCosmosConfiguration;
20+
import com.microsoft.azure.spring.data.cosmosdb.config.CosmosDBConfig;
21+
import com.microsoft.azure.spring.data.cosmosdb.repository.config.EnableReactiveCosmosRepositories;
2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2424
import org.springframework.context.annotation.Bean;
@@ -27,20 +27,20 @@
2727

2828

2929
@Configuration
30-
@EnableDocumentDbRepositories
31-
@EnableConfigurationProperties(DocumentDbProperties.class)
30+
@EnableConfigurationProperties(CosmosDbProperties.class)
31+
@EnableReactiveCosmosRepositories
3232
@PropertySource("classpath:application.properties")
33-
public class UserRepositoryConfiguration extends AbstractDocumentDbConfiguration {
33+
public class UserRepositoryConfiguration extends AbstractCosmosConfiguration {
3434

3535
@Autowired
36-
private DocumentDbProperties properties;
36+
private CosmosDbProperties properties;
3737

3838
private CosmosKeyCredential cosmosKeyCredential;
3939

4040
@Bean
41-
public DocumentDBConfig documentDBConfig() {
41+
public CosmosDBConfig cosmosDbConfig() {
4242
this.cosmosKeyCredential = new CosmosKeyCredential(properties.getKey());
43-
return DocumentDBConfig.builder(properties.getUri(), cosmosKeyCredential, properties.getDatabase()).build();
43+
return CosmosDBConfig.builder(properties.getUri(), cosmosKeyCredential, properties.getDatabase()).build();
4444
}
4545

4646
public void switchToSecondaryKey() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=example.springdata.cosmosdb.UserRepositoryConfiguration

0 commit comments

Comments
 (0)