Skip to content

Commit d045e5e

Browse files
committed
DATACOUCH-963 - Scope and Collection API.
Draft of changes for scope and collection API to share with Michael Nitschinger. Some of the mechanisms in this change-set have been ruled out (the proliferation of all the method signatures, for instance). Closes #963. Original pull request: #1071.
1 parent c303bdc commit d045e5e

File tree

85 files changed

+3095
-695
lines changed

Some content is hidden

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

85 files changed

+3095
-695
lines changed

src/main/java/org/springframework/data/couchbase/SimpleCouchbaseClientFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
2929
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
3030
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
31+
import org.springframework.data.couchbase.core.support.PseudoArgs;
3132
import org.springframework.data.mapping.context.MappingContext;
3233
import org.springframework.lang.Nullable;
3334

@@ -60,14 +61,14 @@ public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final Couch
6061
this.converter = converter;
6162
this.templateSupport = new CouchbaseTemplateSupport(converter, translationService);
6263
this.reactiveCouchbaseTemplate = new ReactiveCouchbaseTemplate(clientFactory, converter, translationService);
63-
64+
6465
this.mappingContext = this.converter.getMappingContext();
65-
if (mappingContext instanceof CouchbaseMappingContext) {
66-
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
67-
if (cmc.isAutoIndexCreation()) {
68-
indexCreator = new CouchbasePersistentEntityIndexCreator(cmc, this);
69-
}
70-
}
66+
if (mappingContext instanceof CouchbaseMappingContext) {
67+
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
68+
if (cmc.isAutoIndexCreation()) {
69+
indexCreator = new CouchbasePersistentEntityIndexCreator(cmc, this);
70+
}
71+
}
7172
}
7273

7374
@Override
@@ -180,4 +181,12 @@ private void prepareIndexCreator(final ApplicationContext context) {
180181
}
181182
}
182183
}
184+
185+
/**
186+
* {@inheritDoc}
187+
*/
188+
public void setThreadLocalArgs(PseudoArgs pseudoArgs) {
189+
reactiveCouchbaseTemplate.setThreadLocalArgs(pseudoArgs);
190+
}
191+
183192
}

src/main/java/org/springframework/data/couchbase/core/ExecutableExistsByIdOperation.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,12 @@
1818
import java.util.Collection;
1919
import java.util.Map;
2020

21+
import org.springframework.data.couchbase.core.support.InCollection;
22+
import org.springframework.data.couchbase.core.support.InScope;
2123
import org.springframework.data.couchbase.core.support.OneAndAllExists;
22-
import org.springframework.data.couchbase.core.support.WithCollection;
24+
import org.springframework.data.couchbase.core.support.WithExistsOptions;
25+
26+
import com.couchbase.client.java.kv.ExistsOptions;
2327

