minor optimization

pull/179/head
Nikita 10 years ago
parent 52168d064e
commit d75a83d68c

@ -54,12 +54,10 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong
@Override
public boolean compareAndSet(final long expect, final long update) {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
return new RedissonScript(connectionManager).evalR(
"if redis.call('get', KEYS[1]) == ARGV[1] then redis.call('set', KEYS[1], ARGV[2]); return true else return false end",
RScript.ReturnType.BOOLEAN,
keys, Collections.EMPTY_LIST, Arrays.asList(expect, update));
Collections.<Object>singletonList(getName()), Collections.EMPTY_LIST, Arrays.asList(expect, update));
}
@Override
@ -79,22 +77,18 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong
@Override
public long getAndAdd(final long delta) {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
return new RedissonScript(connectionManager).evalR(
"local v = redis.call('get', KEYS[1]) or 0; redis.call('set', KEYS[1], v + ARGV[1]); return tonumber(v)",
RScript.ReturnType.INTEGER,
keys, Collections.EMPTY_LIST, Collections.singletonList(delta));
Collections.<Object>singletonList(getName()), Collections.EMPTY_LIST, Collections.singletonList(delta));
}
@Override
public long getAndSet(final long newValue) {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
return new RedissonScript(connectionManager).evalR(
"local v = redis.call('get', KEYS[1]) or 0; redis.call('set', KEYS[1], ARGV[1]); return tonumber(v)",
RScript.ReturnType.INTEGER,
keys, Collections.EMPTY_LIST, Collections.singletonList(newValue));
Collections.<Object>singletonList(getName()), Collections.EMPTY_LIST, Collections.singletonList(newValue));
}
@Override
@ -118,12 +112,10 @@ public class RedissonAtomicLong extends RedissonExpirable implements RAtomicLong
@Override
public void set(final long newValue) {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
new RedissonScript(connectionManager).evalR(
"redis.call('set', KEYS[1], ARGV[1])",
RScript.ReturnType.STATUS,
keys, Collections.EMPTY_LIST, Collections.singletonList(newValue));
Collections.<Object>singletonList(getName()), Collections.EMPTY_LIST, Collections.singletonList(newValue));
}
public String toString() {

@ -36,7 +36,7 @@ import org.redisson.core.RScript;
* {@link LinkedBlockingQueue} where items are added as soon as
* <code>blpop</code> returns. All {@link BlockingQueue} methods are actually
* delegated to this intermediary queue.
*
*
* @author pdeschen@gmail.com
* @author Nikita Koksharov
*/
@ -81,7 +81,7 @@ public class RedissonBlockingQueue<V> extends RedissonQueue<V> implements RBlock
throws InterruptedException {
return pollLastAndOfferFirstTo(queue.getName(), timeout, unit);
}
@Override
public V pollLastAndOfferFirstTo(final String queueName, final long timeout, final TimeUnit unit) throws InterruptedException {
return connectionManager.write(getName(), new SyncInterruptedOperation<V, V>() {
@ -91,7 +91,7 @@ public class RedissonBlockingQueue<V> extends RedissonQueue<V> implements RBlock
}
});
}
@Override
public int remainingCapacity() {
return Integer.MAX_VALUE;
@ -102,14 +102,12 @@ public class RedissonBlockingQueue<V> extends RedissonQueue<V> implements RBlock
if (c == null) {
throw new NullPointerException();
}
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
List<V> list = new RedissonScript(connectionManager).eval(
"local vals = redis.call('lrange', KEYS[1], 0, -1); " +
"redis.call('ltrim', KEYS[1], -1, 0); " +
"return vals",
RScript.ReturnType.MAPVALUELIST,
keys);
Collections.<Object>singletonList(getName()));
c.addAll(list);
return list.size();
}
@ -123,15 +121,13 @@ public class RedissonBlockingQueue<V> extends RedissonQueue<V> implements RBlock
throw new NullPointerException();
}
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
List<V> list = new RedissonScript(connectionManager).evalR(
"local elemNum = math.min(ARGV[1], redis.call('llen', KEYS[1])) - 1;" +
"local vals = redis.call('lrange', KEYS[1], 0, elemNum); " +
"redis.call('ltrim', KEYS[1], elemNum + 1, -1); " +
"return vals",
RScript.ReturnType.MAPVALUELIST,
keys, Collections.emptyList(), Collections.singletonList(maxElements));
Collections.<Object>singletonList(getName()), Collections.emptyList(), Collections.singletonList(maxElements));
c.addAll(list);
return list.size();
}

