From f50aa24fff45fed39232bfd450bafe482a50c858 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 19 May 2017 12:52:36 +0300 Subject: [PATCH] NPE checking in RedissonMapCache added --- .../main/java/org/redisson/RedissonMap.java | 90 +++++++------------ .../java/org/redisson/RedissonMapCache.java | 56 ++++++++++++ 2 files changed, 87 insertions(+), 59 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonMap.java b/redisson/src/main/java/org/redisson/RedissonMap.java index d48f6d418..5eb7552a7 100644 --- a/redisson/src/main/java/org/redisson/RedissonMap.java +++ b/redisson/src/main/java/org/redisson/RedissonMap.java @@ -127,11 +127,15 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture valueSizeAsync(K key) { + checkKey(key); + + return commandExecutor.readAsync(getName(), codec, RedisCommands.HSTRLEN, getName(key), key); + } + + protected void checkKey(Object key) { if (key == null) { throw new NullPointerException("map key can't be null"); } - - return commandExecutor.readAsync(getName(), codec, RedisCommands.HSTRLEN, getName(key), key); } @Override @@ -146,9 +150,7 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture containsKeyAsync(Object key) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } + checkKey(key); return commandExecutor.readAsync(getName(key), codec, RedisCommands.HEXISTS, getName(key), key); } @@ -160,9 +162,7 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture containsValueAsync(Object value) { - if (value == null) { - throw new NullPointerException("map value can't be null"); - } + checkValue(value); return commandExecutor.evalReadAsync(getName(), codec, new RedisCommand("EVAL", new BooleanReplayConvertor(), 4), "local s = redis.call('hvals', KEYS[1]);" + @@ -296,12 +296,8 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture putIfAbsentAsync(K key, V value) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } - if (value == null) { - throw new NullPointerException("map value can't be null"); - } + checkKey(key); + checkValue(key); return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_PUT, "if redis.call('hsetnx', KEYS[1], ARGV[1], ARGV[2]) == 1 then " @@ -319,12 +315,8 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture fastPutIfAbsentAsync(K key, V value) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } - if (value == null) { - throw new NullPointerException("map value can't be null"); - } + checkKey(key); + checkValue(value); return commandExecutor.writeAsync(getName(key), codec, RedisCommands.HSETNX, getName(key), key, value); } @@ -336,12 +328,8 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture removeAsync(Object key, Object value) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } - if (value == null) { - throw new NullPointerException("map value can't be null"); - } + checkKey(key); + checkValue(value); return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REMOVE_VALUE, "if redis.call('hget', KEYS[1], ARGV[1]) == ARGV[2] then " @@ -352,6 +340,12 @@ public class RedissonMap extends RedissonExpirable implements RMap { Collections.singletonList(getName(key)), key, value); } + protected void checkValue(Object value) { + if (value == null) { + throw new NullPointerException("map value can't be null"); + } + } + @Override public boolean replace(K key, V oldValue, V newValue) { return get(replaceAsync(key, oldValue, newValue)); @@ -359,9 +353,7 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture replaceAsync(K key, V oldValue, V newValue) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } + checkKey(key); if (oldValue == null) { throw new NullPointerException("map oldValue can't be null"); } @@ -387,12 +379,8 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture replaceAsync(K key, V value) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } - if (value == null) { - throw new NullPointerException("map value can't be null"); - } + checkKey(key); + checkValue(value); return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REPLACE, "if redis.call('hexists', KEYS[1], ARGV[1]) == 1 then " @@ -407,21 +395,15 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture getAsync(K key) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } + checkKey(key); return commandExecutor.readAsync(getName(key), codec, RedisCommands.HGET, getName(key), key); } @Override public RFuture putAsync(K key, V value) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } - if (value == null) { - throw new NullPointerException("map value can't be null"); - } + checkKey(key); + checkValue(value); return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_PUT, "local v = redis.call('hget', KEYS[1], ARGV[1]); " @@ -433,9 +415,7 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture removeAsync(K key) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } + checkKey(key); return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REMOVE, "local v = redis.call('hget', KEYS[1], ARGV[1]); " @@ -446,12 +426,8 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture fastPutAsync(K key, V value) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } - if (value == null) { - throw new NullPointerException("map value can't be null"); - } + checkKey(key); + checkValue(value); return commandExecutor.writeAsync(getName(key), codec, RedisCommands.HSET, getName(key), key, value); } @@ -491,12 +467,8 @@ public class RedissonMap extends RedissonExpirable implements RMap { @Override public RFuture addAndGetAsync(K key, Number value) { - if (key == null) { - throw new NullPointerException("map key can't be null"); - } - if (value == null) { - throw new NullPointerException("map value can't be null"); - } + checkKey(key); + checkValue(value); byte[] keyState = encodeMapKey(key); return commandExecutor.writeAsync(getName(key), StringCodec.INSTANCE, diff --git a/redisson/src/main/java/org/redisson/RedissonMapCache.java b/redisson/src/main/java/org/redisson/RedissonMapCache.java index 049dff84c..b7df19c49 100644 --- a/redisson/src/main/java/org/redisson/RedissonMapCache.java +++ b/redisson/src/main/java/org/redisson/RedissonMapCache.java @@ -113,6 +113,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture containsKeyAsync(Object key) { + checkKey(key); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_CONTAINS_KEY, "local value = redis.call('hget', KEYS[1], ARGV[2]); " + "local expireDate = 92233720368547758; " + @@ -144,6 +146,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture containsValueAsync(Object value) { + checkValue(value); + return commandExecutor.evalWriteAsync(getName(), codec, EVAL_CONTAINS_VALUE, "local s = redis.call('hgetall', KEYS[1]); " + "for i, v in ipairs(s) do " @@ -244,6 +248,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture putIfAbsentAsync(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { + checkKey(key); + checkValue(value); + if (ttl < 0) { throw new IllegalArgumentException("ttl can't be negative"); } @@ -329,6 +336,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture removeAsync(Object key, Object value) { + checkKey(key); + checkValue(value); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REMOVE_VALUE, "local value = redis.call('hget', KEYS[1], ARGV[1]); " + "if value == false then " @@ -347,6 +357,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture getAsync(K key) { + checkKey(key); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_GET_TTL, "local value = redis.call('hget', KEYS[1], ARGV[2]); " + "if value == false then " @@ -383,6 +395,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture putAsync(K key, V value) { + checkKey(key); + checkValue(value); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_PUT, "local v = redis.call('hget', KEYS[1], ARGV[1]); " + "local value = struct.pack('dLc0', 0, string.len(ARGV[2]), ARGV[2]); " @@ -397,6 +412,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture putIfAbsentAsync(K key, V value) { + checkKey(key); + checkValue(value); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_PUT, "local value = struct.pack('dLc0', 0, string.len(ARGV[2]), ARGV[2]); " + "if redis.call('hsetnx', KEYS[1], ARGV[1], value) == 1 then " @@ -419,6 +437,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture addAndGetAsync(K key, Number value) { + checkKey(key); + checkValue(value); + byte[] keyState = encodeMapKey(key); byte[] valueState; try { @@ -479,6 +500,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture fastPutAsync(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { + checkKey(key); + checkValue(value); + if (ttl < 0) { throw new IllegalArgumentException("ttl can't be negative"); } @@ -537,6 +561,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture putAsync(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { + checkKey(key); + checkValue(value); + if (ttl < 0) { throw new IllegalArgumentException("ttl can't be negative"); } @@ -616,6 +643,8 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture removeAsync(K key) { + checkKey(key); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REMOVE, "local v = redis.call('hget', KEYS[1], ARGV[1]); " + "redis.call('zrem', KEYS[2], ARGV[1]); " @@ -730,6 +759,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture fastPutAsync(K key, V value) { + checkKey(key); + checkValue(value); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_HSET, "local val = struct.pack('dLc0', 0, string.len(ARGV[2]), ARGV[2]); " + "return redis.call('hset', KEYS[1], ARGV[1], val); ", @@ -738,6 +770,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture fastPutIfAbsentAsync(K key, V value) { + checkKey(key); + checkValue(value); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_PUT_IF_ABSENT, "local value = redis.call('hget', KEYS[1], ARGV[2]); " + "if value == false then " @@ -786,6 +821,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture fastPutIfAbsentAsync(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) { + checkKey(key); + checkValue(value); + if (ttl < 0) { throw new IllegalArgumentException("ttl can't be negative"); } @@ -869,6 +907,14 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture replaceAsync(K key, V oldValue, V newValue) { + checkKey(key); + if (oldValue == null) { + throw new NullPointerException("map old value can't be null"); + } + if (newValue == null) { + throw new NullPointerException("map new value can't be null"); + } + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REPLACE_VALUE, "local v = redis.call('hget', KEYS[1], ARGV[2]); " + "if v == false then " @@ -903,6 +949,9 @@ public class RedissonMapCache extends RedissonMap implements RMapCac @Override public RFuture replaceAsync(K key, V value) { + checkKey(key); + checkValue(value); + return commandExecutor.evalWriteAsync(getName(key), codec, EVAL_REPLACE, "local v = redis.call('hget', KEYS[1], ARGV[2]); " + "if v ~= false then " @@ -927,6 +976,13 @@ public class RedissonMapCache extends RedissonMap implements RMapCac List params = new ArrayList(map.size()*2); for (java.util.Map.Entry t : map.entrySet()) { + if (t.getKey() == null) { + throw new NullPointerException("map key can't be null"); + } + if (t.getValue() == null) { + throw new NullPointerException("map value can't be null"); + } + params.add(t.getKey()); params.add(t.getValue()); }