diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java index 1e34883cc..6bfb5ee8a 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java @@ -206,6 +206,12 @@ public interface ChronicleMap extends ConcurrentMap, ChronicleHash { */ V putMapped(K key, @NotNull UnaryOperator unaryOperator); + /** + * A special operation, similar to {@link #replace(Object, Object, Object) + * replace(key, value, value)}, but puts the entry even if it was absent in the map. + */ + UpdateResult update(K key, V value); + /** * Exports all the entries to a {@link java.io.File} storing them in JSON format, an attempt is * made where possible to use standard java serialisation and keep the data human readable, data diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 64906be5e..70a0a877b 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -266,6 +266,24 @@ final class ReplicatedChronicleMap, + RV, VB extends RV, VBI, MVBI extends MetaBytesInterop> + UpdateResult update(@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState, + MKBI metaKeyInterop, KBI keyInterop, KB key, long keySize, + InstanceOrBytesToInstance toKey, + GetValueInterops getValueInterops, VB value, + InstanceOrBytesToInstance toValue, + long hash2, + byte identifier, long timeStamp) { + segmentStateNotNullImpliesCopiesNotNull(copies, segmentState); + if (segmentState == null) { + copies = SegmentState.getCopies(copies); + segmentState = SegmentState.get(copies); + } + writeLock(); + try { + MultiMap hashLookup = hashLookup(); + SearchState searchState = segmentState.searchState; + hashLookup.startSearch(hash2, searchState); + MultiStoreBytes entry = segmentState.tmpBytes; + for (long pos; (pos = hashLookup.nextPos(searchState)) >= 0L; ) { + long offset = offsetFromPos(pos); + reuse(entry, offset); + if (!keyEquals(keyInterop, metaKeyInterop, key, keySize, entry)) + continue; + // key is found + entry.skip(keySize); + + final long timeStampPosAddr = entry.positionAddr(); + + if (shouldIgnore(entry, timeStamp, identifier)) { + throw new IllegalStateException(); + } + + boolean isDeleted = entry.readBoolean(); + + entry.positionAddr(timeStampPosAddr); + entry.writeLong(timeStamp); + entry.writeByte(identifier); + // deleted flag + entry.writeBoolean(false); + + VBI valueInterop = getValueInterops.getValueInterop(copies); + MVBI metaValueInterop = getValueInterops.getMetaValueInterop( + copies, valueInterop, value); + long valueSize = metaValueInterop.size(valueInterop, value); + + long valueSizePos = entry.position(); + long prevValueSize = valueSizeMarshaller.readSize(entry); + long sizeOfEverythingBeforeValue = entry.position(); + alignment.alignPositionAddr(entry); + try { + if (!isDeleted && prevValueSize == valueSize && + metaValueInterop.startsWith(valueInterop, entry, value)) { + return UpdateResult.UNCHANGED; + } + long valueAddr = entry.positionAddr(); + long entryEndAddr = valueAddr + prevValueSize; + + // putValue may relocate entry and change offset + putValue(pos, offset, entry, valueSizePos, entryEndAddr, isDeleted, + segmentState, + metaValueInterop, valueInterop, value, valueSize, hashLookup, + sizeOfEverythingBeforeValue); + + return isDeleted ? UpdateResult.INSERT : 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); + } + + // for DRY (reusing replaceValueAndNotifyPut() method), + // size is updated AFTER callbacks are called. + // however this shouldn't be an issue because exclusive segment lock + // is still held + if (isDeleted) { + incrementSize(); + // if they are NOT equal, it means the entry was relocated in putValue(), + // hence position is already set + if (pos == segmentState.pos) { + hashLookup.putPosition(segmentState.pos); + } else { + assert hashLookup.getPositions().isSet(segmentState.pos); + } + } + } + } + // key is not found + VBI valueInterop = getValueInterops.getValueInterop(copies); + MVBI metaValueInterop = + getValueInterops.getMetaValueInterop(copies, valueInterop, value); + long valueSize = metaValueInterop.size(valueInterop, value); + putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize, + metaValueInterop, valueInterop, value, entry, false); + entry.position(segmentState.valueSizePos - ADDITIONAL_ENTRY_BYTES); + entry.writeLong(timeStamp); + entry.writeByte(identifier); + entry.writeBoolean(false); + + // put callbacks + onPutMaybeRemote(segmentState.pos, false); + if (bytesEventListener != null) + bytesEventListener.onPut(entry, 0L, metaDataBytes, + segmentState.valueSizePos, true, false); + if (eventListener != null) + eventListener.onPut(toKey.toInstance(copies, key, keySize), + toValue.toInstance(copies, value, valueSize), null, false); + + return UpdateResult.INSERT; + } finally { + segmentState.close(); + writeUnlock(); + } + } + @Override , RV, VB extends RV, VBI, MVBI extends MetaBytesInterop> diff --git a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java index 8ab514674..b166c97d6 100755 --- a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java @@ -621,6 +621,11 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon return fetchObject(PUT_MAPPED, key, unaryOperator); } + @Override + public UpdateResult update(K key, V value) { + throw new UnsupportedOperationException(); + } + private Bytes resizeBufferOutBuffer(int newCapacity) { return resizeBufferOutBuffer(newCapacity, outBytes.position()); diff --git a/src/main/java/net/openhft/chronicle/map/UpdateResult.java b/src/main/java/net/openhft/chronicle/map/UpdateResult.java new file mode 100644 index 000000000..2b08061b7 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/map/UpdateResult.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.map; + +/** + * Result of {@link ChronicleMap#update(Object, Object)} operation. + */ +public enum UpdateResult { + /** + * The new entry was put into the Chroncile Map. + */ + INSERT, + + /** + * The existing entry was updated, the previous value differs from the value was put. + */ + UPDATE, + + /** + * The exiting entry was checked, it maps to the same value, as was going to be put. + */ + UNCHANGED +} diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 4bdcf5112..8d5e7bcea 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -550,6 +550,27 @@ class VanillaChronicleMap, return put1(key, value, true); } + @Override + public UpdateResult update(K key, V value) { + if (key == null || value == null) + throw new NullPointerException(); + + checkKey(key); + checkValue(value); + ThreadLocalCopies copies = keyInteropProvider.getCopies(null); + KI keyInterop = keyInteropProvider.get(copies, originalKeyInterop); + copies = metaKeyInteropProvider.getCopies(copies); + MKI metaKeyInterop = + metaKeyInteropProvider.get(copies, originalMetaKeyInterop, keyInterop, key); + long keySize = metaKeyInterop.size(keyInterop, key); + long hash = metaKeyInterop.hash(keyInterop, key); + int segmentNum = getSegment(hash); + long segmentHash = segmentHash(hash); + return segments[segmentNum].update(copies, null, + metaKeyInterop, keyInterop, key, keySize, keyIdentity(), + this, value, valueIdentity(), segmentHash); + } + @Override public final V putIfAbsent(K key, V value) { if (key == null) @@ -2144,6 +2165,91 @@ class VanillaChronicleMap, return v; } + , + RV, VB extends RV, VBI, MVBI extends MetaBytesInterop> + UpdateResult update(@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState, + MKBI metaKeyInterop, KBI keyInterop, KB key, long keySize, + InstanceOrBytesToInstance toKey, + GetValueInterops getValueInterops, VB value, + InstanceOrBytesToInstance toValue, + long hash2) { + shouldNotBeCalledFromReplicatedChronicleMap("Segment.update"); + segmentStateNotNullImpliesCopiesNotNull(copies, segmentState); + if (segmentState == null) { + copies = SegmentState.getCopies(copies); + segmentState = SegmentState.get(copies); + } + + writeLock(); + try { + SearchState searchState = segmentState.searchState; + hashLookup.startSearch(hash2, searchState); + MultiStoreBytes entry = segmentState.tmpBytes; + for (long pos; (pos = hashLookup.nextPos(searchState)) >= 0L; ) { + long offset = offsetFromPos(pos); + reuse(entry, offset); + if (!keyEquals(keyInterop, metaKeyInterop, key, keySize, entry)) + continue; + // key is found + entry.skip(keySize); + VBI valueInterop = getValueInterops.getValueInterop(copies); + MVBI metaValueInterop = getValueInterops.getMetaValueInterop( + copies, valueInterop, value); + long valueSize = metaValueInterop.size(valueInterop, value); + + long valueSizePos = entry.position(); + long prevValueSize = valueSizeMarshaller.readSize(entry); + long sizeOfEverythingBeforeValue = entry.position(); + alignment.alignPositionAddr(entry); + try { + if (prevValueSize == valueSize && + metaValueInterop.startsWith(valueInterop, entry, value)) { + return UpdateResult.UNCHANGED; + } + long valueAddr = entry.positionAddr(); + long entryEndAddr = valueAddr + prevValueSize; + + // putValue may relocate entry and change offset + putValue(pos, offset, 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); + } + } + } + // key is not found + VBI valueInterop = getValueInterops.getValueInterop(copies); + MVBI metaValueInterop = + getValueInterops.getMetaValueInterop(copies, valueInterop, value); + long valueSize = metaValueInterop.size(valueInterop, value); + putEntry(segmentState, metaKeyInterop, keyInterop, key, keySize, + metaValueInterop, valueInterop, value, entry, false); + + // put callbacks + onPut(this, segmentState.pos); + if (bytesEventListener != null) + bytesEventListener.onPut(entry, 0L, metaDataBytes, + segmentState.valueSizePos, true, false); + if (eventListener != null) + eventListener.onPut(toKey.toInstance(copies, key, keySize), + toValue.toInstance(copies, value, valueSize), null, false); + + return UpdateResult.INSERT; + } finally { + segmentState.close(); + writeUnlock(); + } + } + , RV, VB extends RV, VBI, MVBI extends MetaBytesInterop> RV put3(@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState, diff --git a/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java b/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java index 7394ebe74..1e2e06a09 100644 --- a/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java +++ b/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java @@ -226,6 +226,11 @@ public class ReplicationCheckingMap implements ChronicleMap { return map1.putMapped(key, unaryOperator); } + @Override + public UpdateResult update(K key, V value) { + return map1.update(key, value); + } + @Override public void getAll(File toFile) { throw new UnsupportedOperationException(); diff --git a/src/test/java/net/openhft/chronicle/map/UpdateTest.java b/src/test/java/net/openhft/chronicle/map/UpdateTest.java new file mode 100644 index 000000000..d4b91eb33 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/UpdateTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2015 higherfrequencytrading.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +package net.openhft.chronicle.map; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class UpdateTest { + + @Test + public void testUpdateVanilla() { + test(ChronicleMapBuilder.of(Integer.class, CharSequence.class) + .entries(1).create()); + } + + @Test + public void testUpdateReplicated() { + test(ChronicleMapBuilder.of(Integer.class, CharSequence.class) + .entries(1).replication((byte) 1).create()); + } + + public static void test(ChronicleMap map) { + assertEquals(UpdateResult.INSERT, map.update(1, "1")); + + assertEquals(UpdateResult.UPDATE, map.update(1, "2")); + assertEquals(UpdateResult.UNCHANGED, map.update(1, "2")); + + Assert.assertNotNull(map.remove(1)); + + assertEquals(UpdateResult.INSERT, map.update(1, "2")); + } +} diff --git a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java index 4461a8e23..c38a485ec 100755 --- a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java @@ -179,6 +179,11 @@ public class StatelessChronicleMapTest extends JSR166TestCase { return d.putMapped(key, unaryOperator); } + @Override + public UpdateResult update(K key, V value) { + return d.update(key, value); + } + @Override public void getAll(File toFile) throws IOException { d.getAll(toFile);