Skip to content

Commit 81b436f

Browse files
DATAREDIS-425 - Move default ReferenceResolver to RedisKeyValueAdapter.
Change visibility and add as default in setup.
1 parent a7bed94 commit 81b436f

File tree

6 files changed

+60
-76
lines changed

6 files changed

+60
-76
lines changed

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public List<byte[]> convert(KeyValue<byte[], byte[]> source) {
137137
public Map<byte[], byte[]> convert(final List<byte[]> source) {
138138

139139
if (CollectionUtils.isEmpty(source)) {
140-
Collections.emptyMap();
140+
return Collections.emptyMap();
141141
}
142142

143143
Map<byte[], byte[]> target = new LinkedHashMap<byte[], byte[]>();

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

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,12 @@
3838
import org.springframework.data.redis.connection.Message;
3939
import org.springframework.data.redis.connection.RedisConnection;
4040
import org.springframework.data.redis.connection.RedisConnectionFactory;
41+
import org.springframework.data.redis.core.convert.IndexResolverImpl;
42+
import org.springframework.data.redis.core.convert.MappingRedisConverter;
4143
import org.springframework.data.redis.core.convert.RedisConverter;
4244
import org.springframework.data.redis.core.convert.RedisData;
45+
import org.springframework.data.redis.core.convert.ReferenceResolver;
46+
import org.springframework.data.redis.core.mapping.RedisMappingContext;
4347
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
4448
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
4549
import org.springframework.data.redis.util.ByteUtils;
@@ -61,21 +65,35 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter implements App
6165
private RedisMessageListenerContainer messageListenerContainer;
6266
private KeyExpirationEventMessageListener expirationListener;
6367

68+
public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps) {
69+
this(redisOps, new RedisMappingContext());
70+
}
71+
72+
public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext) {
73+
74+
super(new RedisQueryEngine());
75+
76+
Assert.notNull(redisOps, "RedisOperations must not be null!");
77+
78+
MappingRedisConverter mappingConverter = new MappingRedisConverter(mappingContext, new IndexResolverImpl(
79+
mappingContext.getMappingConfiguration().getIndexConfiguration()), new ReferenceResolverImpl(this));
80+
mappingConverter.afterPropertiesSet();
81+
82+
converter = mappingConverter;
83+
this.redisOps = redisOps;
84+
85+
initKeyExpirationListener();
86+
}
87+
6488
/**
6589
* @param redisOps must not be {@literal null}.
6690
* @param mappingContext must not be {@literal null}.
6791
*/
68-
// TODO: change context to RedisConverter
6992
public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisConverter redisConverter) {
7093

7194
super(new RedisQueryEngine());
7295

7396
Assert.notNull(redisOps, "RedisOperations must not be null!");
74-
// Assert.notNull(mappingContext, "RedisMappingContext must not be null!");
75-
//
76-
// MappingRedisConverter mappingConverter = new MappingRedisConverter(mappingContext, new IndexResolverImpl(
77-
// mappingContext.getMappingConfiguration().getIndexConfiguration()), new ReferenceResolverImpl(this));
78-
// mappingConverter.afterPropertiesSet();
7997

8098
converter = redisConverter;
8199
this.redisOps = redisOps;
@@ -454,4 +472,33 @@ private boolean isKeyExpirationMessage(Message message) {
454472
}
455473
}
456474

475+
static class ReferenceResolverImpl implements ReferenceResolver {
476+
477+
private RedisKeyValueAdapter adapter;
478+
479+
ReferenceResolverImpl() {
480+
481+
}
482+
483+
/**
484+
* @param adapter must not be {@literal null}.
485+
*/
486+
public ReferenceResolverImpl(RedisKeyValueAdapter adapter) {
487+
this.adapter = adapter;
488+
}
489+
490+
public void setAdapter(RedisKeyValueAdapter adapter) {
491+
this.adapter = adapter;
492+
}
493+
494+
/*
495+
* (non-Javadoc)
496+
* @see org.springframework.data.redis.core.convert.ReferenceResolver#resolveReference(java.io.Serializable, java.io.Serializable, java.lang.Class)
497+
*/
498+
@Override
499+
public <T> T resolveReference(Serializable id, Serializable keyspace, Class<T> type) {
500+
return (T) adapter.get(id, keyspace);
501+
}
502+
}
503+
457504
}

