diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index 498704817..e6cc6c3b5 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -1378,13 +1378,22 @@ public class ChronicleMapBuilder implements Cloneable, } else { replication = channel.hub(); } - return new ReplicatedChronicleMap, - V, Object, MetaBytesInterop>(this, replication); + return newReplicatedMap(replication); } else { return new VanillaChronicleMap, V, Object, MetaBytesInterop>(this); } } + + /** + * create the replicated map, as subclass can self implements of map. + * @param replication + * @return + */ + protected ReplicatedChronicleMap newReplicatedMap(AbstractReplication replication){ + return new ReplicatedChronicleMap, + V, Object, MetaBytesInterop>(this, replication); + } void preMapConstruction(boolean replicated) { keyBuilder.objectSerializer(acquireObjectSerializer(JDKObjectSerializer.INSTANCE)); diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 69b554c21..466b276b7 100755 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -1496,6 +1496,7 @@ class ReplicatedChronicleMap, if (!keyEquals(keyInterop, metaKeyInterop, key, keySize, entry)) continue; // key is found + onRemoveEntry(this, pos); entry.skip(keySize); long timestampPos = entry.position(); long replacedTimestamp = entry.readLong(timestampPos); @@ -1572,6 +1573,7 @@ class ReplicatedChronicleMap, hashLookup.putAfterFailedSearch(searchState, pos); hashLookup.removePosition(pos); + onRemoveEntry(this, pos); // listener.onRemove() might be missing here // depends on onRemove() semantics } @@ -1583,6 +1585,15 @@ class ReplicatedChronicleMap, return resultUnused ? null : readValue.readNull(); } } + + /** + * call when remove an entry in segment, regardless of local or remote remove, entry found or not found.
+ * caller has held the segment write lock + * @param segment + * @param pos + */ + void onRemoveEntry(Segment segment, long pos){ + } , RV, VB extends RV, VBI, MVBI extends MetaBytesInterop> diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index c24d23cc9..40750ab8a 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -155,7 +155,7 @@ class VanillaChronicleMap, lockTimeOutNS = builder.lockTimeOut(TimeUnit.NANOSECONDS); - boolean replicated = getClass() == ReplicatedChronicleMap.class; + boolean replicated = this instanceof ReplicatedChronicleMap; this.chunkSize = builder.chunkSize(replicated); this.alignment = builder.valueAlignment(); this.constantlySizedEntry = builder.constantlySizedEntries(); @@ -2618,7 +2618,7 @@ class VanillaChronicleMap, } } - private void free(long fromPos, int chunks) { + void free(long fromPos, int chunks) { freeList.clear(fromPos, fromPos + chunks); if (fromPos < nextPosToSearchFrom) nextPosToSearchFrom = fromPos;