Skip to content

Scope and collection API for template. #1133

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 16 commits into from
Jun 2, 2021
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
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pipeline {
}
}

stage("test: baseline (jdk15)") {
stage("test: baseline (jdk16)") {
agent {
label 'data'
}
Expand All @@ -88,7 +88,7 @@ pipeline {
steps {
script {
docker.withRegistry('', 'hub.docker.com-springbuildmaster') {
docker.image('adoptopenjdk/openjdk15:latest').inside('-u root -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -v $HOME:/tmp/jenkins-home') {
docker.image('adoptopenjdk/openjdk16:latest').inside('-u root -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker -v $HOME:/tmp/jenkins-home') {
sh "docker login --username ${DOCKER_HUB_USR} --password ${DOCKER_HUB_PSW}"
sh 'PROFILE=ci,java11 ci/test.sh'
sh "ci/clean.sh"
Expand Down
28 changes: 11 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
Expand Down Expand Up @@ -88,7 +88,7 @@
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.1.0.RELEASE</version>
<version>3.4.6</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -170,12 +170,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Kotlin extension -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package org.springframework.data.couchbase.config;

import static com.couchbase.client.java.ClusterOptions.*;
import static com.couchbase.client.java.ClusterOptions.clusterOptions;

import java.util.Collections;
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 All @@ -48,11 +49,19 @@
import org.springframework.util.StringUtils;

import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.DeserializationFeature;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.Module;
import com.couchbase.client.core.encryption.CryptoManager;
import com.couchbase.client.core.env.Authenticator;
import com.couchbase.client.core.env.PasswordAuthenticator;
import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.codec.JacksonJsonSerializer;
import com.couchbase.client.java.encryption.databind.jackson.EncryptionModule;
import com.couchbase.client.java.env.ClusterEnvironment;
import com.couchbase.client.java.json.JacksonTransformers;
import com.couchbase.client.java.json.JsonValueModule;
import com.couchbase.client.java.json.RepackagedJsonValueModule;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Base class for Spring Data Couchbase configuration using JavaConfig.
Expand All @@ -65,12 +74,13 @@
@Configuration
public abstract class AbstractCouchbaseConfiguration {

@Autowired ObjectMapper couchbaseObjectMapper;

/**
* The connection string which allows the SDK to connect to the cluster.
* <p>
* Note that the connection string can take many forms, in its simplest it is just a single hostname
* like "127.0.0.1". Please refer to the couchbase Java SDK documentation for all the different
* possibilities and options.
* Note that the connection string can take many forms, in its simplest it is just a single hostname like "127.0.0.1".
* Please refer to the couchbase Java SDK documentation for all the different possibilities and options.
*/
public abstract String getConnectionString();

Expand Down Expand Up @@ -130,6 +140,10 @@ public Cluster couchbaseCluster(ClusterEnvironment couchbaseClusterEnvironment)
@Bean(destroyMethod = "shutdown")
public ClusterEnvironment couchbaseClusterEnvironment() {
ClusterEnvironment.Builder builder = ClusterEnvironment.builder();
if (!nonShadowedJacksonPresent()) {
throw new CouchbaseException("non-shadowed Jackson not present");
}
builder.jsonSerializer(JacksonJsonSerializer.create(couchbaseObjectMapper));
configureEnvironment(builder);
return builder.build();
}
Expand Down Expand Up @@ -273,6 +287,25 @@ public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customC
return mappingContext;
}

/**
* Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
*
* @throws Exception on Bean construction failure.
* @return ObjectMapper
*/

@Bean
public ObjectMapper couchbaseObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new JsonValueModule());
CryptoManager cryptoManager = null;
if (cryptoManager != null) {
mapper.registerModule(new EncryptionModule(cryptoManager));
}
return mapper;
}

/**
* Configure whether to automatically create indices for domain types by deriving the from the entity or not.
*/
Expand Down Expand Up @@ -327,4 +360,14 @@ protected FieldNamingStrategy fieldNamingStrategy() {
return abbreviateFieldNames() ? new CamelCaseAbbreviatingFieldNamingStrategy()
: PropertyNameFieldNamingStrategy.INSTANCE;
}

private boolean nonShadowedJacksonPresent() {
try {
JacksonJsonSerializer.preflightCheck();
return true;
} catch (Throwable t) {
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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 @@ -60,14 +61,14 @@ public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final Couch
this.converter = converter;
this.templateSupport = new CouchbaseTemplateSupport(converter, translationService);
this.reactiveCouchbaseTemplate = new ReactiveCouchbaseTemplate(clientFactory, converter, translationService);

this.mappingContext = this.converter.getMappingContext();
if (mappingContext instanceof CouchbaseMappingContext) {
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
if (cmc.isAutoIndexCreation()) {
indexCreator = new CouchbasePersistentEntityIndexCreator(cmc, this);
}
}
if (mappingContext instanceof CouchbaseMappingContext) {
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
if (cmc.isAutoIndexCreation()) {
indexCreator = new CouchbasePersistentEntityIndexCreator(cmc, this);
}
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

just formatting changes


@Override
Expand Down Expand Up @@ -184,4 +185,5 @@ private void prepareIndexCreator(final ApplicationContext context) {
TemplateSupport support() {
return templateSupport;
}

}
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 @@ -18,16 +18,29 @@
import java.util.Collection;
import java.util.Map;

import org.springframework.data.couchbase.core.support.InCollection;
import org.springframework.data.couchbase.core.support.InScope;
import org.springframework.data.couchbase.core.support.OneAndAllExists;
import org.springframework.data.couchbase.core.support.WithCollection;
import org.springframework.data.couchbase.core.support.WithExistsOptions;

import com.couchbase.client.java.kv.ExistsOptions;

/**
* Insert Operations
*
* @author Christoph Strobl
* @since 2.0
*/
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm just adding javadoc that was missing. As near as I can figure, the original author was Christoph

public interface ExecutableExistsByIdOperation {

/**
* Checks if the document exists in the bucket.
*/
ExecutableExistsById existsById();

/**
* Terminating operations invoking the actual execution.
*/
interface TerminatingExistsById extends OneAndAllExists {

/**
Expand All @@ -36,6 +49,7 @@ interface TerminatingExistsById extends OneAndAllExists {
* @param id the ID to perform the operation on.
* @return true if the document exists, false otherwise.
*/
@Override
boolean one(String id);

/**
Expand All @@ -44,20 +58,59 @@ interface TerminatingExistsById extends OneAndAllExists {
* @param ids the ids to check.
* @return a map consisting of the document IDs as the keys and if they exist as the value.
*/
@Override
Map<String, Boolean> all(Collection<String> ids);
}

/**
* Fluent method to specify options.
*
* @param <T> the entity type to use for the results.
*/
interface ExistsByIdWithOptions<T> extends TerminatingExistsById, WithExistsOptions<T> {
/**
* Fluent method to specify options to use for execution
*
* @param options options to use for execution
*/
@Override
TerminatingExistsById withOptions(ExistsOptions options);
}

interface ExistsByIdWithCollection extends TerminatingExistsById, WithCollection {
/**
*
* Fluent method to specify the collection.
*
* @param <T> the entity type to use for the results.
*/
interface ExistsByIdInCollection<T> extends ExistsByIdWithOptions<T>, InCollection<T> {
/**
* With a different collection
*
* @param collection the collection to use.
*/
@Override
ExistsByIdWithOptions<T> inCollection(String collection);
}

/**
* Fluent method to specify the scope.
*
* @param <T> the entity type to use for the results.
*/
interface ExistsByIdInScope<T> extends ExistsByIdInCollection<T>, InScope<T> {
/**
* Allows to specify a different collection than the default one configured.
* With a different scope
*
* @param collection the collection to use in this scope.
* @param scope the scope to use.
*/
TerminatingExistsById inCollection(String collection);
@Override
ExistsByIdInCollection<T> inScope(String scope);
}

interface ExecutableExistsById extends ExistsByIdWithCollection {}
/**
* Provides methods for constructing KV exists operations in a fluent way.
*/
interface ExecutableExistsById extends ExistsByIdInScope {}

}
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 @@ -15,13 +15,14 @@
*/
package org.springframework.data.couchbase.core;

import org.springframework.data.couchbase.core.ReactiveExistsByIdOperationSupport.ReactiveExistsByIdSupport;

import java.util.Collection;
import java.util.Map;

import org.springframework.data.couchbase.core.ReactiveExistsByIdOperationSupport.ReactiveExistsByIdSupport;
import org.springframework.util.Assert;

import com.couchbase.client.java.kv.ExistsOptions;

public class ExecutableExistsByIdOperationSupport implements ExecutableExistsByIdOperation {

private final CouchbaseTemplate template;
Expand All @@ -32,17 +33,25 @@ public class ExecutableExistsByIdOperationSupport implements ExecutableExistsByI

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

static class ExecutableExistsByIdSupport implements ExecutableExistsById {

private final CouchbaseTemplate template;
private final String scope;
private final String collection;
private final ExistsOptions options;

private final ReactiveExistsByIdSupport reactiveSupport;

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

@Override
Expand All @@ -56,11 +65,22 @@ public Map<String, Boolean> all(final Collection<String> ids) {
}

@Override
public TerminatingExistsById inCollection(final String collection) {
public ExistsByIdWithOptions inCollection(final String collection) {
Assert.hasText(collection, "Collection must not be null nor empty.");
return new ExecutableExistsByIdSupport(template, collection);
return new ExecutableExistsByIdSupport(template, 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);
}

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

}
Loading