Skip to content

Commit

Permalink
(ISPN-1433) Async operations don't work on transactional or batch-usi…
Browse files Browse the repository at this point in the history
…ng caches
  • Loading branch information
mmarkus committed Oct 3, 2011
1 parent 07b179c commit 44b52a7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 16 deletions.
32 changes: 16 additions & 16 deletions core/src/main/java/org/infinispan/CacheImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,10 @@ public final NotifyingFuture<V> putAsync(K key, V value, long lifespan, TimeUnit

final NotifyingFuture<V> putAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit, EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
assertKeyNotNull(key);
InvocationContext ctx = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
InvocationContext ctx = getInvocationContextWithImplicitTransaction(explicitFlags, explicitClassLoader);
ctx.setUseFutureReturnType(true);
PutKeyValueCommand command = commandsFactory.buildPutKeyValueCommand(key, value, lifespanUnit.toMillis(lifespan), maxIdleUnit.toMillis(maxIdle), ctx.getFlags());
return wrapInFuture(invoker.invoke(ctx, command));
return wrapInFuture(executeCommandAndCommitIfNeeded(ctx, command));
}

public final NotifyingFuture<Void> putAllAsync(Map<? extends K, ? extends V> data, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
Expand All @@ -712,21 +712,21 @@ public final NotifyingFuture<Void> putAllAsync(Map<? extends K, ? extends V> dat

final NotifyingFuture<Void> putAllAsync(Map<? extends K, ? extends V> data, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit, EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
assertKeysNotNull(data);
InvocationContext ctx = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
InvocationContext ctx = getInvocationContextWithImplicitTransaction(explicitFlags, explicitClassLoader);
ctx.setUseFutureReturnType(true);
PutMapCommand command = commandsFactory.buildPutMapCommand(data, lifespanUnit.toMillis(lifespan), maxIdleUnit.toMillis(maxIdle), ctx.getFlags());
return wrapInFuture(invoker.invoke(ctx, command));
return wrapInFuture(executeCommandAndCommitIfNeeded(ctx, command));
}

public final NotifyingFuture<Void> clearAsync() {
return clearAsync(null, null);
}

final NotifyingFuture<Void> clearAsync(EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
InvocationContext ctx = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
InvocationContext ctx = getInvocationContextWithImplicitTransaction(explicitFlags, explicitClassLoader);
ctx.setUseFutureReturnType(true);
ClearCommand command = commandsFactory.buildClearCommand(ctx.getFlags());
return wrapInFuture(invoker.invoke(ctx, command));
return wrapInFuture(executeCommandAndCommitIfNeeded(ctx, command));
}

public final NotifyingFuture<V> putIfAbsentAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
Expand All @@ -735,11 +735,11 @@ public final NotifyingFuture<V> putIfAbsentAsync(K key, V value, long lifespan,

final NotifyingFuture<V> putIfAbsentAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit, EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
assertKeyNotNull(key);
InvocationContext ctx = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
InvocationContext ctx = getInvocationContextWithImplicitTransaction(explicitFlags, explicitClassLoader);
ctx.setUseFutureReturnType(true);
PutKeyValueCommand command = commandsFactory.buildPutKeyValueCommand(key, value, lifespanUnit.toMillis(lifespan), maxIdleUnit.toMillis(maxIdle), ctx.getFlags());
command.setPutIfAbsent(true);
return wrapInFuture(invoker.invoke(ctx, command));
return wrapInFuture(executeCommandAndCommitIfNeeded(ctx, command));
}

public final NotifyingFuture<V> removeAsync(Object key) {
Expand All @@ -748,10 +748,10 @@ public final NotifyingFuture<V> removeAsync(Object key) {

final NotifyingFuture<V> removeAsync(Object key, EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
assertKeyNotNull(key);
InvocationContext ctx = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
InvocationContext ctx = getInvocationContextWithImplicitTransaction(explicitFlags, explicitClassLoader);
ctx.setUseFutureReturnType(true);
RemoveCommand command = commandsFactory.buildRemoveCommand(key, null, ctx.getFlags());
return wrapInFuture(invoker.invoke(ctx, command));
return wrapInFuture(executeCommandAndCommitIfNeeded(ctx, command));
}

public final NotifyingFuture<Boolean> removeAsync(Object key, Object value) {
Expand All @@ -760,10 +760,10 @@ public final NotifyingFuture<Boolean> removeAsync(Object key, Object value) {

final NotifyingFuture<Boolean> removeAsync(Object key, Object value, EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
assertKeyNotNull(key);
InvocationContext ctx = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
InvocationContext ctx = getInvocationContextWithImplicitTransaction(explicitFlags, explicitClassLoader);
ctx.setUseFutureReturnType(true);
RemoveCommand command = commandsFactory.buildRemoveCommand(key, value, ctx.getFlags());
return wrapInFuture(invoker.invoke(ctx, command));
return wrapInFuture(executeCommandAndCommitIfNeeded(ctx, command));
}

public final NotifyingFuture<V> replaceAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
Expand All @@ -772,10 +772,10 @@ public final NotifyingFuture<V> replaceAsync(K key, V value, long lifespan, Time

final NotifyingFuture<V> replaceAsync(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit, EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
assertKeyNotNull(key);
InvocationContext ctx = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
InvocationContext ctx = getInvocationContextWithImplicitTransaction(explicitFlags, explicitClassLoader);
ctx.setUseFutureReturnType(true);
ReplaceCommand command = commandsFactory.buildReplaceCommand(key, null, value, lifespanUnit.toMillis(lifespan), maxIdleUnit.toMillis(maxIdle), ctx.getFlags());
return wrapInFuture(invoker.invoke(ctx, command));
return wrapInFuture(executeCommandAndCommitIfNeeded(ctx, command));
}

public final NotifyingFuture<Boolean> replaceAsync(K key, V oldValue, V newValue, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit) {
Expand All @@ -784,10 +784,10 @@ public final NotifyingFuture<Boolean> replaceAsync(K key, V oldValue, V newValue

final NotifyingFuture<Boolean> replaceAsync(K key, V oldValue, V newValue, long lifespan, TimeUnit lifespanUnit, long maxIdle, TimeUnit maxIdleUnit, EnumSet<Flag> explicitFlags, ClassLoader explicitClassLoader) {
assertKeyNotNull(key);
InvocationContext ctx = getInvocationContextForWrite(explicitFlags, explicitClassLoader);
InvocationContext ctx = getInvocationContextWithImplicitTransaction(explicitFlags, explicitClassLoader);
ctx.setUseFutureReturnType(true);
ReplaceCommand command = commandsFactory.buildReplaceCommand(key, oldValue, newValue, lifespanUnit.toMillis(lifespan), maxIdleUnit.toMillis(maxIdle), ctx.getFlags());
return wrapInFuture(invoker.invoke(ctx, command));
return wrapInFuture(executeCommandAndCommitIfNeeded(ctx, command));
}

public NotifyingFuture<V> getAsync(K key) {
Expand Down
61 changes: 61 additions & 0 deletions core/src/test/java/org/infinispan/api/TxCacheAndAsyncOpsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other
* contributors as indicated by the @author tags. All rights reserved.
* See the copyright.txt in the distribution for a full listing of
* individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.infinispan.api;

import org.infinispan.config.Configuration;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.test.SingleCacheManagerTest;
import org.infinispan.util.concurrent.NotifyingFuture;
import org.testng.annotations.Test;

import java.util.Collections;

/**
* @author Mircea Markus
* @since 5.1
*/
@Test (groups = "functional", testName = "api.TxAndAsyncOpsTest")
public class TxCacheAndAsyncOpsTest extends SingleCacheManagerTest {

@Override
protected EmbeddedCacheManager createCacheManager() throws Exception {
final Configuration defaultStandaloneConfig = getDefaultStandaloneConfig(true);
return new DefaultCacheManager(defaultStandaloneConfig);
}

public void testAsyncOps() throws Exception {

NotifyingFuture<Object> result = cache.putAsync("k", "v");
assert result.get() == null;

result = cache.removeAsync("k");
assert result.get().equals("v");

final NotifyingFuture<Void> voidNotifyingFuture = cache.putAllAsync(Collections.singletonMap("k", "v"));
voidNotifyingFuture.get();

assert cache.get("k").equals("v");
}
}

0 comments on commit 44b52a7

Please sign in to comment.