Skip to content

Incorporate changes from 5.0.x and bump Couchbase SDK. #1287

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 1 commit into from
Jan 6, 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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</parent>

<properties>
<couchbase>3.2.3</couchbase>
<couchbase.osgi>3.2.3</couchbase.osgi>
<couchbase>3.2.4</couchbase>
<couchbase.osgi>3.2.4</couchbase.osgi>
<springdata.commons>2.7.0-SNAPSHOT</springdata.commons>
<java-module-name>spring.data.couchbase</java-module-name>
</properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 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 @@ -64,7 +64,7 @@ public static CouchbaseCacheConfiguration defaultCacheConfig() {
* <ul>
* <li>{@link String} to {@link byte byte[]} using UTF-8 encoding.</li>
* <li>{@link SimpleKey} to {@link String}</li>
*
* </ul>
* @param registry must not be {@literal null}.
*/
public static void registerDefaultConverters(final ConverterRegistry registry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 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 @@ -228,7 +228,7 @@ public CouchbaseCacheManagerBuilder withCacheConfiguration(String cacheName,

/**
* Disable in-flight {@link org.springframework.cache.Cache} creation for unconfigured caches.
* <p />
* <p>
* {@link CouchbaseCacheManager#getMissingCache(String)} returns {@literal null} for any unconfigured
* {@link org.springframework.cache.Cache} instead of a new {@link CouchbaseCache} instance. This allows eg.
* {@link org.springframework.cache.support.CompositeCacheManager} to chime in.
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 @@ -254,8 +254,6 @@ public String typeKey() {

/**
* Creates a {@link MappingCouchbaseConverter} using the configured {@link #couchbaseMappingContext}.
*
* @throws Exception on Bean construction failure.
*/
@Bean
public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingContext couchbaseMappingContext,
Expand Down Expand Up @@ -283,7 +281,6 @@ public TranslationService couchbaseTranslationService() {
/**
* Creates a {@link CouchbaseMappingContext} equipped with entity classes scanned from the mapping base package.
*
* @throws Exception on Bean construction failure.
*/
@Bean
public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customConversions) throws Exception {
Expand All @@ -299,7 +296,6 @@ public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customC
/**
* Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
*
* @throws Exception on Bean construction failure.
* @return ObjectMapper
*/

Expand Down Expand Up @@ -337,11 +333,9 @@ public CustomConversions customConversions() {
/**
* Return the base package to scan for mapped {@link Document}s. Will return the package name of the configuration
* class (the concrete class, not this one here) by default.
* <p/>
* <p>
* So if you have a {@code com.acme.AppConfig} extending {@link AbstractCouchbaseConfiguration} the base package will
* be considered {@code com.acme} unless the method is overridden to implement alternate behavior.
* </p>
*
* @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for
* entities.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 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 @@ -33,7 +33,7 @@

/**
* Simple {@link PersistenceExceptionTranslator} for Couchbase.
* <p/>
* <p>
* Convert the given runtime exception to an appropriate exception from the {@code org.springframework.dao} hierarchy.
* Return {@literal null} if no translation is appropriate: any other exception may have resulted from user code, and
* should not be translated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +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 All @@ -17,6 +16,9 @@

package org.springframework.data.couchbase.core;

import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
Expand All @@ -40,6 +42,7 @@
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* Internal encode/decode support for CouchbaseTemplate.
Expand Down Expand Up @@ -84,7 +87,18 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass) {
final CouchbaseDocument converted = new CouchbaseDocument(id);
converted.setId(id);
CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityClass);
CouchbasePersistentEntity persistentEntity = couldBePersistentEntity(entityClass);

if (persistentEntity == null) { // method could return a Long, Boolean, String etc.
// QueryExecutionConverters.unwrapWrapperTypes will recursively unwrap until there is nothing left
// to unwrap. This results in List<String[]> being unwrapped past String[] to String, so this may also be a
// Collection (or Array) of entityClass. We have no way of knowing - so just assume it is what we are told.
// if this is a Collection or array, only the first element will be returned.
Set<Map.Entry<String, Object>> set = ((CouchbaseDocument) translationService.decode(source, converted))
.getContent().entrySet();
return (T) set.iterator().next().getValue();
}

if (cas != 0 && persistentEntity.getVersionProperty() != null) {
converted.put(persistentEntity.getVersionProperty().getName(), cas);
}
Expand All @@ -98,6 +112,12 @@ public <T> T decodeEntity(String id, String source, long cas, Class<T> entityCla
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template.reactive(), id);
return accessor.getBean();
}
CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
if (ClassUtils.isPrimitiveOrWrapper(entityClass) || entityClass == String.class) {
return null;
}
return mappingContext.getPersistentEntity(entityClass);
}

@Override
public Object applyUpdatedCas(final Object entity, CouchbaseDocument converted, final long cas) {
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 All @@ -18,6 +18,9 @@

import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
Expand All @@ -42,6 +45,7 @@
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
* Internal encode/decode support for {@link ReactiveCouchbaseTemplate}.
Expand Down Expand Up @@ -84,7 +88,19 @@ public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> ent
return Mono.fromSupplier(() -> {
final CouchbaseDocument converted = new CouchbaseDocument(id);
converted.setId(id);
CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityClass);

CouchbasePersistentEntity persistentEntity = couldBePersistentEntity(entityClass);

if (persistentEntity == null) { // method could return a Long, Boolean, String etc.
// QueryExecutionConverters.unwrapWrapperTypes will recursively unwrap until there is nothing left
// to unwrap. This results in List<String[]> being unwrapped past String[] to String, so this may also be a
// Collection (or Array) of entityClass. We have no way of knowing - so just assume it is what we are told.
// if this is a Collection or array, only the first element will be returned.
Set<Map.Entry<String, Object>> set = ((CouchbaseDocument) translationService.decode(source, converted))
.getContent().entrySet();
return (T) set.iterator().next().getValue();
}

if (cas != 0 && persistentEntity.getVersionProperty() != null) {
converted.put(persistentEntity.getVersionProperty().getName(), cas);
}
Expand All @@ -100,6 +116,13 @@ public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> ent
});
}

CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
if (ClassUtils.isPrimitiveOrWrapper(entityClass) || entityClass == String.class) {
return null;
}
return mappingContext.getPersistentEntity(entityClass);
}

@Override
public Mono<Object> applyUpdatedCas(final Object entity, CouchbaseDocument converted, final long cas) {
return Mono.fromSupplier(() -> {
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 All @@ -22,7 +22,7 @@
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.convert.EntityInstantiators;
import org.springframework.data.mapping.model.EntityInstantiators;

/**
* An abstract {@link CouchbaseConverter} that provides the basics for the {@link MappingCouchbaseConverter}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-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 @@ -24,11 +24,9 @@

/**
* Value object to capture custom conversion.
* <p/>
* <p>
* Types that can be mapped directly onto JSON are considered simple ones, because they neither need deeper inspection
* nor nested conversion.
* </p>
*
* @author Michael Nitschinger
* @author Oliver Gierke
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 the original author or authors.
* Copyright 2017-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 @@ -27,6 +27,7 @@
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
Expand Down Expand Up @@ -71,6 +72,9 @@ private CouchbaseJsr310Converters() {
converters.add(StringToDurationConverter.INSTANCE);
converters.add(PeriodToStringConverter.INSTANCE);
converters.add(StringToPeriodConverter.INSTANCE);
converters.add(ZonedDateTimeToLongConverter.INSTANCE);
converters.add(NumberToZonedDateTimeConverter.INSTANCE);

return converters;
}

Expand Down Expand Up @@ -99,6 +103,31 @@ public Long convert(LocalDateTime source) {
}
}

@ReadingConverter
public enum NumberToZonedDateTimeConverter implements Converter<Number, ZonedDateTime> {

INSTANCE;

@Override
public ZonedDateTime convert(Number source) {
return source == null ? null
: ZonedDateTime.ofInstant(DateConverters.SerializedObjectToDateConverter.INSTANCE.convert(source).toInstant(),
systemDefault());
}
}

@WritingConverter
public enum ZonedDateTimeToLongConverter implements Converter<ZonedDateTime, Long> {

INSTANCE;

@Override
public Long convert(ZonedDateTime source) {
return source == null ? null
: DateConverters.DateToLongConverter.INSTANCE.convert(Date.from(source.toInstant()));
}
}

@ReadingConverter
public enum NumberToLocalDateConverter implements Converter<Number, LocalDate> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 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 All @@ -20,11 +20,9 @@

/**
* Value object to capture custom conversion.
* <p/>
* <p>
* Types that can be mapped directly onto JSON are considered simple ones, because they neither need deeper inspection
* nor nested conversion.
* </p>
*
* @author Michael Nitschinger
* @author Oliver Gierke
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 @@ -36,7 +36,6 @@
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.data.annotation.Transient;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.convert.EntityInstantiator;
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
import org.springframework.data.couchbase.core.mapping.CouchbaseList;
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
Expand All @@ -60,6 +59,7 @@
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
import org.springframework.data.mapping.model.EntityInstantiator;
import org.springframework.data.mapping.model.ParameterValueProvider;
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
import org.springframework.data.mapping.model.PropertyValueProvider;
Expand Down Expand Up @@ -816,7 +816,6 @@ public void setApplicationContext(ApplicationContext applicationContext) {
/**
* COPIED Set the {@link EntityCallbacks} instance to use when invoking
* {@link org.springframework.data.mapping.callback.EntityCallback callbacks} like the {@link AfterConvertCallback}.
* <p/>
* Overrides potentially existing {@link EntityCallbacks}.
*
* @param entityCallbacks must not be {@literal 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 @@ -32,11 +32,9 @@

/**
* Implements annotated property representations of a given {@link Field} instance.
* <p/>
* <p>
* This object is used to gather information out of properties on objects that need to be persisted. For example, it
* supports overriding of the actual property name by providing custom annotations.
* </p>
*
* @author Michael Nitschinger
* @author Mark Paluch
Expand Down Expand Up @@ -71,7 +69,7 @@ protected Association<CouchbasePersistentProperty> createAssociation() {

/**
* Returns the field name of the property.
* <p/>
* <p>
* The field name can be different from the actual property name by using a custom annotation.
*/
@Override
Expand Down
Loading