Skip to content

Commit 7534d08

Browse files
DATAREDIS-425 - Cleanup.
- Keyspace and id types are now consequently converted into String types as those build up the actual key 'keyspace:id'. - Update test class name to reflect unit under tests name.
1 parent 84351c0 commit 7534d08

File tree

6 files changed

+50
-23
lines changed

6 files changed

+50
-23
lines changed

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext
131131
Assert.notNull(redisOps, "RedisOperations must not be null!");
132132
Assert.notNull(mappingContext, "RedisMappingContext must not be null!");
133133

134-
MappingRedisConverter mappingConverter = new MappingRedisConverter(mappingContext,
135-
new PathIndexResolver(mappingContext), new ReferenceResolverImpl(this));
134+
MappingRedisConverter mappingConverter = new MappingRedisConverter(mappingContext, new PathIndexResolver(
135+
mappingContext), new ReferenceResolverImpl(this));
136136
mappingConverter.setCustomConversions(customConversions == null ? new CustomConversions() : customConversions);
137137
mappingConverter.afterPropertiesSet();
138138

@@ -173,7 +173,7 @@ public Object put(final Serializable id, final Object item, final Serializable k
173173

174174
if (rdo.getId() == null) {
175175

176-
rdo.setId(id);
176+
rdo.setId(converter.getConversionService().convert(id, String.class));
177177

178178
if (!(item instanceof RedisData)) {
179179
KeyValuePersistentProperty idProperty = converter.getMappingContext().getPersistentEntity(item.getClass())
@@ -249,7 +249,10 @@ public Object get(Serializable id, Serializable keyspace) {
249249
*/
250250
public <T> T get(Serializable id, Serializable keyspace, Class<T> type) {
251251

252-
final byte[] binId = createKey(keyspace, id);
252+
String stringId = asString(id);
253+
String stringKeyspace = asString(keyspace);
254+
255+
final byte[] binId = createKey(stringKeyspace, stringId);
253256

254257
Map<byte[], byte[]> raw = redisOps.execute(new RedisCallback<Map<byte[], byte[]>>() {
255258

@@ -260,8 +263,8 @@ public Map<byte[], byte[]> doInRedis(RedisConnection connection) throws DataAcce
260263
});
261264

262265
RedisData data = new RedisData(raw);
263-
data.setId(id);
264-
data.setKeyspace(keyspace.toString());
266+
data.setId(stringId);
267+
data.setKeyspace(stringKeyspace);
265268

266269
return converter.read(type, data);
267270
}
@@ -287,15 +290,17 @@ public <T> T delete(final Serializable id, final Serializable keyspace, final Cl
287290

288291
if (o != null) {
289292

293+
final byte[] keyToDelete = createKey(asString(keyspace), asString(id));
294+
290295
redisOps.execute(new RedisCallback<Void>() {
291296

292297
@Override
293298
public Void doInRedis(RedisConnection connection) throws DataAccessException {
294299

295-
connection.del(createKey(keyspace, id));
300+
connection.del(keyToDelete);
296301
connection.sRem(binKeyspace, binId);
297302

298-
new IndexWriter(connection, converter).removeKeyFromIndexes(keyspace.toString(), binId);
303+
new IndexWriter(connection, converter).removeKeyFromIndexes(asString(keyspace), binId);
299304
return null;
300305
}
301306
});
@@ -322,7 +327,8 @@ public List<Map<byte[], byte[]>> doInRedis(RedisConnection connection) throws Da
322327
Set<byte[]> members = connection.sMembers(binKeyspace);
323328

324329
for (byte[] id : members) {
325-
rawData.add(connection.hGetAll(createKey(binKeyspace, id)));
330+
rawData.add(connection.hGetAll(createKey(asString(keyspace),
331+
getConverter().getConversionService().convert(id, String.class))));
326332
}
327333

328334
return rawData;
@@ -349,7 +355,7 @@ public void deleteAllOf(final Serializable keyspace) {
349355
public Void doInRedis(RedisConnection connection) throws DataAccessException {
350356

351357
connection.del(toBytes(keyspace));
352-
new IndexWriter(connection, converter).removeAllIndexes(keyspace.toString());
358+
new IndexWriter(connection, converter).removeAllIndexes(asString(keyspace));
353359
return null;
354360
}
355361
});
@@ -404,7 +410,12 @@ public void clear() {
404410
// nothing to do
405411
}
406412

407-
public byte[] createKey(Serializable keyspace, Serializable id) {
413+
private String asString(Serializable value) {
414+
return value instanceof String ? (String) value : getConverter().getConversionService()
415+
.convert(value, String.class);
416+
}
417+
418+
public byte[] createKey(String keyspace, String id) {
408419
return toBytes(keyspace + ":" + id);
409420
}
410421

@@ -600,7 +611,7 @@ public void setAdapter(RedisKeyValueAdapter adapter) {
600611
* @see org.springframework.data.redis.core.convert.ReferenceResolver#resolveReference(java.io.Serializable, java.io.Serializable, java.lang.Class)
601612
*/
602613
@Override
603-
public <T> T resolveReference(Serializable id, Serializable keyspace, Class<T> type) {
614+
public <T> T resolveReference(Serializable id, String keyspace, Class<T> type) {
604615
return (T) adapter.get(id, keyspace, type);
605616
}
606617
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.redis.core.convert;
1717

18-
import java.io.Serializable;
1918
import java.util.Collection;
2019
import java.util.HashMap;
2120
import java.util.Map;
@@ -345,7 +344,7 @@ public void write(Object source, final RedisData sink) {
345344
sink.setKeyspace(entity.getKeySpace());
346345

347346
writeInternal(entity.getKeySpace(), "", source, entity.getTypeInformation(), sink);
348-
sink.setId((Serializable) entity.getIdentifierAccessor(source).getIdentifier());
347+
sink.setId(getConversionService().convert(entity.getIdentifierAccessor(source).getIdentifier(), String.class));
349348

350349
Long ttl = entity.getTimeToLiveAccessor().getTimeToLive(source);
351350
if (ttl != null && ttl > 0) {

src/main/java/org/springframework/data/redis/core/convert/RedisData.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.core.convert;
1717

18-
import java.io.Serializable;
1918
import java.util.Collections;
2019
import java.util.HashSet;
2120
import java.util.Map;
@@ -34,7 +33,7 @@
3433
public class RedisData {
3534

3635
private String keyspace;
37-
private Serializable id;
36+
private String id;
3837

3938
private Bucket bucket;
4039
private Set<IndexedData> indexedData;
@@ -74,14 +73,14 @@ public RedisData(Bucket bucket) {
7473
*
7574
* @param id
7675
*/
77-
public void setId(Serializable id) {
76+
public void setId(String id) {
7877
this.id = id;
7978
}
8079

8180
/**
8281
* @return
8382
*/
84-
public Serializable getId() {
83+
public String getId() {
8584
return this.id;
8685
}
8786

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ public interface ReferenceResolver {
3333
* @param type must not be {@literal null}.
3434
* @return {@literal null} if referenced object does not exist.
3535
*/
36-
<T> T resolveReference(Serializable id, Serializable keyspace, Class<T> type);
36+
<T> T resolveReference(Serializable id, String keyspace, Class<T> type);
3737
}

src/main/java/org/springframework/data/redis/core/index/RedisIndexDefinition.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import org.springframework.util.StringUtils;
2626

2727
/**
28-
* {@link IndexDefinition} implementation.
28+
* Base {@link IndexDefinition} implementation.
2929
*
3030
* @author Christoph Strobl
3131
* @since 1.7
3232
*/
33-
public class RedisIndexDefinition implements IndexDefinition {
33+
public abstract class RedisIndexDefinition implements IndexDefinition {
3434

3535
private final String keyspace;
3636
private final String indexName;
@@ -45,7 +45,7 @@ public class RedisIndexDefinition implements IndexDefinition {
4545
* @param path
4646
* @param indexName
4747
*/
48-
public RedisIndexDefinition(String keyspace, String path, String indexName) {
48+
protected RedisIndexDefinition(String keyspace, String path, String indexName) {
4949

5050
this.keyspace = keyspace;
5151
this.indexName = indexName;

src/test/java/org/springframework/data/redis/core/convert/IndexResolverImplUnitTests.java renamed to src/test/java/org/springframework/data/redis/core/convert/PathIndexResolverUnitTests.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.redis.core.convert;
1717

18+
import static org.hamcrest.collection.IsEmptyCollection.*;
1819
import static org.hamcrest.core.Is.*;
1920
import static org.hamcrest.core.IsCollectionContaining.*;
2021
import static org.hamcrest.core.IsNull.*;
@@ -41,6 +42,7 @@
4142
import org.springframework.data.redis.core.convert.ConversionTestEntities.Location;
4243
import org.springframework.data.redis.core.convert.ConversionTestEntities.Person;
4344
import org.springframework.data.redis.core.convert.ConversionTestEntities.PersonWithAddressReference;
45+
import org.springframework.data.redis.core.convert.ConversionTestEntities.Size;
4446
import org.springframework.data.redis.core.convert.ConversionTestEntities.TaVeren;
4547
import org.springframework.data.redis.core.convert.ConversionTestEntities.TheWheelOfTime;
4648
import org.springframework.data.redis.core.index.IndexConfiguration;
@@ -53,7 +55,7 @@
5355
* @author Christoph Strobl
5456
*/
5557
@RunWith(MockitoJUnitRunner.class)
56-
public class IndexResolverImplUnitTests {
58+
public class PathIndexResolverUnitTests {
5759

5860
IndexConfiguration indexConfig;
5961
PathIndexResolver indexResolver;
@@ -409,6 +411,7 @@ public void resolveIndexShouldInspectObjectTypeValuesInListProperties() {
409411
*/
410412
@Test
411413
public void resolveIndexAllowCustomIndexName() {
414+
412415
indexConfig.addIndexDefinition(new SimpleIndexDefinition(KEYSPACE_PERSON, "items.type", "itemsType"));
413416

414417
Item hat = new Item();
@@ -425,6 +428,21 @@ public void resolveIndexAllowCustomIndexName() {
425428
assertThat(indexes, hasItem(new SimpleIndexedPropertyValue(KEYSPACE_PERSON, "itemsType", "hat")));
426429
}
427430

431+
/**
432+
* @see DATAREDIS-425
433+
*/
434+
@Test
435+
public void resolveIndexForTypeThatHasNoIndexDefined() {
436+
437+
Size size = new Size();
438+
size.height = 10;
439+
size.length = 20;
440+
size.width = 30;
441+
442+
Set<IndexedData> indexes = indexResolver.resolveIndexesFor(ClassTypeInformation.from(Size.class), size);
443+
assertThat(indexes, is(empty()));
444+
}
445+
428446
private IndexedData resolve(String path, Object value) {
429447

430448
Set<IndexedData> data = indexResolver.resolveIndex(KEYSPACE_PERSON, path, propertyMock, value);

0 commit comments

Comments
 (0)