removed the writeLock with eventlisteners are called

This commit is contained in:
Rob Austin
2015-12-16 08:22:05 +00:00
parent 0e9bcbfa75
commit 5ad1c9e620
5 changed files with 156 additions and 66 deletions
@@ -46,13 +46,14 @@ public abstract class BytesMapEventListener implements Serializable {
public void onPut(Bytes entry, long metaDataPos, long keyPos, long valuePos, boolean added,
boolean replicationEvent, boolean hasValueChanged, byte identifier,
byte replacedIdentifier, long timeStamp, long replacedTimeStamp) {
byte replacedIdentifier, long timeStamp, long replacedTimeStamp,
SharedSegment segment) {
// do nothing
}
public void onRemove(Bytes entry, long metaDataPos, long keyPos, long valuePos,
boolean replicationEvent, byte identifier,
byte replacedIdentifier, long timeStamp, long replacedTimeStamp) {
byte replacedIdentifier, long timeStamp, long replacedTimeStamp, SharedSegment segment) {
// do nothing
}
}
@@ -952,13 +952,18 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
if (bytesEventListener != null) {
bytesEventListener.onPut(entry, 0L, metaDataBytes, valuePos, true, false,
true, localIdentifier, replacedIdentifier, timestamp,
replacedTimestamp);
replacedTimestamp, this);
}
if (eventListener != null) {
V valueInstance = toValue.toInstance(copies, v, valueSize);
eventListener.onPut(keyInstance, valueInstance, null, false, true,
true, localIdentifier, replacedIdentifier, timestamp,
replacedTimestamp);
writeUnlock();
try {
eventListener.onPut(keyInstance, valueInstance, null, false, true,
true, localIdentifier, replacedIdentifier, timestamp,
replacedTimestamp);
} finally {
writeLock();
}
}
entryCreated(lock);
return v;
@@ -1162,13 +1167,21 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
entry, 0L, metaDataBytes, valueSizePos, false, false,
hasValueChanged,
identifier, replacedIdentifier,
timeStamp, replacedTimestamp);
timeStamp, replacedTimestamp, this);
}
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false,
isDeleted, hasValueChanged, identifier, replacedIdentifier,
timeStamp, replacedTimestamp);
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue = toValue.toInstance(copies, value, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue, null, false,
isDeleted, hasValueChanged, identifier, replacedIdentifier,
timeStamp, replacedTimestamp);
} finally {
writeLock();
}
}
// for DRY (reusing replaceValueAndNotifyPut() method),
@@ -1208,14 +1221,21 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
bytesEventListener.onPut(entry, 0L, metaDataBytes,
segmentState.valueSizePos, true, false, true, identifier,
replacedIdentifier, timeStamp, replacedTimestamp);
replacedIdentifier, timeStamp, replacedTimestamp, this);
}
if (eventListener != null) {
byte replacedIdentifier = 0;
long replacedTimestamp = 0;
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false, true,
true, identifier, replacedIdentifier, timeStamp, replacedTimestamp);
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue = toValue.toInstance(copies, value, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue, null, false, true,
true, identifier, replacedIdentifier, timeStamp, replacedTimestamp);
} finally {
writeLock();
}
}
return UpdateResult.INSERT;
@@ -1338,15 +1358,22 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
bytesEventListener.onPut(entry, 0L, metaDataBytes,
segmentState.valueSizePos, true, remote, true, identifier,
replacedIdentifier, timestamp,
replacedTimestamp);
replacedTimestamp, this);
}
if (eventListener != null) {
byte replacedIdentifier = 0;
long replacedTimestamp = 0;
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, remote, true,
true, identifier, replacedIdentifier, timestamp,
replacedTimestamp);
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue = toValue.toInstance(copies, value, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue, null, remote, true,
true, identifier, replacedIdentifier, timestamp,
replacedTimestamp);
} finally {
writeLock();
}
}
return resultUnused ? null : readValue.readNull();
@@ -22,10 +22,10 @@ import org.jetbrains.annotations.Nullable;
import java.util.Map;
/**
* Allow access to the Segments that user used to make up the shared map, these methods should be used with
* exceptional caution and were initially introduce to facilitate remote map replication.
* Allow access to the Segments that user used to make up the shared map, these methods should be
* used with exceptional caution and were initially introduce to facilitate remote map replication.
*/
interface SharedSegment<K, V> {
public interface SharedSegment<K, V> {
/**
* if passed segmentState is null, null is returned instead of readLock
@@ -44,4 +44,5 @@ interface SharedSegment<K, V> {
long timeStamp(long pos);
<T> T writeLock();
}
@@ -1895,7 +1895,8 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
}
}
final WriteLocked<K, KI, MKI, V, VI, MVI> writeLock() {
public WriteLocked<K, KI, MKI, V, VI, MVI> writeLock() {
return writeLock(null, false);
}
@@ -1975,6 +1976,11 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
"replicated map");
}
/* @Override
public void writeLock() {
writeLock();
}*/
final MultiStoreBytes reuse(MultiStoreBytes entry, long offset) {
entry.setBytesOffset(bytes, offset);
entry.position(metaDataBytes);
@@ -2113,15 +2119,22 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
bytesEventListener.onPut(entry, 0L, keyPos, segmentState.valueSizePos, true,
false, true,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimeStamp);
segmentState.timestamp, replacedTimeStamp, this);
}
if (eventListener != null) {
byte replacedIdentifier = (byte) 0;
long replacedTimeStamp = 0;
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, v, valueSize), null, false, true, true,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimeStamp);
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue = toValue.toInstance(copies, v, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue, null, false, true, true,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimeStamp);
} finally {
writeLock();
}
}
return v;
@@ -2214,14 +2227,21 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
bytesEventListener.onPut(entry, 0L, metaDataBytes, valueSizePos, false,
false, hasValueChanged,
segmentState.identifier, (byte) 0,
segmentState.timestamp, 0
segmentState.timestamp, 0, this
);
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false,
false, hasValueChanged,
segmentState.identifier, (byte) 0,
segmentState.timestamp, 0);
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue = toValue.toInstance(copies, value, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue, null, false,
false, hasValueChanged,
segmentState.identifier, (byte) 0,
segmentState.timestamp, 0);
} finally {
writeLock();
}
}
return updateResult;
@@ -2241,15 +2261,23 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
bytesEventListener.onPut(entry, 0L, metaDataBytes,
segmentState.valueSizePos, true, false, true,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimeStamp);
segmentState.timestamp, replacedTimeStamp, this);
}
if (eventListener != null) {
byte replacedIdentifier = 0;
long replacedTimeStamp = 0;
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false, true, true,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimeStamp);
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue = toValue.toInstance(copies, value, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue, null, false, true, true,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimeStamp);
} finally {
writeLock();
}
}
return UpdateResult.INSERT;
@@ -2333,11 +2361,19 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
if (bytesEventListener != null)
bytesEventListener.onPut(entry, 0L, metaDataBytes,
segmentState.valueSizePos, true, false, true,
segmentState.identifier, (byte) 0, segmentState.timestamp, 0);
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), null, false, true, true,
segmentState.identifier, (byte) 0, segmentState.timestamp, 0);
segmentState.identifier, (byte) 0, segmentState.timestamp, 0, this);
if (eventListener != null) {
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue = toValue.toInstance(copies, value, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue, null, false, true, true,
segmentState.identifier, (byte) 0, segmentState.timestamp, 0);
} finally {
writeLock();
}
}
return resultUnused ? null : readValue.readNull();
}
@@ -2401,13 +2437,21 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
bytesEventListener.onPut(entry, 0L, metaDataBytes, valueSizePos, false, remote,
hasValueChanged,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
segmentState.timestamp, replacedTimestamp, this);
if (eventListener != null) {
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, value, valueSize), prevValueInstance, remote,
entryIsDeleted, hasValueChanged,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue = toValue.toInstance(copies, value, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue, prevValueInstance, remote,
entryIsDeleted, hasValueChanged,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
} finally {
writeLock();
}
}
return resultUnused ? null : prevValue;
@@ -2649,14 +2693,22 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
if (bytesEventListener != null)
bytesEventListener.onRemove(entry, 0L, metaDataBytes, valueSizePos, remote,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
segmentState.timestamp, replacedTimestamp, this);
if (eventListener != null) {
V removedValueForEventListener =
toValue.toInstance(copies, removedValue, valueSize);
eventListener.onRemove(toKey.toInstance(copies, key, keySize),
removedValueForEventListener, remote,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
final K key1 = toKey.toInstance(copies, key, keySize);
writeUnlock();
try {
eventListener.onRemove(key1,
removedValueForEventListener, remote,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
} finally {
writeLock();
}
}
return booleanResult ? Boolean.TRUE : removedValue;
@@ -2807,16 +2859,25 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
bytesEventListener.onPut(entry, 0L, keyPos, segmentState.valueSizePos, true,
false, hasValueChanged,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
segmentState.timestamp, replacedTimestamp, this);
}
if (eventListener != null)
eventListener.onPut(toKey.toInstance(copies, key, keySize),
toValue.toInstance(copies, newValue, newValueSize),
toValue.toInstance(copies, prevValue, valueSize), false, true,
hasValueChanged,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
if (eventListener != null) {
final K key1 = toKey.toInstance(copies, key, keySize);
final V newValue1 = toValue.toInstance(copies, newValue, newValueSize);
final V replacedValue = toValue.toInstance(copies, prevValue, valueSize);
writeUnlock();
try {
eventListener.onPut(key1,
newValue1,
replacedValue, false, true,
hasValueChanged,
segmentState.identifier, replacedIdentifier,
segmentState.timestamp, replacedTimestamp);
} finally {
writeLock();
}
}
return expectedValue == null ? prevValue : Boolean.TRUE;
}
@@ -74,14 +74,14 @@ public class CHMMetaDataTest {
}
@Override
public void onPut(Bytes entry, long metaDataPos, long keyPos, long valuePos, boolean added, boolean replicationEvent, boolean hasValueChanged, byte identifier, byte replacedIdentifier, long timeStamp, long replacedTimeStamp) {
public void onPut(Bytes entry, long metaDataPos, long keyPos, long valuePos, boolean added, boolean replicationEvent, boolean hasValueChanged, byte identifier, byte replacedIdentifier, long timeStamp, long replacedTimeStamp, SharedSegment segment) {
if (added)
assertEquals(0, entry.readLong(metaDataPos));
entry.writeLong(metaDataPos, timeStamps.incrementAndGet());
}
@Override
public void onRemove(Bytes entry, long metaDataPos, long keyPos, long valuePos, boolean replicationEvent, byte identifier, byte replacedIdentifier, long timeStamp, long replacedTimeStamp) {
public void onRemove(Bytes entry, long metaDataPos, long keyPos, long valuePos, boolean replicationEvent, byte identifier, byte replacedIdentifier, long timeStamp, long replacedTimeStamp, SharedSegment segment) {
System.out.println("Removed entry with ts of " + entry.readLong(metaDataPos));
}