Skip to content

Commit b517a38

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-471 - Update reference documentation and fix managed bean lifecycle.
First round of polishing, update reference documentation, enhance JavaDoc, remove destroy of managed bean, refactor property update of writePartialUpdate in method.
1 parent 68870ad commit b517a38

File tree

6 files changed

+74
-83
lines changed

6 files changed

+74
-83
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ Indexes set on properties of referenced types will not be resolved.
490490
[[redis.repositories.partial-updates]]
491491
== Persisting Partial Updates
492492
In some cases it is not necessary to load and rewrite the entire entity just to set a new value within it. A session timestamp for last active time might be such a scenario where you just want to alter one property.
493-
`PartialUpdate` allows to define `set`, `delete` actions on existing objects while taking care of updating potential expiration times of the entity itself as well as index structures.
493+
`PartialUpdate` allows to define `set` and `delete` actions on existing objects while taking care of updating potential expiration times of the entity itself as well as index structures.
494494

495495
.Sample Partial Update
496496
====
@@ -515,7 +515,7 @@ update = new PartialUpdate<Person>("e2c7dcee", Person.class)
515515
516516
template.update(update);
517517
----
518-
<1> Set the simple property _firstname_ to _mat_
518+
<1> Set the simple property _firstname_ to _mat_.
519519
<2> Set the simple property _address.city_ to _emond's field_ without having to pass in the entire object. This does not work when a custom conversion is registered.
520520
<3> Remove the property _age_.
521521
<4> Set complex property _address_.

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,29 +140,6 @@ public void update(Object objectToUpdate) {
140140
super.update(objectToUpdate);
141141
}
142142

143-
/*
144-
* (non-Javadoc)
145-
* @see org.springframework.data.keyvalue.core.KeyValueTemplate#destroy()
146-
*/
147-
@Override
148-
public void destroy() throws Exception {
149-
150-
execute(new RedisKeyValueCallback<Void>() {
151-
152-
@Override
153-
public Void doInRedis(RedisKeyValueAdapter adapter) {
154-
155-
try {
156-
adapter.destroy();
157-
} catch (Exception e) {
158-
throw new RedisSystemException(e.getMessage(), e);
159-
}
160-
return null;
161-
}
162-
});
163-
164-
}
165-
166143
protected void doPartialUpdate(final PartialUpdate<?> update) {
167144

168145
execute(new RedisKeyValueCallback<Void>() {

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

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
*
109109
* @author Christoph Strobl
110110
* @author Greg Turnquist
111+
* @author Mark Paluch
111112
* @since 1.7
112113
*/
113114
public class MappingRedisConverter implements RedisConverter, InitializingBean {
@@ -411,86 +412,95 @@ protected void writePartialUpdate(PartialUpdate<?> update, RedisData sink) {
411412
String path = pUpdate.getPropertyPath();
412413

413414
if (UpdateCommand.SET.equals(pUpdate.getCmd())) {
415+
writePartialPropertyUpdate(update, pUpdate, sink, entity, path);
416+
}
417+
}
418+
}
414419

415-
KeyValuePersistentProperty targetProperty = getTargetPropertyOrNullForPath(path, update.getTarget());
416-
417-
if (targetProperty == null) {
420+
/**
421+
* @param update
422+
* @param pUpdate
423+
* @param sink
424+
* @param entity
425+
* @param path
426+
*/
427+
private void writePartialPropertyUpdate(PartialUpdate<?> update, PropertyUpdate pUpdate, RedisData sink,
428+
RedisPersistentEntity<?> entity, String path) {
418429

419-
targetProperty = getTargetPropertyOrNullForPath(path.replaceAll("\\.\\[.*\\]", ""), update.getTarget());
430+
KeyValuePersistentProperty targetProperty = getTargetPropertyOrNullForPath(path, update.getTarget());
420431

421-
TypeInformation<?> ti = targetProperty == null ? ClassTypeInformation.OBJECT
422-
: (targetProperty.isMap()
423-
? (targetProperty.getTypeInformation().getMapValueType() != null
424-
? targetProperty.getTypeInformation().getMapValueType() : ClassTypeInformation.OBJECT)
425-
: targetProperty.getTypeInformation().getActualType());
432+
if (targetProperty == null) {
426433

427-
writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(), ti, sink);
428-
continue;
429-
}
434+
targetProperty = getTargetPropertyOrNullForPath(path.replaceAll("\\.\\[.*\\]", ""), update.getTarget());
430435

431-
if (targetProperty.isAssociation()) {
436+
TypeInformation<?> ti = targetProperty == null ? ClassTypeInformation.OBJECT
437+
: (targetProperty.isMap()
438+
? (targetProperty.getTypeInformation().getMapValueType() != null
439+
? targetProperty.getTypeInformation().getMapValueType() : ClassTypeInformation.OBJECT)
440+
: targetProperty.getTypeInformation().getActualType());
432441

433-
if (targetProperty.isCollectionLike()) {
442+
writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(), ti, sink);
443+
return;
444+
}
434445

435-
KeyValuePersistentEntity<?> ref = mappingContext.getPersistentEntity(
436-
targetProperty.getAssociation().getInverse().getTypeInformation().getComponentType().getActualType());
446+
if (targetProperty.isAssociation()) {
437447

438-
int i = 0;
439-
for (Object o : (Collection<?>) pUpdate.getValue()) {
448+
if (targetProperty.isCollectionLike()) {
440449

441-
Object refId = ref.getPropertyAccessor(o).getProperty(ref.getIdProperty());
442-
sink.getBucket().put(pUpdate.getPropertyPath() + ".[" + i + "]",
443-
toBytes(ref.getKeySpace() + ":" + refId));
444-
i++;
445-
}
446-
} else {
450+
KeyValuePersistentEntity<?> ref = mappingContext.getPersistentEntity(
451+
targetProperty.getAssociation().getInverse().getTypeInformation().getComponentType().getActualType());
447452

448-
KeyValuePersistentEntity<?> ref = mappingContext
449-
.getPersistentEntity(targetProperty.getAssociation().getInverse().getTypeInformation());
453+
int i = 0;
454+
for (Object o : (Collection<?>) pUpdate.getValue()) {
450455

451-
Object refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getIdProperty());
452-
sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId));
453-
}
456+
Object refId = ref.getPropertyAccessor(o).getProperty(ref.getIdProperty());
457+
sink.getBucket().put(pUpdate.getPropertyPath() + ".[" + i + "]", toBytes(ref.getKeySpace() + ":" + refId));
458+
i++;
454459
}
460+
} else {
455461

456-
else if (targetProperty.isCollectionLike()) {
462+
KeyValuePersistentEntity<?> ref = mappingContext
463+
.getPersistentEntity(targetProperty.getAssociation().getInverse().getTypeInformation());
457464

458-
Collection<?> collection = pUpdate.getValue() instanceof Collection ? (Collection<?>) pUpdate.getValue()
459-
: Collections.<Object> singleton(pUpdate.getValue());
460-
writeCollection(entity.getKeySpace(), pUpdate.getPropertyPath(), collection,
461-
targetProperty.getTypeInformation().getActualType(), sink);
462-
} else if (targetProperty.isMap()) {
465+
Object refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getIdProperty());
466+
sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId));
467+
}
468+
} else if (targetProperty.isCollectionLike()) {
463469

464-
Map<Object, Object> map = new HashMap<Object, Object>();
470+
Collection<?> collection = pUpdate.getValue() instanceof Collection ? (Collection<?>) pUpdate.getValue()
471+
: Collections.<Object> singleton(pUpdate.getValue());
472+
writeCollection(entity.getKeySpace(), pUpdate.getPropertyPath(), collection,
473+
targetProperty.getTypeInformation().getActualType(), sink);
474+
} else if (targetProperty.isMap()) {
465475

466-
if (pUpdate.getValue() instanceof Map) {
467-
map.putAll((Map<?, ?>) pUpdate.getValue());
468-
} else if (pUpdate.getValue() instanceof Map.Entry) {
469-
map.put(((Map.Entry<?, ?>) pUpdate.getValue()).getKey(), ((Map.Entry<?, ?>) pUpdate.getValue()).getValue());
470-
} else {
471-
throw new MappingException(
472-
String.format("Cannot set update value for map property '%s' to '%s'. Please use a Map or Map.Entry.",
473-
pUpdate.getPropertyPath(), pUpdate.getValue()));
474-
}
476+
Map<Object, Object> map = new HashMap<Object, Object>();
475477

476-
writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType(), map, sink);
477-
} else {
478+
if (pUpdate.getValue() instanceof Map) {
479+
map.putAll((Map<?, ?>) pUpdate.getValue());
480+
} else if (pUpdate.getValue() instanceof Entry) {
481+
map.put(((Entry<?, ?>) pUpdate.getValue()).getKey(), ((Entry<?, ?>) pUpdate.getValue()).getValue());
482+
} else {
483+
throw new MappingException(
484+
String.format("Cannot set update value for map property '%s' to '%s'. Please use a Map or Map.Entry.",
485+
pUpdate.getPropertyPath(), pUpdate.getValue()));
486+
}
478487

479-
writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(),
480-
targetProperty.getTypeInformation(), sink);
488+
writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType(), map, sink);
489+
} else {
481490

482-
Set<IndexedData> data = indexResolver.resolveIndexesFor(entity.getKeySpace(), pUpdate.getPropertyPath(),
483-
targetProperty.getTypeInformation(), pUpdate.getValue());
491+
writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(),
492+
targetProperty.getTypeInformation(), sink);
484493

