diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/internal/BasicCopyingMetaBytesInterop.java b/src/main/java/net/openhft/chronicle/hash/serialization/internal/BasicCopyingMetaBytesInterop.java index 915038128..250cd18f8 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/internal/BasicCopyingMetaBytesInterop.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/internal/BasicCopyingMetaBytesInterop.java @@ -63,14 +63,6 @@ public abstract class BasicCopyingMetaBytesInterop implements MetaBytesInt return h; } - @Override - public boolean isEqual(W write, Bytes bytes, E e, long size) { - if (buffer.buffer.remaining() != size) - return false; - - return bytes.compare(bytes.position(), buffer.buffer, buffer.buffer.position(), size); - } - @Override public void write(W writer, Bytes bytes, E e) { bytes.write(buffer.buffer, buffer.buffer.position(), buffer.buffer.remaining()); diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/internal/ByteableMarshaller.java b/src/main/java/net/openhft/chronicle/hash/serialization/internal/ByteableMarshaller.java index 817bee388..4131be744 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/internal/ByteableMarshaller.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/internal/ByteableMarshaller.java @@ -194,11 +194,6 @@ public abstract class ByteableMarshaller } - @Override - public boolean isEqual(Object write, Bytes bytes, E e, long size) { - // todo - return false; - } } private static class WithCustomFactory extends ByteableMarshaller { @@ -221,10 +216,5 @@ public abstract class ByteableMarshaller } - @Override - public boolean isEqual(Object write, Bytes bytes, E e, long size) { - // todo - return false; - } } } diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/internal/DelegatingMetaBytesInterop.java b/src/main/java/net/openhft/chronicle/hash/serialization/internal/DelegatingMetaBytesInterop.java index 6e0663045..2aad8beb1 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/internal/DelegatingMetaBytesInterop.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/internal/DelegatingMetaBytesInterop.java @@ -52,12 +52,6 @@ public final class DelegatingMetaBytesInterop> interop.write(bytes, e); } - @Override - public boolean isEqual(I interop, Bytes bytes, E e, long size) { - // todo - return false; - } - private Object readResolve() { return INSTANCE; } diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/internal/DirectBytesBuffer.java b/src/main/java/net/openhft/chronicle/hash/serialization/internal/DirectBytesBuffer.java index 649230255..4040dc84c 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/internal/DirectBytesBuffer.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/internal/DirectBytesBuffer.java @@ -137,9 +137,5 @@ class DirectBytesBuffer super(DirectBytesBuffer.this); } - @Override - public boolean isEqual(BytesWriter write, Bytes bytes, E e, long size) { - return false; - } } } diff --git a/src/main/java/net/openhft/chronicle/hash/serialization/internal/MetaBytesWriter.java b/src/main/java/net/openhft/chronicle/hash/serialization/internal/MetaBytesWriter.java index 29575d2ed..b9214320c 100644 --- a/src/main/java/net/openhft/chronicle/hash/serialization/internal/MetaBytesWriter.java +++ b/src/main/java/net/openhft/chronicle/hash/serialization/internal/MetaBytesWriter.java @@ -24,16 +24,4 @@ public interface MetaBytesWriter extends Serializable { long size(W writer, E e); void write(W writer, Bytes bytes, E e); - - - /** - * returns true if the {@code bytes} equals {@code e} - * - * @param write - * @param bytes - * @param e - * @param size - * @return - */ - boolean isEqual(W write, Bytes bytes, E e, long size); } diff --git a/src/main/java/net/openhft/chronicle/map/PrepareValueBytesAsWriter.java b/src/main/java/net/openhft/chronicle/map/PrepareValueBytesAsWriter.java index a16af5563..ce9da985c 100644 --- a/src/main/java/net/openhft/chronicle/map/PrepareValueBytesAsWriter.java +++ b/src/main/java/net/openhft/chronicle/map/PrepareValueBytesAsWriter.java @@ -54,9 +54,4 @@ final class PrepareValueBytesAsWriter write(bytes, k); } - @Override - public boolean isEqual(PrepareValueBytesAsWriter write, Bytes bytes, K k, long size) { - // todo - return false; - } } diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 7bd9eb9b0..791e290b1 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -42,7 +42,6 @@ import org.slf4j.LoggerFactory; import java.io.Closeable; import java.io.IOException; -import java.util.Iterator; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.atomic.AtomicLong; @@ -791,11 +790,6 @@ final class ReplicatedChronicleMap> newEntrySet() { - return new EntrySet(); - } - @Override public K key(@NotNull Bytes entry, K usingKey) { final long start = entry.position(); @@ -901,6 +895,7 @@ final class ReplicatedChronicleMap toKey, GetValueInterops getValueInterops, VB value, InstanceOrBytesToInstance toValue, long hash2, - boolean replaceIfPresent, ReadValue readValue, boolean resultUnused, - final byte identifier, final byte replacedIdentifier, - final long timestamp, final long replacedTimestamp) { + boolean replaceIfPresent, ReadValue readValue, boolean resultUnused) { return putWithoutLock(copies, segmentState, metaKeyInterop, keyInterop, key, keySize, toKey, getValueInterops, value, toValue, hash2, replaceIfPresent, readValue, resultUnused, localIdentifier, currentTime(), false); @@ -1235,6 +1236,8 @@ final class ReplicatedChronicleMap readValue, boolean resultUnused, byte identifier, long timestamp, boolean remote) { + segmentState.identifier = identifier; + segmentState.timestamp = timestamp; MultiMap hashLookup = hashLookup(); SearchState searchState = segmentState.searchState; hashLookup.startSearch(hash2, searchState); @@ -1274,9 +1277,9 @@ final class ReplicatedChronicleMap toValue, long hash2, ReadValue readValue, boolean resultUnused, long timestamp, byte identifier, boolean remote, boolean booleanResult) { + segmentState.identifier = identifier; + segmentState.timestamp = timestamp; assert identifier > 0; expectedValueNotNullImpliesBooleanResult(expectedValue, booleanResult); MultiMap hashLookup = hashLookup(); @@ -1448,9 +1455,10 @@ final class ReplicatedChronicleMap assertion fails. @@ -1466,10 +1474,7 @@ final class ReplicatedChronicleMap.EntryIterator { - - @Override - public Entry next() { - return super.next(); - } - - @Override - void removePresent(VanillaChronicleMap.Segment seg, long pos) { - @SuppressWarnings("unchecked") - Segment segment = (Segment) seg; - - final long offset = segment.offsetFromPos(pos); - final NativeBytes entry = segment.reuse(this.entry, offset); - - final long keySize = keySizeMarshaller.readSize(entry); - long keyPosition = entry.position(); - entry.skip(keySize); - long replacedTimeStamp = entry.readLong(); - byte replacedIdentifier = entry.readByte(entry.position()); - entry.position(keyPosition); - 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(); - ThreadLocalCopies copies = keyInteropProvider.getCopies(null); - KI keyInterop = keyInteropProvider.get(copies, originalKeyInterop); - copies = metaKeyInteropProvider.getCopies(copies); - MKI metaKeyInterop = - metaKeyInteropProvider.get(copies, originalMetaKeyInterop, keyInterop, key); - long returnedKeySize = metaKeyInterop.size(keyInterop, key); - if (returnedKeySize != keySize || - !metaKeyInterop.startsWith(keyInterop, entry, key)) { - // The case: - // 1. iterator.next() - thread 1 - // 2. map.put() which cause relocation of the key, returned above - thread 2 - // OR map.remove() which remove this key - thread 2 - // 3. map.put() which place a new key on the `pos` in current segment - thread 3 - // 4. iterator.remove() - thread 1 - ReplicatedChronicleMap.this.remove(key); - return; - } - } - - removePresent(segment, pos, entry, keySize, 0L, false, (byte) 0, replacedIdentifier, - timestamp, replacedTimeStamp); - } - } - - class EntrySet extends VanillaChronicleMap.EntrySet { - @NotNull - @Override - public Iterator> iterator() { - return new EntryIterator(); - } - } - /** *

Once a change occurs to a map, map replication requires that these changes are picked up * by another thread, this class provides an iterator like interface to poll for such changes. diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index eb1359de6..005c10891 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -872,11 +872,7 @@ class VanillaChronicleMap, @NotNull @Override public final Set> entrySet() { - return (entrySet != null) ? entrySet : (entrySet = newEntrySet()); - } - - Set> newEntrySet() { - return new EntrySet(); + return (entrySet != null) ? entrySet : (entrySet = new EntrySet()); } /** @@ -1377,14 +1373,6 @@ class VanillaChronicleMap, bytes.write(value, value.position(), value.remaining()); } - @Override - public boolean isEqual(Void write, Bytes bytes, Bytes bytes2, long size) { - - if (size != bytes2.remaining()) - return false; - - return bytes.compare(bytes.position(), bytes2, bytes2.position(), bytes2.remaining()); - } } public static final class SegmentState implements StatefulCopyable, @@ -1411,6 +1399,9 @@ class VanillaChronicleMap, private boolean used; private SegmentState next; + byte identifier = 0; + long timestamp = 0; + private SegmentState(int depth) { if (depth > (1 << 10)) throw new IllegalStateException("More than " + (1 << 10) + @@ -1469,13 +1460,22 @@ class VanillaChronicleMap, V, VI, MVI extends MetaBytesInterop> ReadLocked readLocked(VanillaChronicleMap.Segment segment) { readLocked.initSegment(segment); + initIdentifierAndTimestamp(segment); return readLocked; } + private void initIdentifierAndTimestamp(VanillaChronicleMap.Segment segment) { + VanillaChronicleMap map = segment.map(); + identifier = map instanceof ReplicatedChronicleMap ? + ((ReplicatedChronicleMap) map).identifier() : 0; + timestamp = 0; + } + , V, VI, MVI extends MetaBytesInterop> WriteLocked nativeWriteLocked(VanillaChronicleMap.Segment segment) { nativeWriteLocked.initSegment(segment); + initIdentifierAndTimestamp(segment); return nativeWriteLocked; } @@ -1483,6 +1483,7 @@ class VanillaChronicleMap, V, VI, MVI extends MetaBytesInterop> WriteLocked heapWriteLocked(VanillaChronicleMap.Segment segment) { heapWriteLocked.initSegment(segment); + initIdentifierAndTimestamp(segment); return heapWriteLocked; } } @@ -1642,7 +1643,7 @@ class VanillaChronicleMap, MultiStoreBytes entry = segment.reuse(segmentState.tmpBytes, offset); long keySize = map.keySizeMarshaller.readSize(entry); entry.skip(keySize); - segment.manageReplicationBytes(entry, true, false); + segment.manageReplicationBytes(segmentState, entry, true, false); map.onPut(segment, pos); } super.close(); @@ -1694,7 +1695,7 @@ class VanillaChronicleMap, MultiStoreBytes entry = segment.reuse(segmentState.tmpBytes, offset); long keySize = map.keySizeMarshaller.readSize(entry); entry.skip(keySize); - segment.manageReplicationBytes(entry, true, false); + segment.manageReplicationBytes(segmentState, entry, true, false); long valueSizePos = entry.position(); long valueSize = map.valueSizeMarshaller.readSize(entry); long sizeOfEverythingBeforeValue = entry.position(); @@ -1705,7 +1706,8 @@ class VanillaChronicleMap, MVI metaValueInterop = map.metaValueInteropProvider .get(copies, map.originalMetaValueInterop, valueInterop, value); long newValueSize = metaValueInterop.size(valueInterop, value); - segment.putValue(pos, offset, entry, valueSizePos, entryEndAddr, removed, + // doesn't call listeners? + segment.putValue(pos, entry, valueSizePos, entryEndAddr, removed, segmentState, metaValueInterop, valueInterop, value, newValueSize, segment.hashLookup(), sizeOfEverythingBeforeValue); @@ -2100,12 +2102,6 @@ class VanillaChronicleMap, throw defaultValueOrPrepareBytesShouldBeSpecified(); } - - boolean hasValueChanged = valueSize < 0; - - if (valueSize < 0) - valueSize = valueSize * -1; - entry.positionAddr(entry.positionAddr() - valueSize); RV v = readValue.readValue(copies, entry, usingValue, valueSize); @@ -2116,10 +2112,12 @@ class VanillaChronicleMap, bytesEventListener.onPut(entry, 0L, keyPos, segmentState.valueSizePos, true, false); } 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, hasValueChanged, - (byte) 0, - (byte) 0, 0, 0); + toValue.toInstance(copies, v, valueSize), null, false, true, true, + segmentState.identifier, replacedIdentifier, + segmentState.timestamp, replacedTimeStamp); } return v; @@ -2189,42 +2187,44 @@ class VanillaChronicleMap, long sizeOfEverythingBeforeValue = entry.position(); alignment.alignPositionAddr(entry); - boolean hasValueChanged = false; - try { - if (prevValueSize == valueSize && - metaValueInterop.startsWith(valueInterop, entry, value)) { - return UpdateResult.UNCHANGED; - } + UpdateResult updateResult; + if (prevValueSize == valueSize && + metaValueInterop.startsWith(valueInterop, entry, value)) { + updateResult = UpdateResult.UNCHANGED; + } else { long valueAddr = entry.positionAddr(); long entryEndAddr = valueAddr + prevValueSize; // putValue may relocate entry and change offset hasValueChanged = putValue(pos, offset, entry, valueSizePos, entryEndAddr, false, segmentState, + putValue(pos, entry, valueSizePos, entryEndAddr, false, segmentState, metaValueInterop, valueInterop, value, valueSize, hashLookup, sizeOfEverythingBeforeValue); - return UpdateResult.UPDATE; - } finally { - // put callbacks - onPutMaybeRemote(segmentState.pos, false); - if (bytesEventListener != null) - 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, - false, hasValueChanged, (byte) 0, (byte) 0, 0, - 0); - } + updateResult = UpdateResult.UPDATE; } + + // put callbacks + onPutMaybeRemote(segmentState.pos, false); + if (bytesEventListener != null) + bytesEventListener.onPut(entry, 0L, metaDataBytes, valueSizePos, false, false); + if (eventListener != null) { + boolean hasValueChanged = updateResult != UpdateResult.UNCHANGED; + eventListener.onPut(toKey.toInstance(copies, key, keySize), + toValue.toInstance(copies, value, valueSize), null, false, + false, hasValueChanged, + segmentState.identifier, (byte) 0, + segmentState.timestamp, 0); + } + + return updateResult; } // key is not found VBI valueInterop = getValueInterops.getValueInterop(copies); MVBI metaValueInterop = getValueInterops.getMetaValueInterop(copies, valueInterop, value); - long valueSize = metaValueInterop.size(valueInterop, value); - long l = putEntry(segmentState, metaKeyInterop, keyInterop, key, - keySize, + long valueSize = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize, metaValueInterop, valueInterop, value, entry, false); // put callbacks @@ -2232,10 +2232,14 @@ class VanillaChronicleMap, if (bytesEventListener != null) bytesEventListener.onPut(entry, 0L, metaDataBytes, segmentState.valueSizePos, true, false); - if (eventListener != null) + if (eventListener != null) { + byte replacedIdentifier = 0; + long replacedTimeStamp = 0; eventListener.onPut(toKey.toInstance(copies, key, keySize), - toValue.toInstance(copies, value, valueSize), null, false, true, l < - 0, (byte) 0, (byte) 0, 0, 0); + toValue.toInstance(copies, value, valueSize), null, false, true, true, + segmentState.identifier, replacedIdentifier, + segmentState.timestamp, replacedTimeStamp); + } return UpdateResult.INSERT; } finally { @@ -2264,8 +2268,7 @@ class VanillaChronicleMap, return putWithoutLock(copies, segmentState, metaKeyInterop, keyInterop, key, keySize, toKey, getValueInterops, value, toValue, - hash2, replaceIfPresent, readValue, resultUnused, (byte) 0, (byte) 0, 0, - 0); + hash2, replaceIfPresent, readValue, resultUnused); } finally { segmentState.close(); writeUnlock(); @@ -2281,8 +2284,7 @@ class VanillaChronicleMap, GetValueInterops getValueInterops, VB value, InstanceOrBytesToInstance toValue, long hash2, boolean replaceIfPresent, - ReadValue readValue, boolean resultUnused, final byte identifier, - final byte replacedIdentifier, final long timestamp, final long replacedTimestamp) { + ReadValue readValue, boolean resultUnused) { SearchState searchState = segmentState.searchState; hashLookup.startSearch(hash2, searchState); @@ -2298,8 +2300,8 @@ class VanillaChronicleMap, return replaceValueAndNotifyPut(copies, segmentState, key, keySize, toKey, getValueInterops, value, toValue, - entry, pos, offset, hashLookup, readValue, resultUnused, - false, false, identifier, replacedIdentifier, timestamp, replacedTimestamp); + entry, pos, hashLookup, readValue, resultUnused, + false, false, (byte) 0, 0); } else { long valueSize = readValueSize(entry); return resultUnused ? null : @@ -2310,8 +2312,7 @@ class VanillaChronicleMap, VBI valueInterop = getValueInterops.getValueInterop(copies); MVBI metaValueInterop = getValueInterops.getMetaValueInterop(copies, valueInterop, value); - long valueSize = metaValueInterop.size(valueInterop, value); - long l = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize, + long valueSize = putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize, metaValueInterop, valueInterop, value, entry, false); // put callbacks @@ -2321,8 +2322,8 @@ class VanillaChronicleMap, segmentState.valueSizePos, true, false); if (eventListener != null) eventListener.onPut(toKey.toInstance(copies, key, keySize), - toValue.toInstance(copies, value, valueSize), null, false, true, l < 0, - identifier, (byte) 0, 0, 0); + toValue.toInstance(copies, value, valueSize), null, false, true, true, + segmentState.identifier, (byte) 0, segmentState.timestamp, 0); return resultUnused ? null : readValue.readNull(); } @@ -2333,9 +2334,10 @@ class VanillaChronicleMap, KB key, long keySize, InstanceOrBytesToInstance toKey, GetValueInterops getValueInterops, VB value, InstanceOrBytesToInstance toValue, - MultiStoreBytes entry, long pos, long offset, MultiMap searchedHashLookup, + MultiStoreBytes entry, long pos, MultiMap searchedHashLookup, ReadValue readValue, boolean resultUnused, - boolean entryIsDeleted, boolean remote, final byte identifier, byte replacedIdentifier, final long timestamp, final long replacedTimestamp) { + boolean entryIsDeleted, boolean remote, + byte replacedIdentifier, long replacedTimestamp) { VBI valueInterop = getValueInterops.getValueInterop(copies); MVBI metaValueInterop = getValueInterops.getMetaValueInterop( copies, valueInterop, value); @@ -2360,11 +2362,24 @@ class VanillaChronicleMap, prevValueInstance = readValue(copies, entry, null, prevValueSize); } - // putValue may relocate entry and change offset - boolean hasValueChanged = putValue(pos, offset, entry, valueSizePos, entryEndAddr, - entryIsDeleted, segmentState, - metaValueInterop, valueInterop, value, valueSize, searchedHashLookup, - sizeOfEverythingBeforeValue); + entry.positionAddr(valueAddr); + boolean doPutValue; + boolean hasValueChanged = false; + if (eventListener != null) { + hasValueChanged = prevValueSize != valueSize || + !metaValueInterop.startsWith(valueInterop, entry, value); + doPutValue = hasValueChanged; + } else { + doPutValue = true; + } + + if (doPutValue) { + // putValue may relocate entry and change offset + putValue(pos, entry, valueSizePos, entryEndAddr, + entryIsDeleted, segmentState, + metaValueInterop, valueInterop, value, valueSize, searchedHashLookup, + sizeOfEverythingBeforeValue); + } // put callbacks onPutMaybeRemote(segmentState.pos, remote); @@ -2373,8 +2388,9 @@ class VanillaChronicleMap, if (eventListener != null) { eventListener.onPut(toKey.toInstance(copies, key, keySize), toValue.toInstance(copies, value, valueSize), prevValueInstance, remote, - entryIsDeleted, hasValueChanged, identifier, replacedIdentifier, - timestamp, replacedTimestamp); + entryIsDeleted, hasValueChanged, + segmentState.identifier, replacedIdentifier, + segmentState.timestamp, replacedTimestamp); } return resultUnused ? null : prevValue; @@ -2392,8 +2408,6 @@ class VanillaChronicleMap, * Returns value size, writes the entry (key, value, sizes) to the entry, after this method * call entry positioned after value bytes written (i. e. at the end of entry), sets entry * position (in segment) and value size position in the given segmentState - * - * @return the size or negivtive size if the value has changed. */ final , E, EW> long putEntry(SegmentState segmentState, @@ -2409,18 +2423,15 @@ class VanillaChronicleMap, clearMetaData(offset); reuse(entry, offset); - keySizeMarshaller.writeSize(entry, keySize); metaKeyInterop.write(keyInterop, entry, key); - manageReplicationBytes(entry, writeDefaultInitialReplicationValues, false); + manageReplicationBytes( + segmentState, entry, writeDefaultInitialReplicationValues, false); segmentState.valueSizePos = entry.position(); - final long size = valueSizeMarshaller.readSize(entry); - entry.position(segmentState.valueSizePos); valueSizeMarshaller.writeSize(entry, valueSize); alignment.alignPositionAddr(entry); - boolean hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, elem, size); metaElemWriter.write(elemWriter, entry, elem); freeExtraAllocatedChunks(pos, allocatedChunks, entry); @@ -2428,7 +2439,7 @@ class VanillaChronicleMap, hashLookup.putAfterFailedSearch(segmentState.searchState, pos); incrementSize(); - return hasValueChanged ? -valueSize : valueSize; + return valueSize; } final void freeExtraAllocatedChunks(long pos, int allocatedChunks, Bytes entry) { @@ -2439,8 +2450,9 @@ class VanillaChronicleMap, } } - void manageReplicationBytes(Bytes entry, boolean writeDefaultInitialReplicationValues, - boolean remove) { + void manageReplicationBytes( + SegmentState segmentState, Bytes entry, + boolean writeDefaultInitialReplicationValues, boolean remove) { // do nothing } @@ -2576,9 +2588,9 @@ class VanillaChronicleMap, if (!metaValueInterop.startsWith(valueInterop, entry, expectedValue)) return Boolean.FALSE; } - return removeEntry(copies, searchState, key, keySize, toKey, toValue, + return removeEntry(copies, segmentState, key, keySize, toKey, toValue, readValue, resultUnused, hashLookup, entry, pos, valueSizePos, - valueSize, false, true, expectedValue != null, (byte) 0, 0, + valueSize, false, true, expectedValue != null, (byte) 0, 0); } // key is not found @@ -2589,14 +2601,14 @@ class VanillaChronicleMap, } } - final Object removeEntry( - ThreadLocalCopies copies, SearchState searchState, + final Object removeEntry( + ThreadLocalCopies copies, SegmentState segmentState, KB key, long keySize, InstanceOrBytesToInstance toKey, InstanceOrBytesToInstance toValue, ReadValue readValue, boolean resultUnused, MultiMap hashLookup, MultiStoreBytes entry, long pos, long valueSizePos, long valueSize, boolean remote, boolean removeFromMultiMap, - boolean booleanResult, byte identifier, long timestamp, byte replacedIdentifier, long replacedTimestamp) { + boolean booleanResult, byte replacedIdentifier, long replacedTimestamp) { // get the removed value, if needed RV removedValue = null; if ((!booleanResult && !resultUnused) || eventListener != null) { @@ -2607,7 +2619,7 @@ class VanillaChronicleMap, // update segment state if (removeFromMultiMap) { - hashLookup.removePrevPos(searchState); + hashLookup.removePrevPos(segmentState.searchState); long entrySizeInBytes = entry.positionAddr() + valueSize - entry.startAddr(); free(pos, inChunks(entrySizeInBytes)); } else { @@ -2623,8 +2635,9 @@ class VanillaChronicleMap, V removedValueForEventListener = toValue.toInstance(copies, removedValue, valueSize); eventListener.onRemove(toKey.toInstance(copies, key, keySize), - removedValueForEventListener, remote, identifier, replacedIdentifier, - timestamp, replacedTimestamp); + removedValueForEventListener, remote, + segmentState.identifier, replacedIdentifier, + segmentState.timestamp, replacedTimestamp); } return booleanResult ? Boolean.TRUE : removedValue; @@ -2705,7 +2718,7 @@ class VanillaChronicleMap, return onKeyPresentOnReplace(copies, segmentState, key, keySize, toKey, getExpectedValueInterops, expectedValue, getNewValueInterops, newValue, - readValue, toValue, pos, offset, entry, hashLookup); + readValue, toValue, pos, entry, hashLookup, (byte) 0, 0); } // key is not found return expectedValue == null ? readValue.readNull() : Boolean.FALSE; @@ -2722,12 +2735,14 @@ class VanillaChronicleMap, GetValueInterops getExpectedValueInterops, VB expectedValue, GetValueInterops getNewValueInterops, VB newValue, ReadValue readValue, InstanceOrBytesToInstance toValue, - long pos, long offset, MultiStoreBytes entry, MultiMap searchedHashLookup) { + long pos, MultiStoreBytes entry, MultiMap searchedHashLookup, + byte replacedIdentifier, long replacedTimestamp) { long valueSizePos = entry.position(); long valueSize = valueSizeMarshaller.readSize(entry); long sizeOfEverythingBeforeValue = entry.position(); alignment.alignPositionAddr(entry); - long entryEndAddr = entry.positionAddr() + valueSize; + long valueAddr = entry.positionAddr(); + long entryEndAddr = valueAddr + valueSize; RV prevValue; if (expectedValue != null) { @@ -2749,10 +2764,22 @@ class VanillaChronicleMap, MVBI metaValueInterop = getNewValueInterops.getMetaValueInterop( copies, valueInterop, newValue); long newValueSize = metaValueInterop.size(valueInterop, newValue); - boolean entryIsDeleted = false; // couldn't replace deleted entry - boolean hasValueChanged = putValue(pos, offset, entry, valueSizePos, entryEndAddr, entryIsDeleted, segmentState, - metaValueInterop, valueInterop, newValue, newValueSize, searchedHashLookup, - sizeOfEverythingBeforeValue); + entry.positionAddr(valueAddr); + boolean doPutValue; + boolean hasValueChanged = false; + if (eventListener != null) { + hasValueChanged = valueSize != newValueSize || + !metaValueInterop.startsWith(valueInterop, entry, newValue); + doPutValue = hasValueChanged; + } else { + doPutValue = true; + } + if (doPutValue) { + boolean entryIsDeleted = false; // couldn't replace deleted entry + putValue(pos, entry, valueSizePos, entryEndAddr, entryIsDeleted, segmentState, + metaValueInterop, valueInterop, newValue, newValueSize, searchedHashLookup, + sizeOfEverythingBeforeValue); + } // put callbacks onPut(this, segmentState.pos); @@ -2764,7 +2791,9 @@ class VanillaChronicleMap, eventListener.onPut(toKey.toInstance(copies, key, keySize), toValue.toInstance(copies, newValue, newValueSize), toValue.toInstance(copies, prevValue, valueSize), false, true, - hasValueChanged, (byte) 0, (byte) 0, 0, 0); + hasValueChanged, + segmentState.identifier, replacedIdentifier, + segmentState.timestamp, replacedTimestamp); return expectedValue == null ? prevValue : Boolean.TRUE; } @@ -2774,21 +2803,17 @@ class VanillaChronicleMap, * enough space for new value in location already allocated for this entry. * * @param pos index of the first chunk occupied by the entry - * @param offset relative offset of the entry in Segment bytes (before, i. e. - * including metaData) * @param entry relative pointer in Segment bytes * @param valueSizePos relative position of value size in entry * @param entryEndAddr absolute address of the entry end - * @return true if the value has change */ - final boolean putValue( - long pos, long offset, MultiStoreBytes entry, long valueSizePos, long entryEndAddr, + final void putValue( + long pos, MultiStoreBytes entry, long valueSizePos, long entryEndAddr, boolean entryIsDeleted, SegmentState segmentState, MetaBytesWriter metaElemWriter, EW elemWriter, E newElem, long newElemSize, MultiMap searchedHashLookup, long sizeOfEverythingBeforeValue) { - final boolean hasValueChanged; long entryStartAddr = entry.address(); long valueSizeAddr = entryStartAddr + valueSizePos; long newValueAddr = alignment.alignAddr( @@ -2826,20 +2851,11 @@ class VanillaChronicleMap, NativeBytes.UNSAFE.copyMemory(entryStartAddr, newEntryStartAddr, valueSizeAddr - entryStartAddr); entry.position(valueSizePos); - final long originalSize = valueSizeMarshaller.readSize(entry); - entry.position(valueSizePos); valueSizeMarshaller.writeSize(entry, newElemSize); alignment.alignPositionAddr(entry); - - hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, newElem, - originalSize); - if (hasValueChanged) - metaElemWriter.write(elemWriter, entry, newElem); - freeExtraAllocatedChunks(newPos, allocatedChunks, entry); - segmentState.pos = newPos; - return hasValueChanged; + return; // END OF RELOCATION } else if (newSizeInChunks < oldSizeInChunks) { // Freeing extra chunks @@ -2852,15 +2868,10 @@ class VanillaChronicleMap, } // Common code for all cases entry.position(valueSizePos); - final long originalSize = valueSizeMarshaller.readSize(entry); - entry.position(valueSizePos); valueSizeMarshaller.writeSize(entry, newElemSize); alignment.alignPositionAddr(entry); - - hasValueChanged = !metaElemWriter.isEqual(elemWriter, entry, newElem, originalSize); metaElemWriter.write(elemWriter, entry, newElem); segmentState.pos = pos; - return hasValueChanged; } private void clear() { @@ -2908,7 +2919,7 @@ class VanillaChronicleMap, reuse(entry, offset); long keySize = keySizeMarshaller.readSize(entry); entry.skip(keySize); - manageReplicationBytes(entry, false, false); + manageReplicationBytes(segmentState, entry, false, false); long valueSize = valueSizeMarshaller.readSize(entry); long sizeInBytes = entrySize(keySize, valueSize); int entrySizeInChunks = inChunks(sizeInBytes); @@ -3001,74 +3012,9 @@ class VanillaChronicleMap, int segIndex = returnedSeg; if (segIndex < 0) throw new IllegalStateException(); - final Segment segment = segments[segIndex]; - final long pos = returnedPos; - try { - segment.writeLock(); - if (segment.hashLookup().getPositions().isClear(pos)) { - // The case: - // 1. iterator.next() - thread 1 - // 2. map.put() which cause relocation of the key, returned in above - thread 2 - // OR map.remove() which remove this key - thread 2 - // 3. iterator.remove() - thread 1 - segment.writeUnlock(); // not re-entrant. - VanillaChronicleMap.this.remove(returnedEntry.getKey()); - segment.writeLock(); - } else { - removePresent(segment, pos); - } - returnedSeg = -1; - returnedEntry = null; - } finally { - segment.writeUnlock(); - } - } - - void removePresent(Segment segment, long pos) { - // TODO handle the case: - // iterator.next() -- thread 1 - // map.put() which cause relocation of the key, returned above -- thread 2 - // map.put() which place a new key on the `pos` in current segment -- thread 3 - // iterator.remove() -- thread 1 - // The simple solution is to compare bytes in the map with the serialized bytes - // of returnedEntry.getKey(), but it seems rather wasteful to workaround so rare - // case. - final long offset = segment.offsetFromPos(pos); - final NativeBytes entry = segment.reuse(this.entry, offset); - - final long keySize = keySizeMarshaller.readSize(entry); - long position = entry.position(); - final long segmentHash = segmentHash(Hasher.hash(entry, position, position + keySize)); - - 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, final byte identifier, - final byte replacedIdentifier, final long timestamp, final long replacedTimeStamp) { - entry.skip(keySize); - segment.manageReplicationBytes(entry, true, true); - long valueSizePos = entry.position(); - long valueSize = readValueSize(entry); - final long entryEndAddr = entry.positionAddr() + valueSize; - if (removeFromMultiMap) { - segment.hashLookup().remove(segmentHash, pos); - segment.free(pos, segment.inChunks(entryEndAddr - entry.address())); - } else { - segment.hashLookup().removePosition(pos); - } - segment.decrementSize(); - - // remove callbacks - onRemove(segment, pos); - if (bytesEventListener != null) - bytesEventListener.onRemove(entry, 0L, metaDataBytes, valueSizePos, false); - if (eventListener != null) - eventListener.onRemove(returnedEntry.getKey(), returnedEntry.getValue(), false, - identifier, - replacedIdentifier, timestamp, replacedTimeStamp); + VanillaChronicleMap.this.remove(returnedEntry.getKey()); + returnedSeg = -1; + returnedEntry = null; } }