Skip to content

Commit 90e7fd2

Browse files
committed
Incorporate changes from 5.0.x and bump Couchbase SDK.
Closes #1286.
1 parent 6c6acf8 commit 90e7fd2

File tree

49 files changed

+347
-250
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+347
-250
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
</parent>
1919

2020
<properties>
21-
<couchbase>3.2.3</couchbase>
22-
<couchbase.osgi>3.2.3</couchbase.osgi>
21+
<couchbase>3.2.4</couchbase>
22+
<couchbase.osgi>3.2.4</couchbase.osgi>
2323
<springdata.commons>2.7.0-SNAPSHOT</springdata.commons>
2424
<java-module-name>spring.data.couchbase</java-module-name>
2525
</properties>

src/main/java/org/springframework/data/couchbase/cache/CouchbaseCacheConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,7 +64,7 @@ public static CouchbaseCacheConfiguration defaultCacheConfig() {
6464
* <ul>
6565
* <li>{@link String} to {@link byte byte[]} using UTF-8 encoding.</li>
6666
* <li>{@link SimpleKey} to {@link String}</li>
67-
*
67+
* </ul>
6868
* @param registry must not be {@literal null}.
6969
*/
7070
public static void registerDefaultConverters(final ConverterRegistry registry) {

src/main/java/org/springframework/data/couchbase/cache/CouchbaseCacheManager.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -228,7 +228,7 @@ public CouchbaseCacheManagerBuilder withCacheConfiguration(String cacheName,
228228

229229
/**
230230
* Disable in-flight {@link org.springframework.cache.Cache} creation for unconfigured caches.
231-
* <p />
231+
* <p>
232232
* {@link CouchbaseCacheManager#getMissingCache(String)} returns {@literal null} for any unconfigured
233233
* {@link org.springframework.cache.Cache} instead of a new {@link CouchbaseCache} instance. This allows eg.
234234
* {@link org.springframework.cache.support.CompositeCacheManager} to chime in.

src/main/java/org/springframework/data/couchbase/config/AbstractCouchbaseConfiguration.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -254,8 +254,6 @@ public String typeKey() {
254254

255255
/**
256256
* Creates a {@link MappingCouchbaseConverter} using the configured {@link #couchbaseMappingContext}.
257-
*
258-
* @throws Exception on Bean construction failure.
259257
*/
260258
@Bean
261259
public MappingCouchbaseConverter mappingCouchbaseConverter(CouchbaseMappingContext couchbaseMappingContext,
@@ -283,7 +281,6 @@ public TranslationService couchbaseTranslationService() {
283281
/**
284282
* Creates a {@link CouchbaseMappingContext} equipped with entity classes scanned from the mapping base package.
285283
*
286-
* @throws Exception on Bean construction failure.
287284
*/
288285
@Bean
289286
public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customConversions) throws Exception {
@@ -299,7 +296,6 @@ public CouchbaseMappingContext couchbaseMappingContext(CustomConversions customC
299296
/**
300297
* Creates a {@link ObjectMapper} for the jsonSerializer of the ClusterEnvironment
301298
*
302-
* @throws Exception on Bean construction failure.
303299
* @return ObjectMapper
304300
*/
305301

@@ -337,11 +333,9 @@ public CustomConversions customConversions() {
337333
/**
338334
* Return the base package to scan for mapped {@link Document}s. Will return the package name of the configuration
339335
* class (the concrete class, not this one here) by default.
340-
* <p/>
341336
* <p>
342337
* So if you have a {@code com.acme.AppConfig} extending {@link AbstractCouchbaseConfiguration} the base package will
343338
* be considered {@code com.acme} unless the method is overridden to implement alternate behavior.
344-
* </p>
345339
*
346340
* @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for
347341
* entities.

src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@
3333

3434
/**
3535
* Simple {@link PersistenceExceptionTranslator} for Couchbase.
36-
* <p/>
36+
* <p>
3737
* Convert the given runtime exception to an appropriate exception from the {@code org.springframework.dao} hierarchy.
3838
* Return {@literal null} if no translation is appropriate: any other exception may have resulted from user code, and
3939
* should not be translated.

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplateSupport.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
/*
3-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -17,6 +16,9 @@
1716

1817
package org.springframework.data.couchbase.core;
1918

19+
import java.util.Map;
20+
import java.util.Set;
21+
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
2224
import org.springframework.beans.BeansException;
@@ -40,6 +42,7 @@
4042
import org.springframework.data.mapping.context.MappingContext;
4143
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
4244
import org.springframework.util.Assert;
45+
import org.springframework.util.ClassUtils;
4346

4447
/**
4548
* Internal encode/decode support for CouchbaseTemplate.
@@ -84,7 +87,18 @@ public CouchbaseDocument encodeEntity(final Object entityToEncode) {
8487
public <T> T decodeEntity(String id, String source, long cas, Class<T> entityClass) {
8588
final CouchbaseDocument converted = new CouchbaseDocument(id);
8689
converted.setId(id);
87-
CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityClass);
90+
CouchbasePersistentEntity persistentEntity = couldBePersistentEntity(entityClass);
91+
92+
if (persistentEntity == null) { // method could return a Long, Boolean, String etc.
93+
// QueryExecutionConverters.unwrapWrapperTypes will recursively unwrap until there is nothing left
94+
// to unwrap. This results in List<String[]> being unwrapped past String[] to String, so this may also be a
95+
// Collection (or Array) of entityClass. We have no way of knowing - so just assume it is what we are told.
96+
// if this is a Collection or array, only the first element will be returned.
97+
Set<Map.Entry<String, Object>> set = ((CouchbaseDocument) translationService.decode(source, converted))
98+
.getContent().entrySet();
99+
return (T) set.iterator().next().getValue();
100+
}
101+
88102
if (cas != 0 && persistentEntity.getVersionProperty() != null) {
89103
converted.put(persistentEntity.getVersionProperty().getName(), cas);
90104
}
@@ -98,6 +112,12 @@ public <T> T decodeEntity(String id, String source, long cas, Class<T> entityCla
98112
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template.reactive(), id);
99113
return accessor.getBean();
100114
}
115+
CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
116+
if (ClassUtils.isPrimitiveOrWrapper(entityClass) || entityClass == String.class) {
117+
return null;
118+
}
119+
return mappingContext.getPersistentEntity(entityClass);
120+
}
101121

102122
@Override
103123
public Object applyUpdatedCas(final Object entity, CouchbaseDocument converted, final long cas) {

src/main/java/org/springframework/data/couchbase/core/ReactiveCouchbaseTemplateSupport.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
1818

1919
import reactor.core.publisher.Mono;
2020

21+
import java.util.Map;
22+
import java.util.Set;
23+
2124
import org.slf4j.Logger;
2225
import org.slf4j.LoggerFactory;
2326
import org.springframework.beans.BeansException;
@@ -42,6 +45,7 @@
4245
import org.springframework.data.mapping.context.MappingContext;
4346
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
4447
import org.springframework.util.Assert;
48+
import org.springframework.util.ClassUtils;
4549

4650
/**
4751
* Internal encode/decode support for {@link ReactiveCouchbaseTemplate}.
@@ -84,7 +88,19 @@ public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> ent
8488
return Mono.fromSupplier(() -> {
8589
final CouchbaseDocument converted = new CouchbaseDocument(id);
8690
converted.setId(id);
87-
CouchbasePersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(entityClass);
91+
92+
CouchbasePersistentEntity persistentEntity = couldBePersistentEntity(entityClass);
93+
94+
if (persistentEntity == null) { // method could return a Long, Boolean, String etc.
95+
// QueryExecutionConverters.unwrapWrapperTypes will recursively unwrap until there is nothing left
96+
// to unwrap. This results in List<String[]> being unwrapped past String[] to String, so this may also be a
97+
// Collection (or Array) of entityClass. We have no way of knowing - so just assume it is what we are told.
98+
// if this is a Collection or array, only the first element will be returned.
99+
Set<Map.Entry<String, Object>> set = ((CouchbaseDocument) translationService.decode(source, converted))
100+
.getContent().entrySet();
101+
return (T) set.iterator().next().getValue();
102+
}
103+
88104
if (cas != 0 && persistentEntity.getVersionProperty() != null) {
89105
converted.put(persistentEntity.getVersionProperty().getName(), cas);
90106
}
@@ -100,6 +116,13 @@ public <T> Mono<T> decodeEntity(String id, String source, long cas, Class<T> ent
100116
});
101117
}
102118

119+
CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
120+
if (ClassUtils.isPrimitiveOrWrapper(entityClass) || entityClass == String.class) {
121+
return null;
122+
}
123+
return mappingContext.getPersistentEntity(entityClass);
124+
}
125+
103126
@Override
104127
public Mono<Object> applyUpdatedCas(final Object entity, CouchbaseDocument converted, final long cas) {
105128
return Mono.fromSupplier(() -> {

src/main/java/org/springframework/data/couchbase/core/convert/AbstractCouchbaseConverter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@
2222
import org.springframework.core.convert.ConversionService;
2323
import org.springframework.core.convert.support.GenericConversionService;
2424
import org.springframework.data.convert.CustomConversions;
25-
import org.springframework.data.convert.EntityInstantiators;
25+
import org.springframework.data.mapping.model.EntityInstantiators;
2626

2727
/**
2828
* An abstract {@link CouchbaseConverter} that provides the basics for the {@link MappingCouchbaseConverter}.

src/main/java/org/springframework/data/couchbase/core/convert/CouchbaseCustomConversions.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2021 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,11 +24,9 @@
2424

2525
/**
2626
* Value object to capture custom conversion.
27-
* <p/>
2827
* <p>
2928
* Types that can be mapped directly onto JSON are considered simple ones, because they neither need deeper inspection
3029
* nor nested conversion.
31-
* </p>
3230
*
3331
* @author Michael Nitschinger
3432
* @author Oliver Gierke

src/main/java/org/springframework/data/couchbase/core/convert/CouchbaseJsr310Converters.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import java.time.LocalTime;
2828
import java.time.Period;
2929
import java.time.ZoneId;
30+
import java.time.ZonedDateTime;
3031
import java.util.ArrayList;
3132
import java.util.Collection;
3233
import java.util.Date;
@@ -71,6 +72,9 @@ private CouchbaseJsr310Converters() {
7172
converters.add(StringToDurationConverter.INSTANCE);
7273
converters.add(PeriodToStringConverter.INSTANCE);
7374
converters.add(StringToPeriodConverter.INSTANCE);
75+
converters.add(ZonedDateTimeToLongConverter.INSTANCE);
76+
converters.add(NumberToZonedDateTimeConverter.INSTANCE);
77+
7478
return converters;
7579
}
7680

@@ -99,6 +103,31 @@ public Long convert(LocalDateTime source) {
99103
}
100104
}
101105

106+
@ReadingConverter
107+
public enum NumberToZonedDateTimeConverter implements Converter<Number, ZonedDateTime> {
108+
109+
INSTANCE;
110+
111+
@Override
112+
public ZonedDateTime convert(Number source) {
113+
return source == null ? null
114+
: ZonedDateTime.ofInstant(DateConverters.SerializedObjectToDateConverter.INSTANCE.convert(source).toInstant(),
115+
systemDefault());
116+
}
117+
}
118+
119+
@WritingConverter
120+
public enum ZonedDateTimeToLongConverter implements Converter<ZonedDateTime, Long> {
121+
122+
INSTANCE;
123+
124+
@Override
125+
public Long convert(ZonedDateTime source) {
126+
return source == null ? null
127+
: DateConverters.DateToLongConverter.INSTANCE.convert(Date.from(source.toInstant()));
128+
}
129+
}
130+
102131
@ReadingConverter
103132
public enum NumberToLocalDateConverter implements Converter<Number, LocalDate> {
104133

src/main/java/org/springframework/data/couchbase/core/convert/CustomConversions.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,11 +20,9 @@
2020

2121
/**
2222
* Value object to capture custom conversion.
23-
* <p/>
2423
* <p>
2524
* Types that can be mapped directly onto JSON are considered simple ones, because they neither need deeper inspection
2625
* nor nested conversion.
27-
* </p>
2826
*
2927
* @author Michael Nitschinger
3028
* @author Oliver Gierke

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,6 @@
3636
import org.springframework.core.convert.support.DefaultConversionService;
3737
import org.springframework.data.annotation.Transient;
3838
import org.springframework.data.convert.CustomConversions;
39-
import org.springframework.data.convert.EntityInstantiator;
4039
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
4140
import org.springframework.data.couchbase.core.mapping.CouchbaseList;
4241
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
@@ -60,6 +59,7 @@
6059
import org.springframework.data.mapping.context.MappingContext;
6160
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
6261
import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
62+
import org.springframework.data.mapping.model.EntityInstantiator;
6363
import org.springframework.data.mapping.model.ParameterValueProvider;
6464
import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider;
6565
import org.springframework.data.mapping.model.PropertyValueProvider;

src/main/java/org/springframework/data/couchbase/core/mapping/BasicCouchbasePersistentProperty.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors
2+
* Copyright 2012-2022 the original author or authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,11 +32,9 @@
3232

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

7270
/**
7371
* Returns the field name of the property.
74-
* <p/>
72+
* <p>
7573
* The field name can be different from the actual property name by using a custom annotation.
7674
*/
7775
@Override

0 commit comments

Comments
 (0)