Skip to content

Allow id to be type other than String. #1532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public AbstractTemplateSupport(ReactiveCouchbaseTemplate template, CouchbaseConv

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
Expand Down Expand Up @@ -127,7 +127,7 @@ public <T> T decodeEntityBase(String id, String source, Long cas, Class<T> entit
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
}

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Mono<CouchbaseDocument> encodeEntity(Object entityToEncode) {
}

@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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ ReactiveCouchbaseTemplate getReactiveTemplate() {
}

@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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ interface TerminatingFindById<T> extends OneAndAllIdReactive<T> {
* @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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
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;

Expand Down Expand Up @@ -88,7 +85,7 @@ static class ReactiveFindByIdSupport<T> implements ReactiveFindById<T> {
}

@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);
Expand All @@ -101,17 +98,17 @@ public Mono<T> one(final String id) {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ public Mono<T> one(T object) {
.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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ interface TerminatingRemoveById extends OneAndAllIdReactive<RemoveResult> {
* @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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ static class ReactiveRemoveByIdSupport implements ReactiveRemoveById {
}

@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);
Expand All @@ -101,21 +101,21 @@ public Mono<RemoveResult> one(final String id) {

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();

if (cas == null || cas == 0) {
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));
});

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public Mono<T> one(T object) {
.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));
Expand All @@ -117,8 +117,8 @@ public Mono<T> one(T object) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Mono<T> one(T object) {
.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)));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -68,7 +68,7 @@ public CouchbaseDocument() {
*
* @param id the document ID.
*/
public CouchbaseDocument(final String id) {
public CouchbaseDocument(final Object id) {
this(id, DEFAULT_EXPIRATION_TIME);
}

Expand All @@ -78,7 +78,7 @@ public CouchbaseDocument(final String id) {
* @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<>();
Expand Down Expand Up @@ -245,7 +245,7 @@ public CouchbaseDocument setExpiration(int expiration) {
*
* @return the ID of the document.
*/
public String getId() {
public Object getId() {
return id;
}

Expand All @@ -255,7 +255,7 @@ public String getId() {
* @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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/

public interface OneAndAllIdReactive<T> {
Mono<T> one(String id);
Mono<T> one(Object id);

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