diff --git a/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java index 9025fb5da..60f9e9a17 100644 --- a/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java @@ -18,6 +18,8 @@ package net.openhft.chronicle.hash; +import net.openhft.chronicle.hash.function.Consumer; +import net.openhft.chronicle.hash.function.Predicate; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.set.ChronicleSet; @@ -28,7 +30,7 @@ import java.io.File; * This interface defines common {@link ChronicleMap} and {@link ChronicleSet}, related to off-heap * memory management and file-mapping. Not usable by itself. */ -public interface ChronicleHash extends Closeable { +public interface ChronicleHash> extends Closeable { /** * Returns the file this hash container mapped to, i. e. when it is created by * {@link ChronicleHashBuilder#create()} call, or {@code null} if it is purely in-memory, @@ -42,13 +44,43 @@ public interface ChronicleHash extends Closeable { long longSize(); - KeyContext context(K key); + C context(K key); /** * @return the class of {@code } */ Class keyClass(); + /** + * Checks the given predicate on each entry in this {@code ChronicleHash} until all entries + * have been processed or the predicate returns {@code false} for some entry, or throws + * an {@code Exception}. Exceptions thrown by the predicate are relayed to the caller. + * + *

The order in which the entries will be processed is unspecified. It might differ from + * the order of iteration via {@code Iterator} returned by any method of this + * {@code ChronicleHash} or it's collection view. + * + *

If the {@code ChronicleHash} is empty, this method returns {@code true} immediately. + * + * @param predicate the predicate to be checked for each entry + * @return {@code true} if the predicate returned {@code true} for all entries of + * the {@code ChronicleHash}, {@code false} if it returned {@code false} for the entry + */ + boolean forEachEntryWhile(Predicate predicate); + + /** + * Performs the given action for each entry in this {@code ChronicleHash} until all entries have + * been processed or the action throws an {@code Exception}. Exceptions thrown by the action are + * relayed to the caller. + * + *

The order in which the entries will be processed is unspecified. It might differ from + * the order of iteration via {@code Iterator} returned by any method of this + * {@code ChronicleHash} or it's collection view. + * + * @param action the action to be performed for each entry + */ + void forEachEntry(Consumer action); + /** * Releases the off-heap memory, used by this hash container and resources, used by replication, * if any. However, if hash container (hence off-heap memory, used by it) is mapped to the file diff --git a/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java index 08b8849a4..90082b6c9 100755 --- a/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java @@ -65,7 +65,7 @@ import java.util.concurrent.TimeUnit; * @param the concrete builder type, i. e. {@link ChronicleMapBuilder} * or {@link ChronicleSetBuilder} */ -public interface ChronicleHashBuilder, +public interface ChronicleHashBuilder, B extends ChronicleHashBuilder> extends Cloneable { /** diff --git a/src/main/java/net/openhft/chronicle/hash/KeyContext.java b/src/main/java/net/openhft/chronicle/hash/KeyContext.java index e93246161..2f5f97047 100644 --- a/src/main/java/net/openhft/chronicle/hash/KeyContext.java +++ b/src/main/java/net/openhft/chronicle/hash/KeyContext.java @@ -31,8 +31,21 @@ import java.util.ConcurrentModificationException; *

  • All methods throw {@code IllegalStateException} immediately, if called after * {@link #close()} called on this context and returned.
  • * + * + * @param key type */ -public interface KeyContext extends AutoCloseable, InterProcessReadWriteUpdateLock { +public interface KeyContext extends AutoCloseable, InterProcessReadWriteUpdateLock { + + /** + * Returns the key object of this context. The instance might be reused, so you shouldn't + * save the returned object and use it after the context is closed or the callback (to which + * this context is provided) return. Might acquire {@link #readLock} before reading the key, + * if the context is not locked yet. + * + * @return the key object of this context + */ + @NotNull + K key(); /** * Returns the entry bytes, if the key is present in the hash, otherwise throws @@ -78,8 +91,7 @@ public interface KeyContext extends AutoCloseable, InterProcessReadWriteUpdateLo * is already held. * * @return {@code true} if the key was actually removed (hash was updated), - * {@code false} if the key is already absent in the hash, or the remove operation was rejected - * by the hash for some reason + * {@code false} if the key is already absent in the hash * @throws IllegalStateException if the context is locked for read, but not for update, and * update or write lock is actually required for {@code remove()} in this {@code KeyContext} * implementation diff --git a/src/main/java/net/openhft/chronicle/hash/function/Consumer.java b/src/main/java/net/openhft/chronicle/hash/function/Consumer.java new file mode 100644 index 000000000..5973ae7c3 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/function/Consumer.java @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.openhft.chronicle.hash.function; + +/** + * Represents an operation that accepts a single input argument and returns no + * result. Unlike most other functional interfaces, {@code Consumer} is expected + * to operate via side-effects. + * + *

    This is a functional interface whose functional method is {@link #accept(Object)}. + * + *

    This is a copy of Java 8's {@code java.util.function.Consumer} interface. + * + * @param the type of the input to the operation + */ +public interface Consumer { + + /** + * Performs this operation on the given argument. + * + * @param t the input argument + */ + void accept(T t); +} diff --git a/src/main/java/net/openhft/chronicle/map/Function.java b/src/main/java/net/openhft/chronicle/hash/function/Function.java similarity index 67% rename from src/main/java/net/openhft/chronicle/map/Function.java rename to src/main/java/net/openhft/chronicle/hash/function/Function.java index 27828199a..bc7e3724d 100644 --- a/src/main/java/net/openhft/chronicle/map/Function.java +++ b/src/main/java/net/openhft/chronicle/hash/function/Function.java @@ -1,12 +1,13 @@ -package net.openhft.chronicle.map; +package net.openhft.chronicle.hash.function; import java.io.Serializable; /** * Represents a function that accepts one argument and produces a result. * - *

    This is a functional interface whose functional method is {@link - * #apply(Object)}. + *

    This is a functional interface whose functional method is {@link #apply(Object)}. + * + *

    This is a copy of Java 8's {@code java.util.function.Function} interface. * * @param the type of the input to the function * @param the type of the result of the function diff --git a/src/main/java/net/openhft/chronicle/hash/function/Predicate.java b/src/main/java/net/openhft/chronicle/hash/function/Predicate.java new file mode 100644 index 000000000..05fd9b54f --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/function/Predicate.java @@ -0,0 +1,39 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.openhft.chronicle.hash.function; + + +/** + * Represents a predicate (boolean-valued function) of one argument. + * + *

    This is a functional interface whose functional method is {@link #test(Object)}. + * + *

    This is a copy of Java 8's {@code java.util.function.Predicate} interface. + * + * @param the type of the input to the predicate + */ +public interface Predicate { + + /** + * Evaluates this predicate on the given argument. + * + * @param t the input argument + * @return {@code true} if the input argument matches the predicate, + * otherwise {@code false} + */ + boolean test(T t); +} diff --git a/src/main/java/net/openhft/chronicle/hash/function/package-info.java b/src/main/java/net/openhft/chronicle/hash/function/package-info.java new file mode 100644 index 000000000..8607e8247 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/hash/function/package-info.java @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Copies of some interfaces from Java 8 used by Chronicle Map project + * for Java 7+. + */ +package net.openhft.chronicle.hash.function; \ No newline at end of file diff --git a/src/main/java/net/openhft/chronicle/hash/replication/LateUpdateException.java b/src/main/java/net/openhft/chronicle/hash/replication/LateUpdateException.java index cd8f99da5..29fcbc65c 100644 --- a/src/main/java/net/openhft/chronicle/hash/replication/LateUpdateException.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/LateUpdateException.java @@ -16,11 +16,13 @@ package net.openhft.chronicle.hash.replication; +import net.openhft.chronicle.hash.ChronicleHash; + /** - * Thrown if update to replicated {@link net.openhft.chronicle.hash.ChronicleHash} is rejected - * due to local time is lesser than the time of the latest update to the entry. This most likely - * indicates bugs or largely inconsistent timing across replicating nodes. If this is tolerated, - * you should catch {@code LateUpdateException} and ignore it. + * Thrown if update to replicated {@link ChronicleHash} is rejected due to local time is lesser + * than the time of the latest update to the entry. This most likely indicates bugs or largely + * inconsistent timing across replicating nodes. If this is tolerated, you should catch + * {@code LateUpdateException} and ignore it. */ public class LateUpdateException extends RuntimeException { private static final long serialVersionUID = 0L; diff --git a/src/main/java/net/openhft/chronicle/map/AbstractChronicleMap.java b/src/main/java/net/openhft/chronicle/map/AbstractChronicleMap.java index fdfc336c9..eb38d48db 100644 --- a/src/main/java/net/openhft/chronicle/map/AbstractChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/AbstractChronicleMap.java @@ -17,6 +17,10 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.KeyContext; +import net.openhft.chronicle.hash.function.Consumer; +import net.openhft.chronicle.hash.function.Function; +import net.openhft.chronicle.hash.function.Predicate; +import net.openhft.chronicle.map.ReplicatedChronicleMap.ReplicatedContext; import org.jetbrains.annotations.NotNull; import java.io.File; @@ -26,6 +30,7 @@ import java.util.*; import static java.util.Collections.emptyList; import static net.openhft.chronicle.map.Objects.requireNonNull; +import static net.openhft.chronicle.map.VanillaContext.SearchState.DELETED; abstract class AbstractChronicleMap extends AbstractMap implements ChronicleMap, Serializable { @@ -39,14 +44,14 @@ abstract class AbstractChronicleMap extends AbstractMap @Override @SuppressWarnings("unchecked") public final boolean containsKey(Object key) { - try (MapKeyContext c = context((K) key)) { + try (MapKeyContext c = context((K) key)) { return c.containsKey(); } } @Override public final V put(K key, V value) { - try (MapKeyContext c = context(key)) { + try (MapKeyContext c = context(key)) { // 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 @@ -58,14 +63,14 @@ abstract class AbstractChronicleMap extends AbstractMap } } - V prevValueOnPut(MapKeyContext context) { + V prevValueOnPut(MapKeyContext context) { return context.getUsing(null); } @Override public final V putIfAbsent(K key, V value) { checkValue(value); - try (MapKeyContext c = context(key)) { + try (MapKeyContext c = context(key)) { // putIfAbsent() shouldn't actually put most of the time, // so check if the key is present under read lock first: if (c.readLock().tryLock()) { @@ -99,7 +104,7 @@ abstract class AbstractChronicleMap extends AbstractMap @Override public final V getUsing(K key, V usingValue) { - try (MapKeyContext c = context(key)) { + try (MapKeyContext c = context(key)) { return c.getUsing(usingValue); } } @@ -142,7 +147,7 @@ abstract class AbstractChronicleMap extends AbstractMap @Override public R getMapped(K key, @NotNull Function function) { requireNonNull(function); - try (MapKeyContext c = context(key)) { + try (MapKeyContext c = context(key)) { return c.containsKey() ? function.apply(c.get()) : null; } } @@ -150,7 +155,7 @@ abstract class AbstractChronicleMap extends AbstractMap @Override public V putMapped(K key, @NotNull UnaryOperator unaryOperator) { requireNonNull(unaryOperator); - try (MapKeyContext c = context(key)) { + try (MapKeyContext c = context(key)) { // putMapped() should find a value, update & put most of the time, // so don't try to check key presence under read lock first, // as in putIfAbsent()/acquireUsing(), start with update lock: @@ -178,7 +183,7 @@ abstract class AbstractChronicleMap extends AbstractMap @Override @SuppressWarnings("unchecked") public final V remove(Object key) { - try (MapKeyContext c = context((K) key)) { + try (MapKeyContext c = context((K) key)) { // 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 @@ -190,7 +195,7 @@ abstract class AbstractChronicleMap extends AbstractMap } } - V prevValueOnRemove(MapKeyContext context) { + V prevValueOnRemove(MapKeyContext context) { return context.getUsing(null); } @@ -200,7 +205,7 @@ abstract class AbstractChronicleMap extends AbstractMap if (value == null) return false; // CHM compatibility; General ChronicleMap policy is to throw NPE checkValue((V) value); - try (MapKeyContext c = context((K) key)) { + try (MapKeyContext c = context((K) key)) { // 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: @@ -212,7 +217,7 @@ abstract class AbstractChronicleMap extends AbstractMap @Override public final V replace(K key, V value) { checkValue(value); - try (MapKeyContext c = context(key)) { + try (MapKeyContext c = context(key)) { // 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: @@ -232,7 +237,7 @@ abstract class AbstractChronicleMap extends AbstractMap public final boolean replace(K key, V oldValue, V newValue) { checkValue(oldValue); checkValue(newValue); - try (MapKeyContext c = context(key)) { + try (MapKeyContext c = context(key)) { // 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: @@ -261,7 +266,6 @@ abstract class AbstractChronicleMap extends AbstractMap class EntryIterator implements Iterator>, HashLookup.EntryConsumer { /** * Very inefficient (esp. if segments are large), but CORRECT implementation - * TODO map.forEachEntry(Consumer>) */ private final Thread ownerThread = Thread.currentThread(); private int segmentIndex = actualSegments() - 1; @@ -347,7 +351,7 @@ abstract class AbstractChronicleMap extends AbstractMap @Override public V setValue(V value) { - try (MapKeyContext c = AbstractChronicleMap.this.context(getKey())) { + try (MapKeyContext c = AbstractChronicleMap.this.context(getKey())) { c.put(value); } return super.setValue(value); @@ -401,4 +405,81 @@ abstract class AbstractChronicleMap extends AbstractMap AbstractChronicleMap.this.clear(); } } + + @Override + public void forEachEntry(final Consumer> action) { + forEachEntryWhile(new Predicate>() { + @Override + public boolean test(MapKeyContext c) { + action.accept(c); + return true; + } + }); + } + + @Override + public boolean forEachEntryWhile(final Predicate> predicate) { + boolean interrupt = false; + iteration: + try (VanillaContext c = mapContext()) { + ForEachWhilePredicate hashLookupPredicate = + new ForEachWhilePredicate<>(c, predicate); + for (int segmentIndex = actualSegments() - 1; segmentIndex >= 0; segmentIndex--) { + c.segmentIndex = segmentIndex; + c.forEachEntry = true; + try { + c.updateLock().lock(); + if (c.size() == 0) + continue; + c.initSegment(); + c.hashLookup.forEachRemoving(hashLookupPredicate); + if (hashLookupPredicate.shouldBreak) { + interrupt = true; + break iteration; + } + } finally { + c.forEachEntry = false; + c.closeSegmentIndex(); + } + } + } + return !interrupt; + } + + + private static class ForEachWhilePredicate implements HashLookup.EntryPredicate { + private final VanillaContext c; + private final Predicate> predicate; + boolean shouldBreak = false; + + public ForEachWhilePredicate(VanillaContext c, + Predicate> predicate) { + this.c = c; + this.predicate = predicate; + shouldBreak = false; + } + + @Override + public boolean remove(long hash, long pos) { + c.pos = pos; + c.initKeyFromPos(); + try { + if (!c.containsKey()) // for replicated map + return false; + shouldBreak = predicate.test(c); + // release all exclusive locks: possibly if context.remove() is performed + // in the callback, or acquired manually + while (c.writeLockCount > 0) { + c.writeLock().unlock(); + } + if (!c.isUpdateLocked()) { + throw new IllegalStateException("Shouldn't release update lock " + + "inside forEachEntry() callback"); + } + return c.state == DELETED && !(c instanceof ReplicatedContext); + } finally { + c.closeKeySearch(); + } + } + } } diff --git a/src/main/java/net/openhft/chronicle/map/BytesChronicleMap.java b/src/main/java/net/openhft/chronicle/map/BytesChronicleMap.java index acb5a5ea6..c2afca4ed 100644 --- a/src/main/java/net/openhft/chronicle/map/BytesChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/BytesChronicleMap.java @@ -87,7 +87,8 @@ public class BytesChronicleMap extends AbstractChronicleMap { @NotNull @Override - public MapKeyContext acquireContext(@NotNull Bytes key, @NotNull Bytes usingValue) { + public MapKeyContext acquireContext( + @NotNull Bytes key, @NotNull Bytes usingValue) { throw new UnsupportedOperationException(); } diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java index b1509fcff..fec6c58c1 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMap.java @@ -19,6 +19,9 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.ChronicleHash; +import net.openhft.chronicle.hash.KeyContext; +import net.openhft.chronicle.hash.function.Consumer; +import net.openhft.chronicle.hash.function.Function; import net.openhft.chronicle.hash.serialization.BytesReader; import net.openhft.lang.io.Bytes; import net.openhft.lang.io.serialization.BytesMarshaller; @@ -53,10 +56,8 @@ import java.util.concurrent.ConcurrentMap; * @param the map key type * @param the map value type */ -public interface ChronicleMap extends ConcurrentMap, ChronicleHash { - - @Override - MapKeyContext context(K key); +public interface ChronicleMap + extends ConcurrentMap, ChronicleHash> { /** * Returns the value to which the specified key is mapped, or {@code null} if this map contains @@ -140,7 +141,7 @@ public interface ChronicleMap extends ConcurrentMap, ChronicleHash acquireContext(@NotNull K key, @NotNull V usingValue); + MapKeyContext acquireContext(@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. diff --git a/src/main/java/net/openhft/chronicle/map/HashLookup.java b/src/main/java/net/openhft/chronicle/map/HashLookup.java index d12daf88e..f599d6a38 100644 --- a/src/main/java/net/openhft/chronicle/map/HashLookup.java +++ b/src/main/java/net/openhft/chronicle/map/HashLookup.java @@ -218,8 +218,11 @@ class HashLookup { } private long remove0() { - long posToRemove = searchPos; - long entryToRemove = readEntry(searchPos); + return remove00(searchPos); + } + + private long remove00(long posToRemove) { + long entryToRemove = readEntry(posToRemove); long posToShift = posToRemove; while (true) { posToShift = step(posToShift); @@ -285,4 +288,26 @@ class HashLookup { action.accept(key(entry), value(entry)); } } + + static interface EntryPredicate { + boolean remove(long hash, long pos); + } + + public void forEachRemoving(EntryPredicate predicate) { + long pos = 0L; + while (!empty(readEntry(pos))) { + pos = step(pos); + } + long startPos = pos; + do { + pos = step(pos); + long entry = readEntry(pos); + if (!empty(entry)) { + if (predicate.remove(key(entry), value(entry))) { + remove00(pos); + pos = stepBack(pos); + } + } + } while (pos != startPos); + } } diff --git a/src/main/java/net/openhft/chronicle/map/MapKeyContext.java b/src/main/java/net/openhft/chronicle/map/MapKeyContext.java index 35f433135..a2da94c6a 100644 --- a/src/main/java/net/openhft/chronicle/map/MapKeyContext.java +++ b/src/main/java/net/openhft/chronicle/map/MapKeyContext.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.KeyContext; -public interface MapKeyContext extends KeyContext { +public interface MapKeyContext extends KeyContext { long valueOffset(); diff --git a/src/main/java/net/openhft/chronicle/map/ReplicationHubFindByName.java b/src/main/java/net/openhft/chronicle/map/ReplicationHubFindByName.java index b0a2625f5..c932b8f55 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicationHubFindByName.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicationHubFindByName.java @@ -70,7 +70,7 @@ class ReplicationHubFindByName implements FindByName { int withChannelId = nextFreeChannel.incrementAndGet(); - try (MapKeyContext c = map.context(config.name)) { + try (MapKeyContext c = map.context(config.name)) { c.updateLock().lock(); MapInstanceBuilder value = config.replicatedViaChannel(replicationHub.createChannel(withChannelId)); diff --git a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java index 58a6c2667..ff319671b 100644 --- a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java @@ -20,6 +20,9 @@ package net.openhft.chronicle.map; import com.sun.jdi.connect.spi.ClosedConnectionException; import net.openhft.chronicle.hash.RemoteCallTimeoutException; +import net.openhft.chronicle.hash.function.Consumer; +import net.openhft.chronicle.hash.function.Function; +import net.openhft.chronicle.hash.function.Predicate; import net.openhft.chronicle.hash.serialization.BytesReader; import net.openhft.lang.io.ByteBufferBytes; import net.openhft.lang.io.Bytes; @@ -223,6 +226,18 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon return kClass; } + @Override + public boolean forEachEntryWhile(Predicate> predicate) { + // TODO implement! + throw new UnsupportedOperationException(); + } + + @Override + public void forEachEntry(Consumer> action) { + // TODO implement! + throw new UnsupportedOperationException(); + } + @Override public Class valueClass() { return vClass; @@ -535,7 +550,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon } @Override - public MapKeyContext context(K key) { + public MapKeyContext context(K key) { throw new UnsupportedOperationException("Contexts are not supported by stateless clients"); } @@ -578,7 +593,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon @NotNull @Override - public MapKeyContext acquireContext(@NotNull K key, @NotNull V usingValue) { + public MapKeyContext acquireContext(@NotNull K key, @NotNull V usingValue) { throw new UnsupportedOperationException("Contexts are not supported by stateless clients"); } @@ -1894,7 +1909,6 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon return null; return (R) readObject(transactionId, startTime); - } } diff --git a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java index c948b9599..54fccd37a 100644 --- a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java @@ -18,6 +18,7 @@ package net.openhft.chronicle.map; +import net.openhft.chronicle.hash.function.Function; import net.openhft.chronicle.hash.replication.RemoteNodeValidator; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.hash.replication.ThrottlingConfig; diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 978a8d407..ed92edf7c 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -493,7 +493,7 @@ class VanillaChronicleMap, @NotNull @Override - public final MapKeyContext acquireContext(K key, V usingValue) { + public final MapKeyContext acquireContext(K key, V usingValue) { VanillaContext c = acquireContext(key); // TODO optimize to update lock in certain cases c.writeLock().lock(); diff --git a/src/main/java/net/openhft/chronicle/map/VanillaContext.java b/src/main/java/net/openhft/chronicle/map/VanillaContext.java index f2976e8bd..bed81d417 100644 --- a/src/main/java/net/openhft/chronicle/map/VanillaContext.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaContext.java @@ -44,7 +44,7 @@ import static net.openhft.lang.io.NativeBytes.UNSAFE; class VanillaContext, V, VI, MVI extends MetaBytesInterop> - implements MapKeyContext { + implements MapKeyContext { static interface ContextFactory { T createContext(VanillaContext root, int indexInContextCache); @@ -433,6 +433,18 @@ class VanillaContext, return keySize; } + @NotNull + @Override + public K key() { + if (key != null) + return key; + initKeySearch(); + initMapAndContextLocals(); + mapAndContextLocalsReference.mapAndContextLocals.reusableKey = + key0(mapAndContextLocalsReference.mapAndContextLocals.reusableKey); + assert key != null; + return key; + } ///////////////////////////////////////////////// // Key hash @@ -1654,7 +1666,7 @@ class VanillaContext, if (containsKey()) { initEntrySizeInChunks(); upgradeToWriteLock(); - hashLookup.remove(); + hashLookupRemove(); free(pos, entrySizeInChunks); size(size() - 1L); state = DELETED; @@ -1664,6 +1676,13 @@ class VanillaContext, } } + boolean forEachEntry; + + void hashLookupRemove() { + if (!forEachEntry) + hashLookup.remove(); + } + void initRemoveDependencies() { initSegment(); initLocks(); @@ -1901,9 +1920,13 @@ class VanillaContext, } K immutableKey() { + return key0(null); + } + + K key0(K usingKey) { initKeyReader(); entry.position(keyOffset); - key = keyReader.read(entry, keySize, null); + key = keyReader.read(entry, keySize, usingKey); return key; } diff --git a/src/main/java/net/openhft/chronicle/set/ChronicleSet.java b/src/main/java/net/openhft/chronicle/set/ChronicleSet.java index 2a30e1756..a5620fa2e 100644 --- a/src/main/java/net/openhft/chronicle/set/ChronicleSet.java +++ b/src/main/java/net/openhft/chronicle/set/ChronicleSet.java @@ -19,8 +19,9 @@ package net.openhft.chronicle.set; import net.openhft.chronicle.hash.ChronicleHash; +import net.openhft.chronicle.hash.KeyContext; import java.util.Set; -public interface ChronicleSet extends Set, ChronicleHash { +public interface ChronicleSet extends Set, ChronicleHash> { } diff --git a/src/main/java/net/openhft/chronicle/set/SetFromMap.java b/src/main/java/net/openhft/chronicle/set/SetFromMap.java index a782a632f..80317c016 100644 --- a/src/main/java/net/openhft/chronicle/set/SetFromMap.java +++ b/src/main/java/net/openhft/chronicle/set/SetFromMap.java @@ -19,6 +19,8 @@ package net.openhft.chronicle.set; import net.openhft.chronicle.hash.KeyContext; +import net.openhft.chronicle.hash.function.Consumer; +import net.openhft.chronicle.hash.function.Predicate; import net.openhft.chronicle.map.ChronicleMap; import java.io.File; @@ -130,6 +132,16 @@ class SetFromMap extends AbstractSet return m.keyClass(); } + @Override + public boolean forEachEntryWhile(Predicate> predicate) { + return m.forEachEntryWhile(predicate); + } + + @Override + public void forEachEntry(Consumer> action) { + m.forEachEntry(action); + } + @Override public File file() { return m.file(); diff --git a/src/main/java/net/openhft/xstream/converters/VanillaChronicleMapConverter.java b/src/main/java/net/openhft/xstream/converters/VanillaChronicleMapConverter.java index d49c20b6d..646384abc 100644 --- a/src/main/java/net/openhft/xstream/converters/VanillaChronicleMapConverter.java +++ b/src/main/java/net/openhft/xstream/converters/VanillaChronicleMapConverter.java @@ -19,6 +19,10 @@ package net.openhft.xstream.converters; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; +import net.openhft.chronicle.hash.function.Consumer; +import net.openhft.chronicle.hash.function.Predicate; +import net.openhft.chronicle.map.ChronicleMap; +import net.openhft.chronicle.map.MapKeyContext; import net.openhft.lang.model.constraints.NotNull; import java.util.Map; @@ -33,27 +37,26 @@ public class VanillaChronicleMapConverter extends AbstractChronicleMapConv } @Override - public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext + public void marshal(Object o, final HierarchicalStreamWriter writer, final MarshallingContext marshallingContext) { + ((ChronicleMap) o).forEachEntry(new Consumer>() { + @Override + public void accept(MapKeyContext c) { + writer.startNode("entry"); + { + final Object key = c.key(); + writer.startNode(key.getClass().getName()); + marshallingContext.convertAnother(key); + writer.endNode(); - for (Map.Entry e : (Iterable) ((Map) o).entrySet()) { - - writer.startNode("entry"); - { - final Object key = e.getKey(); - writer.startNode(key.getClass().getName()); - marshallingContext.convertAnother(key); - writer.endNode(); - - Object value = e.getValue(); - writer.startNode(value.getClass().getName()); - marshallingContext.convertAnother(value); + Object value = c.get(); + writer.startNode(value.getClass().getName()); + marshallingContext.convertAnother(value); + writer.endNode(); + } writer.endNode(); } - writer.endNode(); - } - + }); } - } diff --git a/src/test/java/eg/TestNesterInterfaceMarshal.java b/src/test/java/eg/TestNesterInterfaceMarshal.java index 0314ce7a1..7e39339f6 100644 --- a/src/test/java/eg/TestNesterInterfaceMarshal.java +++ b/src/test/java/eg/TestNesterInterfaceMarshal.java @@ -73,7 +73,7 @@ public class TestNesterInterfaceMarshal { TestInstrumentVOInterface intrumentVOInterface = map.newValueInstance(); - try (MapKeyContext wc = map.acquireContext + try (MapKeyContext wc = map.acquireContext ("KEY1", intrumentVOInterface)) { intrumentVOInterface.setSymbol("Flyer"); intrumentVOInterface.setCurrencyCode("USA"); diff --git a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java index 0ce372d5a..3ebc2341f 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java +++ b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java @@ -1,6 +1,7 @@ package net.openhft.chronicle.map; import com.google.common.primitives.Chars; +import net.openhft.chronicle.hash.function.Function; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.map.fromdocs.BondVOInterface; import net.openhft.lang.io.ByteBufferBytes; @@ -665,7 +666,7 @@ public class CHMUseCasesTest { try (ChronicleMap map = newInstance(builder)) { - try (MapKeyContext c = map.context("1")) { + try (MapKeyContext c = map.context("1")) { CharSequence value = c.get(); assertTrue(value instanceof StringBuilder); ((StringBuilder) value).append("Hello World"); @@ -688,7 +689,7 @@ public class CHMUseCasesTest { try (ChronicleMap map = newInstance(builder)) { - try (MapKeyContext c = map.context("1")) { + try (MapKeyContext c = map.context("1")) { IntValue value = c.get(); assertNull(value); } @@ -711,7 +712,7 @@ public class CHMUseCasesTest { try (ChronicleMap map = newInstance(builder)) { - try (MapKeyContext c = map.context("1")) { + try (MapKeyContext c = map.context("1")) { c.updateLock().lock(); IntValue value = c.get(); assertTrue(value instanceof IntValue); @@ -866,7 +867,7 @@ public class CHMUseCasesTest { mapChecks(); StringBuilder sb = new StringBuilder(); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); StringValue v = rc.get(); assertEquals("11", v.getValue()); @@ -876,7 +877,7 @@ public class CHMUseCasesTest { mapChecks(); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); StringValue v = rc.get(); assertEquals("22", v.getValue()); @@ -886,7 +887,7 @@ public class CHMUseCasesTest { mapChecks(); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); StringValue v = rc.get(); assertEquals("11", v.getValue()); @@ -896,7 +897,7 @@ public class CHMUseCasesTest { mapChecks(); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); StringValue v = rc.get(); assertEquals("22", v.getValue()); @@ -932,7 +933,7 @@ public class CHMUseCasesTest { mapChecks(); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals("1234", rc.get().getValue()); } @@ -957,7 +958,7 @@ public class CHMUseCasesTest { mapChecks(); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals("1234", rc.get().getValue()); } @@ -1286,25 +1287,25 @@ map.putMapped(1.0, new UnaryOperator() { ByteBuffer valueA = ByteBuffer.allocateDirect(8); ByteBuffer valueB = ByteBuffer.allocate(8); // assertBBEquals(value1, valueA); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertBBEquals(value1, rc.getUsing(valueA)); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertBBEquals(value2, rc.getUsing(valueA)); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertBBEquals(value1, rc.getUsing(valueB)); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertBBEquals(value2, rc.getUsing(valueB)); } - try (MapKeyContext wc = map.acquireContext(key1, valueA)) { + try (MapKeyContext wc = map.acquireContext(key1, valueA)) { assertBBEquals(value1, valueA); appendMode(valueA); valueA.clear(); @@ -1325,7 +1326,7 @@ map.putMapped(1.0, new UnaryOperator() { valueB.flip(); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); ByteBuffer bb1 = ByteBuffer.allocate(8); @@ -1435,7 +1436,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } @@ -1445,53 +1446,53 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue()); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals(0, value1.getValue()); value1.addValue(123); assertEquals(123, value1.getValue()); } - try (MapKeyContext wc = map.acquireContext(key1, value2)) { + try (MapKeyContext wc = map.acquireContext(key1, value2)) { assertEquals(123, value2.getValue()); value2.addValue(1230 - 123); assertEquals(1230, value2.getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals(0, value2.getValue()); value2.addValue(123); assertEquals(123, value2.getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(123, value1.getValue()); value1.addValue(1230 - 123); assertEquals(1230, value1.getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } @@ -1555,7 +1556,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } @@ -1564,15 +1565,15 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue()); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } @@ -1595,7 +1596,7 @@ map.putMapped(1.0, new UnaryOperator() { value2.addValue(1230 - 123); assertEquals(1230, value2.getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } @@ -1610,7 +1611,7 @@ map.putMapped(1.0, new UnaryOperator() { value1.addValue(1230 - 123); assertEquals(1230, value1.getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } @@ -1652,7 +1653,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } @@ -1661,53 +1662,53 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue()); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals(0, value1.getValue()); value1.addValue((short) 123); assertEquals(123, value1.getValue()); } - try (MapKeyContext wc = map.acquireContext(key1, value2)) { + try (MapKeyContext wc = map.acquireContext(key1, value2)) { assertEquals(123, value2.getValue()); value2.addValue((short) (1230 - 123)); assertEquals(1230, value2.getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals(0, value2.getValue()); value2.addValue((short) 123); assertEquals(123, value2.getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(123, value1.getValue()); value1.addValue((short) (1230 - 123)); assertEquals(1230, value1.getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } @@ -1749,7 +1750,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } @@ -1758,53 +1759,53 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue()); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals(0, value1.getValue()); value1.addValue(123); assertEquals(123, value1.getValue()); } - try (MapKeyContext wc = map.acquireContext(key1, value2)) { + try (MapKeyContext wc = map.acquireContext(key1, value2)) { assertEquals(123, value2.getValue()); value2.addValue(1230 - 123); assertEquals(1230, value2.getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals(0, value2.getValue()); value2.addValue(123); assertEquals(123, value2.getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(123, value1.getValue()); value1.addValue(1230 - 123); assertEquals(1230, value1.getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } @@ -1843,7 +1844,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } @@ -1852,28 +1853,28 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue()); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals('\0', value1.getValue()); value1.setValue('@'); assertEquals('@', value1.getValue()); @@ -1883,22 +1884,22 @@ map.putMapped(1.0, new UnaryOperator() { value2.setValue('#'); assertEquals('#', value2.getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals('#', rc.get().getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals('\0', value2.getValue()); value2.setValue(';'); assertEquals(';', value2.getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(';', value1.getValue()); value1.setValue('['); assertEquals('[', value1.getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals('[', rc.get().getValue()); } @@ -1941,7 +1942,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } @@ -1950,53 +1951,53 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue()); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals(0, value1.getValue()); value1.addValue(234); assertEquals(234, value1.getValue()); } - try (MapKeyContext wc = map.acquireContext(key1, value2)) { + try (MapKeyContext wc = map.acquireContext(key1, value2)) { assertEquals(234, value2.getValue()); value2.addValue(-100); assertEquals(134, value2.getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(134, rc.get().getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals(0, value2.getValue()); value2.addValue((byte) 123); assertEquals(123, value2.getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(123, value1.getValue()); value1.addValue((byte) -111); assertEquals(12, value1.getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(12, rc.get().getValue()); } @@ -2037,7 +2038,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(true, rc.get().getValue()); } @@ -2046,53 +2047,53 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(false, value2.getValue()); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(false, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(true, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(false, rc.get().getValue()); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals(false, value1.getValue()); value1.setValue(true); assertEquals(true, value1.getValue()); } - try (MapKeyContext wc = map.acquireContext(key1, value2)) { + try (MapKeyContext wc = map.acquireContext(key1, value2)) { assertEquals(true, value2.getValue()); value2.setValue(false); assertEquals(false, value2.getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(false, rc.get().getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals(false, value2.getValue()); value2.setValue(true); assertEquals(true, value2.getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(true, value1.getValue()); value1.setValue(false); assertEquals(false, value1.getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(false, rc.get().getValue()); } @@ -2133,7 +2134,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue(), 0); } @@ -2142,53 +2143,53 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue(), 0); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue(), 0); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue(), 0); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue(), 0); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals(0, value1.getValue(), 0); value1.addValue(123); assertEquals(123, value1.getValue(), 0); } - try (MapKeyContext wc = map.acquireContext(key1, value2)) { + try (MapKeyContext wc = map.acquireContext(key1, value2)) { assertEquals(123, value2.getValue(), 0); value2.addValue(1230 - 123); assertEquals(1230, value2.getValue(), 0); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue(), 0); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals(0, value2.getValue(), 0); value2.addValue(123); assertEquals(123, value2.getValue(), 0); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(123, value1.getValue(), 0); value1.addValue(1230 - 123); assertEquals(1230, value1.getValue(), 0); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue(), 0); } @@ -2234,7 +2235,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue(), 0); } @@ -2242,53 +2243,53 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue(), 0); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue(), 0); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue(), 0); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue(), 0); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals(0, value1.getValue(), 0); value1.addValue(123); assertEquals(123, value1.getValue(), 0); } - try (MapKeyContext wc = map.acquireContext(key1, value2)) { + try (MapKeyContext wc = map.acquireContext(key1, value2)) { assertEquals(123, value2.getValue(), 0); value2.addValue(1230 - 123); assertEquals(1230, value2.getValue(), 0); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue(), 0); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals(0, value2.getValue(), 0); value2.addValue(123); assertEquals(123, value2.getValue(), 0); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(123, value1.getValue(), 0); value1.addValue(1230 - 123); assertEquals(1230, value1.getValue(), 0); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue(), 0); } @@ -2331,7 +2332,7 @@ map.putMapped(1.0, new UnaryOperator() { map.put(key2, value2); assertEquals(value2, map.get(key2)); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } @@ -2340,53 +2341,53 @@ map.putMapped(1.0, new UnaryOperator() { // assertTrue(rc.present()); // assertEquals(22, value2.getValue()); // } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(11, rc.get().getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(22, rc.get().getValue()); } key1.setValue(3); - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertFalse(rc.containsKey()); } key2.setValue(4); - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertFalse(rc.containsKey()); } - try (MapKeyContext wc = map.acquireContext(key1, value1)) { + try (MapKeyContext wc = map.acquireContext(key1, value1)) { assertEquals(0, value1.getValue()); value1.addValue(123); assertEquals(123, value1.getValue()); } - try (MapKeyContext wc = map.acquireContext(key1, value2)) { + try (MapKeyContext wc = map.acquireContext(key1, value2)) { assertEquals(123, value2.getValue()); value2.addValue(1230 - 123); assertEquals(1230, value2.getValue()); } - try (MapKeyContext rc = map.context(key1)) { + try (MapKeyContext rc = map.context(key1)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value2)) { + try (MapKeyContext wc = map.acquireContext(key2, value2)) { assertEquals(0, value2.getValue()); value2.addValue(123); assertEquals(123, value2.getValue()); } - try (MapKeyContext wc = map.acquireContext(key2, value1)) { + try (MapKeyContext wc = map.acquireContext(key2, value1)) { assertEquals(123, value1.getValue()); value1.addValue(1230 - 123); assertEquals(1230, value1.getValue()); } - try (MapKeyContext rc = map.context(key2)) { + try (MapKeyContext rc = map.context(key2)) { assertTrue(rc.containsKey()); assertEquals(1230, rc.get().getValue()); } @@ -2467,7 +2468,7 @@ map.putMapped(1.0, new UnaryOperator() { assertEquals(new LinkedHashSet(asList("two")), list1); } Set list2 = new LinkedHashSet<>(); - try (MapKeyContext> rc = map.context("1")) { + try (MapKeyContext> rc = map.context("1")) { assertTrue(rc.containsKey()); assertEquals(new LinkedHashSet(asList("two")), rc.getUsing(list2)); } @@ -2475,7 +2476,7 @@ map.putMapped(1.0, new UnaryOperator() { list1.add("three"); assertEquals(new LinkedHashSet(asList("one", "three")), list1); } - try (MapKeyContext> rc = map.context("2")) { + try (MapKeyContext> rc = map.context("2")) { assertTrue(rc.containsKey()); assertEquals(new LinkedHashSet(asList("one", "three")), rc.getUsing(list2)); } @@ -2512,7 +2513,7 @@ map.putMapped(1.0, new UnaryOperator() { assertEquals(mapOf("two", "bi"), map1); } Map map2 = new LinkedHashMap<>(); - try (MapKeyContext> rc = map.context("1")) { + try (MapKeyContext> rc = map.context("1")) { assertTrue(rc.containsKey()); assertEquals(mapOf("two", "bi"), rc.getUsing(map2)); } @@ -2520,7 +2521,7 @@ map.putMapped(1.0, new UnaryOperator() { map1.put("three", "tri"); assertEquals(mapOf("one", "uni", "three", "tri"), map1); } - try (MapKeyContext> rc = map.context("2")) { + try (MapKeyContext> rc = map.context("2")) { assertTrue(rc.containsKey()); assertEquals(mapOf("one", "uni", "three", "tri"), rc.getUsing(map2)); } @@ -2550,7 +2551,7 @@ map.putMapped(1.0, new UnaryOperator() { assertEquals(mapOf("two", 2), map1); } Map map2 = new LinkedHashMap<>(); - try (MapKeyContext> rc = map.context("1")) { + try (MapKeyContext> rc = map.context("1")) { assertTrue(rc.containsKey()); assertEquals(mapOf("two", 2), rc.getUsing(map2)); } @@ -2558,7 +2559,7 @@ map.putMapped(1.0, new UnaryOperator() { map1.put("three", 3); assertEquals(mapOf("one", 1, "three", 3), map1); } - try (MapKeyContext> rc = map.context("2")) { + try (MapKeyContext> rc = map.context("2")) { assertTrue(rc.containsKey()); assertEquals(mapOf("one", 1, "three", 3), rc.getUsing(map2)); } diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapImportExportTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapImportExportTest.java index 3e9e3c10a..e63b602b2 100644 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapImportExportTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapImportExportTest.java @@ -255,7 +255,7 @@ public class ChronicleMapImportExportTest { LongValue value = expected.newValueInstance(); // this will add the entry - try (MapKeyContext context = expected.acquireContext("one", value)) { + try (MapKeyContext context = expected.acquireContext("one", value)) { assertEquals(0, context.get().getValue()); assert value == context.get(); LongValue value1 = context.get(); diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java index 7d4701142..67c0be5bc 100755 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java @@ -921,7 +921,7 @@ public class ChronicleMapTest { long n; // 75% read if (rand.nextBoolean() || rand.nextBoolean()) { - try (MapKeyContext c = map.context(sb)) { + try (MapKeyContext c = map.context(sb)) { if (c.containsKey()) { // Attempt to pass abstraction hierarchies n = c.entry().readVolatileLong(c.valueOffset()) + 1; @@ -930,7 +930,7 @@ public class ChronicleMapTest { } } } else { - try (MapKeyContext wc = map.context(sb)) { + try (MapKeyContext wc = map.context(sb)) { wc.updateLock().lock(); if (wc.containsKey()) { n = wc.entry().addLong(wc.valueOffset(), 1); @@ -1636,7 +1636,7 @@ public class ChronicleMapTest { LongValue value = nativeLongValue(); // this will add the entry - try (MapKeyContext context = map.acquireContext("one", value)) { + try (MapKeyContext context = map.acquireContext("one", value)) { assertEquals(0, context.get().getValue()); assert value == context.get(); LongValue value1 = context.get(); @@ -1644,7 +1644,7 @@ public class ChronicleMapTest { } // check that the entry was added - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { assertEquals(true, context.containsKey()); LongValue v = context.getUsing(value); assert v == value; @@ -1652,29 +1652,29 @@ public class ChronicleMapTest { } // this will remove the entry - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { context.remove(); } // check that the entry was removed - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { context.updateLock().lock(); assertEquals(false, context.containsKey()); assertEquals(null, context.get()); } - try (MapKeyContext context = map.acquireContext("one", value)) { + try (MapKeyContext context = map.acquireContext("one", value)) { assert value == context.get(); assertEquals(0, context.get().getValue()); } - try (MapKeyContext context = map.acquireContext("one", value)) { + try (MapKeyContext context = map.acquireContext("one", value)) { assert value == context.get(); context.get().addValue(1); } // check that the entry was removed - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { LongValue v = context.getUsing(value); assert value == v; assertEquals(1, context.get().getValue()); @@ -1728,7 +1728,7 @@ public class ChronicleMapTest { try (final ChronicleMap map = builder.create()) { // this will add the entry - try (MapKeyContext context = map.acquireContext("one", "")) { + try (MapKeyContext context = map.acquireContext("one", "")) { // do nothing } } @@ -1746,7 +1746,7 @@ public class ChronicleMapTest { .create()) { StringBuilder value = new StringBuilder(); - try (MapKeyContext context = map.acquireContext("one", value)) { + try (MapKeyContext context = map.acquireContext("one", value)) { value.append("Hello World"); } @@ -1766,55 +1766,55 @@ public class ChronicleMapTest { LongValue value = DataValueClasses.newDirectReference(LongValue.class); - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { assertEquals(false, context.containsKey()); assertEquals(null, context.get()); } - try (MapKeyContext context = map.acquireContext("one", value)) { + try (MapKeyContext context = map.acquireContext("one", value)) { value.setValue(10); } // this will add the entry - try (MapKeyContext context = map.acquireContext("one", value)) { + try (MapKeyContext context = map.acquireContext("one", value)) { LongValue value1 = context.get(); value1.addValue(1); } // check that the entry was added - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { assertEquals(true, context.containsKey()); assertEquals(11, context.get().getValue()); } // this will remove the entry - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { context.remove(); } // check that the entry was removed - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { assertEquals(false, context.containsKey()); assertEquals(null, context.get()); } - try (MapKeyContext context = map.acquireContext("one", value)) { + try (MapKeyContext context = map.acquireContext("one", value)) { assertEquals(0, context.get().getValue()); } value.setValue(1); - try (MapKeyContext lockedEntry = map.context("one")) { + try (MapKeyContext lockedEntry = map.context("one")) { assertEquals(1, lockedEntry.get().getValue()); } - try (MapKeyContext context = map.acquireContext("one", value)) { + try (MapKeyContext context = map.acquireContext("one", value)) { LongValue value1 = context.get(); value1.addValue(1); } // check that the entry was removed - try (MapKeyContext context = map.context("one")) { + try (MapKeyContext context = map.context("one")) { LongValue value1 = context.get(); assertEquals(2, value1.getValue()); } diff --git a/src/test/java/net/openhft/chronicle/map/MarkTest.java b/src/test/java/net/openhft/chronicle/map/MarkTest.java index 4496be6aa..825f5025c 100644 --- a/src/test/java/net/openhft/chronicle/map/MarkTest.java +++ b/src/test/java/net/openhft/chronicle/map/MarkTest.java @@ -16,6 +16,7 @@ package net.openhft.chronicle.map; +import net.openhft.chronicle.hash.function.Function; import net.openhft.lang.MemoryUnit; import org.junit.Test; diff --git a/src/test/java/net/openhft/chronicle/map/NoTcpReplicationSoakTest.java b/src/test/java/net/openhft/chronicle/map/NoTcpReplicationSoakTest.java index 3886bbd38..00cce8dd4 100644 --- a/src/test/java/net/openhft/chronicle/map/NoTcpReplicationSoakTest.java +++ b/src/test/java/net/openhft/chronicle/map/NoTcpReplicationSoakTest.java @@ -123,7 +123,7 @@ public class NoTcpReplicationSoakTest { if (rnd.nextBoolean()) { final CharSequence value = "test" + j; if (rnd.nextBoolean()) { - try (MapKeyContext c = map1.context(key)) { + try (MapKeyContext c = map1.context(key)) { ReplicatedChronicleMap.ReplicatedContext rc = (ReplicatedChronicleMap.ReplicatedContext) c; rc.newTimestamp = t; @@ -135,7 +135,7 @@ public class NoTcpReplicationSoakTest { executorService.submit(new Runnable() { @Override public void run() { - try (MapKeyContext c = map2.context(key)) { + try (MapKeyContext c = map2.context(key)) { ReplicatedChronicleMap.ReplicatedContext rc = (ReplicatedChronicleMap.ReplicatedContext) c; rc.newTimestamp = t; @@ -147,7 +147,7 @@ public class NoTcpReplicationSoakTest { }); } else { - try (MapKeyContext c = map2.context(key)) { + try (MapKeyContext c = map2.context(key)) { ReplicatedChronicleMap.ReplicatedContext rc = (ReplicatedChronicleMap.ReplicatedContext) c; rc.newTimestamp = t; @@ -159,7 +159,7 @@ public class NoTcpReplicationSoakTest { executorService.submit(new Runnable() { @Override public void run() { - try (MapKeyContext c = map1.context(key)) { + try (MapKeyContext c = map1.context(key)) { ReplicatedChronicleMap.ReplicatedContext rc = (ReplicatedChronicleMap.ReplicatedContext) c; rc.newTimestamp = t; @@ -174,7 +174,7 @@ public class NoTcpReplicationSoakTest { } else { if (rnd.nextBoolean()) { - try (MapKeyContext c = map1.context(key)) { + try (MapKeyContext c = map1.context(key)) { ReplicatedChronicleMap.ReplicatedContext rc = (ReplicatedChronicleMap.ReplicatedContext) c; rc.newTimestamp = t; @@ -186,7 +186,7 @@ public class NoTcpReplicationSoakTest { executorService.submit(new Runnable() { @Override public void run() { - try (MapKeyContext c = map2.context(key)) { + try (MapKeyContext c = map2.context(key)) { ReplicatedChronicleMap.ReplicatedContext rc = (ReplicatedChronicleMap.ReplicatedContext) c; rc.newTimestamp = t; @@ -198,7 +198,7 @@ public class NoTcpReplicationSoakTest { }); } else { - try (MapKeyContext c = map2.context(key)) { + try (MapKeyContext c = map2.context(key)) { ReplicatedChronicleMap.ReplicatedContext rc = (ReplicatedChronicleMap.ReplicatedContext) c; rc.newTimestamp = t; @@ -210,7 +210,7 @@ public class NoTcpReplicationSoakTest { executorService.submit(new Runnable() { @Override public void run() { - try (MapKeyContext c = map1.context(key)) { + try (MapKeyContext c = map1.context(key)) { ReplicatedChronicleMap.ReplicatedContext rc = (ReplicatedChronicleMap.ReplicatedContext) c; rc.newTimestamp = t; diff --git a/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java b/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java index de03f0a5e..56ec89e1f 100644 --- a/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java +++ b/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java @@ -18,6 +18,9 @@ package net.openhft.chronicle.map; +import net.openhft.chronicle.hash.function.Consumer; +import net.openhft.chronicle.hash.function.Function; +import net.openhft.chronicle.hash.function.Predicate; import org.jetbrains.annotations.NotNull; import org.junit.Assert; @@ -252,6 +255,16 @@ public class ReplicationCheckingMap implements ChronicleMap { return map1.keyClass(); } + @Override + public boolean forEachEntryWhile(Predicate> predicate) { + return map1.forEachEntryWhile(predicate); + } + + @Override + public void forEachEntry(Consumer> action) { + map1.forEachEntry(action); + } + @Override public Class valueClass() { return map1.valueClass(); @@ -264,12 +277,12 @@ public class ReplicationCheckingMap implements ChronicleMap { @NotNull @Override - public MapKeyContext acquireContext(@NotNull K key, @NotNull V usingValue) { + public MapKeyContext acquireContext(@NotNull K key, @NotNull V usingValue) { throw new UnsupportedOperationException(); } @Override - public MapKeyContext context(K key) { + public MapKeyContext context(K key) { throw new UnsupportedOperationException(); } diff --git a/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java b/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java index 76c36a822..9613ba244 100644 --- a/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java +++ b/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java @@ -19,6 +19,7 @@ package net.openhft.chronicle.map; import junit.framework.Assert; +import net.openhft.chronicle.hash.function.Function; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.lang.io.ByteBufferBytes; import net.openhft.lang.io.Bytes; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java index 19e5054ac..12d03f813 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java @@ -92,7 +92,7 @@ public class TCPSocketReplicationNativeTest { LongValue value = DataValueClasses.newDirectReference(LongValue.class); - try (MapKeyContext c = map1.acquireContext(1, value)) { + try (MapKeyContext c = map1.acquireContext(1, value)) { value.setValue(10); } @@ -108,11 +108,11 @@ public class TCPSocketReplicationNativeTest { LongValue value = DataValueClasses.newDirectReference(LongValue.class); - try (MapKeyContext c = map1.acquireContext(1, value)) { + try (MapKeyContext c = map1.acquireContext(1, value)) { value.setValue(10); } - try (MapKeyContext c = map1.acquireContext(1, value)) { + try (MapKeyContext c = map1.acquireContext(1, value)) { value.setValue(20); } diff --git a/src/test/java/net/openhft/chronicle/map/TcpReplicationWithAcquireContextTest.java b/src/test/java/net/openhft/chronicle/map/TcpReplicationWithAcquireContextTest.java index 650dfbe1d..69551dc8b 100644 --- a/src/test/java/net/openhft/chronicle/map/TcpReplicationWithAcquireContextTest.java +++ b/src/test/java/net/openhft/chronicle/map/TcpReplicationWithAcquireContextTest.java @@ -45,7 +45,7 @@ public class TcpReplicationWithAcquireContextTest { TestInstrumentVOInterface instrumentVOInterface = map1.newValueInstance(); - try (MapKeyContext wc = + try (MapKeyContext wc = map1.acquireContext("KEY1", instrumentVOInterface)) { instrumentVOInterface.setSymbol("Flyer"); instrumentVOInterface.setCurrencyCode("USA"); diff --git a/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java b/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java index 64d0e936b..338c4fb54 100644 --- a/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java @@ -314,7 +314,7 @@ public class TimeBasedReplicationTest extends JSR166TestCase { // now test assume that we receive a late update to the map, the following update // should be ignored final long late = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(5); - try (MapKeyContext c = map.context("key-1")) { + try (MapKeyContext c = map.context("key-1")) { c.writeLock().lock(); ((ReplicatedChronicleMap.ReplicatedContext) c).newIdentifier = (byte) 2; ((ReplicatedChronicleMap.ReplicatedContext) c).newTimestamp = late; @@ -350,7 +350,7 @@ public class TimeBasedReplicationTest extends JSR166TestCase { // test assume that we receive a late update to the map, the following update should be ignored final long late = System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(50); - try (MapKeyContext c = map.context("key-1")) { + try (MapKeyContext c = map.context("key-1")) { c.writeLock().lock(); ((ReplicatedChronicleMap.ReplicatedContext) c).newIdentifier = IDENTIFIER; ((ReplicatedChronicleMap.ReplicatedContext) c).newTimestamp = late; diff --git a/src/test/java/net/openhft/chronicle/map/fromdocs/OpenJDKAndHashMapExamplesTest.java b/src/test/java/net/openhft/chronicle/map/fromdocs/OpenJDKAndHashMapExamplesTest.java index fb6e6512d..73452ca9d 100644 --- a/src/test/java/net/openhft/chronicle/map/fromdocs/OpenJDKAndHashMapExamplesTest.java +++ b/src/test/java/net/openhft/chronicle/map/fromdocs/OpenJDKAndHashMapExamplesTest.java @@ -80,7 +80,7 @@ public class OpenJDKAndHashMapExamplesTest { .entries(1000) .createPersistedTo(file); - try (MapKeyContext c = chmB.context("369604103")) { + try (MapKeyContext c = chmB.context("369604103")) { BondVOInterface bond = c.get(); if (bond != null) { assertEquals(5.0 / 100, bond.getCoupon(), 0.0); @@ -97,7 +97,7 @@ public class OpenJDKAndHashMapExamplesTest { BondVOInterface bond = chm.newValueInstance(); // lookup the key and give me a reference I can update in a thread safe way. - try (MapKeyContext c = chm.acquireContext("369604103", bond)) { + try (MapKeyContext c = chm.acquireContext("369604103", bond)) { // found a key and bond has been set // get directly without touching the rest of the record. long _matDate = bond.getMaturityDate(); 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 eb224f30e..88928ee77 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 @@ -18,6 +18,9 @@ package net.openhft.chronicle.map.jsr166.map; +import net.openhft.chronicle.hash.function.Consumer; +import net.openhft.chronicle.hash.function.Function; +import net.openhft.chronicle.hash.function.Predicate; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.map.*; import net.openhft.chronicle.map.MapKeyContext; @@ -107,7 +110,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase { } @Override - public MapKeyContext context(K key) { + public MapKeyContext context(K key) { return d.context(key); } @@ -166,7 +169,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase { @NotNull @Override - public MapKeyContext acquireContext(@NotNull K key, @NotNull V usingValue) { + public MapKeyContext acquireContext(@NotNull K key, @NotNull V usingValue) { throw new UnsupportedOperationException(); } @@ -205,6 +208,16 @@ public class StatelessChronicleMapTest extends JSR166TestCase { return d.keyClass(); } + @Override + public boolean forEachEntryWhile(Predicate> predicate) { + return d.forEachEntryWhile(predicate); + } + + @Override + public void forEachEntry(Consumer> action) { + d.forEachEntry(action); + } + @Override public Class valueClass() { return d.valueClass();