HCOLL-81 extend support for mapForkey to all maps.

This commit is contained in:
Peter Lawrey
2014-11-15 12:31:02 +00:00
parent b8ac1da4e1
commit 2aaf5b5395
6 changed files with 40 additions and 21 deletions
@@ -166,7 +166,7 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
* defaultValueProvider.}
*
* <p>If the {@code ChronicleMap} is off-heap updatable, i. e. created via {@link
* ChronicleMapBuilder} builder (values are {@link Byteable}), there is one more option of
* ChronicleMapBuilder} builder (values are {@tlink Byteable}), there is one more option of
* what to do if the key is absent in the map, see {@link ChronicleMapBuilder#prepareValueBytesOnAcquire(PrepareValueBytes)}.
* By default, value bytes are just zeroed out, no default value, either provided for key or constant, is
* put for the absent key.
@@ -182,5 +182,16 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
@NotNull
WriteContext<K, V> acquireUsingLocked(@NotNull K key, @NotNull V usingValue);
/**
* Apply a mapping to the value returned by a key and return a result.
* A read lock is assumed.
*
* @param key to apply the mapping to
* @param function to calculate a result
* @param <R> return type.
* @return the result of the function.
*/
<R> R mapForKey(K key, @NotNull Function<? super V, R> function);
}
@@ -458,7 +458,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable {
}
public synchronized <R> R mapForKey(K key, @NotNull Function<V, R> function) {
public synchronized <R> R mapForKey(K key, @NotNull Function<? super V, R> function) {
final ThreadLocalCopies local = keyValueSerializer.threadLocalCopies();
final long sizeLocation = writeEventAnSkip(MAP_FOR_KEY);
@@ -471,7 +471,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable {
}
private <R> void writeObject(@NotNull Function<V, R> function) {
private <R> void writeObject(@NotNull Object function) {
long start = bytes.position();
for (; ; ) {
try {
@@ -34,8 +34,6 @@ import net.openhft.lang.model.constraints.Nullable;
import net.openhft.lang.threadlocal.Provider;
import net.openhft.lang.threadlocal.ThreadLocalCopies;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
@@ -56,9 +54,6 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, KI>,
V, VW, MVW extends MetaBytesWriter<V, VW>> extends AbstractMap<K, V>
implements ChronicleMap<K, V>, Serializable {
private static final Logger LOG = LoggerFactory.getLogger(VanillaChronicleMap.class);
/**
* Because DirectBitSet implementations couldn't find more than 64 continuous clear or set bits.
*/
@@ -372,6 +367,14 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, KI>,
}
}
@Override
public <R> R mapForKey(K key, @NotNull Function<? super V, R> function) {
try (Context<K, V> entry = lookupUsing(key, null,
LockType.READ_LOCK, false)) {
return function.apply(entry.value());
}
}
@NotNull
@Override
public WriteContext<K, V> acquireUsingLocked(@NotNull K key, @NotNull V usingValue) {