Skip to content

Commit 5719947

Browse files
committed
DATAREDIS-664 - Polishing.
Remove Serializable ID constraints from factory beans. Replace casts with type.cast(…). Convert anonymous inner classes to lambdas. Remove unused code and casts. Simplify test entities by removing Serializable and using lombok. Formatting, Javadoc. Original pull request: #256.
1 parent bb19a9a commit 5719947

File tree

11 files changed

+208
-392
lines changed

11 files changed

+208
-392
lines changed

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

Lines changed: 125 additions & 175 deletions
Large diffs are not rendered by default.

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.ArrayList;
1919
import java.util.Collections;
2020
import java.util.List;
21-
import java.util.Optional;
2221

2322
import org.springframework.data.keyvalue.core.KeyValueAdapter;
2423
import org.springframework.data.keyvalue.core.KeyValueCallback;
@@ -29,15 +28,16 @@
2928

3029
/**
3130
* Redis specific implementation of {@link KeyValueTemplate}.
32-
*
31+
*
3332
* @author Christoph Strobl
33+
* @author Mark Paluch
3434
* @since 1.7
3535
*/
3636
public class RedisKeyValueTemplate extends KeyValueTemplate {
3737

3838
/**
3939
* Create new {@link RedisKeyValueTemplate}.
40-
*
40+
*
4141
* @param adapter must not be {@literal null}.
4242
* @param mappingContext must not be {@literal null}.
4343
*/
@@ -58,7 +58,7 @@ public RedisMappingContext getMappingContext() {
5858
* Retrieve entities by resolving their {@literal id}s and converting them into required type. <br />
5959
* The callback provides either a single {@literal id} or an {@link Iterable} of {@literal id}s, used for retrieving
6060
* the actual domain types and shortcuts manual retrieval and conversion of {@literal id}s via {@link RedisTemplate}.
61-
*
61+
*
6262
* <pre>
6363
* <code>
6464
* List&#60;RedisSession&#62; sessions = template.find(new RedisCallback&#60;Set&#60;byte[]&#62;&#62;() {
@@ -69,14 +69,14 @@ public RedisMappingContext getMappingContext() {
6969
* }
7070
* }, RedisSession.class);
7171
* </code>
72-
*
72+
*
7373
* <pre>
7474
*
7575
* @param callback provides the to retrieve entity ids. Must not be {@literal null}.
7676
* @param type must not be {@literal null}.
7777
* @return empty list if not elements found.
7878
*/
79-
public <T> List<T> find(final RedisCallback<?> callback, final Class<T> type) {
79+
public <T> List<T> find(RedisCallback<?> callback, Class<T> type) {
8080

8181
Assert.notNull(callback, "Callback must not be null.");
8282

@@ -92,18 +92,17 @@ public List<T> doInRedis(RedisKeyValueAdapter adapter) {
9292
}
9393

9494
Iterable<?> ids = ClassUtils.isAssignable(Iterable.class, callbackResult.getClass())
95-
? (Iterable<?>) callbackResult : Collections.singleton(callbackResult);
95+
? (Iterable<?>) callbackResult
96+
: Collections.singleton(callbackResult);
9697

9798
List<T> result = new ArrayList<T>();
9899
for (Object id : ids) {
99100

100101
String idToUse = adapter.getConverter().getConversionService().canConvert(id.getClass(), String.class)
101-
? adapter.getConverter().getConversionService().convert(id, String.class) : id.toString();
102+
? adapter.getConverter().getConversionService().convert(id, String.class)
103+
: id.toString();
102104

103-
Optional<T> candidate = findById(idToUse, type);
104-
if (candidate.isPresent()) {
105-
result.add(candidate.get());
106-
}
105+
findById(idToUse, type).ifPresent(result::add);
107106
}
108107

109108
return result;
@@ -116,7 +115,7 @@ public List<T> doInRedis(RedisKeyValueAdapter adapter) {
116115
* @see org.springframework.data.keyvalue.core.KeyValueTemplate#insert(java.lang.Object, java.lang.Object)
117116
*/
118117
@Override
119-
public void insert(final Object id, final Object objectToInsert) {
118+
public void insert(Object id, Object objectToInsert) {
120119

121120
if (objectToInsert instanceof PartialUpdate) {
122121
doPartialUpdate((PartialUpdate<?>) objectToInsert);
@@ -156,7 +155,7 @@ public Void doInRedis(RedisKeyValueAdapter adapter) {
156155

157156
/**
158157
* Redis specific {@link KeyValueCallback}.
159-
*
158+
*
160159
* @author Christoph Strobl
161160
* @param <T>
162161
* @since 1.7

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

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26-
import org.springframework.dao.DataAccessException;
2726
import org.springframework.data.geo.Circle;
2827
import org.springframework.data.geo.GeoResult;
2928
import org.springframework.data.geo.GeoResults;
3029
import org.springframework.data.keyvalue.core.CriteriaAccessor;
3130
import org.springframework.data.keyvalue.core.QueryEngine;
3231
import org.springframework.data.keyvalue.core.SortAccessor;
3332
import org.springframework.data.keyvalue.core.query.KeyValueQuery;
34-
import org.springframework.data.redis.connection.RedisConnection;
3533
import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
3634
import org.springframework.data.redis.core.convert.GeoIndexedPropertyValue;
3735
import org.springframework.data.redis.core.convert.RedisData;
@@ -53,7 +51,7 @@ class RedisQueryEngine extends QueryEngine<RedisKeyValueAdapter, RedisOperationC
5351
/**
5452
* Creates new {@link RedisQueryEngine} with defaults.
5553
*/
56-
public RedisQueryEngine() {
54+
RedisQueryEngine() {
5755
this(new RedisCriteriaAccessor(), null);
5856
}
5957

@@ -64,7 +62,7 @@ public RedisQueryEngine() {
6462
* @param sortAccessor
6563
* @see QueryEngine#QueryEngine(CriteriaAccessor, SortAccessor)
6664
*/
67-
public RedisQueryEngine(CriteriaAccessor<RedisOperationChain> criteriaAccessor,
65+
private RedisQueryEngine(CriteriaAccessor<RedisOperationChain> criteriaAccessor,
6866
SortAccessor<Comparator<?>> sortAccessor) {
6967
super(criteriaAccessor, sortAccessor);
7068
}
@@ -75,69 +73,64 @@ public RedisQueryEngine(CriteriaAccessor<RedisOperationChain> criteriaAccessor,
7573
*/
7674
@Override
7775
@SuppressWarnings("unchecked")
78-
public <T> Collection<T> execute(final RedisOperationChain criteria, final Comparator<?> sort, final long offset,
79-
final int rows, final String keyspace, Class<T> type) {
76+
public <T> Collection<T> execute(RedisOperationChain criteria, Comparator<?> sort, long offset, int rows,
77+
String keyspace, Class<T> type) {
8078

8179
if (criteria == null
8280
|| (CollectionUtils.isEmpty(criteria.getOrSismember()) && CollectionUtils.isEmpty(criteria.getSismember()))
8381
&& criteria.getNear() == null) {
8482
return (Collection<T>) getAdapter().getAllOf(keyspace, offset, rows);
8583
}
8684

87-
RedisCallback<Map<byte[], Map<byte[], byte[]>>> callback = new RedisCallback<Map<byte[], Map<byte[], byte[]>>>() {
85+
RedisCallback<Map<byte[], Map<byte[], byte[]>>> callback = connection -> {
8886

89-
@Override
90-
public Map<byte[], Map<byte[], byte[]>> doInRedis(RedisConnection connection) throws DataAccessException {
91-
92-
List<byte[]> allKeys = new ArrayList<byte[]>();
93-
if (!criteria.getSismember().isEmpty()) {
94-
allKeys.addAll(connection.sInter(keys(keyspace + ":", criteria.getSismember())));
95-
}
87+
List<byte[]> allKeys = new ArrayList<>();
88+
if (!criteria.getSismember().isEmpty()) {
89+
allKeys.addAll(connection.sInter(keys(keyspace + ":", criteria.getSismember())));
90+
}
9691

97-
if (!criteria.getOrSismember().isEmpty()) {
98-
allKeys.addAll(connection.sUnion(keys(keyspace + ":", criteria.getOrSismember())));
99-
}
92+
if (!criteria.getOrSismember().isEmpty()) {
93+
allKeys.addAll(connection.sUnion(keys(keyspace + ":", criteria.getOrSismember())));
94+
}
10095

101-
if (criteria.getNear() != null) {
96+
if (criteria.getNear() != null) {
10297

103-
GeoResults<GeoLocation<byte[]>> x = connection.geoRadius(geoKey(keyspace + ":", criteria.getNear()),
104-
new Circle(criteria.getNear().getPoint(), criteria.getNear().getDistance()));
105-
for (GeoResult<GeoLocation<byte[]>> y : x) {
106-
allKeys.add(y.getContent().getName());
107-
}
98+
GeoResults<GeoLocation<byte[]>> x = connection.geoRadius(geoKey(keyspace + ":", criteria.getNear()),
99+
new Circle(criteria.getNear().getPoint(), criteria.getNear().getDistance()));
100+
for (GeoResult<GeoLocation<byte[]>> y : x) {
101+
allKeys.add(y.getContent().getName());
108102
}
103+
}
109104

110-
byte[] keyspaceBin = getAdapter().getConverter().getConversionService().convert(keyspace + ":", byte[].class);
111-
112-
final Map<byte[], Map<byte[], byte[]>> rawData = new LinkedHashMap<byte[], Map<byte[], byte[]>>();
113-
114-
if (allKeys.isEmpty() || allKeys.size() < offset) {
115-
return Collections.emptyMap();
116-
}
105+
byte[] keyspaceBin = getAdapter().getConverter().getConversionService().convert(keyspace + ":", byte[].class);
117106

118-
int offsetToUse = Math.max(0, (int) offset);
119-
if (rows > 0) {
120-
allKeys = allKeys.subList(Math.max(0, offsetToUse), Math.min(offsetToUse + rows, allKeys.size()));
121-
}
122-
for (byte[] id : allKeys) {
107+
Map<byte[], Map<byte[], byte[]>> rawData = new LinkedHashMap<>();
123108

124-
byte[] singleKey = ByteUtils.concat(keyspaceBin, id);
125-
rawData.put(id, connection.hGetAll(singleKey));
126-
}
109+
if (allKeys.isEmpty() || allKeys.size() < offset) {
110+
return Collections.emptyMap();
111+
}
127112

128-
return rawData;
113+
int offsetToUse = Math.max(0, (int) offset);
114+
if (rows > 0) {
115+
allKeys = allKeys.subList(Math.max(0, offsetToUse), Math.min(offsetToUse + rows, allKeys.size()));
116+
}
117+
for (byte[] id : allKeys) {
129118

119+
byte[] singleKey = ByteUtils.concat(keyspaceBin, id);
120+
rawData.put(id, connection.hGetAll(singleKey));
130121
}
122+
123+
return rawData;
131124
};
132125

133126
Map<byte[], Map<byte[], byte[]>> raw = this.getAdapter().execute(callback);
134127

135-
List<T> result = new ArrayList<T>(raw.size());
128+
List<T> result = new ArrayList<>(raw.size());
136129
for (Map.Entry<byte[], Map<byte[], byte[]>> entry : raw.entrySet()) {
137130

138131
RedisData data = new RedisData(entry.getValue());
139132
data.setId(getAdapter().getConverter().getConversionService().convert(entry.getKey(), String.class));
140-
data.setKeyspace(keyspace.toString());
133+
data.setKeyspace(keyspace);
141134

142135
T converted = this.getAdapter().getConverter().read(type, data);
143136

@@ -153,8 +146,8 @@ public Map<byte[], Map<byte[], byte[]>> doInRedis(RedisConnection connection) th
153146
* @see org.springframework.data.keyvalue.core.QueryEngine#execute(java.lang.Object, java.lang.Object, int, int, java.lang.String)
154147
*/
155148
@Override
156-
public Collection<?> execute(final RedisOperationChain criteria, Comparator<?> sort, long offset, int rows,
157-
final String keyspace) {
149+
public Collection<?> execute(RedisOperationChain criteria, Comparator<?> sort, long offset, int rows,
150+
String keyspace) {
158151
return execute(criteria, sort, offset, rows, keyspace, Object.class);
159152
}
160153

@@ -163,26 +156,22 @@ public Collection<?> execute(final RedisOperationChain criteria, Comparator<?> s
163156
* @see org.springframework.data.keyvalue.core.QueryEngine#count(java.lang.Object, java.lang.String)
164157
*/
165158
@Override
166-
public long count(final RedisOperationChain criteria, final String keyspace) {
159+
public long count(RedisOperationChain criteria, String keyspace) {
167160

168161
if (criteria == null) {
169162
return this.getAdapter().count(keyspace);
170163
}
171164

172-
return this.getAdapter().execute(new RedisCallback<Long>() {
165+
return this.getAdapter().execute(connection -> {
173166

174-
@Override
175-
public Long doInRedis(RedisConnection connection) throws DataAccessException {
176-
177-
String key = keyspace + ":";
178-
byte[][] keys = new byte[criteria.getSismember().size()][];
179-
int i = 0;
180-
for (Object o : criteria.getSismember()) {
181-
keys[i] = getAdapter().getConverter().getConversionService().convert(key + o, byte[].class);
182-
}
183-
184-
return (long) connection.sInter(keys).size();
167+
String key = keyspace + ":";
168+
byte[][] keys = new byte[criteria.getSismember().size()][];
169+
int i = 0;
170+
for (Object o : criteria.getSismember()) {
171+
keys[i] = getAdapter().getConverter().getConversionService().convert(key + o, byte[].class);
185172
}
173+
174+
return (long) connection.sInter(keys).size();
186175
});
187176
}
188177

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 the original author or authors.
2+
* Copyright 2015-2017 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.
@@ -15,15 +15,15 @@
1515
*/
1616
package org.springframework.data.redis.core.convert;
1717

18-
import java.io.Serializable;
1918
import java.util.Map;
2019

2120
import org.springframework.data.annotation.Reference;
2221

2322
/**
2423
* {@link ReferenceResolver} retrieves Objects marked with {@link Reference} from Redis.
25-
*
24+
*
2625
* @author Christoph Strobl
26+
* @author Mark Paluch
2727
* @since 1.7
2828
*/
2929
public interface ReferenceResolver {
@@ -33,5 +33,5 @@ public interface ReferenceResolver {
3333
* @param keyspace must not be {@literal null}.
3434
* @return {@literal null} if referenced object does not exist.
3535
*/
36-
Map<byte[], byte[]> resolveReference(Serializable id, String keyspace);
36+
Map<byte[], byte[]> resolveReference(Object id, String keyspace);
3737
}

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
*/
1616
package org.springframework.data.redis.core.convert;
1717

18-
import java.io.Serializable;
1918
import java.util.Map;
2019

21-
import org.springframework.dao.DataAccessException;
22-
import org.springframework.data.redis.connection.RedisConnection;
2320
import org.springframework.data.redis.core.RedisCallback;
2421
import org.springframework.data.redis.core.RedisKeyValueAdapter;
2522
import org.springframework.data.redis.core.RedisOperations;
@@ -28,8 +25,9 @@
2825

2926
/**
3027
* {@link ReferenceResolver} using {@link RedisKeyValueAdapter} to read raw data.
31-
*
28+
*
3229
* @author Christoph Strobl
30+
* @author Mark Paluch
3331
* @since 1.7
3432
*/
3533
public class ReferenceResolverImpl implements ReferenceResolver {
@@ -50,19 +48,13 @@ public ReferenceResolverImpl(RedisOperations<?, ?> redisOperations) {
5048

5149
/*
5250
* (non-Javadoc)
53-
* @see org.springframework.data.redis.core.convert.ReferenceResolver#resolveReference(java.io.Serializable, java.io.Serializable, java.lang.Class)
51+
* @see org.springframework.data.redis.core.convert.ReferenceResolver#resolveReference(java.lang.Object, java.lang.String)
5452
*/
5553
@Override
56-
public Map<byte[], byte[]> resolveReference(Serializable id, String keyspace) {
57-
58-
final byte[] key = converter.convert(keyspace + ":" + id);
54+
public Map<byte[], byte[]> resolveReference(Object id, String keyspace) {
5955

60-
return redisOps.execute(new RedisCallback<Map<byte[], byte[]>>() {
56+
byte[] key = converter.convert(keyspace + ":" + id);
6157

62-
@Override
63-
public Map<byte[], byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
64-
return connection.hGetAll(key);
65-
}
66-
});
58+
return redisOps.execute((RedisCallback<Map<byte[], byte[]>>) connection -> connection.hGetAll(key));
6759
}
6860
}

src/main/java/org/springframework/data/redis/hash/ObjectHashMapper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.redis.hash;
1717

18-
import java.io.Serializable;
1918
import java.util.Collections;
2019
import java.util.Map;
2120
import java.util.Set;
@@ -144,7 +143,7 @@ public Object fromHash(Map<byte[], byte[]> hash) {
144143
* @return
145144
*/
146145
public <T> T fromHash(Map<byte[], byte[]> hash, Class<T> type) {
147-
return (T) fromHash(hash);
146+
return type.cast(fromHash(hash));
148147
}
149148

150149
/**
@@ -158,10 +157,10 @@ private static class NoOpReferenceResolver implements ReferenceResolver {
158157

159158
/*
160159
* (non-Javadoc)
161-
* @see org.springframework.data.redis.core.convert.ReferenceResolver#resolveReference(java.io.Serializable, java.lang.String)
160+
* @see org.springframework.data.redis.core.convert.ReferenceResolver#resolveReference(java.lang.Object, java.lang.String)
162161
*/
163162
@Override
164-
public Map<byte[], byte[]> resolveReference(Serializable id, String keyspace) {
163+
public Map<byte[], byte[]> resolveReference(Object id, String keyspace) {
165164
return NO_REFERENCE;
166165
}
167166
}

0 commit comments

Comments
 (0)