Skip to content

Add collections support to N1qlJoin. #1333

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 2 commits into from
Feb 15, 2022
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 @@ -84,7 +84,7 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
}

@Override
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass) {
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope, String collection) {
final CouchbaseDocument converted = new CouchbaseDocument(id);
converted.setId(id);
CouchbasePersistentEntity persistentEntity = couldBePersistentEntity(entityClass);
Expand All @@ -109,9 +109,10 @@ public <T> T decodeEntity(String id, String source, long cas, Class<T> entityCla
if (persistentEntity.getVersionProperty() != null) {
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
}
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template.reactive(), id);
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template.reactive(), id, scope, collection);
return accessor.getBean();
}

CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
if (ClassUtils.isPrimitiveOrWrapper(entityClass) || entityClass == String.class) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,8 +40,9 @@ public Mono<CouchbaseDocument> encodeEntity(Object entityToEncode) {
}

@Override
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass) {
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass));
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope,
String collection) {
return Mono.fromSupplier(() -> support.decodeEntity(id, source, cas, entityClass, scope, collection));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public Mono<CouchbaseDocument> encodeEntity(final Object entityToEncode) {
}

@Override
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass) {
public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope,
String collection) {
return Mono.fromSupplier(() -> {
final CouchbaseDocument converted = new CouchbaseDocument(id);
converted.setId(id);
Expand All @@ -111,7 +112,7 @@ public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> ent
if (persistentEntity.getVersionProperty() != null) {
accessor.setProperty(persistentEntity.getVersionProperty(), cas);
}
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template, id);
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template, id, scope, collection);
return accessor.getBean();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors
* Copyright 2012-2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -133,7 +133,7 @@ public Flux<T> all() {
cas = row.getLong(TemplateUtils.SELECT_CAS);
row.removeKey(TemplateUtils.SELECT_ID);
row.removeKey(TemplateUtils.SELECT_CAS);
return support.decodeEntity(id, row.toString(), cas, returnType);
return support.decodeEntity(id, row.toString(), cas, returnType, null, null);
});
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors
* Copyright 2012-2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,8 +90,8 @@ public Mono<T> one(final String id) {
} else {
return reactive.get(docId, (GetOptions) pArgs.getOptions());
}
}).flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType))
.onErrorResume(throwable -> {
}).flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), domainType,
pArgs.getScope(), pArgs.getCollection())).onErrorResume(throwable -> {
if (throwable instanceof RuntimeException) {
if (throwable instanceof DocumentNotFoundException) {
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors
* Copyright 2012-2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -202,7 +202,7 @@ public Flux<T> all() {
row.removeKey(TemplateUtils.SELECT_ID);
row.removeKey(TemplateUtils.SELECT_CAS);
}
return support.decodeEntity(id, row.toString(), cas, returnType);
return support.decodeEntity(id, row.toString(), cas, returnType, pArgs.getScope(), pArgs.getCollection());
}));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors
* Copyright 2012-2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -77,7 +77,8 @@ public Mono<T> any(final String id) {
return Mono.just(id)
.flatMap(docId -> template.getCouchbaseClientFactory().withScope(pArgs.getScope())
.getCollection(pArgs.getCollection()).reactive().getAnyReplica(docId, pArgs.getOptions()))
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), returnType))
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), returnType,
pArgs.getScope(), pArgs.getCollection()))
.onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors
* Copyright 2021-2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,11 +20,15 @@
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;

/**
*
* @author Michael Reiche
*/
public interface ReactiveTemplateSupport {

Mono<CouchbaseDocument> encodeEntity(Object entityToEncode);

<T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass);
<T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope, String collection);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume our template support classes are not public API so we can break them by extending?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. They are public. So I'll keep make a method with the old signature to call this one.


<T> Mono<T> applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors
* Copyright 2021-2022 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,11 +18,15 @@
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.event.CouchbaseMappingEvent;

/**
*
* @author Michael Reiche
*/
public interface TemplateSupport {

CouchbaseDocument encodeEntity(Object entityToEncode);

<T> T decodeEntity(String id, String source, long cas, Class<T> entityClass);
<T> T decodeEntity(String id, String source, long cas, Class<T> entityClass, String scope, String collection);

<T> T applyUpdatedCas(T entity, CouchbaseDocument converted, long cas);

Expand Down
Loading