Make sure HotRod store does not return empty delegate

Closes #12304
This commit is contained in:
Michal Hajas 2022-06-21 11:41:42 +02:00 committed by Hynek Mlnařík
parent c972ec4383
commit e0efdcae22
4 changed files with 19 additions and 6 deletions

View file

@ -169,6 +169,7 @@ public class GenerateHotRodEntityImplementationsProcessor extends AbstractGenera
pw.println(" " + pw.println(" " +
"public " + hotRodSimpleClassName + "(" + className + " " + ENTITY_VARIABLE + ") {" "public " + hotRodSimpleClassName + "(" + className + " " + ENTITY_VARIABLE + ") {"
); );
pw.println(" java.util.Objects.requireNonNull(" + ENTITY_VARIABLE + ");");
pw.println(" this." + ENTITY_VARIABLE + " = " + ENTITY_VARIABLE + ";"); pw.println(" this." + ENTITY_VARIABLE + " = " + ENTITY_VARIABLE + ";");
if (usingGeneratedCloner) { if (usingGeneratedCloner) {
pw.println(" this.cloner = DeepCloner.DUMB_CLONER;"); pw.println(" this.cloner = DeepCloner.DUMB_CLONER;");

View file

@ -93,15 +93,25 @@ public class HotRodMapStorage<K, E extends AbstractHotRodEntity, V extends HotRo
Objects.requireNonNull(key, "Key must be non-null"); Objects.requireNonNull(key, "Key must be non-null");
K k = keyConverter.fromStringSafe(key); K k = keyConverter.fromStringSafe(key);
V v = delegateProducer.apply(remoteCache.get(k)); // Obtain value from Infinispan
if (v == null || v.getHotRodEntity() == null) return null; E hotRodEntity = remoteCache.get(k);
return isExpirableEntity && isExpired((ExpirableEntity) v, true) ? null : v; if (hotRodEntity == null) return null;
// Create delegate that implements Map*Entity
V delegateEntity = delegateProducer.apply(hotRodEntity);
// Check expiration if necessary and return value
return isExpirableEntity && isExpired((ExpirableEntity) delegateEntity, true) ? null : delegateEntity;
} }
@Override @Override
public V update(V value) { public V update(V value) {
K key = keyConverter.fromStringSafe(value.getId()); K key = keyConverter.fromStringSafe(value.getId());
return delegateProducer.apply(remoteCache.replace(key, value.getHotRodEntity()));
E previousValue = remoteCache.replace(key, value.getHotRodEntity());
if (previousValue == null) return null;
return delegateProducer.apply(previousValue);
} }
@Override @Override

View file

@ -95,8 +95,7 @@ public class SingleUseObjectHotRodMapStorage<K, E extends AbstractHotRodEntity,
SingleUseObjectModelCriteriaBuilder mcb = criteria.flashToModelCriteriaBuilder(createSingleUseObjectCriteriaBuilder()); SingleUseObjectModelCriteriaBuilder mcb = criteria.flashToModelCriteriaBuilder(createSingleUseObjectCriteriaBuilder());
if (mcb.isValid()) { if (mcb.isValid()) {
HotRodSingleUseObjectEntityDelegate value = read(mcb.getKey()); HotRodSingleUseObjectEntityDelegate value = read(mcb.getKey());
return value != null ? Stream.of(value) : Stream.empty();
return value != null && value.getHotRodEntity() != null ? Stream.of(value) : Stream.empty();
} }
return super.read(queryParameters); return super.read(queryParameters);

View file

@ -37,6 +37,9 @@ public interface ConcurrentHashMapCrudOperations<V extends AbstractEntity & Upda
* Updates the object with the key of the {@code value}'s ID in the storage if it already exists. * Updates the object with the key of the {@code value}'s ID in the storage if it already exists.
* *
* @param value Updated value * @param value Updated value
* @return the previous value associated with the specified key, or null if there was no mapping for the key.
* (A null return can also indicate that the map previously associated null with the key,
* if the implementation supports null values.)
* @throws NullPointerException if the object or its {@code id} is {@code null} * @throws NullPointerException if the object or its {@code id} is {@code null}
* @see AbstractEntity#getId() * @see AbstractEntity#getId()
*/ */