HCOLL-382 add the UpdateResult field to net.openhft.chronicle.map.MapEventListener#onPut

This commit is contained in:
Rob Austin
2015-08-20 14:04:49 +01:00
parent f15d087234
commit a0ecb8fc6b
6 changed files with 53 additions and 39 deletions
+1 -1
View File
@@ -27,7 +27,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>chronicle-map</artifactId>
<version>2.2.3-SNAPSHOT</version>
<version>2.3.0-SNAPSHOT</version>
<name>OpenHFT/Chronicle-Map</name>
<description>Chronicle-Map</description>
@@ -17,6 +17,7 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -79,7 +80,9 @@ public abstract class MapEventListener<K, V> implements Serializable {
}
@Override
public void onPut(Object key, Object value, Object replacedValue, boolean replicationEvent) {
public void onPut(Object key, Object value, Object replacedValue, boolean
replicationEvent,
@NotNull boolean updateResult) {
LOGGER.info("{} put {} => {}", prefix, key, value);
}
@@ -105,26 +108,32 @@ public abstract class MapEventListener<K, V> implements Serializable {
* This method is called whenever a new value is put for the key in the map during calls of such
* methods as {@link ChronicleMap#put put}, {@link ChronicleMap#putIfAbsent putIfAbsent}, {@link
* ChronicleMap#replace(Object, Object, Object) replace}, etc. When a new value is {@linkplain
* ChronicleMapBuilder#defaultValue(Object) default} for the map or obtained during
* {@link ChronicleMap#acquireUsing acquireUsing} call is put for the key, this method is called
* as well.
* ChronicleMapBuilder#defaultValue(Object) default} for the map or obtained during {@link
* ChronicleMap#acquireUsing acquireUsing} call is put for the key, this method is called as
* well.
*
* <p>This method is called when put is already happened.
* @param key the key the given value is put for
* @param newValue the value which is now associated with the given key
* @param replacedValue the value which was replaced by {@code newValue}, {@code null} if the
* key was absent in the map before current {@code ChronicleMap}
*
* @param key the key the given value is put for
* @param newValue the value which is now associated with the given key
* @param replacedValue the value which was replaced by {@code newValue}, {@code null} if the
* key was absent in the map before current {@code ChronicleMap}
* @param replicationEvent
*/
public void onPut(K key, V newValue, @Nullable V replacedValue, boolean replicationEvent) {
public void onPut(K key,
V newValue,
@Nullable V replacedValue,
boolean replicationEvent,
boolean added) {
// do nothing
}
/**
* This is called when an entry is removed. Misses, i. e. when {@code map.remove(key)} is
* called, but key is already absent in the map, are not notified.
* @param key the key removed from the map
* @param value the value which was associated with the given key
*
* @param key the key removed from the map
* @param value the value which was associated with the given key
* @param replicationEvent
*/
public void onRemove(K key, V value, boolean replicationEvent) {
@@ -962,7 +962,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
}
if (eventListener != null) {
V valueInstance = toValue.toInstance(copies, v, valueSize);
eventListener.onPut(keyInstance, valueInstance, null, false);
eventListener.onPut(keyInstance, valueInstance, null, false, true);
}
entryCreated(lock);
return v;
@@ -1086,12 +1086,12 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
<KB, KBI, MKBI extends MetaBytesInterop<KB, ? super KBI>,
RV, VB extends RV, VBI, MVBI extends MetaBytesInterop<RV, ? super VBI>>
UpdateResult update(@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState,
MKBI metaKeyInterop, KBI keyInterop, KB key, long keySize,
InstanceOrBytesToInstance<KB, K> toKey,
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
InstanceOrBytesToInstance<? super VB, V> toValue,
long hash2,
byte identifier, long timeStamp) {
MKBI metaKeyInterop, KBI keyInterop, KB key, long keySize,
InstanceOrBytesToInstance<KB, K> toKey,
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
InstanceOrBytesToInstance<? super VB, V> toValue,
long hash2,
byte identifier, long timeStamp) {
segmentStateNotNullImpliesCopiesNotNull(copies, segmentState);
if (segmentState == null) {
copies = SegmentState.getCopies(copies);
@@ -1157,7 +1157,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
entry, 0L, metaDataBytes, valueSizePos, false, false);
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false);
toValue.toInstance(copies, value, valueSize), null, false,
false);
}
// for DRY (reusing replaceValueAndNotifyPut() method),
@@ -1195,7 +1196,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
segmentState.valueSizePos, true, false);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false);
toValue.toInstance(copies, value, valueSize), null, false, true);
return UpdateResult.INSERT;
} finally {
@@ -1311,7 +1312,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
segmentState.valueSizePos, true, remote);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, remote);
toValue.toInstance(copies, value, valueSize), null, remote, true);
return resultUnused ? null : readValue.readNull();
}
@@ -1319,8 +1320,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
/**
* Used only with replication, its sometimes possible to receive an old ( or stale update )
* from a remote map. This method is used to determine if we should ignore such updates.
* <p/>
* <p>We can reject put() and removes() when comparing times stamps with remote systems
* <p/> <p>We can reject put() and removes() when comparing times stamps with remote
* systems
*
* @param entry the maps entry
* @param timestamp the time the entry was created or updated
@@ -57,8 +57,9 @@ class ReplicationHubFindByName<K> implements FindByName {
// creates a map based on the details that are sent to the map of builders
@Override
public void onPut(CharSequence key, MapInstanceBuilder value,
MapInstanceBuilder replacedValue, boolean replicationEvent) {
super.onPut(key, value, replacedValue, replicationEvent);
MapInstanceBuilder replacedValue, boolean replicationEvent,
boolean added0) {
super.onPut(key, value, replacedValue, replicationEvent, added0);
boolean added = replacedValue == null;
if (!added || value == null)
return;
@@ -2132,7 +2132,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, v, valueSize), null, false);
toValue.toInstance(copies, v, valueSize), null, false, true);
}
return v;
@@ -2168,11 +2168,11 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
<KB, KBI, MKBI extends MetaBytesInterop<KB, ? super KBI>,
RV, VB extends RV, VBI, MVBI extends MetaBytesInterop<RV, ? super VBI>>
UpdateResult update(@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState,
MKBI metaKeyInterop, KBI keyInterop, KB key, long keySize,
InstanceOrBytesToInstance<KB, K> toKey,
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
InstanceOrBytesToInstance<? super VB, V> toValue,
long hash2) {
MKBI metaKeyInterop, KBI keyInterop, KB key, long keySize,
InstanceOrBytesToInstance<KB, K> toKey,
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
InstanceOrBytesToInstance<? super VB, V> toValue,
long hash2) {
shouldNotBeCalledFromReplicatedChronicleMap("Segment.update");
segmentStateNotNullImpliesCopiesNotNull(copies, segmentState);
if (segmentState == null) {
@@ -2222,7 +2222,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
bytesEventListener.onPut(entry, 0L, metaDataBytes, valueSizePos, false, false);
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false);
toValue.toInstance(copies, value, valueSize), null, false,
false);
}
}
}
@@ -2241,7 +2242,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
segmentState.valueSizePos, true, false);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false);
toValue.toInstance(copies, value, valueSize), null, false, true);
return UpdateResult.INSERT;
} finally {
@@ -2334,7 +2335,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
segmentState.valueSizePos, true, false);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), oldValue, false);
toValue.toInstance(copies, value, valueSize), oldValue, false, true);
return resultUnused ? null : readValue.readNull();
}
@@ -2383,7 +2384,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
bytesEventListener.onPut(entry, 0L, metaDataBytes, valueSizePos, false, remote);
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), prevValueInstance, remote);
toValue.toInstance(copies, value, valueSize), prevValueInstance, remote,
true);
}
return resultUnused ? null : prevValue;
@@ -2764,7 +2766,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, newValue, newValueSize),
toValue.toInstance(copies, prevValue, valueSize), false);
toValue.toInstance(copies, prevValue, valueSize), false, true);
return expectedValue == null ? prevValue : Boolean.TRUE;
}
@@ -2971,7 +2973,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
final Segment segment = segments[segIndex];
ThreadLocalCopies copies = SegmentState.getCopies(null);
try (SegmentState segmentState = SegmentState.get(copies)) {
segment.readLock(null);
segment.readLock(null);
if (segment.hashLookup().getPositions().isClear(pos)) {
// the pos was removed after the previous advance
@@ -54,7 +54,8 @@ public class EventListenerWithTCPSocketReplicationTest {
private static final long serialVersionUID = 1L;
@Override
public void onPut(Integer key, CharSequence value, CharSequence replacedValue, boolean replicationEvent) {
public void onPut(Integer key, CharSequence value, CharSequence replacedValue,
boolean replicationEvent, boolean added) {
putWasCalled.getAndSet(true);
keyRef.set(key);
valueRef.set(value);