@ -193,15 +193,14 @@ public class RedissonCountDownLatch extends RedissonObject implements RCountDown
if (getCount() <= 0) {
return;
}
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
new RedissonScript(connectionManager).evalR(
"local v = redis.call('decr', KEYS[1]);" +
"if v <= 0 then redis.call('del', KEYS[1]) end;" +
"if v == 0 then redis.call('publish', ARGV[2], ARGV[1]) end;" +
"return 'OK'",
RScript.ReturnType.STATUS,
keys, Collections.singletonList(zeroCountMessage), Collections.singletonList(getChannelName()));
Collections.<Object>singletonList(getName()), Collections.singletonList(zeroCountMessage), Collections.singletonList(getChannelName()));
}
private String getEntryName() {
@ -218,13 +217,11 @@ public class RedissonCountDownLatch extends RedissonObject implements RCountDown
}
private long getCountInner() {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
Long val = new RedissonScript(connectionManager).eval(
"return redis.call('get', KEYS[1])",
RScript.ReturnType.INTEGER,
keys);
Collections.<Object>singletonList(getName()));
if (val == null) {
return 0;
@ -233,23 +230,19 @@ public class RedissonCountDownLatch extends RedissonObject implements RCountDown
}
@Override
public boolean trySetCount(final long count) {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
public boolean trySetCount(long count) {
return new RedissonScript(connectionManager).evalR(
"if redis.call('exists', KEYS[1]) == 0 then redis.call('set', KEYS[1], ARGV[2]); redis.call('publish', ARGV[3], ARGV[1]); return true else return false end",
RScript.ReturnType.BOOLEAN,
keys, Collections.singletonList(newCountMessage), Arrays.asList(count, getChannelName()));
Collections.<Object>singletonList(getName()), Collections.singletonList(newCountMessage), Arrays.asList(count, getChannelName()));
}
@Override
public boolean delete() {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
Boolean deleted = new RedissonScript(connectionManager).evalR(
"if redis.call('del', KEYS[1]) == 1 then redis.call('publish', ARGV[2], ARGV[1]); return true else return false end",
RScript.ReturnType.BOOLEAN,
keys, Collections.singletonList(newCountMessage), Collections.singletonList(getChannelName()));
Collections.<Object>singletonList(getName()), Collections.singletonList(newCountMessage), Collections.singletonList(getChannelName()));
if (!deleted) {
throw new IllegalStateException();
}

@ -95,7 +95,7 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
public boolean add(V e) {
return addAll(Collections.singleton(e));
}
@Override
public Future<Boolean> addAsync(V e) {
return addAllAsync(Collections.singleton(e));
@ -147,12 +147,12 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
public boolean addAll(Collection<? extends V> c) {
return connectionManager.get(addAllAsync(c));
}
@Override
public Future<Boolean> addAllAsync(final Collection<? extends V> c) {
if (c.isEmpty()) {
return connectionManager.getGroup().next().newSucceededFuture(false);
}
}
return connectionManager.writeAsync(getName(), new AsyncOperation<Object, Boolean>() {
@Override
public void execute(final Promise<Boolean> promise, RedisAsyncConnection<Object, Object> async) {
@ -188,9 +188,6 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
// insert into middle of list
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
return "OK".equals(new RedissonScript(connectionManager).evalR(
"local ind = table.remove(ARGV); " + // index is last parameter
"local tail = redis.call('lrange', KEYS[1], ind, -1); " +
@ -199,7 +196,7 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
"for i, v in ipairs(tail) do redis.call('rpush', KEYS[1], v) end;" +
"return 'OK'",
RScript.ReturnType.STATUS,
keys, new ArrayList<Object>(coll), Collections.singletonList(index)));
Collections.<Object>singletonList(getName()), new ArrayList<Object>(coll), Collections.singletonList(index)));
} else {
// append to list
return addAll(coll);
@ -260,7 +257,7 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
}
});
}
@Override
public V get(int index) {
checkIndex(index);
@ -296,15 +293,14 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
public V set(final int index, final V element) {
checkIndex(index);
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
return new RedissonScript(connectionManager).evalR(
"local v = redis.call('lindex', KEYS[1], ARGV[2]); " +
"redis.call('lset', KEYS[1], ARGV[2], ARGV[1]); " +
"return v",
RScript.ReturnType.VALUE,
keys, Collections.singletonList(element), Collections.singletonList(index)
Collections.<Object>singletonList(getName()),
Collections.singletonList(element),
Collections.singletonList(index)
);
}
@ -338,8 +334,6 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
});
}
// else
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
return new RedissonScript(connectionManager).evalR(
"local v = redis.call('lindex', KEYS[1], ARGV[1]); " +
"local tail = redis.call('lrange', KEYS[1], ARGV[1]);" +
@ -347,7 +341,7 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
"for i, v in ipairs(tail) do redis.call('rpush', KEYS[1], v) end;" +
"return v",
RScript.ReturnType.VALUE,
keys, Collections.emptyList(), Collections.singletonList(index));
Collections.<Object>singletonList(getName()), Collections.emptyList(), Collections.singletonList(index));
}
@Override
@ -356,14 +350,12 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
return -1;
}
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
Long index = new RedissonScript(connectionManager).eval(
"local s = redis.call('llen', KEYS[1]);" +
"for i = 0, s, 1 do if ARGV[1] == redis.call('lindex', KEYS[1], i) then return i end end;" +
"return -1",
RScript.ReturnType.INTEGER,
keys, o);
Collections.<Object>singletonList(getName()), o);
return index.intValue();
}
@ -373,14 +365,12 @@ public class RedissonList<V> extends RedissonExpirable implements RList<V> {
return -1;
}
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
Long index = new RedissonScript(connectionManager).eval(
"local s = redis.call('llen', KEYS[1]);" +
"for i = s, 0, -1 do if ARGV[1] == redis.call('lindex', KEYS[1], i) then return i end end;" +
"return -1",
RScript.ReturnType.INTEGER,
keys, o);
Collections.<Object>singletonList(getName()), o);
return index.intValue();
}

@ -221,6 +221,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
private Long tryLockInner() {
Long ttlRemaining = tryLockInner(LOCK_EXPIRATION_INTERVAL_SECONDS, TimeUnit.SECONDS);
// lock acquired
if (ttlRemaining == null) {
newRefreshTask();
}
@ -229,8 +230,9 @@ public class RedissonLock extends RedissonExpirable implements RLock {
private void newRefreshTask() {
if (refreshTaskMap.containsKey(getName())) {
return;
return;
}
Timeout task = connectionManager.newTimeout(new TimerTask() {
@Override
public void run(Timeout timeout) throws Exception {
@ -239,6 +241,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
newRefreshTask(); // reschedule itself
}
}, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);
if (refreshTaskMap.putIfAbsent(getName(), task) != null) {
task.cancel();
}
@ -248,22 +251,17 @@ public class RedissonLock extends RedissonExpirable implements RLock {
* Stop refresh timer
* @return true if timer was stopped successfully
*/
private boolean stopRefreshTask() {
boolean returnValue =false;
Timeout task = refreshTaskMap.get(getName());
private void stopRefreshTask() {
Timeout task = refreshTaskMap.remove(getName());
if (task != null) {
returnValue = task.cancel();
refreshTaskMap.remove(getName());
task.cancel();
}
return returnValue;
}
private Long tryLockInner(final long leaseTime, final TimeUnit unit) {
internalLockLeaseTime = unit.toMillis(leaseTime);
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
return new RedissonScript(connectionManager)
.evalR("local v = redis.call('get', KEYS[1]); " +
"if (v == false) then " +
@ -278,7 +276,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
" return redis.call('pttl', KEYS[1]); " +
"end",
RScript.ReturnType.INTEGER,
keys, Collections.singletonList(id.toString() + "-" + Thread.currentThread().getId()), Collections.singletonList(internalLockLeaseTime));
Collections.<Object>singletonList(getName()), Collections.singletonList(id.toString() + "-" + Thread.currentThread().getId()), Collections.singletonList(internalLockLeaseTime));
}
public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
@ -340,8 +338,6 @@ public class RedissonLock extends RedissonExpirable implements RLock {
@Override
public void unlock() {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
String opStatus = new RedissonScript(connectionManager)
.evalR("local v = redis.call('get', KEYS[1]); " +
"if (v == false) then " +
@ -363,7 +359,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
" return nil; " +
"end",
RScript.ReturnType.STATUS,
keys, Arrays.asList(id.toString() + "-" + Thread.currentThread().getId(), unlockMessage), Arrays.asList(internalLockLeaseTime, getChannelName()));
Collections.<Object>singletonList(getName()), Arrays.asList(id.toString() + "-" + Thread.currentThread().getId(), unlockMessage), Arrays.asList(internalLockLeaseTime, getChannelName()));
if ("OK".equals(opStatus)) {
stopRefreshTask();
} else if ("FALSE".equals(opStatus)) {
@ -383,13 +379,11 @@ public class RedissonLock extends RedissonExpirable implements RLock {
@Override
public void forceUnlock() {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
stopRefreshTask();
new RedissonScript(connectionManager)
.evalR("redis.call('del', KEYS[1]); redis.call('publish', ARGV[2], ARGV[1]); return 'OK'",
RScript.ReturnType.STATUS,
keys, Collections.singletonList(unlockMessage), Collections.singletonList(getChannelName()));
Collections.<Object>singletonList(getName()), Collections.singletonList(unlockMessage), Collections.singletonList(getChannelName()));
}
@Override
@ -404,8 +398,6 @@ public class RedissonLock extends RedissonExpirable implements RLock {
@Override
public boolean isHeldByCurrentThread() {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
String opStatus = new RedissonScript(connectionManager)
.eval("local v = redis.call('get', KEYS[1]); " +
"if (v == false) then " +
@ -419,14 +411,12 @@ public class RedissonLock extends RedissonExpirable implements RLock {
" end;" +
"end",
RScript.ReturnType.STATUS,
keys, id.toString() + "-" + Thread.currentThread().getId());
Collections.<Object>singletonList(getName()), id.toString() + "-" + Thread.currentThread().getId());
return "OK".equals(opStatus);
}
@Override
public int getHoldCount() {
ArrayList<Object> keys = new ArrayList<Object>();
keys.add(getName());
Long opStatus = new RedissonScript(connectionManager)
.eval("local v = redis.call('get', KEYS[1]); " +
"if (v == false) then " +
@ -436,7 +426,7 @@ public class RedissonLock extends RedissonExpirable implements RLock {
" return o['c']; " +
"end",
RScript.ReturnType.INTEGER,
keys, id.toString() + "-" + Thread.currentThread().getId());
Collections.<Object>singletonList(getName()), id.toString() + "-" + Thread.currentThread().getId());
return opStatus.intValue();
}

Loading…
Cancel
Save