diff --git a/src/main/java/net/openhft/chronicle/map/MapEventListener.java b/src/main/java/net/openhft/chronicle/map/MapEventListener.java index 90bfa492c..51215d8d7 100644 --- a/src/main/java/net/openhft/chronicle/map/MapEventListener.java +++ b/src/main/java/net/openhft/chronicle/map/MapEventListener.java @@ -137,6 +137,15 @@ public abstract class MapEventListener implements Serializable { return true; } + /** + * This performs an early check to see if the values will be consumed or discarded. + * + * @return true unless you know the event won't be needed. + */ + public boolean usesValue() { + return true; + } + private static class LoggingMapEventListener extends MapEventListener { public final static Logger LOGGER = LoggerFactory.getLogger(LoggingMapEventListener.class); private static final long serialVersionUID = 0L; diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 21be35531..3d1af9d9e 100755 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -959,7 +959,7 @@ final class ReplicatedChronicleMap, byte replacedIdentifier = (byte) 0; long replacedTimeStamp = 0; final K key1 = toKey.toInstance(copies, key, keySize); - final V newValue = toValue.toInstance(copies, v, valueSize); + final V newValue = eventListener.usesValue() ? toValue.toInstance(copies, v, valueSize) : null; writeUnlock(); // TODO unlocking here is dangerous, because this method is a part of // acquireUsingLocked(), after which lock assumed to be valid @@ -2161,7 +2161,7 @@ class VanillaChronicleMap, } if (eventListener != null && eventListener.isActive()) { eventListener.onGetFound(toKey.toInstance(copies, key, keySize), - toValue.toInstance(copies, v, valueSize)); + eventListener.usesValue() ? toValue.toInstance(copies, v, valueSize) : null); } return v; @@ -2234,7 +2234,7 @@ class VanillaChronicleMap, ); if (eventListener != null && eventListener.isActive()) { final K key1 = toKey.toInstance(copies, key, keySize); - final V newValue = toValue.toInstance(copies, value, valueSize); + final V newValue = eventListener.usesValue() ? toValue.toInstance(copies, value, valueSize) : null; writeUnlock(); try { eventListener.onPut(key1, @@ -2270,7 +2270,7 @@ class VanillaChronicleMap, byte replacedIdentifier = 0; long replacedTimeStamp = 0; final K key1 = toKey.toInstance(copies, key, keySize); - final V newValue = toValue.toInstance(copies, value, valueSize); + final V newValue = eventListener.usesValue() ? toValue.toInstance(copies, value, valueSize) : null; writeUnlock(); try { @@ -2383,7 +2383,7 @@ class VanillaChronicleMap, segmentState.identifier, (byte) 0, segmentState.timestamp, 0, this); if (eventListener != null && eventListener.isActive()) { final K key1 = toKey.toInstance(copies, key, keySize); - final V newValue = toValue.toInstance(copies, value, valueSize); + final V newValue = eventListener.usesValue() ? toValue.toInstance(copies, value, valueSize) : null; writeUnlock(); try { eventListener.onPut(key1, @@ -2428,7 +2428,7 @@ class VanillaChronicleMap, entry.positionAddr(valueAddr); prevValueInstance = null; if (!putReturnsNull) - prevValueInstance = readValue(copies, entry, null, prevValueSize); + prevValueInstance = eventListener.usesValue() ? readValue(copies, entry, null, prevValueSize) : null; } boolean doPutValue; @@ -2466,7 +2466,7 @@ class VanillaChronicleMap, segmentState.timestamp, replacedTimestamp, this); if (eventListener != null && eventListener.isActive()) { final K key1 = toKey.toInstance(copies, key, keySize); - final V newValue = toValue.toInstance(copies, value, valueSize); + final V newValue = eventListener.usesValue() ? toValue.toInstance(copies, value, valueSize) : null; writeUnlock(); try { @@ -2714,6 +2714,7 @@ class VanillaChronicleMap, RV removedValue = null; if ((!booleanResult && !resultUnused) || (eventListener != null && eventListener.isActive())) { // todo reuse some value + // todo check eventListener.usesValue() removedValue = readValue.readValue(copies, entry, null, valueSize); entry.position(entry.position() - valueSize); } @@ -2735,8 +2736,8 @@ class VanillaChronicleMap, segmentState.identifier, replacedIdentifier, segmentState.timestamp, replacedTimestamp, this); if (eventListener != null && eventListener.isActive()) { - V removedValueForEventListener = - toValue.toInstance(copies, removedValue, valueSize); + V removedValueForEventListener = eventListener.usesValue() ? + toValue.toInstance(copies, removedValue, valueSize) : null; final K key1 = toKey.toInstance(copies, key, keySize); writeUnlock(); @@ -2913,8 +2914,8 @@ class VanillaChronicleMap, } if (eventListener != null && eventListener.isActive()) { final K key1 = toKey.toInstance(copies, key, keySize); - final V newValue1 = toValue.toInstance(copies, newValue, newValueSize); - final V replacedValue = toValue.toInstance(copies, prevValue, valueSize); + final V newValue1 = eventListener.usesValue() ? toValue.toInstance(copies, newValue, newValueSize) : null; + final V replacedValue = eventListener.usesValue() ? toValue.toInstance(copies, prevValue, valueSize) : null; writeUnlock(); try { eventListener.onPut(key1,