mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
added identifier etc for remove events.
This commit is contained in:
@@ -97,6 +97,7 @@ public abstract class MapEventListener<K, V> implements Serializable {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class LoggingMapEventListener extends MapEventListener {
|
||||
private static final long serialVersionUID = 0L;
|
||||
public final static Logger LOGGER = LoggerFactory.getLogger(LoggingMapEventListener.class);
|
||||
@@ -119,7 +120,7 @@ public abstract class MapEventListener<K, V> implements Serializable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemove(Object key, Object value, boolean replicationEvent) {
|
||||
public void onRemove(Object key, Object value, boolean replicationEvent, byte identifier, byte replacedIdentifier, long timestamp, long replacedTimeStamp) {
|
||||
LOGGER.info("{} remove {} was {}", prefix, key, value);
|
||||
}
|
||||
}
|
||||
@@ -136,47 +137,22 @@ public abstract class MapEventListener<K, V> implements Serializable {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* <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 replicationEvent {@code true} if its a replicaiton event
|
||||
* @param timestamp the time the event occurred
|
||||
* @param replacedTimestamp the time stamp that has just been replaced
|
||||
* @param identifier the node identifier associated with this update
|
||||
* @param replacedTimestamp the identifier that has just been replaced.
|
||||
*/
|
||||
public void onPut(K key,
|
||||
V newValue,
|
||||
@Nullable V replacedValue,
|
||||
boolean replicationEvent,
|
||||
boolean added, long timestamp,
|
||||
long replacedTimestamp,
|
||||
byte identifier,
|
||||
byte replacedIdentifier,
|
||||
boolean hasValueChanged) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 replicationEvent
|
||||
* @param key the key removed from the map
|
||||
* @param value the value which was associated with the given key
|
||||
* @param replicationEvent {@code true} if its a replication event
|
||||
* @param identifier the identifer used for replicaiton or zero if not definded
|
||||
* @param replacedIdentifier the replaced identifer used for replicaiton or zero if not
|
||||
* definded
|
||||
* @param timestamp the timestamp used for replicaiton or zero if not definded
|
||||
* @param replacedTimeStamp the replaced timestamp used for replicaiton or zero if not
|
||||
* definded
|
||||
*/
|
||||
public void onRemove(K key, V value, boolean replicationEvent) {
|
||||
public void onRemove(K key, V value, boolean replicationEvent, byte identifier,
|
||||
byte replacedIdentifier, long timestamp, long replacedTimeStamp) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1448,7 +1448,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
continue;
|
||||
// key is found
|
||||
entry.skip(keySize);
|
||||
|
||||
long replacedTimestamp = 0;
|
||||
byte replacedIdentifier = 0;
|
||||
long timestampPos = entry.position();
|
||||
if (shouldIgnore(entry, timestamp, identifier)) {
|
||||
// the following assert should be enabled, but TimeBasedReplicationTest
|
||||
@@ -1465,7 +1466,10 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
if (expectedValue != null)
|
||||
return Boolean.FALSE;
|
||||
entry.position(timestampPos);
|
||||
replacedTimestamp = entry.readLong(entry.position());
|
||||
entry.writeLong(timestamp);
|
||||
|
||||
replacedIdentifier = entry.readByte(entry.position());
|
||||
entry.writeByte(identifier);
|
||||
onRemoveMaybeRemote(pos, remote);
|
||||
break returnNothing;
|
||||
@@ -1494,7 +1498,7 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
|
||||
return removeEntry(copies, searchState, key, keySize, toKey, toValue, readValue,
|
||||
resultUnused, hashLookup, entry, pos, valueSizePos,
|
||||
valueSize, remote, false, booleanResult);
|
||||
valueSize, remote, false, booleanResult, identifier, timestamp, replacedIdentifier, replacedTimestamp);
|
||||
}
|
||||
// key is not found
|
||||
if (remote) {
|
||||
@@ -1692,9 +1696,12 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
final long keySize = keySizeMarshaller.readSize(entry);
|
||||
long keyPosition = entry.position();
|
||||
entry.skip(keySize);
|
||||
long timestamp = entry.readLong();
|
||||
long replacedTimeStamp = entry.readLong();
|
||||
byte replacedIdentifier = entry.readByte(entry.position());
|
||||
entry.position(keyPosition);
|
||||
if (timestamp > ((TimestampTrackingEntry) returnedEntry).timestamp) {
|
||||
final long timestamp = ((TimestampTrackingEntry) returnedEntry).timestamp;
|
||||
|
||||
if (replacedTimeStamp > timestamp) {
|
||||
// The entry was updated after being returned from iterator.next()
|
||||
// Check that it is still the entry with the same key
|
||||
K key = returnedEntry.getKey();
|
||||
@@ -1717,7 +1724,8 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
|
||||
}
|
||||
}
|
||||
|
||||
removePresent(segment, pos, entry, keySize, 0L, false);
|
||||
removePresent(segment, pos, entry, keySize, 0L, false, (byte) 0, replacedIdentifier,
|
||||
timestamp, replacedTimeStamp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2577,7 +2577,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
}
|
||||
return removeEntry(copies, searchState, key, keySize, toKey, toValue,
|
||||
readValue, resultUnused, hashLookup, entry, pos, valueSizePos,
|
||||
valueSize, false, true, expectedValue != null);
|
||||
valueSize, false, true, expectedValue != null, (byte) 0, 0,
|
||||
(byte) 0, 0);
|
||||
}
|
||||
// key is not found
|
||||
if (expectedValue == null) {
|
||||
@@ -2594,7 +2595,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
ReadValue<RV> readValue, boolean resultUnused,
|
||||
MultiMap hashLookup, MultiStoreBytes entry, long pos,
|
||||
long valueSizePos, long valueSize, boolean remote, boolean removeFromMultiMap,
|
||||
boolean booleanResult) {
|
||||
boolean booleanResult, byte identifier, long timestamp, byte replacedIdentifier, long replacedTimestamp) {
|
||||
// get the removed value, if needed
|
||||
RV removedValue = null;
|
||||
if ((!booleanResult && !resultUnused) || eventListener != null) {
|
||||
@@ -2621,7 +2622,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
V removedValueForEventListener =
|
||||
toValue.toInstance(copies, removedValue, valueSize);
|
||||
eventListener.onRemove(toKey.toInstance(copies, key, keySize),
|
||||
removedValueForEventListener, remote);
|
||||
removedValueForEventListener, remote, identifier, replacedIdentifier,
|
||||
timestamp, replacedTimestamp);
|
||||
}
|
||||
|
||||
return booleanResult ? Boolean.TRUE : removedValue;
|
||||
@@ -3037,11 +3039,14 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
long position = entry.position();
|
||||
final long segmentHash = segmentHash(Hasher.hash(entry, position, position + keySize));
|
||||
|
||||
removePresent(segment, pos, entry, keySize, segmentHash, true);
|
||||
removePresent(segment, pos, entry, keySize, segmentHash, true, (byte) 0,
|
||||
(byte) 0,
|
||||
0, 0);
|
||||
}
|
||||
|
||||
final void removePresent(Segment segment, long pos, NativeBytes entry, long keySize,
|
||||
long segmentHash, boolean removeFromMultiMap) {
|
||||
long segmentHash, boolean removeFromMultiMap, final byte identifier,
|
||||
final byte replacedIdentifier, final long timestamp, final long replacedTimeStamp) {
|
||||
entry.skip(keySize);
|
||||
segment.manageReplicationBytes(entry, true, true);
|
||||
long valueSizePos = entry.position();
|
||||
@@ -3060,7 +3065,9 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
|
||||
if (bytesEventListener != null)
|
||||
bytesEventListener.onRemove(entry, 0L, metaDataBytes, valueSizePos, false);
|
||||
if (eventListener != null)
|
||||
eventListener.onRemove(returnedEntry.getKey(), returnedEntry.getValue(), false);
|
||||
eventListener.onRemove(returnedEntry.getKey(), returnedEntry.getValue(), false,
|
||||
identifier,
|
||||
replacedIdentifier, timestamp, replacedTimeStamp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user