Allow id to be type other than String. (#1532)

Closes #1531.
This commit is contained in:
Michael Reiche
2022-08-05 10:29:06 -07:00
committed by GitHub
parent 5d20cd249a
commit b68b3d8b4d
15 changed files with 29 additions and 32 deletions

View File

@@ -67,7 +67,7 @@ public abstract class AbstractTemplateSupport {
abstract ReactiveCouchbaseTemplate getReactiveTemplate();
public <T> T decodeEntityBase(String id, String source, Long cas, Class<T> entityClass, String scope,
public <T> T decodeEntityBase(Object id, String source, Long cas, Class<T> entityClass, String scope,
String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
// 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 abstract class AbstractTemplateSupport {
if (cas != null && cas != 0 && persistentEntity.getVersionProperty() != null) {
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
}
N1qlJoinResolver.handleProperties(persistentEntity, accessor, getReactiveTemplate(), id, scope, collection);
N1qlJoinResolver.handleProperties(persistentEntity, accessor, getReactiveTemplate(), id.toString(), scope, collection);
if (holder != null) {
holder.transactionResultHolder(txResultHolder, (T) accessor.getBean());

View File

@@ -62,7 +62,7 @@ class CouchbaseTemplateSupport extends AbstractTemplateSupport implements Applic
}
@Override
public <T> T decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection,
public <T> T decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
Object txHolder, CouchbaseResourceHolder holder) {
return decodeEntityBase(id, source, cas, entityClass, scope, collection, txHolder, holder);
}

View File

@@ -42,7 +42,7 @@ public class NonReactiveSupportWrapper implements ReactiveTemplateSupport {
}
@Override
public <T> Mono<T> decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection,
public <T> Mono<T> decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
Object txResultHolder, CouchbaseResourceHolder holder) {
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass, scope, collection, txResultHolder, holder));
}

View File

