Skip to content

Commit 042db1c

Browse files
DATAREDIS-471 - Hacking.
Do not return the updated entity by default.
1 parent 0eb2faa commit 042db1c

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,9 @@ public Long doInRedis(RedisConnection connection) throws DataAccessException {
394394
return count != null ? count.longValue() : 0;
395395
}
396396

397-
@SuppressWarnings("unchecked")
398-
public <T> T update(final PartialUpdate<T> update) {
397+
public void update(final PartialUpdate<?> update) {
399398

400-
final RedisPersistentEntity<T> entity = (RedisPersistentEntity<T>) this.converter.getMappingContext()
401-
.getPersistentEntity(update.getTarget());
399+
final RedisPersistentEntity<?> entity = this.converter.getMappingContext().getPersistentEntity(update.getTarget());
402400

403401
final String keyspace = entity.getKeySpace();
404402
final Object id = update.getId();
@@ -471,8 +469,6 @@ public Void doInRedis(RedisConnection connection) throws DataAccessException {
471469
}
472470

473471
});
474-
475-
return get(toBytes(id), keyspace, update.getTarget());
476472
}
477473

478474
/**

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public RedisMappingContext getMappingContext() {
7070
* </code>
7171
*
7272
* <pre>
73+
*
7374
* @param callback provides the to retrieve entity ids. Must not be {@literal null}.
7475
* @param type must not be {@literal null}.
7576
* @return empty list if not elements found.
@@ -89,15 +90,14 @@ public List<T> doInRedis(RedisKeyValueAdapter adapter) {
8990
return Collections.emptyList();
9091
}
9192

92-
Iterable<?> ids = ClassUtils.isAssignable(Iterable.class, callbackResult.getClass()) ? (Iterable<?>) callbackResult
93-
: Collections.singleton(callbackResult);
93+
Iterable<?> ids = ClassUtils.isAssignable(Iterable.class, callbackResult.getClass())
94+
? (Iterable<?>) callbackResult : Collections.singleton(callbackResult);
9495

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

98-
String idToUse = adapter.getConverter().getConversionService().canConvert(id.getClass(), String.class) ? adapter
99-
.getConverter().getConversionService().convert(id, String.class)
100-
: id.toString();
99+
String idToUse = adapter.getConverter().getConversionService().canConvert(id.getClass(), String.class)
100+
? adapter.getConverter().getConversionService().convert(id, String.class) : id.toString();
101101

102102
T candidate = findById(idToUse, type);
103103
if (candidate != null) {
@@ -118,20 +118,21 @@ public List<T> doInRedis(RedisKeyValueAdapter adapter) {
118118
public void update(Object objectToUpdate) {
119119

120120
if (objectToUpdate instanceof PartialUpdate) {
121-
doPartialUpdate((PartialUpdate<Object>) objectToUpdate);
121+
doPartialUpdate((PartialUpdate<?>) objectToUpdate);
122122
}
123123

124124
super.update(objectToUpdate);
125125
}
126126

127-
protected <T> T doPartialUpdate(final PartialUpdate<T> update) {
127+
protected void doPartialUpdate(final PartialUpdate<?> update) {
128128

129-
return execute(new RedisKeyValueCallback<T>() {
129+
execute(new RedisKeyValueCallback<Void>() {
130130

131131
@Override
132-
public T doInRedis(RedisKeyValueAdapter adapter) {
132+
public Void doInRedis(RedisKeyValueAdapter adapter) {
133133

134-
return adapter.update(update);
134+
adapter.update(update);
135+
return null;
135136
}
136137
});
137138
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.data.convert.EntityConverter;
1919
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentEntity;
2020
import org.springframework.data.keyvalue.core.mapping.KeyValuePersistentProperty;
21+
import org.springframework.data.redis.core.mapping.RedisMappingContext;
2122

2223
/**
2324
* Redis specific {@link EntityConverter}.
@@ -28,4 +29,10 @@
2829
public interface RedisConverter
2930
extends EntityConverter<KeyValuePersistentEntity<?>, KeyValuePersistentProperty, Object, RedisData> {
3031

32+
/*
33+
* (non-Javadoc)
34+
* @see org.springframework.data.convert.EntityConverter#getMappingContext()
35+
*/
36+
@Override
37+
RedisMappingContext getMappingContext();
3138
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.hamcrest.core.Is.*;
1919
import static org.hamcrest.core.IsCollectionContaining.*;
20+
import static org.hamcrest.core.IsEqual.*;
2021
import static org.junit.Assert.*;
2122

