Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISPN-12142 Implement conditional methods for RemoteCache compute #10967

Merged
merged 1 commit into from May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -64,6 +64,7 @@
import org.infinispan.commons.util.CloseableIteratorSet;
import org.infinispan.commons.util.Closeables;
import org.infinispan.commons.util.IntSet;
import org.infinispan.commons.util.concurrent.CompletableFutures;
import org.infinispan.query.dsl.Query;
import org.reactivestreams.Publisher;

Expand Down Expand Up @@ -362,12 +363,40 @@ public CompletableFuture<V> computeAsync(K key, BiFunction<? super K, ? super V,

@Override
public CompletableFuture<V> computeIfAbsentAsync(K key, Function<? super K, ? extends V> mappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
throw new UnsupportedOperationException();
CompletableFuture<V> cf = getAsync(key);
return cf.thenCompose(oldValue -> {
if (oldValue != null) return CompletableFuture.completedFuture(oldValue);

V newValue = mappingFunction.apply(key);
if (newValue == null) return CompletableFutures.completedNull();

return putIfAbsentAsync(key, newValue, lifespan, lifespanUnit, maxIdle, maxIdleUnit)
.thenApply(v -> v == null ? newValue : v);
});
}

@Override
public CompletableFuture<V> computeIfPresentAsync(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
throw new UnsupportedOperationException();
CompletableFuture<MetadataValue<V>> cf = getWithMetadataAsync(key);
return cf.thenCompose(metadata -> {
if (metadata == null || metadata.getValue() == null) return CompletableFutures.completedNull();

V newValue = remappingFunction.apply(key, metadata.getValue());
CompletableFuture<Boolean> done;
if (newValue == null) {
done = removeWithVersionAsync(key, metadata.getVersion());
} else {
done = replaceWithVersionAsync(key, newValue, metadata.getVersion(), lifespan, lifespanUnit, maxIdle, maxIdleUnit);
}

return done.thenCompose(success -> {
if (success) {
return CompletableFuture.completedFuture(newValue);
}

return computeIfPresentAsync(key, remappingFunction, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
});
});
}

@Override
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.function.Function;

import jakarta.transaction.SystemException;
Expand Down Expand Up @@ -208,6 +209,16 @@ public CompletableFuture<Boolean> removeAsync(Object key, Object value) {
txContext.compute((K) key, entry -> removeEntryIfEquals(entry, value), remoteGet);
}

@Override
public CompletableFuture<V> computeIfAbsentAsync(K key, Function<? super K, ? extends V> mappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
throw new UnsupportedOperationException();
}

@Override
public CompletableFuture<V> computeIfPresentAsync(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
throw new UnsupportedOperationException();
}

@Override
public TransactionManager getTransactionManager() {
return transactionManager;
Expand Down
Expand Up @@ -21,7 +21,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.ProtocolVersion;
Expand Down Expand Up @@ -129,34 +128,46 @@ public void testComputeIfAbsentMethods() {

final K targetKey = generator.key(0);

Function<? super K, ? extends V> remappingFunction = key ->
generator.value(1);
V value = generator.value(1);
assertNull(cache.computeIfAbsent(targetKey, ignore -> null));

Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfAbsent(targetKey, remappingFunction));
Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfAbsent(targetKey, remappingFunction, 1, TimeUnit.SECONDS));
Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfAbsent(targetKey, remappingFunction, 1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS));
// Exception are only thrown when value not exists.
expectException(TransportException.class, RuntimeException.class, "expected exception", () ->
cache.computeIfAbsent(targetKey, ignore -> { throw new RuntimeException("expected exception"); }));

Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfAbsentAsync(targetKey, remappingFunction));
Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfAbsentAsync(targetKey, remappingFunction, 1, TimeUnit.SECONDS));
Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfAbsentAsync(targetKey, remappingFunction, 1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS));
generator.assertEquals(value, cache.computeIfAbsent(targetKey, ignore -> value));
generator.assertEquals(value, cache.get(targetKey));
generator.assertEquals(value, cache.computeIfAbsent(targetKey, ignore -> generator.value(2)));
generator.assertEquals(value, cache.get(targetKey));

K anotherKey = generator.key(1);
V anotherValue = generator.value(3);
generator.assertEquals(anotherValue, cache.computeIfAbsent(anotherKey, ignore -> anotherValue, 1, TimeUnit.MINUTES, 3, TimeUnit.MINUTES));
}

@Test
public void testComputeIfPresentMethods() {
RemoteCache<K, V> cache = remoteCache();

final K targetKey = generator.key(0);

BiFunction<? super K, ? super V, ? extends V> remappingFunction = (key, value) ->
generator.value(1);

Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfPresent(targetKey, remappingFunction));
Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfPresent(targetKey, remappingFunction, 1, TimeUnit.SECONDS));
Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfPresent(targetKey, remappingFunction, 1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS));

Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfPresentAsync(targetKey, remappingFunction));
Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfPresentAsync(targetKey, remappingFunction, 1, TimeUnit.SECONDS));
Exceptions.expectException(UnsupportedOperationException.class, () -> cache.computeIfPresentAsync(targetKey, remappingFunction, 1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS));
V value = generator.value(0);
assertNull(cache.computeIfPresent(targetKey, (k, v) -> value));
assertNull(cache.get(targetKey));
assertNull(cache.put(targetKey, value));
generator.assertEquals(value, cache.get(targetKey));

V anotherValue = generator.value(1);
generator.assertEquals(anotherValue, cache.computeIfPresent(targetKey, (k, v) -> anotherValue));
generator.assertEquals(anotherValue, cache.get(targetKey));

// Exception are only thrown if a value exists.
expectException(TransportException.class, RuntimeException.class, "expected exception", () ->
cache.computeIfPresent(targetKey, (k, v) -> { throw new RuntimeException("expected exception"); }));

int beforeSize = cache.size();
assertNull(cache.computeIfPresent(targetKey, (k, v) -> null));
assertNull(cache.get(targetKey));
assertEquals(beforeSize - 1, cache.size());
}

@Test
Expand Down