@@ -69,7 +69,7 @@ class ReactiveCouchbaseTemplateSupport extends AbstractTemplateSupport
}
@Override
public <T> Mono<T> decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope,
public <T> Mono<T> decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope,
String collection, Object txResultHolder, CouchbaseResourceHolder holder) {
return Mono
.fromSupplier(() -> decodeEntityBase(id, source, cas, entityClass, scope, collection, txResultHolder, holder));

View File

@@ -58,7 +58,7 @@ public interface ReactiveFindByIdOperation {
* @param id the document ID.
* @return the entity if found.
*/
Mono<T> one(String id);
Mono<T> one(Object id);
/**
* Finds a list of documents based on the given IDs.

View File

@@ -18,9 +18,6 @@ package org.springframework.data.couchbase.core;
import static com.couchbase.client.java.kv.GetAndTouchOptions.getAndTouchOptions;
import static com.couchbase.client.java.transactions.internal.ConverterUtil.makeCollectionIdentifier;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.kv.PersistTo;
import com.couchbase.client.java.kv.ReplicateTo;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@@ -88,7 +85,7 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati
}
@Override
public Mono<T> one(final String id) {
public Mono<T> one(final Object id) {
CommonOptions<?> gOptions = initGetOptions();
PseudoArgs<?> pArgs = new PseudoArgs(template, scope, collection, gOptions, domainType);
@@ -101,17 +98,17 @@ public class ReactiveFindByIdOperationSupport implements ReactiveFindByIdOperati
Mono<T> reactiveEntity = TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(ctxOpt -> {
if (!ctxOpt.isPresent()) {
if (pArgs.getOptions() instanceof GetAndTouchOptions) {
return rc.getAndTouch(id, expiryToUse(), (GetAndTouchOptions) pArgs.getOptions())
return rc.getAndTouch(id.toString(), expiryToUse(), (GetAndTouchOptions) pArgs.getOptions())
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType,
pArgs.getScope(), pArgs.getCollection(), null, null));
} else {
return rc.get(id, (GetOptions) pArgs.getOptions())
return rc.get(id.toString(), (GetOptions) pArgs.getOptions())
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType,
pArgs.getScope(), pArgs.getCollection(), null, null));
}
} else {
rejectInvalidTransactionalOptions();
return ctxOpt.get().getCore().get(makeCollectionIdentifier(rc.async()), id)
return ctxOpt.get().getCore().get(makeCollectionIdentifier(rc.async()), id.toString())
.flatMap(result -> support.decodeEntity(id, new String(result.contentAsBytes(), StandardCharsets.UTF_8),
result.cas(), domainType, pArgs.getScope(), pArgs.getCollection(),
null, null));

View File

@@ -97,13 +97,13 @@ public class ReactiveInsertByIdOperationSupport implements ReactiveInsertByIdOpe
.flatMap(converted -> TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(ctxOpt -> {
if (!ctxOpt.isPresent()) {
return collection.reactive()
.insert(converted.getId(), converted.export(), buildOptions(pArgs.getOptions(), converted))
.insert(converted.getId().toString(), converted.export(), buildOptions(pArgs.getOptions(), converted))
.flatMap(result -> this.support.applyResult(object, converted, converted.getId(), result.cas(),
null, null));
} else {
rejectInvalidTransactionalOptions();
return ctxOpt.get().getCore()
.insert(makeCollectionIdentifier(collection.async()), converted.getId(),
.insert(makeCollectionIdentifier(collection.async()), converted.getId().toString(),
template.getCouchbaseClientFactory().getCluster().environment().transcoder()
.encode(converted.export()).encoded())
.flatMap(result -> this.support.applyResult(object, converted, converted.getId(), result.cas(),

View File

@@ -62,7 +62,7 @@ public interface ReactiveRemoveByIdOperation {
* @return result of the remove
*/
@Override
Mono<RemoveResult> one(String id);
Mono<RemoveResult> one(Object id);
/**
* Remove one document. Requires whole entity for transaction to have the cas.

View File

@@ -91,7 +91,7 @@ public class ReactiveRemoveByIdOperationSupport implements ReactiveRemoveByIdOpe
}
@Override
public Mono<RemoveResult> one(final String id) {
public Mono<RemoveResult> one(final Object id) {
PseudoArgs<RemoveOptions> pArgs = new PseudoArgs<>(template, scope, collection, options, domainType);
if (LOG.isDebugEnabled()) {
LOG.debug("removeById key={} {}", id, pArgs);
@@ -101,7 +101,7 @@ public class ReactiveRemoveByIdOperationSupport implements ReactiveRemoveByIdOpe
return TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(s -> {
if (!s.isPresent()) {
return rc.remove(id, buildRemoveOptions(pArgs.getOptions())).map(r -> RemoveResult.from(id, r));
return rc.remove(id.toString(), buildRemoveOptions(pArgs.getOptions())).map(r -> RemoveResult.from(id.toString(), r));
} else {
rejectInvalidTransactionalOptions();
@@ -109,13 +109,13 @@ public class ReactiveRemoveByIdOperationSupport implements ReactiveRemoveByIdOpe
throw new IllegalArgumentException("cas must be supplied for tx remove");
}
CoreTransactionAttemptContext ctx = s.get().getCore();
Mono<CoreTransactionGetResult> gr = ctx.get(makeCollectionIdentifier(rc.async()), id);
Mono<CoreTransactionGetResult> gr = ctx.get(makeCollectionIdentifier(rc.async()), id.toString());
return gr.flatMap(getResult -> {
if (getResult.cas() != cas) {
return Mono.error(TransactionalSupport.retryTransactionOnCasMismatch(ctx, getResult.cas(), cas));
}
return ctx.remove(getResult).map(r -> new RemoveResult(id, 0, null));
return ctx.remove(getResult).map(r -> new RemoveResult(id.toString(), 0, null));
});
}

View File

@@ -101,7 +101,7 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
.flatMap(converted -> TransactionalSupport.checkForTransactionInThreadLocalStorage().flatMap(ctxOpt -> {
if (!ctxOpt.isPresent()) {
return collection.reactive()
.replace(converted.getId(), converted.export(),
.replace(converted.getId().toString(), converted.export(),
buildReplaceOptions(pArgs.getOptions(), object, converted))
.flatMap(result -> support.applyResult(object, converted, converted.getId(), result.cas(), null,
null));
@@ -117,8 +117,8 @@ public class ReactiveReplaceByIdOperationSupport implements ReactiveReplaceByIdO
CollectionIdentifier collId = makeCollectionIdentifier(collection.async());
CoreTransactionAttemptContext ctx = ctxOpt.get().getCore();
ctx.logger().info(ctx.attemptId(), "refetching %s for Spring replace",
DebugUtil.docId(collId, converted.getId()));
Mono<CoreTransactionGetResult> gr = ctx.get(collId, converted.getId());
DebugUtil.docId(collId, converted.getId().toString()));
Mono<CoreTransactionGetResult> gr = ctx.get(collId, converted.getId().toString());
return gr.flatMap(getResult -> {
if (getResult.cas() != cas) {

View File

@@ -30,7 +30,7 @@ public interface ReactiveTemplateSupport {
Mono<CouchbaseDocument> encodeEntity(Object entityToEncode);
<T> Mono<T> decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection,
<T> Mono<T> decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
Object txResultHolder, CouchbaseResourceHolder holder);
<T> Mono<T> applyResult(T entity, CouchbaseDocument converted, Object id, Long cas,

View File

@@ -95,7 +95,7 @@ public class ReactiveUpsertByIdOperationSupport implements ReactiveUpsertByIdOpe
.just(template.getCouchbaseClientFactory().withScope(pArgs.getScope())
.getCollection(pArgs.getCollection()))
.flatMap(collection -> collection.reactive()
.upsert(converted.getId(), converted.export(), buildUpsertOptions(pArgs.getOptions(), converted))
.upsert(converted.getId().toString(), converted.export(), buildUpsertOptions(pArgs.getOptions(), converted))
.flatMap(
result -> support.applyResult(object, converted, converted.getId(), result.cas(), null, null)));
});

View File

@@ -26,7 +26,7 @@ public interface TemplateSupport {
CouchbaseDocument encodeEntity(Object entityToEncode);
<T> T decodeEntity(String id, String source, Long cas, Class<T> entityClass, String scope, String collection,
<T> T decodeEntity(Object id, String source, Long cas, Class<T> entityClass, String scope, String collection,
Object txResultHolder, CouchbaseResourceHolder holder);
<T> T applyResult(T entity, CouchbaseDocument converted, Object id, long cas, Object txResultHolder,

View File

@@ -49,7 +49,7 @@ public class CouchbaseDocument implements CouchbaseStorable {
/**
* Represents the document ID used to identify the document in the bucket.
*/
private String id;
private Object id;
/**
* Contains the expiration time of the document.
@@ -68,7 +68,7 @@ public class CouchbaseDocument implements CouchbaseStorable {
*
* @param id the document ID.
*/
public CouchbaseDocument(final String id) {
public CouchbaseDocument(final Object id) {
this(id, DEFAULT_EXPIRATION_TIME);
}
@@ -78,7 +78,7 @@ public class CouchbaseDocument implements CouchbaseStorable {
* @param id the document ID.
* @param expiration the expiration time of the document.
*/
public CouchbaseDocument(final String id, final int expiration) {
public CouchbaseDocument(final Object id, final int expiration) {
this.id = id;
this.expiration = expiration;
content = new TreeMap<>();
@@ -245,7 +245,7 @@ public class CouchbaseDocument implements CouchbaseStorable {
*
* @return the ID of the document.
*/
public String getId() {
public Object getId() {
return id;
}
@@ -255,7 +255,7 @@ public class CouchbaseDocument implements CouchbaseStorable {
* @param id the ID of the document.
* @return the {@link CouchbaseDocument} for chaining.
*/
public CouchbaseDocument setId(String id) {
public CouchbaseDocument setId(Object id) {
this.id = id;
return this;
}

View File

@@ -28,7 +28,7 @@ import java.util.Collection;
*/
public interface OneAndAllIdReactive<T> {
Mono<T> one(String id);
Mono<T> one(Object id);
Flux<? extends T> all(Collection<String> ids);
}