mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-216 added a test for testPutLater
This commit is contained in:
@@ -204,17 +204,33 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
|
||||
*/
|
||||
<R> R updateForKey(K key, @NotNull Mutator<? super V, R> mutator);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a {@link java.util.concurrent.Future} containing the value to which the specified key is
|
||||
* mapped, or {@code null} if this map contains no mapping for the key. This method behaves the same as
|
||||
* {@link java.util.Map#get(java.lang.Object)} yet wraps result in a future a {@link
|
||||
* java.util.concurrent.Future}.
|
||||
*
|
||||
* @return a future containing the value to which the specified key is mapped after this method call, or
|
||||
* {@code null} if no value is mapped
|
||||
* @see java.util.Map#get(java.lang.Object)
|
||||
* @see java.util.concurrent.Future
|
||||
*/
|
||||
Future<V> getLater(K key);
|
||||
Future<V> getLater(@NotNull K key);
|
||||
|
||||
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map (optional operation). If the map
|
||||
* previously contained a mapping for the key, the old value is replaced by the specified value. (A map
|
||||
* <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only if {@link #containsKey(Object)
|
||||
* m.containsKey(k)} would return <tt>true</tt>.)
|
||||
*
|
||||
* @return a future containing the previous value associated with <tt>key</tt>, or <tt>null</tt> if there
|
||||
* was no mapping for <tt>key</tt>. (A <tt>null</tt> return can also indicate that the map previously
|
||||
* associated <tt>null</tt> with <tt>key</tt>, if the implementation supports <tt>null</tt> values.)
|
||||
* @see java.util.Map#get(java.lang.Object)
|
||||
* @see java.util.concurrent.Future
|
||||
*/
|
||||
Future<V> putLater(@NotNull K key,@NotNull V value);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -40,8 +40,10 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
|
||||
|
||||
private final AbstractChronicleMapBuilder chronicleMapBuilder;
|
||||
private final StatelessMapConfig builder;
|
||||
ChronicleMap<K, V> statelessClient;
|
||||
private final ChronicleMap<K, V> statelessClient;
|
||||
|
||||
private ChronicleMap<K, V> asyncStatelessClient;
|
||||
private ExecutorService executorService;
|
||||
|
||||
public StatelessChronicleMap(final StatelessMapConfig builder,
|
||||
final AbstractChronicleMapBuilder chronicleMapBuilder) throws IOException {
|
||||
@@ -53,7 +55,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
|
||||
|
||||
|
||||
@Override
|
||||
public synchronized Future<V> getLater(final K key) {
|
||||
public Future<V> getLater(@NotNull final K key) {
|
||||
return lazyExecutorService().submit(new Callable<V>() {
|
||||
@Override
|
||||
public V call() throws Exception {
|
||||
@@ -62,7 +64,16 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
|
||||
});
|
||||
}
|
||||
|
||||
private ExecutorService executorService = null;
|
||||
@Override
|
||||
public Future<V> putLater(@NotNull final K key, @NotNull final V value) {
|
||||
return lazyExecutorService().submit(new Callable<V>() {
|
||||
@Override
|
||||
public V call() throws Exception {
|
||||
return lazyAsyncStatelessClient().put(key, value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private ExecutorService lazyExecutorService() {
|
||||
|
||||
@@ -79,8 +90,6 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
|
||||
}
|
||||
|
||||
|
||||
private ChronicleMap<K, V> asyncStatelessClient;
|
||||
|
||||
/**
|
||||
* @return a lazily created instance of the StatelessClient used for the async calls.
|
||||
*/
|
||||
@@ -90,7 +99,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable, Clon
|
||||
synchronized (this) {
|
||||
if (asyncStatelessClient == null)
|
||||
try {
|
||||
asyncStatelessClient = new StatelessChronicleMapUnsynchronized<K,V>(builder,
|
||||
asyncStatelessClient = new StatelessChronicleMapUnsynchronized<K, V>(builder,
|
||||
chronicleMapBuilder);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
|
||||
@@ -497,7 +497,12 @@ class StatelessChronicleMapUnsynchronized<K, V> implements ChronicleMap<K, V>, C
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<V> getLater(K key) {
|
||||
public Future<V> getLater(@NotNull K key) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<V> putLater(@NotNull K key, @NotNull V value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
@@ -385,7 +385,7 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, KI>,
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<V> getLater(K key) {
|
||||
public Future<V> getLater(@NotNull K key) {
|
||||
final V v = get(key);
|
||||
return new Future<V>() {
|
||||
|
||||
@@ -416,6 +416,38 @@ class VanillaChronicleMap<K, KI, MKI extends MetaBytesInterop<K, KI>,
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<V> putLater(@NotNull K key, @NotNull V value) {
|
||||
final V v = put(key, value);
|
||||
return new Future<V>() {
|
||||
|
||||
@Override
|
||||
public boolean cancel(boolean mayInterruptIfRunning) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDone() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get() throws InterruptedException, ExecutionException {
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||
return v;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public WriteContext<K, V> acquireUsingLocked(@NotNull K key, @NotNull V usingValue) {
|
||||
|
||||
@@ -256,7 +256,12 @@ public class ReplicationCheckingMap<K, V> implements ChronicleMap<K, V> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<V> getLater(K key) {
|
||||
public Future<V> getLater(@NotNull K key) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<V> putLater(@NotNull K key, @NotNull V value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
|
||||
@@ -418,10 +418,7 @@ public class StatelessClientTest {
|
||||
|
||||
statelessMap.remove(1);
|
||||
|
||||
|
||||
Assert.assertEquals(null, statelessMap.getLater(1).get());
|
||||
|
||||
|
||||
Assert.assertEquals(0, statelessMap.size());
|
||||
|
||||
}
|
||||
@@ -430,6 +427,27 @@ public class StatelessClientTest {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testPutLater() throws IOException,
|
||||
InterruptedException, ExecutionException {
|
||||
|
||||
try (ChronicleMap<Integer, CharSequence> serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
|
||||
.replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)).create()) {
|
||||
|
||||
try (ChronicleMap<Integer, CharSequence> statelessMap = ChronicleMapBuilder.of(Integer
|
||||
.class, CharSequence.class)
|
||||
.statelessClient(new InetSocketAddress("localhost", 8056)).create()) {
|
||||
|
||||
CharSequence oldValue = statelessMap.putLater(1, "some value").get();
|
||||
Assert.assertEquals("some value", statelessMap.get(1));
|
||||
Assert.assertEquals(1, statelessMap.size());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEquals() throws IOException, InterruptedException {
|
||||
|
||||
|
||||
@@ -178,10 +178,15 @@ public class StatelessChronicleMapTest extends JSR166TestCase {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<V> getLater(K key) {
|
||||
public Future<V> getLater(@NotNull K key) {
|
||||
return d.getLater(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<V> putLater(@NotNull K key, @NotNull V value) {
|
||||
return d.putLater(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V putIfAbsent(K key, V value) {
|
||||
return d.putIfAbsent(key, value);
|
||||
|
||||
Reference in New Issue
Block a user