Fixed several replication bugs; Value -> Data; extracted ReplicableEntry as a superclass of HashReplicableEntry

This commit is contained in:
leventov
2015-06-15 21:34:45 +03:00
parent f1da352a5d
commit bf20bc98c3
55 changed files with 6708 additions and 6379 deletions
@@ -18,11 +18,11 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.Value;
import net.openhft.chronicle.hash.Data;
import java.util.function.BiFunction;
import static net.openhft.chronicle.hash.Value.bytesEquivalent;
import static net.openhft.chronicle.hash.Data.bytesEquivalent;
import static net.openhft.chronicle.map.MapMethodsSupport.returnCurrentValueIfPresent;
import static net.openhft.chronicle.map.MapMethodsSupport.tryReturnCurrentValueIfPresent;
@@ -43,7 +43,7 @@ public interface MapMethods<K, V, R> {
returnCurrentValueIfPresent(q, returnValue);
}
default void put(MapQueryContext<K, V, R> q, Value<V, ?> value, ReturnValue<V> returnValue) {
default void put(MapQueryContext<K, V, R> q, Data<V, ?> value, ReturnValue<V> returnValue) {
// We cannot read the previous value under read lock, because then we will need
// to release the read lock -> acquire write lock, the value might be updated in
// between, that will break ConcurrentMap.put() atomicity guarantee. So, we acquire
@@ -59,7 +59,7 @@ public interface MapMethods<K, V, R> {
}
default void putIfAbsent(MapQueryContext<K, V, R> q,
Value<V, ?> value, ReturnValue<V> returnValue) {
Data<V, ?> value, ReturnValue<V> returnValue) {
if (tryReturnCurrentValueIfPresent(q, returnValue))
return;
// Key is absent
@@ -90,7 +90,7 @@ public interface MapMethods<K, V, R> {
}
}
default boolean remove(MapQueryContext<K, V, R> q, Value<V, ?> value) {
default boolean remove(MapQueryContext<K, V, R> q, Data<V, ?> value) {
// remove(key, value) should find the entry & remove most of the time,
// so don't try to check key presence and value equivalence under read lock first,
// as in putIfAbsent()/acquireUsing(), start with update lock:
@@ -105,7 +105,7 @@ public interface MapMethods<K, V, R> {
}
default void replace(MapQueryContext<K, V, R> q,
Value<V, ?> value, ReturnValue<V> returnValue) {
Data<V, ?> value, ReturnValue<V> returnValue) {
// replace(key, value) should find the key & put the value most of the time,
// so don't try to check key presence under read lock first,
// as in putIfAbsent()/acquireUsing(), start with update lock:
@@ -118,7 +118,7 @@ public interface MapMethods<K, V, R> {
}
default boolean replace(MapQueryContext<K, V, R> q,
Value<V, ?> oldValue, Value<V, ?> newValue) {
Data<V, ?> oldValue, Data<V, ?> newValue) {
// replace(key, old, new) should find the entry & put new value most of the time,
// so don't try to check key presence and value equivalence under read lock first,
// as in putIfAbsent()/acquireUsing(), start with update lock:
@@ -140,23 +140,23 @@ public interface MapMethods<K, V, R> {
V oldValue = entry != null ? entry.value().get() : null;
V newValue = remappingFunction.apply(q.queriedKey().get(), oldValue);
if (newValue != null) {
Value<V, ?> newValueValue = q.wrapValueAsValue(newValue);
Data<V, ?> newValueData = q.wrapValueAsValue(newValue);
if (entry != null) {
q.replaceValue(entry, newValueValue);
q.replaceValue(entry, newValueData);
} else {
q.insert(q.absentEntry(), newValueValue);
q.insert(q.absentEntry(), newValueData);
}
returnValue.returnValue(newValueValue);
returnValue.returnValue(newValueData);
} else if (entry != null) {
q.remove(entry);
}
}
default void merge(MapQueryContext<K, V, R> q, Value<V, ?> value,
default void merge(MapQueryContext<K, V, R> q, Data<V, ?> value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction,
ReturnValue<V> returnValue) {
q.updateLock().lock();
Value<V, ?> newValueValue;
Data<V, ?> newValueData;
MapEntry<K, V> entry = q.entry();
if (entry != null) {
V oldValue = entry.value().get();
@@ -165,12 +165,12 @@ public interface MapMethods<K, V, R> {
q.remove(entry);
return;
}
newValueValue = q.wrapValueAsValue(newValue);
q.replaceValue(entry, newValueValue);
newValueData = q.wrapValueAsValue(newValue);
q.replaceValue(entry, newValueData);
} else {
newValueValue = value;
q.insert(q.absentEntry(), newValueValue);
newValueData = value;
q.insert(q.absentEntry(), newValueData);
}
returnValue.returnValue(newValueValue);
returnValue.returnValue(newValueData);
}
}