2223
import java.util.Arrays;
@@ -202,6 +203,9 @@ public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessExcep
202203
assertThat(result.size(), is(0));
203204
}
204205

206+
/**
207+
* @see DATAREDIS-471
208+
*/
205209
@Test
206210
public void partialUpdate() {
207211

@@ -213,16 +217,19 @@ public void partialUpdate() {
213217
/*
214218
* Set the lastname and make sure we've an index on it afterwards
215219
*/
216-
Person update1 = new Person(rand.id, "Rand-Update", "al-thor");
220+
Person update1 = new Person(rand.id, null, "al-thor");
217221
PartialUpdate<Person> update = new PartialUpdate<Person>(rand.id, update1);
218222

219-
assertThat(template.doPartialUpdate(update), is(update1));
223+
template.doPartialUpdate(update);
220224

225+
assertThat(template.findById(rand.id, Person.class), is(equalTo(new Person(rand.id, "rand", "al-thor"))));
221226
nativeTemplate.execute(new RedisCallback<Void>() {
222227

223228
@Override
224229
public Void doInRedis(RedisConnection connection) throws DataAccessException {
225230

231+
assertThat(connection.hGet(("template-test-person:" + rand.id).getBytes(), "firstname".getBytes()),
232+
is(equalTo("rand".getBytes())));
226233
assertThat(connection.exists("template-test-person:lastname:al-thor".getBytes()), is(true));
227234
assertThat(connection.sIsMember("template-test-person:lastname:al-thor".getBytes(), rand.id.getBytes()),
228235
is(true));
@@ -235,8 +242,9 @@ public Void doInRedis(RedisConnection connection) throws DataAccessException {
235242
*/
236243
update = new PartialUpdate<Person>(rand.id, Person.class).set("firstname", "frodo");
237244

238-
assertThat(template.doPartialUpdate(update), is(new Person(rand.id, "frodo", "al-thor")));
245+
template.doPartialUpdate(update);
239246

247+
assertThat(template.findById(rand.id, Person.class), is(equalTo(new Person(rand.id, "frodo", "al-thor"))));
240248
nativeTemplate.execute(new RedisCallback<Void>() {
241249

242250
@Override
@@ -256,8 +264,9 @@ public Void doInRedis(RedisConnection connection) throws DataAccessException {
256264
update.del("firstname");
257265
update.set("lastname", "baggins");
258266

259-
assertThat(template.doPartialUpdate(update), is(new Person(rand.id, null, "baggins")));
267+
template.doPartialUpdate(update);
260268

269+
assertThat(template.findById(rand.id, Person.class), is(equalTo(new Person(rand.id, null, "baggins"))));
261270
nativeTemplate.execute(new RedisCallback<Void>() {
262271

263272
@Override
@@ -277,8 +286,9 @@ public Void doInRedis(RedisConnection connection) throws DataAccessException {
277286
update = new PartialUpdate<Person>(rand.id, Person.class);
278287
update.del("lastname");
279288

280-
assertThat(template.doPartialUpdate(update), is(new Person(rand.id, null, null)));
289+
template.doPartialUpdate(update);
281290

291+
assertThat(template.findById(rand.id, Person.class), is(equalTo(new Person(rand.id, null, null))));
282292
nativeTemplate.execute(new RedisCallback<Void>() {
283293

284294
@Override

0 commit comments

Comments
 (0)