Skip to content

Change method visibility CouchbaseTemplate#support & ReactiveCouchbaseTemplate#support #1210

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

Closed
jorgerod opened this issue Sep 6, 2021 · 6 comments · Fixed by #1233
Closed
Labels
type: enhancement A general enhancement

Comments

@jorgerod
Copy link
Contributor

jorgerod commented Sep 6, 2021

Hi

We have the need to extend the functionality of CouchbaseTemplate and ReactiveCouchbaseTemplate.

For this, we would also need to use a custom CouchbaseTemplateSupport and ReactiveCouchbaseTemplateSupport but it is not possible because the support() method only has visibility.

It would be possible to add this method as protected or public?

Thank you very much, best regards

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 6, 2021
@mikereiche
Copy link
Collaborator

We have the need to extend the functionality of CouchbaseTemplate and ReactiveCouchbaseTemplate.

Why? How would that be configured? maintained? debugged?
Would providing a custom mapper be sufficient?
Is this a normal required for other spring-data projects?

@daschl
Copy link
Contributor

daschl commented Sep 10, 2021

@jorgerod to clarify, the template support classes were never intended to be used publicly, that's why they are package private.

If you could describe what feature you need we can see how that fits onto the public API and will also help other users along the way - thank you!

@jorgerod
Copy link
Contributor Author

jorgerod commented Sep 16, 2021

Hello, thank you for your answers

I would like to extend template with new operations. For this, it is necessary that the support is protected to be able to use it and thus to maintain the conversion of objects in the same point.

I will give an example for better understanding:

I want to add the getAndTouch method that currently does not exist in the template.
For it, I will create a template that extends from ReactiveCouchbaseTemplate and I will add the method getAndTouch (this method would be accessible from the repository)

public class CustomReactiveCouchbaseTemplate extends ReactiveCouchbaseTemplate {

	public CustomReactiveCouchbaseTemplate(CouchbaseClientFactory clientFactory,
		CouchbaseConverter converter,
		TranslationService translationService) {
		super(clientFactory, converter, translationService);
                      ...
	}

	public <T> ReactiveFindById<T> getAndTouch(final Class<T> domainType) {
		return new ReactiveGetAndTouchOperationSupport(this).getAndTouch(domainType);
	}
}

and I will create a support class to execute that operation. I will make the request to the database and on return I would want to use the same decodeEntity that is being used for the other operations but there is no access to the support method.

public class ReactiveGetAndTouchOperationSupport {

    private final CustomReactiveCouchbaseTemplate myTemplate;

    public ReactiveGetAndTouchOperationSupport(CustomReactiveCouchbaseTemplate myTemplate) {
        this.myTemplate= myTemplate;
    }

    public <T> ReactiveFindById<T> getAndTouch(Class<T> domainType) {
        return new ReactiveFindByIdSupport<>(this.myTemplate, domainType, null,
            null, null, null, this.myTemplate.support()); //Not compile. No visibility to the ReactiveTemplate#support
    }

    static class ReactiveGetAndTouchSupport<T> {

        private final CustomReactiveCouchbaseTemplate myTemplate;
        private ReactiveTemplateSupport support;
        private final Class<T> domainType;
        private final String scope;
        private final String collection;
        private final GetOptions options;
        private final List<String> fields;

        public <T> ReactiveGetAndTouchSupport(final CustomReactiveCouchbaseTemplate myTemplate, final Class<T> domainType,
            final String scope, final String collection, final GetOptions options, final List<String> fields,
            final ReactiveTemplateSupport support) {
            this.myTemplate= myTemplate;
            this.domainType = domainType; 
            this.scope = scope;
            this.collection = collection;
            this.options = options;
            this.fields = fields;
            this.support = support;

        }

        @Override
        public Mono<T> one(final String id) {
            return Mono.just(id).flatMap(docId -> {
                    final Mono<GetResult> getResultMono;
                    if (isTouchOnRead()) {
                        doGetAndTouch();
                    } else {
                        doGet();
                    }
                    return getResultMono;
                })
                .flatMap(result -> this.support.decodeEntity(id, result.contentAs(String.class), result.cas(),
                    this.domainType))  //I need ReactiveTemplateSupport for decode entity
                .onErrorResume(throwable -> {
                    return doErrorResume();
                })
                .onErrorMap(throwable -> {
                    if (throwable instanceof RuntimeException) {
                        return this.template.potentiallyConvertRuntimeException((RuntimeException) throwable);
                    } else {
                        return throwable;
                    }
                });
        }
    }
}

We have the need to extend the functionality of CouchbaseTemplate and ReactiveCouchbaseTemplate.

Is this a normal required for other spring-data projects?

Apparently yes, because a template can be extended to add more functionality with respect to the driver.

Thank you in advance

@mikereiche
Copy link
Collaborator

I want to add the getAndTouch method that currently does not exist in the template

GetAndTouch will be available tomorrow in 4.3.0-M3. #982

@jorgerod
Copy link
Contributor Author

Hi @mikereiche
GetAndTouch is only a sample. I have other extensions and the problem is the same

@mikereiche
Copy link
Collaborator

mikereiche commented Sep 16, 2021

Edit: If you just want to create your own CustomReactiveCouchbaseTemplate, it would need to be in the same package (or the visibility of template.support() would need to be changed). ReactiveCouchbaseTemplate and CouchbaseTemplate are not final, so it's possible - it's just that I cannot find anyone doing that.


I would like to extend template with new operations.

Assuming that there was no GetAndTouch available, let's do that.

Let's look at an existing operation. FindById (since you would like to create something like FindByIdAndTouch.

find . -name '*.java' | xargs grep -l 'FindById' 
./org/springframework/data/couchbase/core/ReactiveCouchbaseTemplate.java
./org/springframework/data/couchbase/core/FluentCouchbaseOperations.java
./org/springframework/data/couchbase/core/ReactiveFluentCouchbaseOperations.java
./org/springframework/data/couchbase/core/CouchbaseTemplate.java
./org/springframework/data/couchbase/core/ExecutableFindByIdOperation.java
./org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java
./org/springframework/data/couchbase/core/ExecutableFindByIdOperationSupport.java
./org/springframework/data/couchbase/core/ReactiveFindByIdOperation.java

You will need to create four new classes:

./org/springframework/data/couchbase/core/ExecutableFindByIdAndTouchOperation.java
./org/springframework/data/couchbase/core/ReactiveFindByIdAndTouchOperationSupport.java
./org/springframework/data/couchbase/core/ExecutableFindByIdAndTouchOperationSupport.java
./org/springframework/data/couchbase/core/ReactiveFindByIdAndTouchOperation.java

Just copy/rename the classes we found earlier (better yet, sub-class them). Then add the withExpiry() method to them.
Then add to

./org/springframework/data/couchbase/core/ReactiveCouchbaseTemplate.java
./org/springframework/data/couchbase/core/FluentCouchbaseOperations.java
./org/springframework/data/couchbase/core/ReactiveFluentCouchbaseOperations.java
./org/springframework/data/couchbase/core/CouchbaseTemplate.java

Then add some test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants