RLexSortedSetReactive added. #210
parent
b7cf9cf824
commit
3ad12cb416
@ -0,0 +1,141 @@
|
||||
/**
|
||||
* 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.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.redisson.client.codec.StringCodec;
|
||||
import org.redisson.client.protocol.RedisCommands;
|
||||
import org.redisson.core.RLexSortedSetReactive;
|
||||
|
||||
public class RedissonLexSortedSetReactive extends RedissonScoredSortedSetReactive<String> implements RLexSortedSetReactive {
|
||||
|
||||
public RedissonLexSortedSetReactive(CommandReactiveExecutor commandExecutor, String name) {
|
||||
super(StringCodec.INSTANCE, commandExecutor, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Integer> removeRangeHeadByLex(String toElement, boolean toInclusive) {
|
||||
String toValue = value(toElement, toInclusive);
|
||||
return commandExecutor.writeObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZREMRANGEBYLEX, getName(), "-", toValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Integer> removeRangeTailByLex(String fromElement, boolean fromInclusive) {
|
||||
String fromValue = value(fromElement, fromInclusive);
|
||||
return commandExecutor.writeObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZREMRANGEBYLEX, getName(), fromValue, "+");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Integer> removeRangeByLex(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) {
|
||||
String fromValue = value(fromElement, fromInclusive);
|
||||
String toValue = value(toElement, toInclusive);
|
||||
|
||||
return commandExecutor.writeObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZREMRANGEBYLEX, getName(), fromValue, toValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Collection<String>> lexRangeHead(String toElement, boolean toInclusive) {
|
||||
String toValue = value(toElement, toInclusive);
|
||||
return commandExecutor.readObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), "-", toValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Collection<String>> lexRangeTail(String fromElement, boolean fromInclusive) {
|
||||
String fromValue = value(fromElement, fromInclusive);
|
||||
return commandExecutor.readObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, "+");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Publisher<Collection<String>> lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) {
|
||||
String fromValue = value(fromElement, fromInclusive);
|
||||
String toValue = value(toElement, toInclusive);
|
||||
|
||||
return commandExecutor.readObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, toValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Collection<String>> lexRangeHead(String toElement, boolean toInclusive, int offset, int count) {
|
||||
String toValue = value(toElement, toInclusive);
|
||||
return commandExecutor.readObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), "-", toValue, "LIMIT", offset, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Collection<String>> lexRangeTail(String fromElement, boolean fromInclusive, int offset, int count) {
|
||||
String fromValue = value(fromElement, fromInclusive);
|
||||
return commandExecutor.readObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, "+", "LIMIT", offset, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Collection<String>> lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count) {
|
||||
String fromValue = value(fromElement, fromInclusive);
|
||||
String toValue = value(toElement, toInclusive);
|
||||
|
||||
return commandExecutor.readObservable(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, toValue, "LIMIT", offset, count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Integer> lexCountTail(String fromElement, boolean fromInclusive) {
|
||||
String fromValue = value(fromElement, fromInclusive);
|
||||
|
||||
return commandExecutor.readObservable(getName(), RedisCommands.ZLEXCOUNT, getName(), fromValue, "+");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Integer> lexCountHead(String toElement, boolean toInclusive) {
|
||||
String toValue = value(toElement, toInclusive);
|
||||
|
||||
return commandExecutor.readObservable(getName(), RedisCommands.ZLEXCOUNT, getName(), "-", toValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Integer> lexCount(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) {
|
||||
String fromValue = value(fromElement, fromInclusive);
|
||||
String toValue = value(toElement, toInclusive);
|
||||
|
||||
return commandExecutor.readObservable(getName(), RedisCommands.ZLEXCOUNT, getName(), fromValue, toValue);
|
||||
}
|
||||
|
||||
private String value(String fromElement, boolean fromInclusive) {
|
||||
String fromValue = fromElement.toString();
|
||||
if (fromInclusive) {
|
||||
fromValue = "[" + fromValue;
|
||||
} else {
|
||||
fromValue = "(" + fromValue;
|
||||
}
|
||||
return fromValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Long> add(String e) {
|
||||
return commandExecutor.writeObservable(getName(), codec, RedisCommands.ZADD, getName(), 0, e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Long> addAll(Collection<? extends String> c) {
|
||||
List<Object> params = new ArrayList<Object>(2*c.size());
|
||||
for (Object param : c) {
|
||||
params.add(0);
|
||||
params.add(param);
|
||||
}
|
||||
return commandExecutor.writeObservable(getName(), codec, RedisCommands.ZADD, getName(), params.toArray());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 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.Collection;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
public interface RLexSortedSetReactive extends RCollectionReactive<String> {
|
||||
|
||||
Publisher<Integer> removeRangeByLex(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive);
|
||||
|
||||
Publisher<Integer> removeRangeTailByLex(String fromElement, boolean fromInclusive);
|
||||
|
||||
Publisher<Integer> removeRangeHeadByLex(String toElement, boolean toInclusive);
|
||||
|
||||
Publisher<Integer> lexCountTail(String fromElement, boolean fromInclusive);
|
||||
|
||||
Publisher<Integer> lexCountHead(String toElement, boolean toInclusive);
|
||||
|
||||
Publisher<Collection<String>> lexRangeTail(String fromElement, boolean fromInclusive);
|
||||
|
||||
Publisher<Collection<String>> lexRangeHead(String toElement, boolean toInclusive);
|
||||
|
||||
Publisher<Collection<String>> lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive);
|
||||
|
||||
Publisher<Collection<String>> lexRangeTail(String fromElement, boolean fromInclusive, int offset, int count);
|
||||
|
||||
Publisher<Collection<String>> lexRangeHead(String toElement, boolean toInclusive, int offset, int count);
|
||||
|
||||
Publisher<Collection<String>> lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive, int offset, int count);
|
||||
|
||||
Publisher<Integer> lexCount(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive);
|
||||
|
||||
Publisher<Long> rank(String o);
|
||||
|
||||
Publisher<Collection<String>> valueRange(int startIndex, int endIndex);
|
||||
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
package org.redisson;
|
||||
|
||||
import org.hamcrest.MatcherAssert;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.redisson.core.RLexSortedSetReactive;
|
||||
|
||||
public class RedissonLexSortedSetReactiveTest extends BaseReactiveTest {
|
||||
|
||||
@Test
|
||||
public void testRemoveLexRangeTail() {
|
||||
RLexSortedSetReactive set = redisson.getLexSortedSet("simple");
|
||||
Assert.assertTrue(sync(set.add("a")) == 1);
|
||||
Assert.assertFalse(sync(set.add("a")) == 1);
|
||||
Assert.assertTrue(sync(set.add("b")) == 1);
|
||||
Assert.assertTrue(sync(set.add("c")) == 1);
|
||||
Assert.assertTrue(sync(set.add("d")) == 1);
|
||||
Assert.assertTrue(sync(set.add("e")) == 1);
|
||||
Assert.assertTrue(sync(set.add("f")) == 1);
|
||||
Assert.assertTrue(sync(set.add("g")) == 1);
|
||||
|
||||
Assert.assertEquals(0, sync(set.removeRangeTailByLex("z", false)).intValue());
|
||||
|
||||
Assert.assertEquals(4, sync(set.removeRangeTailByLex("c", false)).intValue());
|
||||
MatcherAssert.assertThat(sync(set), Matchers.contains("a", "b", "c"));
|
||||
Assert.assertEquals(1, sync(set.removeRangeTailByLex("c", true)).intValue());
|
||||
MatcherAssert.assertThat(sync(set), Matchers.contains("a", "b"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRemoveLexRangeHead() {
|
||||
RLexSortedSetReactive set = redisson.getLexSortedSet("simple");
|
||||
sync(set.add("a"));
|
||||
sync(set.add("b"));
|
||||
sync(set.add("c"));
|
||||
sync(set.add("d"));
|
||||
sync(set.add("e"));
|
||||
sync(set.add("f"));
|
||||
sync(set.add("g"));
|
||||
|
||||
Assert.assertEquals(2, sync(set.removeRangeHeadByLex("c", false)).intValue());
|
||||
MatcherAssert.assertThat(sync(set), Matchers.contains("c", "d", "e", "f", "g"));
|
||||
Assert.assertEquals(1, (int)sync(set.removeRangeHeadByLex("c", true)));
|
||||
MatcherAssert.assertThat(sync(set), Matchers.contains("d", "e", "f", "g"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveLexRange() {
|
||||
RLexSortedSetReactive set = redisson.getLexSortedSet("simple");
|
||||
sync(set.add("a"));
|
||||
sync(set.add("b"));
|
||||
sync(set.add("c"));
|
||||
sync(set.add("d"));
|
||||
sync(set.add("e"));
|
||||
sync(set.add("f"));
|
||||
sync(set.add("g"));
|
||||
|
||||
Assert.assertEquals(5, sync(set.removeRangeByLex("aaa", true, "g", false)).intValue());
|
||||
MatcherAssert.assertThat(sync(set), Matchers.contains("a", "g"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testLexRangeTail() {
|
||||
RLexSortedSetReactive set = redisson.getLexSortedSet("simple");
|
||||
Assert.assertTrue(sync(set.add("a")) == 1);
|
||||
Assert.assertFalse(sync(set.add("a")) == 1);
|
||||
Assert.assertTrue(sync(set.add("b")) == 1);
|
||||
Assert.assertTrue(sync(set.add("c")) == 1);
|
||||
Assert.assertTrue(sync(set.add("d")) == 1);
|
||||
Assert.assertTrue(sync(set.add("e")) == 1);
|
||||
Assert.assertTrue(sync(set.add("f")) == 1);
|
||||
Assert.assertTrue(sync(set.add("g")) == 1);
|
||||
|
||||
MatcherAssert.assertThat(sync(set.lexRangeTail("c", false)), Matchers.contains("d", "e", "f", "g"));
|
||||
MatcherAssert.assertThat(sync(set.lexRangeTail("c", true)), Matchers.contains("c", "d", "e", "f", "g"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testLexRangeHead() {
|
||||
RLexSortedSetReactive set = redisson.getLexSortedSet("simple");
|
||||
sync(set.add("a"));
|
||||
sync(set.add("b"));
|
||||
sync(set.add("c"));
|
||||
sync(set.add("d"));
|
||||
sync(set.add("e"));
|
||||
sync(set.add("f"));
|
||||
sync(set.add("g"));
|
||||
|
||||
MatcherAssert.assertThat(sync(set.lexRangeHead("c", false)), Matchers.contains("a", "b"));
|
||||
MatcherAssert.assertThat(sync(set.lexRangeHead("c", true)), Matchers.contains("a", "b", "c"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testLexRange() {
|
||||
RLexSortedSetReactive set = redisson.getLexSortedSet("simple");
|
||||
sync(set.add("a"));
|
||||
sync(set.add("b"));
|
||||
sync(set.add("c"));
|
||||
sync(set.add("d"));
|
||||
sync(set.add("e"));
|
||||
sync(set.add("f"));
|
||||
sync(set.add("g"));
|
||||
|
||||
MatcherAssert.assertThat(sync(set.lexRange("aaa", true, "g", false)), Matchers.contains("b", "c", "d", "e", "f"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLexCount() {
|
||||
RLexSortedSetReactive set = redisson.getLexSortedSet("simple");
|
||||
sync(set.add("a"));
|
||||
sync(set.add("b"));
|
||||
sync(set.add("c"));
|
||||
sync(set.add("d"));
|
||||
sync(set.add("e"));
|
||||
sync(set.add("f"));
|
||||
sync(set.add("g"));
|
||||
|
||||
Assert.assertEquals(5, (int)sync(set.lexCount("b", true, "f", true)));
|
||||
Assert.assertEquals(3, (int)sync(set.lexCount("b", false, "f", false)));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue