diff --git a/src/main/java/org/redisson/Redisson.java b/src/main/java/org/redisson/Redisson.java index a6c88ea08..fd9e63201 100755 --- a/src/main/java/org/redisson/Redisson.java +++ b/src/main/java/org/redisson/Redisson.java @@ -47,6 +47,7 @@ import org.redisson.core.RBlockingDeque; import org.redisson.core.RBlockingQueue; import org.redisson.core.RBloomFilter; import org.redisson.core.RBucket; +import org.redisson.core.RBuckets; import org.redisson.core.RCountDownLatch; import org.redisson.core.RDeque; import org.redisson.core.RGeo; @@ -202,6 +203,16 @@ public class Redisson implements RedissonClient { return new RedissonBucket(codec, commandExecutor, name); } + @Override + public RBuckets getBuckets() { + return new RedissonBuckets(this, commandExecutor); + } + + @Override + public RBuckets getBuckets(Codec codec) { + return new RedissonBuckets(this, codec, commandExecutor); + } + @Override public List> findBuckets(String pattern) { Collection keys = commandExecutor.get(commandExecutor., String>readAllAsync(RedisCommands.KEYS, pattern)); @@ -260,11 +271,6 @@ public class Redisson implements RedissonClient { commandExecutor.write(params.get(0).toString(), RedisCommands.MSET, params.toArray()); } - @Override - public List> getBuckets(String pattern) { - return findBuckets(pattern); - } - @Override public RHyperLogLog getHyperLogLog(String name) { return new RedissonHyperLogLog(commandExecutor, name); @@ -537,16 +543,6 @@ public class Redisson implements RedissonClient { return new RedisNodes(connectionManager); } - @Override - public void flushdb() { - commandExecutor.get(commandExecutor.writeAllAsync(RedisCommands.FLUSHDB)); - } - - @Override - public void flushall() { - commandExecutor.get(commandExecutor.writeAllAsync(RedisCommands.FLUSHALL)); - } - @Override public boolean isShutdown() { return connectionManager.isShutdown(); diff --git a/src/main/java/org/redisson/RedissonBuckets.java b/src/main/java/org/redisson/RedissonBuckets.java new file mode 100644 index 000000000..43a118771 --- /dev/null +++ b/src/main/java/org/redisson/RedissonBuckets.java @@ -0,0 +1,118 @@ +/** + * Copyright 2014 Nikita Koksharov, Nickolay Borbit + * + * 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; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.redisson.client.codec.Codec; +import org.redisson.client.codec.DelegateDecoderCodec; +import org.redisson.client.protocol.RedisCommand; +import org.redisson.client.protocol.RedisCommand.ValueType; +import org.redisson.client.protocol.RedisCommands; +import org.redisson.command.CommandExecutor; +import org.redisson.connection.decoder.MapGetAllDecoder; +import org.redisson.core.RBucket; +import org.redisson.core.RBuckets; + +import io.netty.util.concurrent.Future; + +public class RedissonBuckets implements RBuckets { + + private final Codec codec; + private final CommandExecutor commandExecutor; + private final Redisson redisson; + + public RedissonBuckets(Redisson redisson, CommandExecutor commandExecutor) { + this(redisson, commandExecutor.getConnectionManager().getCodec(), commandExecutor); + } + + public RedissonBuckets(Redisson redisson, Codec codec, CommandExecutor commandExecutor) { + super(); + this.codec = codec; + this.commandExecutor = commandExecutor; + this.redisson = redisson; + } + + @Override + public List> find(String pattern) { + Collection keys = commandExecutor.get(commandExecutor., String>readAllAsync(RedisCommands.KEYS, pattern)); + List> buckets = new ArrayList>(keys.size()); + for (String key : keys) { + if(key == null) { + continue; + } + buckets.add(redisson.getBucket(key, codec)); + } + return buckets; + } + + @Override + public Map get(String... keys) { + if (keys.length == 0) { + return Collections.emptyMap(); + } + + RedisCommand> command = new RedisCommand>("MGET", new MapGetAllDecoder(Arrays.asList(keys), 0), ValueType.OBJECTS); + Future> future = commandExecutor.readAsync(keys[0], new DelegateDecoderCodec(codec), command, keys); + return commandExecutor.get(future); + } + + @Override + public boolean trySet(Map buckets) { + if (buckets.isEmpty()) { + return false; + } + + List params = new ArrayList(buckets.size()); + for (Entry entry : buckets.entrySet()) { + params.add(entry.getKey()); + try { + params.add(codec.getValueEncoder().encode(entry.getValue())); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + return commandExecutor.write(params.get(0).toString(), RedisCommands.MSETNX, params.toArray()); + } + + @Override + public void set(Map buckets) { + if (buckets.isEmpty()) { + return; + } + + List params = new ArrayList(buckets.size()); + for (Entry entry : buckets.entrySet()) { + params.add(entry.getKey()); + try { + params.add(codec.getValueEncoder().encode(entry.getValue())); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } + } + + commandExecutor.write(params.get(0).toString(), RedisCommands.MSET, params.toArray()); + } + +} diff --git a/src/main/java/org/redisson/RedissonClient.java b/src/main/java/org/redisson/RedissonClient.java index a83750c7d..fffc0e54b 100755 --- a/src/main/java/org/redisson/RedissonClient.java +++ b/src/main/java/org/redisson/RedissonClient.java @@ -31,6 +31,7 @@ import org.redisson.core.RBlockingDeque; import org.redisson.core.RBlockingQueue; import org.redisson.core.RBloomFilter; import org.redisson.core.RBucket; +import org.redisson.core.RBuckets; import org.redisson.core.RCountDownLatch; import org.redisson.core.RDeque; import org.redisson.core.RGeo; @@ -151,57 +152,43 @@ public interface RedissonClient { RBucket getBucket(String name, Codec codec); /** - *

Returns a list of object holder instances by a key pattern. + * Returns interface for mass operations with Bucket objects. * - *

Supported glob-style patterns:
-     *    h?llo subscribes to hello, hallo and hxllo
-     *    h*llo subscribes to hllo and heeeello
-     *    h[ae]llo subscribes to hello and hallo, but not hillo
-     *    h[^e]llo matches hallo, hbllo, ... but not hello
-     *    h[a-b]llo matches hallo and hbllo
- *

Use \ to escape special characters if you want to match them verbatim. - * - *

Uses KEYS Redis command. - * - * @param pattern * @return */ - List> findBuckets(String pattern); + RBuckets getBuckets(); /** - *

Returns Redis object mapped by key. Result Map is not contains - * key-value entry for null values. - * - *

Uses MGET Redis command. + * Returns interface for mass operations with Bucket objects + * using provided codec for object. * - * @param keys * @return */ - Map loadBucketValues(Collection keys); + RBuckets getBuckets(Codec codec); /** - *

Returns Redis object mapped by key. Result Map is not contains - * key-value entry for null values. - * - *

Uses MGET Redis command. - * - * @param keys - * @return + * Use {@link RBuckets#find(String)} */ - Map loadBucketValues(String ... keys); + @Deprecated + List> findBuckets(String pattern); /** - * Saves Redis object mapped by key. - * - * @param buckets + * Use {@link RBuckets#get(String...)} */ - void saveBuckets(Map buckets); + @Deprecated + Map loadBucketValues(Collection keys); + + /** + * Use {@link RBuckets#get(String...)} + */ + @Deprecated + Map loadBucketValues(String ... keys); /** - * Use {@link #findBuckets(String)} + * Use {@link RBuckets#set(Map)} */ @Deprecated - List> getBuckets(String pattern); + void saveBuckets(Map buckets); /** * Returns HyperLogLog instance by name. @@ -658,18 +645,6 @@ public interface RedissonClient { */ NodesGroup getClusterNodesGroup(); - /** - * Use {@link RKeys#flushdb()} - */ - @Deprecated - void flushdb(); - - /** - * Use {@link RKeys#flushall()} - */ - @Deprecated - void flushall(); - /** * Returns {@code true} if this Redisson instance has been shut down. * diff --git a/src/main/java/org/redisson/RedissonGeo.java b/src/main/java/org/redisson/RedissonGeo.java index dd36d3764..7a62e165c 100644 --- a/src/main/java/org/redisson/RedissonGeo.java +++ b/src/main/java/org/redisson/RedissonGeo.java @@ -104,7 +104,7 @@ public class RedissonGeo extends RedissonExpirable implements RGeo { List params = new ArrayList(members.length + 1); params.add(getName()); params.addAll(Arrays.asList(members)); - RedisCommand> command = new RedisCommand>("GEOHASH", new MapGetAllDecoder(params), 2, ValueType.OBJECTS); + RedisCommand> command = new RedisCommand>("GEOHASH", new MapGetAllDecoder(params, 1), 2, ValueType.OBJECTS); return commandExecutor.readAsync(getName(), new ScoredCodec(codec), command, params.toArray()); } diff --git a/src/main/java/org/redisson/RedissonLexSortedSet.java b/src/main/java/org/redisson/RedissonLexSortedSet.java index 2616d064e..35eb8c17a 100644 --- a/src/main/java/org/redisson/RedissonLexSortedSet.java +++ b/src/main/java/org/redisson/RedissonLexSortedSet.java @@ -32,33 +32,64 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem super(StringCodec.INSTANCE, commandExecutor, name); } + @Override + public int removeRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { + return removeRangeByLex(fromElement, fromInclusive, toElement, toInclusive); + } + @Override public int removeRangeByLex(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { - return get(removeRangeByLexAsync(fromElement, fromInclusive, toElement, toInclusive)); + return get(removeRangeAsync(fromElement, fromInclusive, toElement, toInclusive)); } + @Override + public int removeRangeHead(String toElement, boolean toInclusive) { + return removeRangeHeadByLex(toElement, toInclusive); + } + @Override public int removeRangeHeadByLex(String toElement, boolean toInclusive) { - return get(removeRangeHeadByLexAsync(toElement, toInclusive)); + return get(removeRangeHeadAsync(toElement, toInclusive)); } + @Override + public Future removeRangeHeadAsync(String toElement, boolean toInclusive) { + return removeRangeHeadByLexAsync(toElement, toInclusive); + } + @Override public Future removeRangeHeadByLexAsync(String toElement, boolean toInclusive) { String toValue = value(toElement, toInclusive); return commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZREMRANGEBYLEX, getName(), "-", toValue); } + + @Override + public int removeRangeTail(String fromElement, boolean fromInclusive) { + return removeRangeTailByLex(fromElement, fromInclusive); + } @Override public int removeRangeTailByLex(String fromElement, boolean fromInclusive) { - return get(removeRangeTailByLexAsync(fromElement, fromInclusive)); + return get(removeRangeTailAsync(fromElement, fromInclusive)); } + @Override + public Future removeRangeTailAsync(String fromElement, boolean fromInclusive) { + return removeRangeTailByLexAsync(fromElement, fromInclusive); + } + @Override public Future removeRangeTailByLexAsync(String fromElement, boolean fromInclusive) { String fromValue = value(fromElement, fromInclusive); return commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZREMRANGEBYLEX, getName(), fromValue, "+"); } + @Override + public Future removeRangeAsync(String fromElement, boolean fromInclusive, String toElement, + boolean toInclusive) { + return removeRangeByLexAsync(fromElement, fromInclusive, toElement, toInclusive); + } + @Override public Future removeRangeByLexAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { String fromValue = value(fromElement, fromInclusive); @@ -67,33 +98,63 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem return commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZREMRANGEBYLEX, getName(), fromValue, toValue); } + @Override + public Collection range(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { + return lexRange(fromElement, fromInclusive, toElement, toInclusive); + } + @Override public Collection lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { - return get(lexRangeAsync(fromElement, fromInclusive, toElement, toInclusive)); + return get(rangeAsync(fromElement, fromInclusive, toElement, toInclusive)); } + @Override + public Collection rangeHead(String toElement, boolean toInclusive) { + return lexRangeHead(toElement, toInclusive); + } + @Override public Collection lexRangeHead(String toElement, boolean toInclusive) { - return get(lexRangeHeadAsync(toElement, toInclusive)); + return get(rangeHeadAsync(toElement, toInclusive)); } + @Override + public Future> rangeHeadAsync(String toElement, boolean toInclusive) { + return lexRangeHeadAsync(toElement, toInclusive); + } + @Override public Future> lexRangeHeadAsync(String toElement, boolean toInclusive) { String toValue = value(toElement, toInclusive); return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), "-", toValue); } + + @Override + public Collection rangeTail(String fromElement, boolean fromInclusive) { + return lexRangeTail(fromElement, fromInclusive); + } @Override public Collection lexRangeTail(String fromElement, boolean fromInclusive) { - return get(lexRangeTailAsync(fromElement, fromInclusive)); + return get(rangeTailAsync(fromElement, fromInclusive)); } + @Override + public Future> rangeTailAsync(String fromElement, boolean fromInclusive) { + return lexRangeTailAsync(fromElement, fromInclusive); + } + @Override public Future> lexRangeTailAsync(String fromElement, boolean fromInclusive) { String fromValue = value(fromElement, fromInclusive); return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, "+"); } + @Override + public Future> rangeAsync(String fromElement, boolean fromInclusive, String toElement, + boolean toInclusive) { + return lexRangeAsync(fromElement, fromInclusive, toElement, toInclusive); + } @Override public Future> lexRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { @@ -103,33 +164,65 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, toValue); } + @Override + public Collection range(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, + int offset, int count) { + return lexRange(fromElement, fromInclusive, toElement, toInclusive, offset, count); + } + @Override public Collection lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count) { - return get(lexRangeAsync(fromElement, fromInclusive, toElement, toInclusive, offset, count)); + return get(rangeAsync(fromElement, fromInclusive, toElement, toInclusive, offset, count)); } + @Override + public Collection rangeHead(String toElement, boolean toInclusive, int offset, int count) { + return lexRangeHead(toElement, toInclusive, offset, count); + } + @Override public Collection lexRangeHead(String toElement, boolean toInclusive, int offset, int count) { - return get(lexRangeHeadAsync(toElement, toInclusive, offset, count)); + return get(rangeHeadAsync(toElement, toInclusive, offset, count)); } + @Override + public Future> rangeHeadAsync(String toElement, boolean toInclusive, int offset, int count) { + return lexRangeHeadAsync(toElement, toInclusive, offset, count); + } + @Override public Future> lexRangeHeadAsync(String toElement, boolean toInclusive, int offset, int count) { String toValue = value(toElement, toInclusive); return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), "-", toValue, "LIMIT", offset, count); } + @Override + public Collection rangeTail(String fromElement, boolean fromInclusive, int offset, int count) { + return lexRangeTail(fromElement, fromInclusive, offset, count); + } + @Override public Collection lexRangeTail(String fromElement, boolean fromInclusive, int offset, int count) { - return get(lexRangeTailAsync(fromElement, fromInclusive, offset, count)); + return get(rangeTailAsync(fromElement, fromInclusive, offset, count)); } + @Override + public Future> rangeTailAsync(String fromElement, boolean fromInclusive, int offset, int count) { + return lexRangeTailAsync(fromElement, fromInclusive, offset, count); + } + @Override public Future> lexRangeTailAsync(String fromElement, boolean fromInclusive, int offset, int count) { String fromValue = value(fromElement, fromInclusive); return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, "+", "LIMIT", offset, count); } + @Override + public Future> rangeAsync(String fromElement, boolean fromInclusive, String toElement, + boolean toInclusive, int offset, int count) { + return lexRangeAsync(fromElement, fromInclusive, toElement, toInclusive, offset, count); + } + @Override public Future> lexRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count) { String fromValue = value(fromElement, fromInclusive); @@ -137,10 +230,20 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, toValue, "LIMIT", offset, count); } + + @Override + public int countTail(String fromElement, boolean fromInclusive) { + return lexCountTail(fromElement, fromInclusive); + } @Override public int lexCountTail(String fromElement, boolean fromInclusive) { - return get(lexCountTailAsync(fromElement, fromInclusive)); + return get(countTailAsync(fromElement, fromInclusive)); + } + + @Override + public Future countTailAsync(String fromElement, boolean fromInclusive) { + return lexCountTailAsync(fromElement, fromInclusive); } @Override @@ -149,10 +252,20 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZLEXCOUNT, getName(), fromValue, "+"); } + + @Override + public int countHead(String toElement, boolean toInclusive) { + return lexCountHead(toElement, toInclusive); + } @Override public int lexCountHead(String toElement, boolean toInclusive) { - return get(lexCountHeadAsync(toElement, toInclusive)); + return get(countHeadAsync(toElement, toInclusive)); + } + + @Override + public Future countHeadAsync(String toElement, boolean toInclusive) { + return lexCountHeadAsync(toElement, toInclusive); } @Override @@ -162,11 +275,22 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZLEXCOUNT, getName(), "-", toValue); } + @Override + public int count(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { + return lexCount(fromElement, fromInclusive, toElement, toInclusive); + } + @Override public int lexCount(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { - return get(lexCountAsync(fromElement, fromInclusive, toElement, toInclusive)); + return get(countAsync(fromElement, fromInclusive, toElement, toInclusive)); } + @Override + public Future countAsync(String fromElement, boolean fromInclusive, String toElement, + boolean toInclusive) { + return lexCountAsync(fromElement, fromInclusive, toElement, toInclusive); + } + @Override public Future lexCountAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) { String fromValue = value(fromElement, fromInclusive); @@ -192,6 +316,9 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem @Override public Future addAllAsync(Collection c) { + if (c.isEmpty()) { + return newSucceededFuture(false); + } List params = new ArrayList(2*c.size()); for (Object param : c) { params.add(0); @@ -210,4 +337,14 @@ public class RedissonLexSortedSet extends RedissonScoredSortedSet implem return get(addAllAsync(c)); } + @Override + public Collection range(int startIndex, int endIndex) { + return valueRange(startIndex, endIndex); + } + + @Override + public Future> rangeAsync(int startIndex, int endIndex) { + return valueRangeAsync(startIndex, endIndex); + } + } diff --git a/src/main/java/org/redisson/RedissonMap.java b/src/main/java/org/redisson/RedissonMap.java index 245e6a7cd..cb8c8b54a 100644 --- a/src/main/java/org/redisson/RedissonMap.java +++ b/src/main/java/org/redisson/RedissonMap.java @@ -130,7 +130,7 @@ public class RedissonMap extends RedissonExpirable implements RMap { List args = new ArrayList(keys.size() + 1); args.add(getName()); args.addAll(keys); - return commandExecutor.readAsync(getName(), codec, new RedisCommand>("HMGET", new MapGetAllDecoder(args), 2, ValueType.MAP_KEY, ValueType.MAP_VALUE), args.toArray()); + return commandExecutor.readAsync(getName(), codec, new RedisCommand>("HMGET", new MapGetAllDecoder(args, 1), 2, ValueType.MAP_KEY, ValueType.MAP_VALUE), args.toArray()); } @Override diff --git a/src/main/java/org/redisson/RedissonMapCache.java b/src/main/java/org/redisson/RedissonMapCache.java index d529c2b03..0167015c8 100644 --- a/src/main/java/org/redisson/RedissonMapCache.java +++ b/src/main/java/org/redisson/RedissonMapCache.java @@ -162,11 +162,11 @@ public class RedissonMapCache extends RedissonMap implements RMapCac return newSucceededFuture(Collections.emptyMap()); } - List args = new ArrayList(keys.size() + 2); + List args = new ArrayList(keys.size() + 1); args.add(System.currentTimeMillis()); args.addAll(keys); - return commandExecutor.evalWriteAsync(getName(), codec, new RedisCommand>("EVAL", new MapGetAllDecoder(args), 7, ValueType.MAP_KEY, ValueType.MAP_VALUE), + return commandExecutor.evalWriteAsync(getName(), codec, new RedisCommand>("EVAL", new MapGetAllDecoder(args, 1), 7, ValueType.MAP_KEY, ValueType.MAP_VALUE), "local expireHead = redis.call('zrange', KEYS[2], 0, 0, 'withscores');" + "local expireIdleHead = redis.call('zrange', KEYS[3], 0, 0, 'withscores');" + "local maxDate = table.remove(ARGV, 1); " // index is the first parameter diff --git a/src/main/java/org/redisson/RedissonScoredSortedSet.java b/src/main/java/org/redisson/RedissonScoredSortedSet.java index 62c493436..0ffb1122d 100644 --- a/src/main/java/org/redisson/RedissonScoredSortedSet.java +++ b/src/main/java/org/redisson/RedissonScoredSortedSet.java @@ -121,6 +121,9 @@ public class RedissonScoredSortedSet extends RedissonExpirable implements RSc @Override public Future addAllAsync(Map objects) { + if (objects.isEmpty()) { + return newSucceededFuture(0L); + } List params = new ArrayList(objects.size()*2+1); params.add(getName()); try { diff --git a/src/main/java/org/redisson/client/codec/DelegateDecoderCodec.java b/src/main/java/org/redisson/client/codec/DelegateDecoderCodec.java new file mode 100644 index 000000000..407cc5b93 --- /dev/null +++ b/src/main/java/org/redisson/client/codec/DelegateDecoderCodec.java @@ -0,0 +1,34 @@ +/** + * Copyright 2014 Nikita Koksharov, Nickolay Borbit + * + * 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.client.codec; + +import org.redisson.client.protocol.Decoder; + +public class DelegateDecoderCodec extends StringCodec { + + private final Codec delegate; + + public DelegateDecoderCodec(Codec delegate) { + super(); + this.delegate = delegate; + } + + @Override + public Decoder getValueDecoder() { + return delegate.getValueDecoder(); + } + +} diff --git a/src/main/java/org/redisson/client/protocol/RedisCommands.java b/src/main/java/org/redisson/client/protocol/RedisCommands.java index d56eae403..e3737d259 100644 --- a/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -196,6 +196,7 @@ public interface RedisCommands { RedisStrictCommand> KEYS = new RedisStrictCommand>("KEYS", new StringListReplayDecoder()); RedisCommand> MGET = new RedisCommand>("MGET", new ObjectListReplayDecoder()); RedisStrictCommand MSET = new RedisStrictCommand("MSET", new VoidReplayConvertor()); + RedisStrictCommand MSETNX = new RedisStrictCommand("MSETNX", new BooleanReplayConvertor()); RedisCommand HSETNX = new RedisCommand("HSETNX", new BooleanReplayConvertor(), 2, ValueType.MAP); RedisCommand HSET = new RedisCommand("HSET", new BooleanReplayConvertor(), 2, ValueType.MAP); diff --git a/src/main/java/org/redisson/client/protocol/decoder/GeoDistanceMapDecoder.java b/src/main/java/org/redisson/client/protocol/decoder/GeoDistanceMapDecoder.java index ac4f98393..9e9d526e5 100644 --- a/src/main/java/org/redisson/client/protocol/decoder/GeoDistanceMapDecoder.java +++ b/src/main/java/org/redisson/client/protocol/decoder/GeoDistanceMapDecoder.java @@ -25,7 +25,6 @@ import org.redisson.client.codec.DoubleCodec; import org.redisson.client.handler.State; import io.netty.buffer.ByteBuf; -import io.netty.util.CharsetUtil; public class GeoDistanceMapDecoder implements MultiDecoder> { @@ -40,7 +39,6 @@ public class GeoDistanceMapDecoder implements MultiDecoder> @Override public Object decode(ByteBuf buf, State state) throws IOException { - System.out.println("1 " + buf.toString(CharsetUtil.UTF_8)); if (pos.get() % 2 == 0) { return codec.getValueDecoder().decode(buf, state); } @@ -55,7 +53,6 @@ public class GeoDistanceMapDecoder implements MultiDecoder> @Override public Map decode(List parts, State state) { - System.out.println(parts); Map result = new HashMap(parts.size()/2); for (int i = 0; i < parts.size(); i++) { if (i % 2 != 0) { diff --git a/src/main/java/org/redisson/connection/decoder/MapGetAllDecoder.java b/src/main/java/org/redisson/connection/decoder/MapGetAllDecoder.java index 35aa171df..7ce90ee4f 100644 --- a/src/main/java/org/redisson/connection/decoder/MapGetAllDecoder.java +++ b/src/main/java/org/redisson/connection/decoder/MapGetAllDecoder.java @@ -28,10 +28,12 @@ import io.netty.buffer.ByteBuf; public class MapGetAllDecoder implements MultiDecoder> { + private final int shiftIndex; private final List args; - public MapGetAllDecoder(List args) { + public MapGetAllDecoder(List args, int shiftIndex) { this.args = args; + this.shiftIndex = shiftIndex; } @Override @@ -50,12 +52,12 @@ public class MapGetAllDecoder implements MultiDecoder> { return Collections.emptyMap(); } Map result = new HashMap(parts.size()); - for (int index = 0; index < args.size()-1; index++) { + for (int index = 0; index < args.size()-shiftIndex; index++) { Object value = parts.get(index); if (value == null) { continue; } - result.put(args.get(index+1), value); + result.put(args.get(index+shiftIndex), value); } return result; } diff --git a/src/main/java/org/redisson/core/RBuckets.java b/src/main/java/org/redisson/core/RBuckets.java new file mode 100644 index 000000000..036d7f88f --- /dev/null +++ b/src/main/java/org/redisson/core/RBuckets.java @@ -0,0 +1,64 @@ +/** + * Copyright 2014 Nikita Koksharov, Nickolay Borbit + * + * 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.core; + +import java.util.List; +import java.util.Map; + +public interface RBuckets { + + /** + *

Returns a list of object holder instances by a key pattern. + * + *

Supported glob-style patterns:
+     *    h?llo subscribes to hello, hallo and hxllo
+     *    h*llo subscribes to hllo and heeeello
+     *    h[ae]llo subscribes to hello and hallo, but not hillo
+     *    h[^e]llo matches hallo, hbllo, ... but not hello
+     *    h[a-b]llo matches hallo and hbllo
+ *

Use \ to escape special characters if you want to match them verbatim. + * + * @param pattern + * @return + */ + List> find(String pattern); + + /** + * Returns Redis object mapped by key. Result Map is not contains + * key-value entry for null values. + * + * @param keys + * @return + */ + Map get(String ... keys); + + /** + * Try to save objects mapped by Redis key. + * If at least one of them is already exist then + * don't set none of them. + * + * @param buckets + */ + boolean trySet(Map buckets); + + /** + * Saves objects mapped by Redis key. + * + * @param buckets + */ + void set(Map buckets); + +} diff --git a/src/main/java/org/redisson/core/RLexSortedSet.java b/src/main/java/org/redisson/core/RLexSortedSet.java index 1f68f6180..598df157f 100644 --- a/src/main/java/org/redisson/core/RLexSortedSet.java +++ b/src/main/java/org/redisson/core/RLexSortedSet.java @@ -20,32 +20,110 @@ import java.util.Set; public interface RLexSortedSet extends RLexSortedSetAsync, Set, RExpirable { + int removeRangeTail(String fromElement, boolean fromInclusive); + + /** + * Use {@link RLexSortedSet#removeRangeTail(String, boolean)} + */ + @Deprecated int removeRangeTailByLex(String fromElement, boolean fromInclusive); + int removeRangeHead(String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSet#removeRangeHead(String, boolean)} + */ + @Deprecated int removeRangeHeadByLex(String toElement, boolean toInclusive); + int removeRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSet#removeRange(String, boolean)} + */ + @Deprecated int removeRangeByLex(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + int countTail(String fromElement, boolean fromInclusive); + + /** + * Use {@link RLexSortedSet#countTail(String, boolean)} + */ + @Deprecated int lexCountTail(String fromElement, boolean fromInclusive); + int countHead(String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSet#countHead(String, boolean)} + */ + @Deprecated int lexCountHead(String toElement, boolean toInclusive); + Collection rangeTail(String fromElement, boolean fromInclusive); + + /** + * Use {@link RLexSortedSet#rangeTail(String, boolean)} + */ + @Deprecated Collection lexRangeTail(String fromElement, boolean fromInclusive); + Collection rangeHead(String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSet#rangeHead(String, boolean)} + */ + @Deprecated Collection lexRangeHead(String toElement, boolean toInclusive); + Collection range(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSet#range(String, boolean, String, boolean)} + */ + @Deprecated Collection lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + Collection rangeTail(String fromElement, boolean fromInclusive, int offset, int count); + + /** + * Use {@link RLexSortedSet#rangeTail(String, boolean, int, int)} + */ + @Deprecated Collection lexRangeTail(String fromElement, boolean fromInclusive, int offset, int count); + Collection rangeHead(String toElement, boolean toInclusive, int offset, int count); + + /** + * Use {@link RLexSortedSet#rangeHead(String, boolean, int, int)} + */ + @Deprecated Collection lexRangeHead(String toElement, boolean toInclusive, int offset, int count); + Collection range(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count); + + /** + * Use {@link RLexSortedSet#range(String, boolean, String, boolean, int, int)} + */ + @Deprecated Collection lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count); + int count(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSet#count(String, boolean, String, boolean)} + */ + @Deprecated int lexCount(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); int rank(String o); + Collection range(int startIndex, int endIndex); + + /** + * Use {@link RLexSortedSet#range(int, int)} + */ + @Deprecated Collection valueRange(int startIndex, int endIndex); } diff --git a/src/main/java/org/redisson/core/RLexSortedSetAsync.java b/src/main/java/org/redisson/core/RLexSortedSetAsync.java index abdb18cb4..6616e25e5 100644 --- a/src/main/java/org/redisson/core/RLexSortedSetAsync.java +++ b/src/main/java/org/redisson/core/RLexSortedSetAsync.java @@ -21,32 +21,110 @@ import io.netty.util.concurrent.Future; public interface RLexSortedSetAsync extends RCollectionAsync { + Future removeRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSetAsync#removeRangeAsync(String, boolean, String, boolean)} + */ + @Deprecated Future removeRangeByLexAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + Future removeRangeTailAsync(String fromElement, boolean fromInclusive); + + /** + * Use {@link RLexSortedSetAsync#removeRangeTailAsync(String, boolean, String, boolean)} + */ + @Deprecated Future removeRangeTailByLexAsync(String fromElement, boolean fromInclusive); + Future removeRangeHeadAsync(String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSetAsync#removeRangeHeadAsync(String, boolean)} + */ + @Deprecated Future removeRangeHeadByLexAsync(String toElement, boolean toInclusive); + Future countTailAsync(String fromElement, boolean fromInclusive); + + /** + * Use {@link RLexSortedSetAsync#countTailAsync(String, boolean)} + */ + @Deprecated Future lexCountTailAsync(String fromElement, boolean fromInclusive); + Future countHeadAsync(String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSetAsync#countHeadAsync(String, boolean)} + */ + @Deprecated Future lexCountHeadAsync(String toElement, boolean toInclusive); + Future> rangeTailAsync(String fromElement, boolean fromInclusive); + + /** + * Use {@link RLexSortedSetAsync#rangeTailAsync(String, boolean)} + */ + @Deprecated Future> lexRangeTailAsync(String fromElement, boolean fromInclusive); + Future> rangeHeadAsync(String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSetAsync#rangeHeadAsync(String, boolean)} + */ + @Deprecated Future> lexRangeHeadAsync(String toElement, boolean toInclusive); + Future> rangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSetAsync#rangeAsync(String, boolean, String, boolean)} + */ + @Deprecated Future> lexRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + Future> rangeTailAsync(String fromElement, boolean fromInclusive, int offset, int count); + + /** + * Use {@link RLexSortedSetAsync#rangeTailAsync(String, boolean, int, int)} + */ + @Deprecated Future> lexRangeTailAsync(String fromElement, boolean fromInclusive, int offset, int count); + Future> rangeHeadAsync(String toElement, boolean toInclusive, int offset, int count); + + /** + * Use {@link RLexSortedSetAsync#rangeHeadAsync(String, boolean, int, int)} + */ + @Deprecated Future> lexRangeHeadAsync(String toElement, boolean toInclusive, int offset, int count); + Future> rangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count); + + /** + * Use {@link RLexSortedSetAsync#rangeAsync(String, boolean, String, boolean, int, int)} + */ + @Deprecated Future> lexRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count); + Future countAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); + + /** + * Use {@link RLexSortedSetAsync#countAsync(String, boolean, String, boolean)} + */ + @Deprecated Future lexCountAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive); Future rankAsync(String o); + Future> rangeAsync(int startIndex, int endIndex); + + /** + * Use {@link RLexSortedSetAsync#rangeAsync(int, int)} + */ + @Deprecated Future> valueRangeAsync(int startIndex, int endIndex); } diff --git a/src/test/java/org/redisson/RedissonBucketTest.java b/src/test/java/org/redisson/RedissonBucketTest.java index 7ed1bba45..0688f55b3 100755 --- a/src/test/java/org/redisson/RedissonBucketTest.java +++ b/src/test/java/org/redisson/RedissonBucketTest.java @@ -59,40 +59,11 @@ public class RedissonBucketTest extends BaseTest { assertThat(r1.trySet("4", 500, TimeUnit.MILLISECONDS)).isFalse(); assertThat(r1.get()).isEqualTo("3"); - Thread.sleep(500); + Thread.sleep(1000); assertThat(r1.get()).isNull(); } - @Test - public void testSaveBuckets() { - Map buckets = new HashMap(); - buckets.put("12", 1); - buckets.put("41", 2); - redisson.saveBuckets(buckets); - - RBucket r1 = redisson.getBucket("12"); - assertThat(r1.get()).isEqualTo(1); - - RBucket r2 = redisson.getBucket("41"); - assertThat(r2.get()).isEqualTo(2); - } - - @Test - public void testLoadBucketValues() { - RBucket bucket1 = redisson.getBucket("test1"); - bucket1.set("someValue1"); - RBucket bucket3 = redisson.getBucket("test3"); - bucket3.set("someValue3"); - - Map result = redisson.loadBucketValues("test1", "test2", "test3", "test4"); - Map expected = new HashMap(); - expected.put("test1", "someValue1"); - expected.put("test3", "someValue3"); - - Assert.assertEquals(expected, result); - } - @Test public void testExpire() throws InterruptedException { RBucket bucket = redisson.getBucket("test1"); @@ -175,20 +146,4 @@ public class RedissonBucketTest extends BaseTest { Assert.assertFalse(bucket.isExists()); } - @Test - public void testGetPattern() { - Collection names = Arrays.asList("test:testGetPattern:one", "test:testGetPattern:two"); - Collection vals = Arrays.asList("one-val", "two-val"); - redisson.getBucket("test:testGetPattern:one").set("one-val"); - redisson.getBucket("test:testGetPattern:two").set("two-val"); - List> buckets = redisson.getBuckets("test:testGetPattern:*"); - Assert.assertEquals(2, buckets.size()); - Assert.assertTrue(names.contains(buckets.get(0).getName())); - Assert.assertTrue(names.contains(buckets.get(1).getName())); - Assert.assertTrue(vals.contains(buckets.get(0).get())); - Assert.assertTrue(vals.contains(buckets.get(1).get())); - for (RBucket bucket : buckets) { - bucket.delete(); - } - } } diff --git a/src/test/java/org/redisson/RedissonBucketsTest.java b/src/test/java/org/redisson/RedissonBucketsTest.java new file mode 100644 index 000000000..02d8a29da --- /dev/null +++ b/src/test/java/org/redisson/RedissonBucketsTest.java @@ -0,0 +1,91 @@ +package org.redisson; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.Assert; +import org.junit.Test; +import org.redisson.core.RBucket; + +public class RedissonBucketsTest extends BaseTest { + + @Test + public void testGet() { + RBucket bucket1 = redisson.getBucket("test1"); + bucket1.set("someValue1"); + RBucket bucket3 = redisson.getBucket("test3"); + bucket3.set("someValue3"); + + Map result = redisson.getBuckets().get("test1", "test2", "test3", "test4"); + Map expected = new HashMap(); + expected.put("test1", "someValue1"); + expected.put("test3", "someValue3"); + + Assert.assertEquals(expected, result); + } + + @Test + public void testFind() { + Collection names = Arrays.asList("test:testGetPattern:one", "test:testGetPattern:two"); + Collection vals = Arrays.asList("one-val", "two-val"); + + redisson.getBucket("test:testGetPattern:one").set("one-val"); + redisson.getBucket("test:testGetPattern:two").set("two-val"); + + List> buckets = redisson.getBuckets().find("test:testGetPattern:*"); + Assert.assertEquals(2, buckets.size()); + Assert.assertTrue(names.contains(buckets.get(0).getName())); + Assert.assertTrue(names.contains(buckets.get(1).getName())); + Assert.assertTrue(vals.contains(buckets.get(0).get())); + Assert.assertTrue(vals.contains(buckets.get(1).get())); + for (RBucket bucket : buckets) { + bucket.delete(); + } + } + + + @Test + public void testSet() { + Map buckets = new HashMap(); + buckets.put("12", 1); + buckets.put("41", 2); + redisson.getBuckets().set(buckets); + + RBucket r1 = redisson.getBucket("12"); + assertThat(r1.get()).isEqualTo(1); + + RBucket r2 = redisson.getBucket("41"); + assertThat(r2.get()).isEqualTo(2); + } + + @Test + public void testTrySet() { + redisson.getBucket("12").set("341"); + + Map buckets = new HashMap(); + buckets.put("12", 1); + buckets.put("41", 2); + assertThat(redisson.getBuckets().trySet(buckets)).isFalse(); + + RBucket r2 = redisson.getBucket("41"); + assertThat(r2.get()).isNull(); + + Map buckets2 = new HashMap(); + buckets2.put("61", 1); + buckets2.put("41", 2); + assertThat(redisson.getBuckets().trySet(buckets2)).isTrue(); + + RBucket r1 = redisson.getBucket("61"); + assertThat(r1.get()).isEqualTo(1); + + RBucket r3 = redisson.getBucket("41"); + assertThat(r3.get()).isEqualTo(2); + } + + +} diff --git a/src/test/java/org/redisson/RedissonConcurrentMapTest.java b/src/test/java/org/redisson/RedissonConcurrentMapTest.java index ed3903744..adaa1f142 100644 --- a/src/test/java/org/redisson/RedissonConcurrentMapTest.java +++ b/src/test/java/org/redisson/RedissonConcurrentMapTest.java @@ -94,7 +94,7 @@ public class RedissonConcurrentMapTest extends BaseConcurrentTest { } assertMapSize(5, name); - redisson.flushdb(); + redisson.getKeys().flushdb(); redisson.shutdown(); } diff --git a/src/test/java/org/redisson/RedissonGeoTest.java b/src/test/java/org/redisson/RedissonGeoTest.java new file mode 100644 index 000000000..fc689e006 --- /dev/null +++ b/src/test/java/org/redisson/RedissonGeoTest.java @@ -0,0 +1,183 @@ +package org.redisson; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Test; +import org.redisson.core.GeoEntry; +import org.redisson.core.GeoPosition; +import org.redisson.core.GeoUnit; +import org.redisson.core.RGeo; + +public class RedissonGeoTest extends BaseTest { + + @Test + public void testAdd() { + RGeo geo = redisson.getGeo("test"); + assertThat(geo.add(2.51, 3.12, "city1")).isEqualTo(1); + } + + @Test + public void testAddEntries() { + RGeo geo = redisson.getGeo("test"); + assertThat(geo.add(new GeoEntry(3.11, 9.10321, "city1"), new GeoEntry(81.1231, 38.65478, "city2"))).isEqualTo(2); + } + + @Test + public void testDist() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + assertThat(geo.dist("Palermo", "Catania", GeoUnit.METERS)).isEqualTo(166274.15156960033D); + } + + @Test + public void testDistEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.dist("Palermo", "Catania", GeoUnit.METERS)).isNull(); + } + + @Test + public void testHash() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + Map expected = new LinkedHashMap(); + expected.put("Palermo", "sqc8b49rny0"); + expected.put("Catania", "sqdtr74hyu0"); + assertThat(geo.hash("Palermo", "Catania")).isEqualTo(expected); + } + + @Test + public void testHashEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.hash("Palermo", "Catania")).isEmpty(); + } + + + @Test + public void testPos() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + Map expected = new LinkedHashMap(); + expected.put("Palermo", new GeoPosition(13.361389338970184, 38.115556395496299)); + expected.put("Catania", new GeoPosition(15.087267458438873, 37.50266842333162)); + assertThat(geo.pos("test2", "Palermo", "test3", "Catania", "test1")).isEqualTo(expected); + } + + @Test + public void testPosEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.pos("test2", "Palermo", "test3", "Catania", "test1")).isEmpty(); + } + + @Test + public void testRadius() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + assertThat(geo.radius(15, 37, 200, GeoUnit.KILOMETERS)).containsExactly("Palermo", "Catania"); + } + + @Test + public void testRadiusEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.radius(15, 37, 200, GeoUnit.KILOMETERS)).isEmpty(); + } + + @Test + public void testRadiusWithDistance() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + Map expected = new HashMap(); + expected.put("Palermo", 190.4424); + expected.put("Catania", 56.4413); + assertThat(geo.radiusWithDistance(15, 37, 200, GeoUnit.KILOMETERS)).isEqualTo(expected); + } + + @Test + public void testRadiusWithDistanceEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.radiusWithDistance(15, 37, 200, GeoUnit.KILOMETERS)).isEmpty(); + } + + @Test + public void testRadiusWithPosition() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + Map expected = new HashMap(); + expected.put("Palermo", new GeoPosition(13.361389338970184, 38.115556395496299)); + expected.put("Catania", new GeoPosition(15.087267458438873, 37.50266842333162)); + assertThat(geo.radiusWithPosition(15, 37, 200, GeoUnit.KILOMETERS)).isEqualTo(expected); + } + + @Test + public void testRadiusWithPositionEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.radiusWithPosition(15, 37, 200, GeoUnit.KILOMETERS)).isEmpty(); + } + + @Test + public void testRadiusMember() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + assertThat(geo.radius("Palermo", 200, GeoUnit.KILOMETERS)).containsExactly("Palermo", "Catania"); + } + + @Test + public void testRadiusMemberEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.radius("Palermo", 200, GeoUnit.KILOMETERS)).isEmpty(); + } + + @Test + public void testRadiusMemberWithDistance() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + Map expected = new HashMap(); + expected.put("Palermo", 0.0); + expected.put("Catania", 166.2742); + assertThat(geo.radiusWithDistance("Palermo", 200, GeoUnit.KILOMETERS)).isEqualTo(expected); + } + + @Test + public void testRadiusMemberWithDistanceEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.radiusWithDistance("Palermo", 200, GeoUnit.KILOMETERS)).isEmpty(); + } + + @Test + public void testRadiusMemberWithPosition() { + RGeo geo = redisson.getGeo("test"); + geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania")); + + Map expected = new HashMap(); + expected.put("Palermo", new GeoPosition(13.361389338970184, 38.115556395496299)); + expected.put("Catania", new GeoPosition(15.087267458438873, 37.50266842333162)); + assertThat(geo.radiusWithPosition("Palermo", 200, GeoUnit.KILOMETERS)).isEqualTo(expected); + } + + @Test + public void testRadiusMemberWithPositionEmpty() { + RGeo geo = redisson.getGeo("test"); + + assertThat(geo.radiusWithPosition("Palermo", 200, GeoUnit.KILOMETERS)).isEmpty(); + } + +} diff --git a/src/test/java/org/redisson/RedissonKeysTest.java b/src/test/java/org/redisson/RedissonKeysTest.java index 4380d26df..ef6a4d4b9 100644 --- a/src/test/java/org/redisson/RedissonKeysTest.java +++ b/src/test/java/org/redisson/RedissonKeysTest.java @@ -58,7 +58,7 @@ public class RedissonKeysTest extends BaseTest { assertThat(redisson.getKeys().randomKey()).isIn("test1", "test2"); redisson.getKeys().delete("test1"); Assert.assertEquals(redisson.getKeys().randomKey(), "test2"); - redisson.flushdb(); + redisson.getKeys().flushdb(); Assert.assertNull(redisson.getKeys().randomKey()); } diff --git a/src/test/java/org/redisson/RedissonLexSortedSetTest.java b/src/test/java/org/redisson/RedissonLexSortedSetTest.java index b048b0176..e6ce62d4c 100644 --- a/src/test/java/org/redisson/RedissonLexSortedSetTest.java +++ b/src/test/java/org/redisson/RedissonLexSortedSetTest.java @@ -1,7 +1,6 @@ package org.redisson; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; +import static org.assertj.core.api.Assertions.*; import org.junit.Assert; import org.junit.Test; import org.redisson.core.RLexSortedSet; @@ -20,12 +19,12 @@ public class RedissonLexSortedSetTest extends BaseTest { Assert.assertTrue(set.add("f")); Assert.assertTrue(set.add("g")); - Assert.assertEquals(0, (int)set.removeRangeTailByLex("z", false)); + Assert.assertEquals(0, (int)set.removeRangeTail("z", false)); - Assert.assertEquals(4, (int)set.removeRangeTailByLex("c", false)); - MatcherAssert.assertThat(set, Matchers.contains("a", "b", "c")); - Assert.assertEquals(1, (int)set.removeRangeTailByLex("c", true)); - MatcherAssert.assertThat(set, Matchers.contains("a", "b")); + Assert.assertEquals(4, (int)set.removeRangeTail("c", false)); + assertThat(set).containsExactly("a", "b", "c"); + Assert.assertEquals(1, (int)set.removeRangeTail("c", true)); + assertThat(set).containsExactly("a", "b"); } @@ -40,10 +39,10 @@ public class RedissonLexSortedSetTest extends BaseTest { set.add("f"); set.add("g"); - Assert.assertEquals(2, (int)set.removeRangeHeadByLex("c", false)); - MatcherAssert.assertThat(set, Matchers.contains("c", "d", "e", "f", "g")); - Assert.assertEquals(1, (int)set.removeRangeHeadByLex("c", true)); - MatcherAssert.assertThat(set, Matchers.contains("d", "e", "f", "g")); + Assert.assertEquals(2, (int)set.removeRangeHead("c", false)); + assertThat(set).containsExactly("c", "d", "e", "f", "g"); + Assert.assertEquals(1, (int)set.removeRangeHead("c", true)); + assertThat(set).containsExactly("d", "e", "f", "g"); } @Test @@ -57,8 +56,8 @@ public class RedissonLexSortedSetTest extends BaseTest { set.add("f"); set.add("g"); - Assert.assertEquals(5, set.removeRangeByLex("aaa", true, "g", false)); - MatcherAssert.assertThat(set, Matchers.contains("a", "g")); + Assert.assertEquals(5, set.removeRange("aaa", true, "g", false)); + assertThat(set).containsExactly("a", "g"); } @@ -74,8 +73,8 @@ public class RedissonLexSortedSetTest extends BaseTest { Assert.assertTrue(set.add("f")); Assert.assertTrue(set.add("g")); - MatcherAssert.assertThat(set.lexRangeTail("c", false), Matchers.contains("d", "e", "f", "g")); - MatcherAssert.assertThat(set.lexRangeTail("c", true), Matchers.contains("c", "d", "e", "f", "g")); + assertThat(set.rangeTail("c", false)).containsExactly("d", "e", "f", "g"); + assertThat(set.rangeTail("c", true)).containsExactly("c", "d", "e", "f", "g"); } @@ -90,8 +89,8 @@ public class RedissonLexSortedSetTest extends BaseTest { set.add("f"); set.add("g"); - MatcherAssert.assertThat(set.lexRangeHead("c", false), Matchers.contains("a", "b")); - MatcherAssert.assertThat(set.lexRangeHead("c", true), Matchers.contains("a", "b", "c")); + assertThat(set.rangeHead("c", false)).containsExactly("a", "b"); + assertThat(set.rangeHead("c", true)).containsExactly("a", "b", "c"); } @@ -106,7 +105,7 @@ public class RedissonLexSortedSetTest extends BaseTest { set.add("f"); set.add("g"); - MatcherAssert.assertThat(set.lexRange("aaa", true, "g", false), Matchers.contains("b", "c", "d", "e", "f")); + assertThat(set.range("aaa", true, "g", false)).containsExactly("b", "c", "d", "e", "f"); } @Test @@ -120,8 +119,8 @@ public class RedissonLexSortedSetTest extends BaseTest { set.add("f"); set.add("g"); - Assert.assertEquals(5, (int)set.lexCount("b", true, "f", true)); - Assert.assertEquals(3, (int)set.lexCount("b", false, "f", false)); + assertThat(set.count("b", true, "f", true)).isEqualTo(5); + assertThat(set.count("b", false, "f", false)).isEqualTo(3); } } diff --git a/src/test/java/org/redisson/RedissonSetMultimapCacheTest.java b/src/test/java/org/redisson/RedissonSetMultimapCacheTest.java index 442855b8c..a4da5c6dd 100644 --- a/src/test/java/org/redisson/RedissonSetMultimapCacheTest.java +++ b/src/test/java/org/redisson/RedissonSetMultimapCacheTest.java @@ -102,7 +102,7 @@ public class RedissonSetMultimapCacheTest extends BaseTest { multimap.put("1", "3"); multimap.expireKey("1", 1, TimeUnit.SECONDS); - Thread.sleep(1000); + Thread.sleep(1500); assertThat(multimap.get("1").size()).isZero(); assertThat(multimap.get("1")).contains();