Skip to content

Scopes and collections for repositories #1149

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
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors
* Copyright 2012-2021 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 @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.data.couchbase;

import java.util.function.Supplier;
Expand All @@ -33,6 +32,9 @@

/**
* The default implementation of a {@link CouchbaseClientFactory}.
*
* @author Michael Nitschinger
* @author Michael Reiche
*/
public class SimpleCouchbaseClientFactory implements CouchbaseClientFactory {

Expand Down Expand Up @@ -74,7 +76,7 @@ private SimpleCouchbaseClientFactory(final Supplier<Cluster> cluster, final Stri

@Override
public CouchbaseClientFactory withScope(final String scopeName) {
return new SimpleCouchbaseClientFactory(cluster, bucket.name(), scopeName);
return new SimpleCouchbaseClientFactory(cluster, bucket.name(), scopeName != null ? scopeName : getScope().name());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
import org.springframework.data.couchbase.core.support.PseudoArgs;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.lang.Nullable;

Expand Down Expand Up @@ -107,13 +106,25 @@ public <T> ExecutableFindByAnalytics<T> findByAnalytics(Class<T> domainType) {
}

@Override
@Deprecated
public ExecutableRemoveById removeById() {
return new ExecutableRemoveByIdOperationSupport(this).removeById();
return removeById(null);
}

@Override
public ExecutableRemoveById removeById(Class<?> domainType) {
return new ExecutableRemoveByIdOperationSupport(this).removeById(domainType);
}

@Override
@Deprecated
public ExecutableExistsById existsById() {
return new ExecutableExistsByIdOperationSupport(this).existsById();
return existsById(null);
}

@Override
public ExecutableExistsById existsById(Class<?> domainType) {
return new ExecutableExistsByIdOperationSupport(this).existsById(domainType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.springframework.data.couchbase.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
Expand All @@ -38,9 +40,6 @@
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.util.Assert;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Internal encode/decode support for CouchbaseTemplate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ public interface ExecutableExistsByIdOperation {
/**
* Checks if the document exists in the bucket.
*/
@Deprecated
ExecutableExistsById existsById();

/**
* Checks if the document exists in the bucket.
*/
ExecutableExistsById existsById(Class<?> domainType);

/**
* Terminating operations invoking the actual execution.
*/
Expand Down Expand Up @@ -78,7 +84,6 @@ interface ExistsByIdWithOptions<T> extends TerminatingExistsById, WithExistsOpti
}

/**
*
* Fluent method to specify the collection.
*
* @param <T> the entity type to use for the results.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,34 @@ public class ExecutableExistsByIdOperationSupport implements ExecutableExistsByI
}

@Override
@Deprecated
public ExecutableExistsById existsById() {
return new ExecutableExistsByIdSupport(template, null, null, null);
return existsById(null);
}

@Override
public ExecutableExistsById existsById(Class<?> domainType) {
return new ExecutableExistsByIdSupport(template, domainType, null, null, null);
}

static class ExecutableExistsByIdSupport implements ExecutableExistsById {

private final CouchbaseTemplate template;
private final Class<?> domainType;
private final String scope;
private final String collection;
private final ExistsOptions options;

private final ReactiveExistsByIdSupport reactiveSupport;

ExecutableExistsByIdSupport(final CouchbaseTemplate template, final String scope, final String collection,
final ExistsOptions options) {
ExecutableExistsByIdSupport(final CouchbaseTemplate template, final Class<?> domainType, final String scope,
final String collection, final ExistsOptions options) {
this.template = template;
this.domainType = domainType;
this.scope = scope;
this.collection = collection;
this.options = options;
this.reactiveSupport = new ReactiveExistsByIdSupport(template.reactive(), scope, collection, options);
this.reactiveSupport = new ReactiveExistsByIdSupport(template.reactive(), domainType, scope, collection, options);
}

@Override
Expand All @@ -66,20 +74,18 @@ public Map<String, Boolean> all(final Collection<String> ids) {

@Override
public ExistsByIdWithOptions inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableExistsByIdSupport(template, scope, collection, options);
return new ExecutableExistsByIdSupport(template, domainType, scope, collection, options);
}

@Override
public TerminatingExistsById withOptions(final ExistsOptions options) {
Assert.notNull(options, "Options must not be null.");
return new ExecutableExistsByIdSupport(template, scope, collection, options);
return new ExecutableExistsByIdSupport(template, domainType, scope, collection, options);
}

@Override
public ExistsByIdInCollection inScope(final String scope) {
Assert.hasText(scope, "Scope must not be null nor empty.");
return new ExecutableExistsByIdSupport(template, scope, collection, options);
return new ExecutableExistsByIdSupport(template, domainType, scope, collection, options);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,12 @@ public FindByAnalyticsWithQuery<T> withOptions(final AnalyticsOptions options) {

@Override
public FindByAnalyticsInCollection<T> inScope(final String scope) {
Assert.hasText(scope, "Scope must not be null nor empty.");
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
collection, options);
}

@Override
public FindByAnalyticsWithConsistency<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableFindByAnalyticsSupport<>(template, domainType, returnType, query, scanConsistency, scope,
collection, options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,17 @@ public TerminatingFindById<T> withOptions(final GetOptions options) {

@Override
public FindByIdWithOptions<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableFindByIdSupport<>(template, domainType, scope, collection, options, fields);
}

@Override
public FindByIdInCollection<T> inScope(final String scope) {
Assert.hasText(scope, "Scope must not be null nor empty.");
return new ExecutableFindByIdSupport<>(template, domainType, scope, collection, options, fields);
}

@Override
public FindByIdInScope<T> project(String... fields) {
Assert.notEmpty(fields, "Fields must not be null nor empty.");
Assert.notEmpty(fields, "Fields must not be null.");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Empty list means do not use the project option.

return new ExecutableFindByIdSupport<>(template, domainType, scope, collection, options, Arrays.asList(fields));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ static class ExecutableFindByQuerySupport<T> implements ExecutableFindByQuery<T>
this.returnType = returnType;
this.query = query;
this.reactiveSupport = new ReactiveFindByQuerySupport<T>(template.reactive(), domainType, returnType, query,
scanConsistency, scope, collection, options, distinctFields, new NonReactiveSupportWrapper(template.support()));
scanConsistency, scope, collection, options, distinctFields,
new NonReactiveSupportWrapper(template.support()));
this.scanConsistency = scanConsistency;
this.scope = scope;
this.collection = collection;
Expand Down Expand Up @@ -126,8 +127,12 @@ public <R> FindByQueryWithConsistency<R> as(final Class<R> returnType) {
@Override
public FindByQueryWithProjection<T> distinct(final String[] distinctFields) {
Assert.notNull(distinctFields, "distinctFields must not be null!");
// Coming from an annotation, this cannot be null.
// But a non-null but empty distinctFields means distinct on all fields
// So to indicate do not use distinct, we use {"-"} from the annotation, and here we change it to null.
String[] dFields = distinctFields.length == 1 && "-".equals(distinctFields[0]) ? null : distinctFields;
return new ExecutableFindByQuerySupport<>(template, domainType, returnType, query, scanConsistency, scope,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Coming from an annotation, this cannot be null.
But a non-null but empty distinctFields means distinct on all fields
So to indicate do not use distinct, we use {"-"} from the annotation, and here we change it to null.

collection, options, distinctFields);
collection, options, dFields);
}

@Override
Expand All @@ -154,14 +159,12 @@ public TerminatingFindByQuery<T> withOptions(final QueryOptions options) {

@Override
public FindByQueryInCollection<T> inScope(final String scope) {
Assert.hasText(scope, "Scope must not be null nor empty.");
return new ExecutableFindByQuerySupport<>(template, domainType, returnType, query, scanConsistency, scope,
collection, options, distinctFields);
}

@Override
public FindByQueryWithConsistency<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableFindByQuerySupport<>(template, domainType, returnType, query, scanConsistency, scope,
collection, options, distinctFields);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import java.util.Collection;

import org.springframework.data.couchbase.core.ReactiveFindFromReplicasByIdOperationSupport.ReactiveFindFromReplicasByIdSupport;
import org.springframework.util.Assert;

import com.couchbase.client.java.kv.GetAnyReplicaOptions;
import org.springframework.util.Assert;

public class ExecutableFindFromReplicasByIdOperationSupport implements ExecutableFindFromReplicasByIdOperation {

Expand Down Expand Up @@ -54,7 +54,7 @@ static class ExecutableFindFromReplicasByIdSupport<T> implements ExecutableFindF
this.options = options;
this.returnType = returnType;
this.reactiveSupport = new ReactiveFindFromReplicasByIdSupport<>(template.reactive(), domainType, returnType,
scope, collection, options, new NonReactiveSupportWrapper(template.support()));
scope, collection, options, new NonReactiveSupportWrapper(template.support()));
}

@Override
Expand All @@ -75,13 +75,11 @@ public TerminatingFindFromReplicasById<T> withOptions(final GetAnyReplicaOptions

@Override
public FindFromReplicasByIdWithOptions<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableFindFromReplicasByIdSupport<>(template, domainType, returnType, scope, collection, options);
}

@Override
public FindFromReplicasByIdInCollection<T> inScope(final String scope) {
Assert.hasText(scope, "Scope must not be null nor empty.");
return new ExecutableFindFromReplicasByIdSupport<>(template, domainType, returnType, scope, collection, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,12 @@ public TerminatingInsertById<T> withOptions(final InsertOptions options) {

@Override
public InsertByIdInCollection<T> inScope(final String scope) {
Assert.hasText(scope, "Scope must not be null nor empty.");
return new ExecutableInsertByIdSupport<>(template, domainType, scope, collection, options, persistTo, replicateTo,
durabilityLevel, expiry);
}

@Override
public InsertByIdWithOptions<T> inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableInsertByIdSupport<>(template, domainType, scope, collection, options, persistTo, replicateTo,
durabilityLevel, expiry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Collection;
import java.util.List;

import org.springframework.data.couchbase.core.query.WithConsistency;
import org.springframework.data.couchbase.core.support.InCollection;
import org.springframework.data.couchbase.core.support.InScope;
import org.springframework.data.couchbase.core.support.OneAndAllId;
Expand All @@ -39,6 +38,12 @@ public interface ExecutableRemoveByIdOperation {
/**
* Removes a document.
*/
ExecutableRemoveById removeById(Class<?> domainType);

/**
* Removes a document.
*/
@Deprecated
ExecutableRemoveById removeById();

/**
Expand Down
Loading