src/main/java/org/springframework/data/redis/core/convert/ReferenceResolverImpl.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/main/java/org/springframework/data/redis/core/mapping/RedisMappingContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ public RedisMappingContext(MappingConfiguration mappingConfiguration) {
6666
this.mappingConfiguration = mappingConfiguration != null ? mappingConfiguration : new MappingConfiguration(
6767
new IndexConfiguration(), new KeyspaceConfiguration());
6868

69-
setFallbackKeySpaceResolver(new ConfigAwareKeySpaceResolver(mappingConfiguration.getKeyspaceConfiguration()));
70-
this.timeToLiveAccessor = new ConfigAwareTimeToLiveAccessor(mappingConfiguration.getKeyspaceConfiguration(), this);
69+
setFallbackKeySpaceResolver(new ConfigAwareKeySpaceResolver(this.mappingConfiguration.getKeyspaceConfiguration()));
70+
this.timeToLiveAccessor = new ConfigAwareTimeToLiveAccessor(this.mappingConfiguration.getKeyspaceConfiguration(),
71+
this);
7172
}
7273

7374
/**
@@ -207,7 +208,7 @@ static class ConfigAwareTimeToLiveAccessor implements TimeToLiveAccessor {
207208
* @param keyspaceConfig must not be {@literal null}.
208209
* @param mappingContext must not be {@literal null}.
209210
*/
210-
public ConfigAwareTimeToLiveAccessor(KeyspaceConfiguration keyspaceConfig, RedisMappingContext mappingContext) {
211+
ConfigAwareTimeToLiveAccessor(KeyspaceConfiguration keyspaceConfig, RedisMappingContext mappingContext) {
211212

212213
Assert.notNull(keyspaceConfig, "KeyspaceConfiguration must not be null!");
213214
Assert.notNull(mappingContext, "MappingContext must not be null!");

src/main/java/org/springframework/data/redis/repository/configuration/RedisRepositoryConfigurationExtension.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.data.redis.core.convert.CustomConversions;
3232
import org.springframework.data.redis.core.convert.MappingConfiguration;
3333
import org.springframework.data.redis.core.convert.MappingRedisConverter;
34-
import org.springframework.data.redis.core.convert.ReferenceResolverImpl;
3534
import org.springframework.data.redis.core.mapping.RedisMappingContext;
3635
import org.springframework.data.repository.config.RepositoryConfigurationSource;
3736
import org.springframework.util.StringUtils;
@@ -122,7 +121,7 @@ public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConf
122121
private RootBeanDefinition createRedisReferenceResolverDefinition() {
123122

124123
RootBeanDefinition beanDef = new RootBeanDefinition();
125-
beanDef.setBeanClass(ReferenceResolverImpl.class);
124+
beanDef.setBeanClassName("org.springframework.data.redis.core.RedisKeyValueAdapter.ReferenceResolverImpl");
126125

127126
MutablePropertyValues props = new MutablePropertyValues();
128127
props.add("adapter", new RuntimeBeanReference(REDIS_ADAPTER_BEAN_NAME));

src/test/java/org/springframework/data/redis/core/RedisKeyValueAdapterTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@
3737
import org.springframework.data.redis.connection.RedisConnectionFactory;
3838
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
3939
import org.springframework.data.redis.core.convert.Bucket;
40-
import org.springframework.data.redis.core.convert.IndexResolverImpl;
4140
import org.springframework.data.redis.core.convert.KeyspaceConfiguration;
4241
import org.springframework.data.redis.core.convert.MappingConfiguration;
43-
import org.springframework.data.redis.core.convert.MappingRedisConverter;
44-
import org.springframework.data.redis.core.convert.ReferenceResolverImpl;
4542
import org.springframework.data.redis.core.index.IndexConfiguration;
4643
import org.springframework.data.redis.core.index.Indexed;
4744
import org.springframework.data.redis.core.mapping.RedisMappingContext;
@@ -69,11 +66,7 @@ public void setUp() {
6966
new KeyspaceConfiguration()));
7067
mappingContext.afterPropertiesSet();
7168

72-
MappingRedisConverter converter = new MappingRedisConverter(mappingContext, new IndexResolverImpl(), null);
73-
74-
adapter = new RedisKeyValueAdapter(template, converter);
75-
converter.setReferenceResolver(new ReferenceResolverImpl(adapter));
76-
converter.afterPropertiesSet();
69+
adapter = new RedisKeyValueAdapter(template, mappingContext);
7770

7871
template.execute(new RedisCallback<Void>() {
7972

0 commit comments

Comments
 (0)