485-
if (data.isEmpty()) {
494+
Set<IndexedData> data = indexResolver.resolveIndexesFor(entity.getKeySpace(), pUpdate.getPropertyPath(),
495+
targetProperty.getTypeInformation(), pUpdate.getValue());
486496

487-
data = indexResolver.resolveIndexesFor(entity.getKeySpace(), pUpdate.getPropertyPath(),
488-
targetProperty.getOwner().getTypeInformation(), pUpdate.getValue());
497+
if (data.isEmpty()) {
498+
499+
data = indexResolver.resolveIndexesFor(entity.getKeySpace(), pUpdate.getPropertyPath(),
500+
targetProperty.getOwner().getTypeInformation(), pUpdate.getValue());
489501

490-
}
491-
sink.addIndexedData(data);
492-
}
493502
}
503+
sink.addIndexedData(data);
494504
}
495505
}
496506

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public PathIndexResolver() {
6262
/**
6363
* Creates new {@link PathIndexResolver} with given {@link IndexConfiguration}.
6464
*
65-
* @param mapppingContext must not be {@literal null}.
65+
* @param mappingContext must not be {@literal null}.
6666
*/
6767
public PathIndexResolver(RedisMappingContext mappingContext) {
6868

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
package org.springframework.data.redis.core.convert;
1717

1818
/**
19+
* {@link RemoveIndexedData} represents a removed index entry from a secondary index for a property path in a given keyspace.
20+
*
1921
* @author Christoph Strobl
22+
* @author Mark Paluch
2023
*/
2124
public class RemoveIndexedData implements IndexedData {
2225

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public Void doInRedis(RedisConnection connection) throws DataAccessException {
109109
});
110110

111111
template.destroy();
112+
adapter.destroy();
112113
}
113114

114115
/**

0 commit comments

Comments
 (0)