2428
public interface ExecutableExistsByIdOperation {
2529

@@ -48,16 +52,18 @@ interface TerminatingExistsById extends OneAndAllExists {
4852

4953
}
5054

51-
interface ExistsByIdWithCollection extends TerminatingExistsById, WithCollection {
55+
interface ExistsByIdWithOptions<T> extends TerminatingExistsById, WithExistsOptions<T> {
56+
TerminatingExistsById withOptions(ExistsOptions options);
57+
}
5258

53-
/**
54-
* Allows to specify a different collection than the default one configured.
55-
*
56-
* @param collection the collection to use in this scope.
57-
*/
58-
TerminatingExistsById inCollection(String collection);
59+
interface ExistsByIdInCollection<T> extends ExistsByIdWithOptions<T>, InCollection<T> {
60+
ExistsByIdWithOptions<T> inCollection(String collection);
61+
}
62+
63+
interface ExistsByIdInScope<T> extends ExistsByIdInCollection<T>, InScope<T> {
64+
ExistsByIdInCollection<T> inScope(String scope);
5965
}
6066

61-
interface ExecutableExistsById extends ExistsByIdWithCollection {}
67+
interface ExecutableExistsById extends ExistsByIdInScope {}
6268

6369
}

src/main/java/org/springframework/data/couchbase/core/ExecutableExistsByIdOperationSupport.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,12 +15,12 @@
1515
*/
1616
package org.springframework.data.couchbase.core;
1717

18-
import org.springframework.data.couchbase.core.ReactiveExistsByIdOperationSupport.ReactiveExistsByIdSupport;
19-
2018
import java.util.Collection;
2119
import java.util.Map;
2220

23-
import org.springframework.util.Assert;
21+
import org.springframework.data.couchbase.core.ReactiveExistsByIdOperationSupport.ReactiveExistsByIdSupport;
22+
23+
import com.couchbase.client.java.kv.ExistsOptions;
2424

2525
public class ExecutableExistsByIdOperationSupport implements ExecutableExistsByIdOperation {
2626

@@ -32,17 +32,25 @@ public class ExecutableExistsByIdOperationSupport implements ExecutableExistsByI
3232

3333
@Override
3434
public ExecutableExistsById existsById() {
35-
return new ExecutableExistsByIdSupport(template, null);
35+
return new ExecutableExistsByIdSupport(template, null, null, null);
3636
}
3737

3838
static class ExecutableExistsByIdSupport implements ExecutableExistsById {
3939

4040
private final CouchbaseTemplate template;
41+
private final String scope;
42+
private final String collection;
43+
private final ExistsOptions options;
44+
4145
private final ReactiveExistsByIdSupport reactiveSupport;
4246

43-
ExecutableExistsByIdSupport(final CouchbaseTemplate template, final String collection) {
47+
ExecutableExistsByIdSupport(final CouchbaseTemplate template, final String scope, final String collection,
48+
final ExistsOptions options) {
4449
this.template = template;
45-
this.reactiveSupport = new ReactiveExistsByIdSupport(template.reactive(), collection);
50+
this.scope = scope;
51+
this.collection = collection;
52+
this.options = options;
53+
this.reactiveSupport = new ReactiveExistsByIdSupport(template.reactive(), scope, collection, options);
4654
}
4755

4856
@Override
@@ -56,11 +64,19 @@ public Map<String, Boolean> all(final Collection<String> ids) {
5664
}
5765

5866
@Override
59-
public TerminatingExistsById inCollection(final String collection) {
60-
Assert.hasText(collection, "Collection must not be null nor empty.");
61-
return new ExecutableExistsByIdSupport(template, collection);
67+
public ExistsByIdWithOptions inCollection(final String collection) {
68+
return new ExecutableExistsByIdSupport(template, scope, collection, options);
6269
}
6370

71+
@Override
72+
public TerminatingExistsById withOptions(ExistsOptions options) {
73+
return new ExecutableExistsByIdSupport(template, scope, collection, options);
74+
}
75+
76+
@Override
77+
public ExistsByIdInCollection inScope(String scope) {
78+
return new ExecutableExistsByIdSupport(template, scope, collection, options);
79+
}
6480
}
6581

6682
}

src/main/java/org/springframework/data/couchbase/core/ExecutableFindByAnalyticsOperation.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,11 +21,15 @@
2121

2222
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2323
import org.springframework.data.couchbase.core.query.AnalyticsQuery;
24+
import org.springframework.data.couchbase.core.support.InCollection;
25+
import org.springframework.data.couchbase.core.support.InScope;
2426
import org.springframework.data.couchbase.core.support.OneAndAll;
2527
import org.springframework.data.couchbase.core.support.WithAnalyticsConsistency;
28+
import org.springframework.data.couchbase.core.support.WithAnalyticsOptions;
2629
import org.springframework.data.couchbase.core.support.WithAnalyticsQuery;
2730
import org.springframework.lang.Nullable;
2831

32+
import com.couchbase.client.java.analytics.AnalyticsOptions;
2933
import com.couchbase.client.java.analytics.AnalyticsScanConsistency;
3034

3135
public interface ExecutableFindByAnalyticsOperation {
@@ -117,8 +121,20 @@ interface FindByAnalyticsWithQuery<T> extends TerminatingFindByAnalytics<T>, Wit
117121

118122
}
119123

124+
interface FindByAnalyticsWithOptions<T> extends FindByAnalyticsWithQuery<T>, WithAnalyticsOptions<T> {
125+
FindByAnalyticsWithQuery<T> withOptions(AnalyticsOptions options);
126+
}
127+
128+
interface FindByAnalyticsInCollection<T> extends FindByAnalyticsWithOptions<T>, InCollection<T> {
129+
FindByAnalyticsWithOptions<T> inCollection(String collection);
130+
}
131+
132+
interface FindByAnalyticsInScope<T> extends FindByAnalyticsInCollection<T>, InScope<T> {
133+
FindByAnalyticsInCollection<T> inScope(String scope);
134+
}
135+
120136
@Deprecated
121-
interface FindByAnalyticsConsistentWith<T> extends FindByAnalyticsWithQuery<T> {
137+
interface FindByAnalyticsConsistentWith<T> extends FindByAnalyticsInScope<T> {
122138

123139
/**
124140
* Allows to override the default scan consistency.
@@ -141,6 +157,22 @@ interface FindByAnalyticsWithConsistency<T> extends FindByAnalyticsConsistentWit
141157

142158
}
143159

144-
interface ExecutableFindByAnalytics<T> extends FindByAnalyticsWithConsistency<T> {}
160+
/**
161+
* Result type override (Optional).
162+
*/
163+
interface FindByAnalyticsWithProjection<T> extends FindByAnalyticsWithConsistency<T> {
164+
165+
/**
166+
* Define the target type fields should be mapped to. <br />
167+
* Skip this step if you are anyway only interested in the original domain type.
168+
*
169+
* @param returnType must not be {@literal null}.
170+
* @return new instance of {@link ExecutableFindByQueryOperation.FindByQueryWithProjection}.
171+
* @throws IllegalArgumentException if returnType is {@literal null}.
172+
*/
173+
<R> FindByAnalyticsWithConsistency<R> as(Class<R> returnType);
174+
}
175+
176+
interface ExecutableFindByAnalytics<T> extends FindByAnalyticsWithProjection<T> {}
145177

146178
}

src/main/java/org/springframework/data/couchbase/core/ExecutableFindByAnalyticsOperationSupport.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2021 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,9 @@
2121
import org.springframework.data.couchbase.core.ReactiveFindByAnalyticsOperationSupport.ReactiveFindByAnalyticsSupport;
2222
import org.springframework.data.couchbase.core.query.AnalyticsQuery;
2323

24+
import com.couchbase.client.java.analytics.AnalyticsOptions;
2425
import com.couchbase.client.java.analytics.AnalyticsScanConsistency;
26+
import org.springframework.util.Assert;
2527

2628
public class ExecutableFindByAnalyticsOperationSupport implements ExecutableFindByAnalyticsOperation {
2729

@@ -35,26 +37,34 @@ public ExecutableFindByAnalyticsOperationSupport(final CouchbaseTemplate templat
3537

3638
@Override
3739
public <T> ExecutableFindByAnalytics<T> findByAnalytics(final Class<T> domainType) {
38-
return new ExecutableFindByAnalyticsSupport<>(template, domainType, ALL_QUERY,
39-
AnalyticsScanConsistency.NOT_BOUNDED);
40+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, domainType, ALL_QUERY, null, null, null, null);
4041
}
4142

4243
static class ExecutableFindByAnalyticsSupport<T> implements ExecutableFindByAnalytics<T> {
4344

4445
private final CouchbaseTemplate template;
45-
private final Class<T> domainType;
46+
private final Class<?> domainType;
47+
private final Class<T> returnType;
4648
private final ReactiveFindByAnalyticsSupport<T> reactiveSupport;
4749
private final AnalyticsQuery query;
4850
private final AnalyticsScanConsistency scanConsistency;
51+
private final String scope;
52+
private final String collection;
53+
private final AnalyticsOptions options;
4954

50-
ExecutableFindByAnalyticsSupport(final CouchbaseTemplate template, final Class<T> domainType,
51-
final AnalyticsQuery query, final AnalyticsScanConsistency scanConsistency) {
55+
ExecutableFindByAnalyticsSupport(final CouchbaseTemplate template, final Class<?> domainType,
56+
final Class<T> returnType, final AnalyticsQuery query, final AnalyticsScanConsistency scanConsistency,
57+
final String scope, final String collection, final AnalyticsOptions options) {
5258
this.template = template;
5359
this.domainType = domainType;
60+
this.returnType = returnType;
5461
this.query = query;
55-
this.reactiveSupport = new ReactiveFindByAnalyticsSupport<>(template.reactive(), domainType, query,
56-
scanConsistency);
62+
this.reactiveSupport = new ReactiveFindByAnalyticsSupport<>(template.reactive(), domainType, returnType, query,
63+
scanConsistency, scope, collection, options);
5764
this.scanConsistency = scanConsistency;
65+
this.scope = scope;
66+
this.collection = collection;
67+
this.options = options;
5868
}
5969

6070
@Override
@@ -74,18 +84,46 @@ public List<T> all() {
7484

7585
@Override
7686
public TerminatingFindByAnalytics<T> matching(final AnalyticsQuery query) {
77-
return new ExecutableFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
87+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
88+
collection, options);
89+
}
90+
91+
@Override
92+
public FindByAnalyticsWithQuery<T> withOptions(AnalyticsOptions options) {
93+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
94+
collection, options);
95+
}
96+
97+
@Override
98+
public FindByAnalyticsInCollection<T> inScope(String scope) {
99+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
100+
collection, options);
101+
}
102+
103+
@Override
104+
public FindByAnalyticsWithConsistency<T> inCollection(final String collection) {
105+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
106+
collection, options);
78107
}
79108

80109
@Override
81110
@Deprecated
82111
public FindByAnalyticsWithQuery<T> consistentWith(final AnalyticsScanConsistency scanConsistency) {
83-
return new ExecutableFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
112+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
113+
collection, options);
84114
}
85115

86116
@Override
87117
public FindByAnalyticsWithConsistency<T> withConsistency(final AnalyticsScanConsistency scanConsistency) {
88-
return new ExecutableFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
118+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
119+
collection, options);
120+
}
121+
122+
@Override
123+
public <R> FindByAnalyticsWithConsistency<R> as(final Class<R> returnType) {
124+
Assert.notNull(returnType, "returnType must not be null!");
125+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
126+
collection, options);
89127
}
90128

91129
@Override

0 commit comments

Comments
 (0)