Skip to content

Commit cd789f9

Browse files
committed
DATAREDIS-634 - Adapt to moved CustomConversions to Spring Data Commons.
Introduce RedisCustomConversions extending o.s.d.convert.CustomConversions. Remove o.s.d.mongo.core.convert.CustomConversions implementation code and utility classes and let it extend RedisCustomConversions. Replace references to o.s.d.r.c.c.CustomConversions with o.s.d.convert.CustomConversions. Adapt tests. Introduce constructors for ObjectHashMapper and RedisKeyValueAdapter requiring o.s.d.convert.CustomConversions and deprecate constructors accepting o.s.d.r.c.c.CustomConversions. Related ticket: DATACMNS-1035.
1 parent be74bbe commit cd789f9

File tree

9 files changed

+193
-497
lines changed

9 files changed

+193
-497
lines changed

src/main/asciidoc/reference/redis-repositories.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ of Complex Type
158158
addresses.[work].city = "...
159159
|===
160160

161-
Mapping behavior can be customized by registering the according `Converter` in `CustomConversions`. Those converters can take care of converting from/to a single `byte[]` as well as `Map<String,byte[]>` whereas the first one is suitable for eg. converting one complex type to eg. a binary JSON representation that still uses the default mappings hash structure. The second option offers full control over the resulting hash. Writing objects to a Redis hash will delete the content from the hash and re-create the whole hash, so not mapped data will be lost.
161+
Mapping behavior can be customized by registering the according `Converter` in `RedisCustomConversions`. Those converters can take care of converting from/to a single `byte[]` as well as `Map<String,byte[]>` whereas the first one is suitable for eg. converting one complex type to eg. a binary JSON representation that still uses the default mappings hash structure. The second option offers full control over the resulting hash. Writing objects to a Redis hash will delete the content from the hash and re-create the whole hash, so not mapped data will be lost.
162162

163163
.Sample byte[] Converters
164164
====

src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import java.util.concurrent.TimeUnit;
2929
import java.util.concurrent.atomic.AtomicReference;
3030

31-
import org.slf4j.Logger;
32-
import org.slf4j.LoggerFactory;
3331
import org.springframework.beans.BeansException;
3432
import org.springframework.beans.factory.InitializingBean;
3533
import org.springframework.context.ApplicationContext;
@@ -42,7 +40,6 @@
4240
import org.springframework.data.keyvalue.core.AbstractKeyValueAdapter;
4341
import org.springframework.data.keyvalue.core.KeyValueAdapter;
4442
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
45-
import org.springframework.data.mapping.PersistentProperty;
4643
import org.springframework.data.redis.connection.DataType;
4744
import org.springframework.data.redis.connection.Message;
4845
import org.springframework.data.redis.connection.MessageListener;
@@ -55,6 +52,7 @@
5552
import org.springframework.data.redis.core.convert.MappingRedisConverter;
5653
import org.springframework.data.redis.core.convert.PathIndexResolver;
5754
import org.springframework.data.redis.core.convert.RedisConverter;
55+
import org.springframework.data.redis.core.convert.RedisCustomConversions;
5856
import org.springframework.data.redis.core.convert.RedisData;
5957
import org.springframework.data.redis.core.convert.ReferenceResolverImpl;
6058
import org.springframework.data.redis.core.mapping.RedisMappingContext;
@@ -66,7 +64,6 @@
6664
import org.springframework.data.util.CloseableIterator;
6765
import org.springframework.util.Assert;
6866
import org.springframework.util.ObjectUtils;
69-
import org.springframework.util.StringUtils;
7067

7168
/**
7269
* Redis specific {@link KeyValueAdapter} implementation. Uses binary codec to read/write data from/to Redis. Objects
@@ -117,7 +114,7 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter
117114

118115
/**
119116
* Creates new {@link RedisKeyValueAdapter} with default {@link RedisMappingContext} and default
120-
* {@link CustomConversions}.
117+
* {@link RedisCustomConversions}.
121118
*
122119
* @param redisOps must not be {@literal null}.
123120
*/
@@ -126,13 +123,13 @@ public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps) {
126123
}
127124

