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

Commit

Permalink
Full Reactive Repository Support, Rebranding DocumentDb to CosmosDb (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
kushagraThapar authored Sep 27, 2019
1 parent 6c20721 commit d028e00
Show file tree
Hide file tree
Showing 132 changed files with 2,182 additions and 1,590 deletions.
2 changes: 1 addition & 1 deletion .codacy.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exclude_paths:
- 'src/test/java/com/microsoft/azure/spring/data/cosmosdb/domain/Person.java'
- 'src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/DocumentDbEntityInformationUnitTest.java'
- 'src/test/java/com/microsoft/azure/spring/data/cosmosdb/repository/support/CosmosEntityInformationUnitTest.java'
44 changes: 26 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Please refer to [sample project here](./samplecode).
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.

## Feature List
- Spring Data CRUDRepository basic CRUD functionality
- Spring Data ReactiveCrudRepository CrudRepository basic CRUD functionality
- save
- findAll
- findOne by Id
Expand Down Expand Up @@ -73,7 +73,7 @@ If you are using Maven, add the following dependency.
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb</artifactId>
<version>2.1.7</version>
<version>2.2.0.M1</version>
</dependency>
```

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

### Sync and Reactive Repository support
2.2.x supports both sync and reactive repository support.

Use `@EnableCosmosRepositories` to enable sync repository support.

For reactive repository support, use `@EnableReactiveCosmosRepositories`

```java
@Configuration
@EnableDocumentDbRepositories
public class AppConfiguration extends AbstractDocumentDbConfiguration {
@EnableCosmosRepositories
public class AppConfiguration extends AbstractCosmosConfiguration {

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

private CosmosKeyCredential cosmosKeyCredential;

public DocumentDBConfig getConfig() {
public CosmosDBConfig getConfig() {
this.cosmosKeyCredential = new CosmosKeyCredential(key);
return DocumentDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
return CosmosDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
}

public void switchToSecondaryKey() {
Expand All @@ -114,19 +121,19 @@ public class AppConfiguration extends AbstractDocumentDbConfiguration {
```
Or if you want to customize your config:
```java
public DocumentDBConfig getConfig() {
public CosmosDBConfig getConfig() {
this.cosmosKeyCredential = new CosmosKeyCredential(key);
DocumentDBConfig dbConfig = DocumentDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
dbConfig.getConnectionPolicy().setConnectionMode(ConnectionMode.DirectHttps);
dbConfig.getConnectionPolicy().setMaxPoolSize(1000);
return dbConfig;
CosmosDBConfig cosmosDbConfig = CosmosDBConfig.builder(uri, this.cosmosKeyCredential, dbName).build();
cosmosDbConfig.getConnectionPolicy().setConnectionMode(ConnectionMode.DIRECT);
cosmosDbConfig.getConnectionPolicy().setMaxPoolSize(1000);
return cosmosDbConfig;
}
```
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.
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.
```java
@Configuration
@EnableDocumentDbRepositories(basePackageClass=UserRepository.class)
public class AppConfiguration extends AbstractDocumentDbConfiguration {
@EnableCosmosRepositories(basePackageClass=UserRepository.class)
public class AppConfiguration extends AbstractCosmosConfiguration {
// configuration code
}
```
Expand All @@ -140,6 +147,7 @@ Define a simple entity as Document in Azure Cosmos DB.
public class User {
private String id;
private String firstName;

@PartitionKey
private String lastName;

Expand Down Expand Up @@ -178,14 +186,14 @@ public class User {
```

### Create repositories
Extends DocumentDbRepository interface, which provides Spring Data repository support.
Extends CosmosRepository interface, which provides Spring Data repository support.

```java
import DocumentDbRepository;
import CosmosRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends DocumentDbRepository<User, String> {
public interface UserRepository extends CosmosRepository<User, String> {
List<User> findByFirstName(String firstName);
}
```
Expand Down Expand Up @@ -232,7 +240,7 @@ public class SampleApplication implements CommandLineRunner {
}
}
```
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.
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.

## Snapshots
[![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/)
Expand Down
32 changes: 18 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb</artifactId>
<version>2.1.8-SNAPSHOT</version>
<version>2.2.0.M1</version>

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

<spring.springframework.version>5.1.9.RELEASE</spring.springframework.version>
<spring.data.version>2.1.10.RELEASE</spring.data.version>
<spring.springframework.version>5.2.0.RC2</spring.springframework.version>
<spring.data.version>2.2.0.RC3</spring.data.version>
<fasterxml.jackson.version>2.9.5</fasterxml.jackson.version>

<mockito.core.version>2.8.9</mockito.core.version>
Expand All @@ -58,15 +58,22 @@
<gson.version>2.8.4</gson.version>
<project.reactor.test.version>3.2.5.RELEASE</project.reactor.test.version>

<azure.documentdb.version>1.16.2</azure.documentdb.version>
<azure.cosmos.version>3.1.0</azure.cosmos.version>
<azure.cosmos.version>3.2.0</azure.cosmos.version>
<azure.test.resourcegroup>spring-data-cosmosdb-test</azure.test.resourcegroup>
<azure.test.dbname>testdb-${maven.build.timestamp}</azure.test.dbname>
<skip.integration.tests>true</skip.integration.tests>
<test.on.azure>false</test.on.azure>
<test.on.emualator>false</test.on.emualator>
</properties>

<repositories>
<repository>
<id>spring-milestone</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
Expand Down Expand Up @@ -115,13 +122,6 @@
<artifactId>spring-expression</artifactId>
<version>${spring.springframework.version}</version>
</dependency>

<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>${azure.documentdb.version}</version>
</dependency>

<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-cosmos</artifactId>
Expand All @@ -143,7 +143,11 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${fasterxml.jackson.version}</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -232,7 +236,7 @@
<failOnError>false</failOnError>
<sourceFileExcludes>
<exclude>
com/microsoft/azure/spring/data/cosmosdb/documentdb/core/mapping/BasicDocumentDbPersistentProperty.java
com/microsoft/azure/spring/data/cosmosdb/documentdb/core/mapping/BasicCosmosPersistentProperty.java
</exclude>
</sourceFileExcludes>
</configuration>
Expand Down
4 changes: 2 additions & 2 deletions samplecode/example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb-samples</artifactId>
<version>2.1.8-SNAPSHOT</version>
<version>2.2.0.M1</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-data-cosmosdb</artifactId>
<version>2.1.8-SNAPSHOT</version>
<version>2.2.0.M1</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
*/
package example.springdata.cosmosdb;

import com.microsoft.azure.spring.data.cosmosdb.core.query.DocumentDbPageRequest;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

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

@Override
public void run(String... args) throws Exception {
printList(this.repository.findByEmailOrName(this.user_1.getEmail(), this.user_1.getName()));
printList(this.repository.findByEmailOrName(this.user_1.getEmail(),
this.user_1.getName()).collectList().block());

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

printList(this.repository.findByNameIn(Arrays.asList(this.user_1.getName(), "fake-name")));

queryByPageable();
}

private void queryByPageable() {
final int pageSize = 2;
final Pageable pageable = new DocumentDbPageRequest(0, pageSize, null);
final Page<User> page = this.repository.findByAddress(address, pageable);
System.out.println("***** Printing Page 1 *****");
printList(page.getContent());

final Page<User> nextPage = this.repository.findByAddress(address, page.getPageable());
System.out.println("***** Printing Page 2 *****");
printList(nextPage.getContent());
printList(this.repository.findByNameIn(Arrays.asList(this.user_1.getName(),
"fake-name")).collectList().block());
}

@PostConstruct
public void setup() {
this.repository.save(user_1);
this.repository.save(user_2);
this.repository.save(user_3);
this.repository.save(user_1).block();
this.repository.saveAll(Lists.newArrayList(user_2, user_3)).collectList().block();
}

@PreDestroy
public void cleanup() {
this.repository.deleteAll();
this.repository.deleteAll().block();
}

private void printList(List<User> users) {
users.forEach(user -> System.out.println(user));
users.forEach(System.out::println);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Getter
@Setter
@ConfigurationProperties(prefix = "azure.cosmosdb")
public class DocumentDbProperties {
public class CosmosDbProperties {

private String uri;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package example.springdata.cosmosdb;

import com.microsoft.azure.spring.data.cosmosdb.core.mapping.Document;
import com.microsoft.azure.spring.data.cosmosdb.core.mapping.PartitionKey;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -37,6 +38,7 @@ public class User {

private String email;

@PartitionKey
private String name;

private Long count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,28 @@

package example.springdata.cosmosdb;

import com.microsoft.azure.spring.data.cosmosdb.repository.DocumentDbRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.microsoft.azure.spring.data.cosmosdb.repository.ReactiveCosmosRepository;
import org.springframework.data.domain.Sort;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;

import java.util.Collection;
import java.util.List;

@Repository
@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserRepository extends DocumentDbRepository<User, String> {
public interface UserRepository extends ReactiveCosmosRepository<User, String> {

List<User> findByName(String firstName);
Flux<User> findByName(String firstName);

List<User> findByEmailAndAddress(String email, Address address);
Flux<User> findByEmailAndAddress(String email, Address address);

List<User> findByEmailOrName(String email, String Name);
Flux<User> findByEmailOrName(String email, String name);

List<User> findByCount(Long count, Sort sort);
Flux<User> findByCount(Long count, Sort sort);

List<User> findByNameIn(Collection<String> names);
Flux<User> findByNameIn(Collection<String> names);

Page<User> findByAddress(Address address, Pageable pageable);
Flux<User> findByAddress(Address address);
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package example.springdata.cosmosdb;

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


@Configuration
@EnableDocumentDbRepositories
@EnableConfigurationProperties(DocumentDbProperties.class)
@EnableConfigurationProperties(CosmosDbProperties.class)
@EnableReactiveCosmosRepositories
@PropertySource("classpath:application.properties")
public class UserRepositoryConfiguration extends AbstractDocumentDbConfiguration {
public class UserRepositoryConfiguration extends AbstractCosmosConfiguration {

@Autowired
private DocumentDbProperties properties;
private CosmosDbProperties properties;

private CosmosKeyCredential cosmosKeyCredential;

@Bean
public DocumentDBConfig documentDBConfig() {
public CosmosDBConfig cosmosDbConfig() {
this.cosmosKeyCredential = new CosmosKeyCredential(properties.getKey());
return DocumentDBConfig.builder(properties.getUri(), cosmosKeyCredential, properties.getDatabase()).build();
return CosmosDBConfig.builder(properties.getUri(), cosmosKeyCredential, properties.getDatabase()).build();
}

public void switchToSecondaryKey() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=example.springdata.cosmosdb.UserRepositoryConfiguration
Loading

0 comments on commit d028e00

Please sign in to comment.