Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding to reactive.
  • Loading branch information
trande4884 committed Dec 19, 2024
commit bd92e3a0653a50a948aae1a510034a3eb7fb7e80
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ Mono<CosmosContainerProperties> replaceContainerProperties(String containerName,
*/
Mono<Long> count(SqlQuerySpec querySpec, String containerName);

/**
* Sum
*
* @param querySpec the document query spec
* @param containerName the container name
* @return sum result
*/
Mono<Long> sum(SqlQuerySpec querySpec, String containerName);

/**
* To get converter
* @return MappingCosmosConverter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ public Mono<Long> count(String containerName) {
@Override
public Mono<Long> count(CosmosQuery query, String containerName) {
final SqlQuerySpec querySpec = new CountQueryGenerator().generateCosmos(query);
return getCountValue(querySpec, containerName);
return getNumericValue(querySpec, containerName);
}

/**
Expand All @@ -932,7 +932,19 @@ public Mono<Long> count(CosmosQuery query, String containerName) {
*/
@Override
public Mono<Long> count(SqlQuerySpec querySpec, String containerName) {
return getCountValue(querySpec, containerName);
return getNumericValue(querySpec, containerName);
}

/**
* Sum
*
* @param querySpec the document query spec
* @param containerName the container name
* @return Mono with sum or error
*/
@Override
public Mono<Long> sum(SqlQuerySpec querySpec, String containerName) {
return getNumericValue(querySpec, containerName);
}

@Override
Expand Down Expand Up @@ -975,7 +987,7 @@ private Flux<JsonNode> runQuery(SqlQuerySpec querySpec, Class<?> domainType) {
this.responseDiagnosticsProcessor));
}

private Mono<Long> getCountValue(SqlQuerySpec querySpec, String containerName) {
private Mono<Long> getNumericValue(SqlQuerySpec querySpec, String containerName) {
final CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
options.setQueryMetricsEnabled(this.queryMetricsEnabled);
options.setIndexMetricsEnabled(this.indexMetricsEnabled);
Expand All @@ -987,7 +999,7 @@ private Mono<Long> getCountValue(SqlQuerySpec querySpec, String containerName) {
.doOnNext(feedResponse -> CosmosUtils.fillAndProcessResponseDiagnostics(this.responseDiagnosticsProcessor,
feedResponse.getCosmosDiagnostics(), feedResponse))
.onErrorResume(throwable ->
CosmosExceptionUtils.exceptionHandler("Failed to get count value", throwable,
CosmosExceptionUtils.exceptionHandler("Failed to get numeric value", throwable,
this.responseDiagnosticsProcessor))
.next()
.map(r -> r.getResults().get(0).asLong());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public Object execute(final Object[] parameters) {
SqlQuerySpec querySpec = new SqlQuerySpec(stripExtraWhitespaceFromString(expandedQuery), sqlParameters);
if (isCountQuery()) {
final String container = ((SimpleReactiveCosmosEntityMetadata<?>) getQueryMethod().getEntityInformation()).getContainerName();
final Mono<Long> mono = this.operations.count(querySpec, container);
return mono;
return this.operations.count(querySpec, container);
} else if (isSumQuery()) {
final String container = ((SimpleReactiveCosmosEntityMetadata<?>) getQueryMethod().getEntityInformation()).getContainerName();
return this.operations.sum(querySpec, container);
} else {
Flux<?> flux = this.operations.runQuery(querySpec, accessor.getSort(), processor.getReturnedType().getDomainType(),
processor.getReturnedType().getReturnedType());
Expand All @@ -123,4 +125,8 @@ protected boolean isCountQuery() {
return StringBasedCosmosQuery.isCountQuery(query, getQueryMethod().getReturnedObjectType());
}

protected boolean isSumQuery() {
return StringBasedCosmosQuery.isSumQuery(query, getQueryMethod().getReturnedObjectType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public class ReactiveLongIdDomainPartitionPartitionRepositoryIT {
private static final Long ID_2 = 67890L;
private static final String NAME_2 = "camille";

private static final Long ID_3 = 98765L;

private static final LongIdDomainPartition DOMAIN_1 = new LongIdDomainPartition(ID_1, NAME_1);
private static final LongIdDomainPartition DOMAIN_2 = new LongIdDomainPartition(ID_2, NAME_2);
private static final LongIdDomainPartition DOMAIN_3 = new LongIdDomainPartition(ID_3, NAME_1);

@ClassRule
public static final ReactiveIntegrationTestCollectionManager collectionManager = new ReactiveIntegrationTestCollectionManager();
Expand Down Expand Up @@ -192,6 +195,18 @@ public void testFindAllSort() {
StepVerifier.create(descAllFlux).expectNext(DOMAIN_2, other, DOMAIN_1).verifyComplete();
}

@Test
public void testSum() {
Mono<Long> sum1 = this.repository.annotatedSumLongIdValuesByName(NAME_1);
StepVerifier.create(sum1).expectNext(12345L).verifyComplete();

Mono<LongIdDomainPartition> saveMono = this.repository.save(DOMAIN_3);
StepVerifier.create(saveMono).expectNext(DOMAIN_3).expectComplete().verify();

Mono<Long> sum2 = this.repository.annotatedSumLongIdValuesByName(NAME_1);
StepVerifier.create(sum2).expectNext(111110L).verifyComplete();
}

private static class InvalidDomain {

private long count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
package com.azure.spring.data.cosmos.repository.repository;

import com.azure.spring.data.cosmos.domain.LongIdDomainPartition;
import com.azure.spring.data.cosmos.repository.Query;
import com.azure.spring.data.cosmos.repository.ReactiveCosmosRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Mono;

@Repository
public interface ReactiveLongIdDomainPartitionRepository extends ReactiveCosmosRepository<LongIdDomainPartition, Long> {

@Query("SELECT VALUE SUM(a.number) from a where a.name = @name")
Mono<Long> annotatedSumLongIdValuesByName(@Param("name") String name);

}
Loading