CE-134 Add ChronicleMap.update(K, V) method

This commit is contained in:
leventov
2015-08-18 02:31:22 +03:00
parent 2f32764082
commit bc24505e49
8 changed files with 351 additions and 0 deletions
@@ -206,6 +206,12 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
*/
V putMapped(K key, @NotNull UnaryOperator<V> 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
@@ -266,6 +266,24 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
identifier, timeStamp);
}
@Override
public UpdateResult update(K key, V value) {
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 segment(segmentNum).update(copies, null,
metaKeyInterop, keyInterop, key, keySize, keyIdentity(),
this, value, valueIdentity(),
segmentHash, localIdentifier, currentTime());
}
Object remoteRemove(K key,
final byte remoteIdentifier, long timeStamp) {
@@ -1065,6 +1083,127 @@ final class ReplicatedChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? supe
}
}
<KB, KBI, MKBI extends MetaBytesInterop<KB, ? super KBI>,
RV, VB extends RV, VBI, MVBI extends MetaBytesInterop<RV, ? super VBI>>
UpdateResult update(@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState,
MKBI metaKeyInterop, KBI keyInterop, KB key, long keySize,
InstanceOrBytesToInstance<KB, K> toKey,
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
InstanceOrBytesToInstance<? super VB, V> 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
<KB, KBI, MKBI extends MetaBytesInterop<KB, ? super KBI>,
RV, VB extends RV, VBI, MVBI extends MetaBytesInterop<RV, ? super VBI>>
@@ -621,6 +621,11 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, 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());
@@ -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 <http://www.gnu.org/licenses/>.
*/
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
}
@@ -550,6 +550,27 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
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<K, KI, MKI extends MetaBytesInterop<K, ? super KI>,
return v;
}
<KB, KBI, MKBI extends MetaBytesInterop<KB, ? super KBI>,
RV, VB extends RV, VBI, MVBI extends MetaBytesInterop<RV, ? super VBI>>
UpdateResult update(@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState,
MKBI metaKeyInterop, KBI keyInterop, KB key, long keySize,
InstanceOrBytesToInstance<KB, K> toKey,
GetValueInterops<VB, VBI, MVBI> getValueInterops, VB value,
InstanceOrBytesToInstance<? super VB, V> 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();
}
}
<KB, KBI, MKBI extends MetaBytesInterop<KB, ? super KBI>,
RV, VB extends RV, VBI, MVBI extends MetaBytesInterop<RV, ? super VBI>>
RV put3(@Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState,