|
108 | 108 | *
|
109 | 109 | * @author Christoph Strobl
|
110 | 110 | * @author Greg Turnquist
|
| 111 | + * @author Mark Paluch |
111 | 112 | * @since 1.7
|
112 | 113 | */
|
113 | 114 | public class MappingRedisConverter implements RedisConverter, InitializingBean {
|
@@ -411,86 +412,95 @@ protected void writePartialUpdate(PartialUpdate<?> update, RedisData sink) {
|
411 | 412 | String path = pUpdate.getPropertyPath();
|
412 | 413 |
|
413 | 414 | if (UpdateCommand.SET.equals(pUpdate.getCmd())) {
|
| 415 | + writePartialPropertyUpdate(update, pUpdate, sink, entity, path); |
| 416 | + } |
| 417 | + } |
| 418 | + } |
414 | 419 |
|
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) { |
418 | 429 |
|
419 |
| - targetProperty = getTargetPropertyOrNullForPath(path.replaceAll("\\.\\[.*\\]", ""), update.getTarget()); |
| 430 | + KeyValuePersistentProperty targetProperty = getTargetPropertyOrNullForPath(path, update.getTarget()); |
420 | 431 |
|
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) { |
426 | 433 |
|
427 |
| - writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(), ti, sink); |
428 |
| - continue; |
429 |
| - } |
| 434 | + targetProperty = getTargetPropertyOrNullForPath(path.replaceAll("\\.\\[.*\\]", ""), update.getTarget()); |
430 | 435 |
|
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()); |
432 | 441 |
|
433 |
| - if (targetProperty.isCollectionLike()) { |
| 442 | + writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(), ti, sink); |
| 443 | + return; |
| 444 | + } |
434 | 445 |
|
435 |
| - KeyValuePersistentEntity<?> ref = mappingContext.getPersistentEntity( |
436 |
| - targetProperty.getAssociation().getInverse().getTypeInformation().getComponentType().getActualType()); |
| 446 | + if (targetProperty.isAssociation()) { |
437 | 447 |
|
438 |
| - int i = 0; |
439 |
| - for (Object o : (Collection<?>) pUpdate.getValue()) { |
| 448 | + if (targetProperty.isCollectionLike()) { |
440 | 449 |
|
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()); |
447 | 452 |
|
448 |
| - KeyValuePersistentEntity<?> ref = mappingContext |
449 |
| - .getPersistentEntity(targetProperty.getAssociation().getInverse().getTypeInformation()); |
| 453 | + int i = 0; |
| 454 | + for (Object o : (Collection<?>) pUpdate.getValue()) { |
450 | 455 |
|
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++; |
454 | 459 | }
|
| 460 | + } else { |
455 | 461 |
|
456 |
| - else if (targetProperty.isCollectionLike()) { |
| 462 | + KeyValuePersistentEntity<?> ref = mappingContext |
| 463 | + .getPersistentEntity(targetProperty.getAssociation().getInverse().getTypeInformation()); |
457 | 464 |
|
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()) { |
463 | 469 |
|
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()) { |
465 | 475 |
|
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>(); |
475 | 477 |
|
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 | + } |
478 | 487 |
|
479 |
| - writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(), |
480 |
| - targetProperty.getTypeInformation(), sink); |
| 488 | + writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType(), map, sink); |
| 489 | + } else { |
481 | 490 |
|
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); |
484 | 493 |
|
485 |
| - if (data.isEmpty()) { |
| 494 | + Set<IndexedData> data = indexResolver.resolveIndexesFor(entity.getKeySpace(), pUpdate.getPropertyPath(), |
| 495 | + targetProperty.getTypeInformation(), pUpdate.getValue()); |
486 | 496 |
|
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()); |
489 | 501 |
|
490 |
| - } |
491 |
| - sink.addIndexedData(data); |
492 |
| - } |
493 | 502 | }
|
| 503 | + sink.addIndexedData(data); |
494 | 504 | }
|
495 | 505 | }
|
496 | 506 |
|
|
0 commit comments