SPI implementation

This commit is contained in:
leventov
2015-05-27 00:03:20 +01:00
parent 8e4027da08
commit b4c8554b77
111 changed files with 13637 additions and 3991 deletions
@@ -21,16 +21,17 @@ package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.Value;
import java.util.function.BiFunction;
import java.util.function.Function;
import static net.openhft.chronicle.hash.Value.bytesEquivalent;
import static net.openhft.chronicle.map.MapMethodsSupport.returnCurrentValueIfPresent;
import static net.openhft.chronicle.map.MapMethodsSupport.tryReturnCurrentValueIfPresent;
/**
* SPI interface for customizing behaviour of the specific Map's methods with individual keys.
*/
public interface MapMethods<K, V> {
public interface MapMethods<K, V, R> {
default boolean containsKey(MapQueryContext<K, V> q) {
default boolean containsKey(MapQueryContext<K, V, R> q) {
return q.entry() != null;
}
@@ -38,12 +39,11 @@ public interface MapMethods<K, V> {
* Backing {@link ChronicleMap#get}, {@link ChronicleMap#getUsing} and
* {@link ChronicleMap#getOrDefault} methods.
*/
default void get(MapQueryContext<K, V> q, ReturnValue<? super V> returnValue) {
MapMethodsSupport.returnCurrentValueIfPresent(q, returnValue);
default void get(MapQueryContext<K, V, R> q, ReturnValue<V> returnValue) {
returnCurrentValueIfPresent(q, returnValue);
}
default boolean put(MapQueryContext<K, V> q,
Value<V, ?> value, ReturnValue<? super V> returnValue) {
default void put(MapQueryContext<K, V, R> q, Value<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
@@ -52,31 +52,32 @@ public interface MapMethods<K, V> {
MapEntry<K, V> entry = q.entry();
if (entry != null) {
returnValue.returnValue(entry.value());
return q.replaceValue(entry, value);
q.replaceValue(entry, value);
} else {
return q.insert(q.absentEntry(), value);
q.insert(q.absentEntry(), value);
}
}
default boolean putIfAbsent(MapQueryContext<K, V> q,
Value<V, ?> value, ReturnValue<? super V> returnValue) {
if (MapMethodsSupport.tryReturnCurrentValueIfPresent(q, returnValue))
return false;
default void putIfAbsent(MapQueryContext<K, V, R> q,
Value<V, ?> value, ReturnValue<V> returnValue) {
if (tryReturnCurrentValueIfPresent(q, returnValue))
return;
// Key is absent
return q.insert(q.absentEntry(), value);
q.insert(q.absentEntry(), value);
}
default boolean acquireUsing(MapQueryContext<K, V> q, ReturnValue<? super V> returnValue) {
if (MapMethodsSupport.tryReturnCurrentValueIfPresent(q, returnValue))
return false;
default void acquireUsing(MapQueryContext<K, V, R> q, ReturnValue<V> returnValue) {
if (tryReturnCurrentValueIfPresent(q, returnValue))
return;
// Key is absent
Value<V, ?> defaultValue = q.defaultValue(q.absentEntry());
boolean inserted = q.insert(q.absentEntry(), defaultValue);
returnValue.returnValue(defaultValue);
return inserted;
q.insert(q.absentEntry(), q.defaultValue(q.absentEntry()));
// meaningful to return the default as newly-inserted, not the default entry itself.
// map.acquireUsing() is most useful for data-value generated values, for which it makes
// big difference -- what bytes to refer. map.acquireUsing().incrementValue();
returnValue.returnValue(q.entry().value());
}
default boolean remove(MapQueryContext<K, V> q, ReturnValue<? super V> returnValue) {
default void remove(MapQueryContext<K, V, R> q, 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.remove() atomicity guarantee. So, we acquire
@@ -85,25 +86,26 @@ public interface MapMethods<K, V> {
MapEntry<K, V> entry = q.entry();
if (entry != null) {
returnValue.returnValue(entry.value());
return q.remove(entry);
} else {
return false;
q.remove(entry);
}
}
default boolean remove(MapQueryContext<K, V> q, Value<V, ?> value) {
default boolean remove(MapQueryContext<K, V, R> q, Value<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:
q.updateLock().lock();
MapEntry<K, V> entry = q.entry();
return entry != null &&
bytesEquivalent(entry.value(), value) &&
q.remove(entry);
if (entry != null && bytesEquivalent(entry.value(), value)) {
q.remove(entry);
return true;
} else {
return false;
}
}
default boolean replace(MapQueryContext<K, V> q,
Value<V, ?> value, ReturnValue<? super V> returnValue) {
default void replace(MapQueryContext<K, V, R> q,
Value<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:
@@ -111,69 +113,64 @@ public interface MapMethods<K, V> {
MapEntry<K, V> entry = q.entry();
if (entry != null) {
returnValue.returnValue(entry.value());
return q.replaceValue(entry, value);
} else {
return false;
q.replaceValue(entry, value);
}
}
default boolean replace(MapQueryContext<K, V> q,
default boolean replace(MapQueryContext<K, V, R> q,
Value<V, ?> oldValue, Value<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:
q.updateLock().lock();
MapEntry<K, V> entry = q.entry();
return entry != null &&
bytesEquivalent(((MapEntry<K, V>) entry).value(), oldValue) &&
q.replaceValue(entry, newValue);
}
default boolean compute(MapQueryContext<K, V> q,
BiFunction<? super K, ? super V, ? extends V> remappingFunction,
Function<V, Value<V, ?>> newValueObjectToValue,
ReturnValue<? super V> returnValue) {
q.updateLock().lock();
MapEntry<K, V> entry = q.entry();
V oldValue = entry != null ? entry.value().get() : null;
V newValue = remappingFunction.apply(q.queriedKey().get(), oldValue);
if (newValue != null) {
Value<V, ?> newValueValue = newValueObjectToValue.apply(newValue);
boolean updated;
if (entry != null) {
updated = q.replaceValue(entry, newValueValue);
} else {
updated = q.insert(q.absentEntry(), newValueValue);
}
returnValue.returnValue(newValueValue);
return updated;
} else if (entry != null) {
return q.remove(entry);
if (entry != null && bytesEquivalent(((MapEntry<K, V>) entry).value(), oldValue)) {
q.replaceValue(entry, newValue);
return true;
} else {
return false;
}
}
default boolean merge(MapQueryContext<K, V> q, Value<V, ?> value,
default void compute(MapQueryContext<K, V, R> q,
BiFunction<? super K, ? super V, ? extends V> remappingFunction,
ReturnValue<V> returnValue) {
q.updateLock().lock();
MapEntry<K, V> entry = q.entry();
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);
if (entry != null) {
q.replaceValue(entry, newValueValue);
} else {
q.insert(q.absentEntry(), newValueValue);
}
returnValue.returnValue(newValueValue);
} else if (entry != null) {
q.remove(entry);
}
}
default void merge(MapQueryContext<K, V, R> q, Value<V, ?> value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction,
Function<V, Value<V, ?>> newValueObjectToValue,
ReturnValue<? super V> returnValue) {
ReturnValue<V> returnValue) {
q.updateLock().lock();
Value<V, ?> newValueValue;
boolean updated;
MapEntry<K, V> entry = q.entry();
if (entry != null) {
V oldValue = entry.value().get();
V newValue = remappingFunction.apply(oldValue, value.get());
if (newValue == null)
return q.remove(entry);
newValueValue = newValueObjectToValue.apply(newValue);
updated = q.replaceValue(entry, newValueValue);
if (newValue == null) {
q.remove(entry);
return;
}
newValueValue = q.wrapValueAsValue(newValue);
q.replaceValue(entry, newValueValue);
} else {
newValueValue = value;
updated = q.insert(q.absentEntry(), newValueValue);
q.insert(q.absentEntry(), newValueValue);
}
returnValue.returnValue(newValueValue);
return updated;
}
}