Skip to content

Commit afe0bd2

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 afe0bd2

File tree

94 files changed

+2501
-576
lines changed

Some content is hidden

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

94 files changed

+2501
-576
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
@@ -74,7 +74,7 @@ private SimpleCouchbaseClientFactory(final Supplier<Cluster> cluster, final Stri
7474

7575
@Override
7676
public CouchbaseClientFactory withScope(final String scopeName) {
77-
return new SimpleCouchbaseClientFactory(cluster, bucket.name(), scopeName);
77+
return new SimpleCouchbaseClientFactory(cluster, bucket.name(), scopeName != null ? scopeName : getScope().name());
7878
}
7979

8080
@Override

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

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

1919
import org.springframework.data.couchbase.CouchbaseClientFactory;
2020
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
21+
import org.springframework.data.couchbase.core.support.PseudoArgs;
2122

2223
/**
2324
* Defines common operations on the Couchbase data source, most commonly implemented by {@link CouchbaseTemplate}.
@@ -44,4 +45,9 @@ public interface CouchbaseOperations extends FluentCouchbaseOperations {
4445
*/
4546
CouchbaseClientFactory getCouchbaseClientFactory();
4647

48+
/**
49+
* @param pseudoArgs - the metaArgs to set on the ThreadLocal field of the CouchbaseOperations
50+
*/
51+
void setThrdLocalArgs(PseudoArgs<?> pseudoArgs);
52+
4753
}

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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,13 @@ private void prepareIndexCreator(final ApplicationContext context) {
180181
}
181182
}
182183
}
184+
185+
/**
186+
* {@inheritDoc}
187+
*/
188+
@Override
189+
public void setThrdLocalArgs(PseudoArgs pseudoArgs) {
190+
reactiveCouchbaseTemplate.setThrdLocalArgs(pseudoArgs);
191+
}
192+
183193
}

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
import org.springframework.data.couchbase.core.support.OneAndAllExists;
2222
import org.springframework.data.couchbase.core.support.WithCollection;
23+
import org.springframework.data.couchbase.core.support.WithGetOptions;
24+
import org.springframework.data.couchbase.core.support.WithScope;
25+
26+
import com.couchbase.client.java.kv.GetOptions;
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, WithGetOptions<T> {
56+
TerminatingExistsById withOptions(GetOptions 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 ExistsByIdWithCollection<T> extends ExistsByIdWithOptions<T>, WithCollection<T> {
60+
ExistsByIdWithOptions<T> inCollection(String collection);
61+
}
62+
63+
interface ExistsByIdWithScope<T> extends ExistsByIdWithCollection<T>, WithScope<T> {
64+
ExistsByIdWithCollection<T> inScope(String scope);
5965
}
6066

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

6369
}

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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.GetOptions;
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 GetOptions 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 GetOptions 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(GetOptions options) {
73+
return new ExecutableExistsByIdSupport(template, scope, collection, options);
74+
}
75+
76+
@Override
77+
public ExistsByIdWithCollection 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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
import org.springframework.data.couchbase.core.query.AnalyticsQuery;
2424
import org.springframework.data.couchbase.core.support.OneAndAll;
2525
import org.springframework.data.couchbase.core.support.WithAnalyticsConsistency;
26+
import org.springframework.data.couchbase.core.support.WithAnalyticsOptions;
2627
import org.springframework.data.couchbase.core.support.WithAnalyticsQuery;
28+
import org.springframework.data.couchbase.core.support.WithCollection;
29+
import org.springframework.data.couchbase.core.support.WithScope;
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>, WithCollection<T> {
129+
FindByAnalyticsWithOptions<T> inCollection(String collection);
130+
}
131+
132+
interface FindByAnalyticsWithScope<T> extends FindByAnalyticsInCollection<T>, WithScope<T> {
133+
FindByAnalyticsInCollection<T> inScope(String scope);
134+
}
135+
120136
@Deprecated
121-
interface FindByAnalyticsConsistentWith<T> extends FindByAnalyticsWithQuery<T> {
137+
interface FindByAnalyticsConsistentWith<T> extends FindByAnalyticsWithScope<T> {
122138

123139
/**
124140
* Allows to override the default scan consistency.

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

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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;
2526

2627
public class ExecutableFindByAnalyticsOperationSupport implements ExecutableFindByAnalyticsOperation {
@@ -35,26 +36,34 @@ public ExecutableFindByAnalyticsOperationSupport(final CouchbaseTemplate templat
3536

3637
@Override
3738
public <T> ExecutableFindByAnalytics<T> findByAnalytics(final Class<T> domainType) {
38-
return new ExecutableFindByAnalyticsSupport<>(template, domainType, ALL_QUERY,
39-
AnalyticsScanConsistency.NOT_BOUNDED);
39+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, domainType, ALL_QUERY, null, null, null, null);
4040
}
4141

4242
static class ExecutableFindByAnalyticsSupport<T> implements ExecutableFindByAnalytics<T> {
4343

4444
private final CouchbaseTemplate template;
45-
private final Class<T> domainType;
45+
private final Class<?> domainType;
46+
private final Class<T> returnType;
4647
private final ReactiveFindByAnalyticsSupport<T> reactiveSupport;
4748
private final AnalyticsQuery query;
4849
private final AnalyticsScanConsistency scanConsistency;
50+
private final String scope;
51+
private final String collection;
52+
private final AnalyticsOptions options;
4953

50-
ExecutableFindByAnalyticsSupport(final CouchbaseTemplate template, final Class<T> domainType,
51-
final AnalyticsQuery query, final AnalyticsScanConsistency scanConsistency) {
54+
ExecutableFindByAnalyticsSupport(final CouchbaseTemplate template, final Class<?> domainType,
55+
final Class<T> returnType, final AnalyticsQuery query, final AnalyticsScanConsistency scanConsistency,
56+
final String scope, final String collection, final AnalyticsOptions options) {
5257
this.template = template;
5358
this.domainType = domainType;
59+
this.returnType = returnType;
5460
this.query = query;
55-
this.reactiveSupport = new ReactiveFindByAnalyticsSupport<>(template.reactive(), domainType, query,
56-
scanConsistency);
61+
this.reactiveSupport = new ReactiveFindByAnalyticsSupport<>(template.reactive(), domainType, returnType, query,
62+
scanConsistency, scope, collection, options);
5763
this.scanConsistency = scanConsistency;
64+
this.scope = scope;
65+
this.collection = collection;
66+
this.options = options;
5867
}
5968

6069
@Override
@@ -74,18 +83,39 @@ public List<T> all() {
7483

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

80108
@Override
81109
@Deprecated
82110
public FindByAnalyticsWithQuery<T> consistentWith(final AnalyticsScanConsistency scanConsistency) {
83-
return new ExecutableFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
111+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
112+
collection, options);
84113
}
85114

86115
@Override
87116
public FindByAnalyticsWithConsistency<T> withConsistency(final AnalyticsScanConsistency scanConsistency) {
88-
return new ExecutableFindByAnalyticsSupport<>(template, domainType, query, scanConsistency);
117+
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
118+
collection, options);
89119
}
90120

91121
@Override

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
import org.springframework.data.couchbase.core.support.OneAndAllId;
2121
import org.springframework.data.couchbase.core.support.WithCollection;
22+
import org.springframework.data.couchbase.core.support.WithGetOptions;
2223
import org.springframework.data.couchbase.core.support.WithProjectionId;
24+
import org.springframework.data.couchbase.core.support.WithScope;
25+
26+
import com.couchbase.client.java.kv.GetOptions;
2327

2428
public interface ExecutableFindByIdOperation {
2529

@@ -50,25 +54,26 @@ interface TerminatingFindById<T> extends OneAndAllId<T> {
5054

5155
}
5256

53-
interface FindByIdWithCollection<T> extends TerminatingFindById<T>, WithCollection<T> {
57+
interface FindByIdWithOptions<T> extends TerminatingFindById<T>, WithGetOptions<T> {
58+
TerminatingFindById<T> withOptions(GetOptions options);
59+
}
5460

55-
/**
56-
* Allows to specify a different collection than the default one configured.
57-
*
58-
* @param collection the collection to use in this scope.
59-
*/
60-
TerminatingFindById<T> inCollection(String collection);
61+
interface FindByIdWithCollection<T> extends FindByIdWithOptions<T>, WithCollection<T> {
62+
FindByIdWithOptions<T> inCollection(String collection);
63+
}
6164

65+
interface FindByIdWithScope<T> extends FindByIdWithCollection<T>, WithScope<T> {
66+
FindByIdWithCollection<T> inScope(String scope);
6267
}
6368

64-
interface FindByIdWithProjection<T> extends FindByIdWithCollection<T>, WithProjectionId<T> {
69+
interface FindByIdWithProjection<T> extends FindByIdWithScope<T>, WithProjectionId<T> {
6570

6671
/**
6772
* Load only certain fields for the document.
6873
*
6974
* @param fields the projected fields to load.
7075
*/
71-
FindByIdWithCollection<T> project(String... fields);
76+
FindByIdWithScope<T> project(String... fields);
7277

7378
}
7479

0 commit comments

Comments
 (0)