128125
/**
129-
* Creates new {@link RedisKeyValueAdapter} with default {@link CustomConversions}.
126+
* Creates new {@link RedisKeyValueAdapter} with default {@link RedisCustomConversions}.
130127
*
131128
* @param redisOps must not be {@literal null}.
132129
* @param mappingContext must not be {@literal null}.
133130
*/
134131
public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext) {
135-
this(redisOps, mappingContext, new CustomConversions());
132+
this(redisOps, mappingContext, new RedisCustomConversions());
136133
}
137134

138135
/**
@@ -141,9 +138,25 @@ public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext
141138
* @param redisOps must not be {@literal null}.
142139
* @param mappingContext must not be {@literal null}.
143140
* @param customConversions can be {@literal null}.
141+
* @deprecated since 2.0, use
142+
* {@link #RedisKeyValueAdapter(RedisOperations, RedisMappingContext, org.springframework.data.convert.CustomConversions)}.
144143
*/
144+
@Deprecated
145145
public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext,
146146
CustomConversions customConversions) {
147+
this(redisOps, mappingContext, (org.springframework.data.convert.CustomConversions) customConversions);
148+
}
149+
150+
/**
151+
* Creates new {@link RedisKeyValueAdapter}.
152+
*
153+
* @param redisOps must not be {@literal null}.
154+
* @param mappingContext must not be {@literal null}.
155+
* @param customConversions can be {@literal null}.
156+
* @since 2.0
157+
*/
158+
public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext,
159+
org.springframework.data.convert.CustomConversions customConversions) {
147160

148161
super(new RedisQueryEngine());
149162

@@ -152,7 +165,7 @@ public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext
152165

153166
MappingRedisConverter mappingConverter = new MappingRedisConverter(mappingContext,
154167
new PathIndexResolver(mappingContext), new ReferenceResolverImpl(redisOps));
155-
mappingConverter.setCustomConversions(customConversions == null ? new CustomConversions() : customConversions);
168+
mappingConverter.setCustomConversions(customConversions == null ? new RedisCustomConversions() : customConversions);
156169
mappingConverter.afterPropertiesSet();
157170

158171
this.converter = mappingConverter;
@@ -374,7 +387,7 @@ public Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessExcept
374387

375388
offset = Math.max(0, offset);
376389
if (offset >= 0 && rows > 0) {
377-
keys = keys.subList((int)offset, Math.min((int)offset + rows, keys.size()));
390+
keys = keys.subList((int) offset, Math.min((int) offset + rows, keys.size()));
378391
}
379392

380393
for (byte[] key : keys) {
@@ -428,7 +441,8 @@ public Long doInRedis(RedisConnection connection) throws DataAccessException {
428441

429442
public void update(final PartialUpdate<?> update) {
430443

431-
final RedisPersistentEntity<?> entity = this.converter.getMappingContext().getPersistentEntity(update.getTarget()).get();
444+
final RedisPersistentEntity<?> entity = this.converter.getMappingContext().getPersistentEntity(update.getTarget())
445+
.get();
432446

433447
final String keyspace = entity.getKeySpace();
434448
final Object id = update.getId();
@@ -622,11 +636,10 @@ private <T> T readBackTimeToLiveIfSet(final byte[] key, T target) {
622636
if (entity.hasExplictTimeToLiveProperty()) {
623637

624638
Optional<RedisPersistentProperty> ttlProperty = entity.getExplicitTimeToLiveProperty();
625-
if(!ttlProperty.isPresent()) {
639+
if (!ttlProperty.isPresent()) {
626640
return target;
627641
}
628642

629-
630643
final Optional<TimeToLive> ttl = ttlProperty.get().findAnnotation(TimeToLive.class);
631644

632645
Long timeout = redisOps.execute(new RedisCallback<Long>() {

0 commit comments

Comments
 (0)