Skip to content

Commit b68b3d8

Browse files
authored
Allow id to be type other than String. (#1532)
Closes #1531.
1 parent 5d20cd2 commit b68b3d8

15 files changed

+29
-32
lines changed

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/AbstractTemplateSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public AbstractTemplateSupport(ReactiveCouchbaseTemplate template, CouchbaseConv
6767

6868
abstract ReactiveCouchbaseTemplate getReactiveTemplate();
6969

70-
public <T> T decodeEntityBase(String id, String source, Long cas, Class<T> entityClass, String scope,
70+
public <T> T decodeEntityBase(Object id, String source, Long cas, Class<T> entityClass, String scope,
7171
String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
7272

7373
// this is the entity class defined for the repository. It may not be the class of the document that was read
@@ -127,7 +127,7 @@ public <T> T decodeEntityBase(String id, String source, Long cas, Class<T> entit
127127
if (cas != null && cas != 0 && persistentEntity.getVersionProperty() != null) {
128128
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
129129
}
130-
N1qlJoinResolver.handleProperties(persistentEntity, accessor, getReactiveTemplate(), id, scope, collection);
130+
N1qlJoinResolver.handleProperties(persistentEntity, accessor, getReactiveTemplate(), id.toString(), scope, collection);
131131

132132
if (holder != null) {
133133
holder.transactionResultHolder(txResultHolder, (T) accessor.getBean());

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplateSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
6262
}
6363

6464
@Override
65-
public <T> T decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection,
65+
public <T> T decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
6666
Object txHolder, CouchbaseResourceHolder holder) {
6767
return decodeEntityBase(id, source, cas, entityClass, scope, collection, txHolder, holder);
6868
}

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/NonReactiveSupportWrapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Mono<CouchbaseDocument> encodeEntity(Object entityToEncode) {
4242
}
4343

4444
@Override
45-
public <T> Mono<T> decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection,
45+
public <T> Mono<T> decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
4646
Object txResultHolder, CouchbaseResourceHolder holder) {
4747
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass, scope, collection, txResultHolder, holder));
4848
}

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ ReactiveCouchbaseTemplate getReactiveTemplate() {
6969
}
7070

7171
@Override
72-
public <T> Mono<T> decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope,
72+
public <T> Mono<T> decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope,
7373
String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
7474
return Mono
7575
.fromSupplier(() -> decodeEntityBase(id, source, cas, entityClass, scope, collection, txResultHolder, holder));

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ interface TerminatingFindById<T> extends OneAndAllIdReactive<T> {
5858
* @param id the document ID.
5959
* @return the entity if found.
6060
*/
61-
Mono<T> one(String id);
61+
Mono<T> one(Object id);
6262

6363
/**
6464
* Finds a list of documents based on the given IDs.

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
import static com.couchbase.client.java.kv.GetAndTouchOptions.getAndTouchOptions;
1919
import static com.couchbase.client.java.transactions.internal.ConverterUtil.makeCollectionIdentifier;
2020

21-
import com.couchbase.client.core.msg.kv.DurabilityLevel;
22-
import com.couchbase.client.java.kv.PersistTo;
23-
import com.couchbase.client.java.kv.ReplicateTo;
2421
import reactor.core.publisher.Flux;
2522
import reactor.core.publisher.Mono;
2623

@@ -88,7 +85,7 @@ static class ReactiveFindByIdSupport<T> implements ReactiveFindById<T> {
8885
}
8986

9087
@Override
91-
public Mono<T> one(final String id) {
88+
public Mono<T> one(final Object id) {
9289

9390
CommonOptions<?> gOptions = initGetOptions();
9491
PseudoArgs<?> pArgs = new PseudoArgs(template, scope, collection, gOptions, domainType);
@@ -101,17 +98,17 @@ public Mono<T> one(final String id) {
10198
Mono<T> reactiveEntity = TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(ctxOpt -> {
10299
if (!ctxOpt.isPresent()) {
103100
if (pArgs.getOptions() instanceof GetAndTouchOptions) {
104-
return rc.getAndTouch(id, expiryToUse(), (GetAndTouchOptions) pArgs.getOptions())
101+
return rc.getAndTouch(id.toString(), expiryToUse(), (GetAndTouchOptions) pArgs.getOptions())
105102
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType,
106103
pArgs.getScope(), pArgs.getCollection(), null, null));
107104
} else {
108-
return rc.get(id, (GetOptions) pArgs.getOptions())
105+
return rc.get(id.toString(), (GetOptions) pArgs.getOptions())
109106
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType,
110107
pArgs.getScope(), pArgs.getCollection(), null, null));
111108
}
112109
} else {
113110
rejectInvalidTransactionalOptions();
114-
return ctxOpt.get().getCore().get(makeCollectionIdentifier(rc.async()), id)
111+
return ctxOpt.get().getCore().get(makeCollectionIdentifier(rc.async()), id.toString())
115112
.flatMap(result -> support.decodeEntity(id, new String(result.contentAsBytes(), StandardCharsets.UTF_8),
116113
result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(),
117114
null, null));

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveInsertByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ public Mono<T> one(T object) {
9797
.flatMap(converted -> TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(ctxOpt -> {
9898
if (!ctxOpt.isPresent()) {
9999
return collection.reactive()
100-
.insert(converted.getId(), converted.export(), buildOptions(pArgs.getOptions(), converted))
100+
.insert(converted.getId().toString(), converted.export(), buildOptions(pArgs.getOptions(), converted))
101101
.flatMap(result -> this.support.applyResult(object, converted, converted.getId(), result.cas(),
102102
null, null));
103103
} else {
104104
rejectInvalidTransactionalOptions();
105105
return ctxOpt.get().getCore()
106-
.insert(makeCollectionIdentifier(collection.async()), converted.getId(),
106+
.insert(makeCollectionIdentifier(collection.async()), converted.getId().toString(),
107107
template.getCouchbaseClientFactory().getCluster().environment().transcoder()
108108
.encode(converted.export()).encoded())
109109
.flatMap(result -> this.support.applyResult(object, converted, converted.getId(), result.cas(),

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveRemoveByIdOperation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ interface TerminatingRemoveById extends OneAndAllIdReactive<RemoveResult> {
6262
* @return result of the remove
6363
*/
6464
@Override
65-
Mono<RemoveResult> one(String id);
65+
Mono<RemoveResult> one(Object id);
6666

6767
/**
6868
* Remove one document. Requires whole entity for transaction to have the cas.

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveRemoveByIdOperationSupport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static class ReactiveRemoveByIdSupport implements ReactiveRemoveById {
9191
}
9292

9393
@Override
94-
public Mono<RemoveResult> one(final String id) {
94+
public Mono<RemoveResult> one(final Object id) {
9595
PseudoArgs<RemoveOptions> pArgs = new PseudoArgs<>(template, scope, collection, options, domainType);
9696
if (LOG.isDebugEnabled()) {
9797
LOG.debug("removeById key={} {}", id, pArgs);
@@ -101,21 +101,21 @@ public Mono<RemoveResult> one(final String id) {
101101

102102
return TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(s -> {
103103
if (!s.isPresent()) {
104-
return rc.remove(id, buildRemoveOptions(pArgs.getOptions())).map(r -> RemoveResult.from(id, r));
104+
return rc.remove(id.toString(), buildRemoveOptions(pArgs.getOptions())).map(r -> RemoveResult.from(id.toString(), r));
105105
} else {
106106
rejectInvalidTransactionalOptions();
107107

108108
if (cas == null || cas == 0) {
109109
throw new IllegalArgumentException("cas must be supplied for tx remove");
110110
}
111111
CoreTransactionAttemptContext ctx = s.get().getCore();
112-
Mono<CoreTransactionGetResult> gr = ctx.get(makeCollectionIdentifier(rc.async()), id);
112+
Mono<CoreTransactionGetResult> gr = ctx.get(makeCollectionIdentifier(rc.async()), id.toString());
113113

114114
return gr.flatMap(getResult -> {
115115
if (getResult.cas() != cas) {
116116
return Mono.error(TransactionalSupport.retryTransactionOnCasMismatch(ctx, getResult.cas(), cas));
117117
}
118-
return ctx.remove(getResult).map(r -> new RemoveResult(id, 0, null));
118+
return ctx.remove(getResult).map(r -> new RemoveResult(id.toString(), 0, null));
119119
});
120120

121121
}

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveReplaceByIdOperationSupport.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public Mono<T> one(T object) {
101101
.flatMap(converted -> TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(ctxOpt -> {
102102
if (!ctxOpt.isPresent()) {
103103
return collection.reactive()
104-
.replace(converted.getId(), converted.export(),
104+
.replace(converted.getId().toString(), converted.export(),
105105
buildReplaceOptions(pArgs.getOptions(), object, converted))
106106
.flatMap(result -> support.applyResult(object, converted, converted.getId(), result.cas(), null,
107107
null));
@@ -117,8 +117,8 @@ public Mono<T> one(T object) {
117117
CollectionIdentifier collId = makeCollectionIdentifier(collection.async());
118118
CoreTransactionAttemptContext ctx = ctxOpt.get().getCore();
119119
ctx.logger().info(ctx.attemptId(), "refetching %s for Spring replace",
120-
DebugUtil.docId(collId, converted.getId()));
121-
Mono<CoreTransactionGetResult> gr = ctx.get(collId, converted.getId());
120+
DebugUtil.docId(collId, converted.getId().toString()));
121+
Mono<CoreTransactionGetResult> gr = ctx.get(collId, converted.getId().toString());
122122

123123
return gr.flatMap(getResult -> {
124124
if (getResult.cas() != cas) {

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveTemplateSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface ReactiveTemplateSupport {
3030

3131
Mono<CouchbaseDocument> encodeEntity(Object entityToEncode);
3232

33-
<T> Mono<T> decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection,
33+
<T> Mono<T> decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
3434
Object txResultHolder, CouchbaseResourceHolder holder);
3535

3636
<T> Mono<T> applyResult(T entity, CouchbaseDocument converted, Object id, Long cas,

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/ReactiveUpsertByIdOperationSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public Mono<T> one(T object) {
9595
.just(template.getCouchbaseClientFactory().withScope(pArgs.getScope())
9696
.getCollection(pArgs.getCollection()))
9797
.flatMap(collection -> collection.reactive()
98-
.upsert(converted.getId(), converted.export(), buildUpsertOptions(pArgs.getOptions(), converted))
98+
.upsert(converted.getId().toString(), converted.export(), buildUpsertOptions(pArgs.getOptions(), converted))
9999
.flatMap(
100100
result -> support.applyResult(object, converted, converted.getId(), result.cas(), null, null)));
101101
});

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/TemplateSupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface TemplateSupport {
2626

2727
CouchbaseDocument encodeEntity(Object entityToEncode);
2828

29-
<T> T decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection,
29+
<T> T decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
3030
Object txResultHolder, CouchbaseResourceHolder holder);
3131

3232
<T> T applyResult(T entity, CouchbaseDocument converted, Object id, long cas, Object txResultHolder,

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseDocument.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class CouchbaseDocument implements CouchbaseStorable {
4949
/**
5050
* Represents the document ID used to identify the document in the bucket.
5151
*/
52-
private String id;
52+
private Object id;
5353

5454
/**
5555
* Contains the expiration time of the document.
@@ -68,7 +68,7 @@ public CouchbaseDocument() {
6868
*
6969
* @param id the document ID.
7070
*/
71-
public CouchbaseDocument(final String id) {
71+
public CouchbaseDocument(final Object id) {
7272
this(id, DEFAULT_EXPIRATION_TIME);
7373
}
7474

@@ -78,7 +78,7 @@ public CouchbaseDocument(final String id) {
7878
* @param id the document ID.
7979
* @param expiration the expiration time of the document.
8080
*/
81-
public CouchbaseDocument(final String id, final int expiration) {
81+
public CouchbaseDocument(final Object id, final int expiration) {
8282
this.id = id;
8383
this.expiration = expiration;
8484
content = new TreeMap<>();
@@ -245,7 +245,7 @@ public CouchbaseDocument setExpiration(int expiration) {
245245
*
246246
* @return the ID of the document.
247247
*/
248-
public String getId() {
248+
public Object getId() {
249249
return id;
250250
}
251251

@@ -255,7 +255,7 @@ public String getId() {
255255
* @param id the ID of the document.
256256
* @return the {@link CouchbaseDocument} for chaining.
257257
*/
258-
public CouchbaseDocument setId(String id) {
258+
public CouchbaseDocument setId(Object id) {
259259
this.id = id;
260260
return this;
261261
}

spring-data-couchbase/src/main/java/org/springframework/data/couchbase/core/support/OneAndAllIdReactive.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929

3030
public interface OneAndAllIdReactive<T> {
31-
Mono<T> one(String id);
31+
Mono<T> one(Object id);
3232

3333
Flux<? extends T> all(Collection<String> ids);
3434
}

0 commit comments

Comments
 (0)