methods with threadId parameter added to RLockAsync interface

pull/968/head
Nikita 8 years ago
parent dce5467e0e
commit 895994a357

@ -434,6 +434,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
return forceUnlockAsync();
}
@Override
public RFuture<Void> unlockAsync() {
long threadId = Thread.currentThread().getId();
return unlockAsync(threadId);
@ -462,6 +463,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
}
@Override
public RFuture<Void> unlockAsync(final long threadId) {
final RPromise<Void> result = newPromise();
RFuture<Boolean> future = unlockInnerAsync(threadId);
@ -491,15 +493,23 @@ public class RedissonLock extends RedissonExpirable implements RLock {
return result;
}
@Override
public RFuture<Void> lockAsync() {
return lockAsync(-1, null);
}
public RFuture<Void> lockAsync(final long leaseTime, final TimeUnit unit) {
@Override
public RFuture<Void> lockAsync(long leaseTime, TimeUnit unit) {
final long currentThreadId = Thread.currentThread().getId();
return lockAsync(leaseTime, unit, currentThreadId);
}
@Override
public RFuture<Void> lockAsync(long currentThreadId) {
return lockAsync(-1, null, currentThreadId);
}
@Override
public RFuture<Void> lockAsync(final long leaseTime, final TimeUnit unit, final long currentThreadId) {
final RPromise<Void> result = newPromise();
RFuture<Long> ttlFuture = tryAcquireAsync(leaseTime, unit, currentThreadId);
@ -605,6 +615,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
return tryLockAsync(Thread.currentThread().getId());
}
@Override
public RFuture<Boolean> tryLockAsync(long threadId) {
return tryAcquireOnceAsync(-1, null, threadId);
}
@ -620,6 +631,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
return tryLockAsync(waitTime, leaseTime, unit, currentThreadId);
}
@Override
public RFuture<Boolean> tryLockAsync(final long waitTime, final long leaseTime, final TimeUnit unit,
final long currentThreadId) {
final RPromise<Boolean> result = newPromise();

@ -30,14 +30,24 @@ public interface RLockAsync extends RExpirableAsync {
RFuture<Void> unlockAsync();
RFuture<Void> unlockAsync(long threadId);
RFuture<Boolean> tryLockAsync();
RFuture<Void> lockAsync();
RFuture<Void> lockAsync(long threadId);
RFuture<Void> lockAsync(long leaseTime, TimeUnit unit);
RFuture<Void> lockAsync(long leaseTime, TimeUnit unit, long threadId);
RFuture<Boolean> tryLockAsync(long threadId);
RFuture<Boolean> tryLockAsync(long waitTime, TimeUnit unit);
RFuture<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit);
RFuture<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit, long threadId);
}

@ -0,0 +1,38 @@
/**
* Copyright 2016 Nikita Koksharov
*
* 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 org.redisson.api.map;
import java.util.Collection;
import java.util.Map;
/**
*
* @author Nikita Koksharov
*
* @param <K>
* @param <V>
*/
public interface MapWriter<K, V> {
void write(K key, V value);
void writeAll(Map<K, V> map);
void delete(K key);
void deleteAll(Collection<K> keys);
}

@ -0,0 +1,101 @@
package org.redisson;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.junit.Test;
import org.redisson.api.RMap;
import org.redisson.api.map.MapLoader;
public abstract class BaseMapTest extends BaseTest {
protected abstract <K, V> RMap<K, V> getLoaderTestMap(String name, Map<K, V> map);
@Test
public void testLoadAllReplaceValues() {
Map<String, String> cache = new HashMap<String, String>();
for (int i = 0; i < 10; i++) {
cache.put("" + i, "" + i + "" + i);
}
RMap<String, String> map = getLoaderTestMap("test", cache);
map.put("0", "010");
map.put("5", "555");
map.loadAll(false, 2);
assertThat(map.size()).isEqualTo(10);
assertThat(map.get("0")).isEqualTo("010");
assertThat(map.get("5")).isEqualTo("555");
map.clear();
map.put("0", "010");
map.put("5", "555");
map.loadAll(true, 2);
assertThat(map.size()).isEqualTo(10);
assertThat(map.get("0")).isEqualTo("00");
assertThat(map.get("5")).isEqualTo("55");
}
@Test
public void testLoadAll() {
Map<String, String> cache = new HashMap<String, String>();
for (int i = 0; i < 100; i++) {
cache.put("" + i, "" + (i*10 + i));
}
RMap<String, String> map = getLoaderTestMap("test", cache);
assertThat(map.size()).isEqualTo(0);
map.loadAll(false, 2);
assertThat(map.size()).isEqualTo(100);
for (int i = 0; i < 100; i++) {
assertThat(map.containsKey("" + i)).isTrue();
}
}
protected <K, V> MapLoader<K, V> createMapLoader(Map<K, V> map) {
return new MapLoader<K, V>() {
@Override
public V load(K key) {
return map.get(key);
}
@Override
public Iterable<K> loadAllKeys() {
return map.keySet();
}
};
}
@Test
public void testMapLoaderGet() {
Map<String, String> cache = new HashMap<String, String>();
cache.put("1", "11");
cache.put("2", "22");
cache.put("3", "33");
RMap<String, String> map = getLoaderTestMap("test", cache);
assertThat(map.size()).isEqualTo(0);
assertThat(map.get("1")).isEqualTo("11");
assertThat(map.size()).isEqualTo(1);
assertThat(map.get("0")).isNull();
map.put("0", "00");
assertThat(map.get("0")).isEqualTo("00");
assertThat(map.size()).isEqualTo(2);
Map<String, String> s = map.getAll(new HashSet<>(Arrays.asList("1", "2", "9", "3")));
Map<String, String> expectedMap = new HashMap<>();
expectedMap.put("1", "11");
expectedMap.put("2", "22");
expectedMap.put("3", "33");
assertThat(s).isEqualTo(expectedMap);
assertThat(map.size()).isEqualTo(4);
}
}
Loading…
Cancel
Save