Skip to content

Commit

Permalink
RLexSortedSet object introduced. #135
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Sep 14, 2015
1 parent 3ab7e1a commit 1bfc3a7
Show file tree
Hide file tree
Showing 18 changed files with 277 additions and 193 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/redisson/CommandExecutor.java
Expand Up @@ -33,6 +33,8 @@
//TODO ping support //TODO ping support
public interface CommandExecutor { public interface CommandExecutor {


<T, R> R read(RedisClient client, String key, Codec codec, RedisCommand<T> command, Object ... params);

<T, R> R read(RedisClient client, String key, RedisCommand<T> command, Object ... params); <T, R> R read(RedisClient client, String key, RedisCommand<T> command, Object ... params);


<T, R> Future<R> evalWriteAllAsync(RedisCommand<T> command, SlotCallback<T, R> callback, String script, List<Object> keys, Object ... params); <T, R> Future<R> evalWriteAllAsync(RedisCommand<T> command, SlotCallback<T, R> callback, String script, List<Object> keys, Object ... params);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/redisson/CommandExecutorService.java
Expand Up @@ -201,6 +201,12 @@ public <T, R> R read(RedisClient client, String key, RedisCommand<T> command, Ob
return get(res); return get(res);
} }


public <T, R> R read(RedisClient client, String key, Codec codec, RedisCommand<T> command, Object ... params) {
Future<R> res = readAsync(client, key, codec, command, params);
return get(res);
}


public <T, R> Future<R> readAsync(RedisClient client, String key, Codec codec, RedisCommand<T> command, Object ... params) { public <T, R> Future<R> readAsync(RedisClient client, String key, Codec codec, RedisCommand<T> command, Object ... params) {
Promise<R> mainPromise = connectionManager.newPromise(); Promise<R> mainPromise = connectionManager.newPromise();
int slot = connectionManager.calcSlot(key); int slot = connectionManager.calcSlot(key);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/redisson/Redisson.java
Expand Up @@ -39,6 +39,7 @@
import org.redisson.core.RDeque; import org.redisson.core.RDeque;
import org.redisson.core.RHyperLogLog; import org.redisson.core.RHyperLogLog;
import org.redisson.core.RKeys; import org.redisson.core.RKeys;
import org.redisson.core.RLexSortedSet;
import org.redisson.core.RList; import org.redisson.core.RList;
import org.redisson.core.RLock; import org.redisson.core.RLock;
import org.redisson.core.RMap; import org.redisson.core.RMap;
Expand Down Expand Up @@ -221,6 +222,10 @@ public <V> RScoredSortedSet<V> getScoredSortedSet(String name) {
return new RedissonScoredSortedSet<V>(commandExecutor, name); return new RedissonScoredSortedSet<V>(commandExecutor, name);
} }


public RLexSortedSet getLexSortedSet(String name) {
return new RedissonLexSortedSet(commandExecutor, name);
}

/** /**
* Returns topic instance by name. * Returns topic instance by name.
* *
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/redisson/RedissonClient.java
Expand Up @@ -29,11 +29,13 @@
import org.redisson.core.RDeque; import org.redisson.core.RDeque;
import org.redisson.core.RHyperLogLog; import org.redisson.core.RHyperLogLog;
import org.redisson.core.RKeys; import org.redisson.core.RKeys;
import org.redisson.core.RLexSortedSet;
import org.redisson.core.RList; import org.redisson.core.RList;
import org.redisson.core.RLock; import org.redisson.core.RLock;
import org.redisson.core.RMap; import org.redisson.core.RMap;
import org.redisson.core.RPatternTopic; import org.redisson.core.RPatternTopic;
import org.redisson.core.RQueue; import org.redisson.core.RQueue;
import org.redisson.core.RScoredSortedSet;
import org.redisson.core.RScript; import org.redisson.core.RScript;
import org.redisson.core.RSet; import org.redisson.core.RSet;
import org.redisson.core.RSortedSet; import org.redisson.core.RSortedSet;
Expand Down Expand Up @@ -104,6 +106,10 @@ public interface RedissonClient {
*/ */
<V> RSortedSet<V> getSortedSet(String name); <V> RSortedSet<V> getSortedSet(String name);


<V> RScoredSortedSet<V> getScoredSortedSet(String name);

RLexSortedSet getLexSortedSet(String name);

/** /**
* Returns topic instance by name. * Returns topic instance by name.
* *
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/redisson/RedissonExpirable.java
Expand Up @@ -18,6 +18,7 @@
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;


import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec; import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisCommands;
import org.redisson.core.RExpirable; import org.redisson.core.RExpirable;
Expand All @@ -30,6 +31,10 @@ abstract class RedissonExpirable extends RedissonObject implements RExpirable {
super(connectionManager, name); super(connectionManager, name);
} }


RedissonExpirable(Codec codec, CommandExecutor connectionManager, String name) {
super(codec, connectionManager, name);
}

@Override @Override
public boolean expire(long timeToLive, TimeUnit timeUnit) { public boolean expire(long timeToLive, TimeUnit timeUnit) {
return commandExecutor.get(expireAsync(timeToLive, timeUnit)); return commandExecutor.get(expireAsync(timeToLive, timeUnit));
Expand Down
142 changes: 142 additions & 0 deletions src/main/java/org/redisson/RedissonLexSortedSet.java
@@ -0,0 +1,142 @@
/**
* 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.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.core.RLexSortedSet;

import io.netty.util.concurrent.Future;

public class RedissonLexSortedSet extends RedissonScoredSortedSet<String> implements RLexSortedSet {

public RedissonLexSortedSet(CommandExecutor commandExecutor, String name) {
super(StringCodec.INSTANCE, commandExecutor, name);
}

@Override
public Collection<String> lexRange(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) {
return get(lexRangeAsync(fromElement, fromInclusive, toElement, toInclusive));
}

@Override
public Collection<String> lexRangeHead(String toElement, boolean toInclusive) {
return get(lexRangeHeadAsync(toElement, toInclusive));
}

@Override
public Future<Collection<String>> lexRangeHeadAsync(String toElement, boolean toInclusive) {
String toValue = value(toElement, toInclusive);
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), "-", toValue);
}

@Override
public Collection<String> lexRangeTail(String fromElement, boolean fromInclusive) {
return get(lexRangeTailAsync(fromElement, fromInclusive));
}

@Override
public Future<Collection<String>> lexRangeTailAsync(String fromElement, boolean fromInclusive) {
String fromValue = value(fromElement, fromInclusive);
return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, "+");
}


@Override
public Future<Collection<String>> lexRangeAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) {
String fromValue = value(fromElement, fromInclusive);
String toValue = value(toElement, toInclusive);

return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.ZRANGEBYLEX, getName(), fromValue, toValue);
}

@Override
public Integer lexCountTail(String fromElement, boolean fromInclusive) {
return get(lexCountTailAsync(fromElement, fromInclusive));
}

@Override
public Future<Integer> lexCountTailAsync(String fromElement, boolean fromInclusive) {
String fromValue = value(fromElement, fromInclusive);

return commandExecutor.readAsync(getName(), RedisCommands.ZLEXCOUNT, getName(), fromValue, "+");
}

@Override
public Integer lexCountHead(String toElement, boolean toInclusive) {
return get(lexCountHeadAsync(toElement, toInclusive));
}

@Override
public Future<Integer> lexCountHeadAsync(String toElement, boolean toInclusive) {
String toValue = value(toElement, toInclusive);

return commandExecutor.readAsync(getName(), RedisCommands.ZLEXCOUNT, getName(), "-", toValue);
}

@Override
public Integer lexCount(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) {
return get(lexCountAsync(fromElement, fromInclusive, toElement, toInclusive));
}

@Override
public Future<Integer> lexCountAsync(String fromElement, boolean fromInclusive, String toElement, boolean toInclusive) {
String fromValue = value(fromElement, fromInclusive);
String toValue = value(toElement, toInclusive);

return commandExecutor.readAsync(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 Future<Boolean> addAsync(String e) {
return addAsync(0, e);
}

@Override
public Future<Boolean> addAllAsync(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.writeAsync(getName(), codec, RedisCommands.ZADD, getName(), params.toArray());
}

@Override
public boolean add(String e) {
return get(addAsync(e));
}

@Override
public boolean addAll(Collection<? extends String> c) {
return get(addAllAsync(c));
}

}
3 changes: 1 addition & 2 deletions src/main/java/org/redisson/RedissonList.java
Expand Up @@ -92,8 +92,7 @@ private List<V> readAll() {
return (List<V>) get(readAllAsync()); return (List<V>) get(readAllAsync());
} }


@Override private Future<Collection<V>> readAllAsync() {
public Future<Collection<V>> readAllAsync() {
return commandExecutor.readAsync(getName(), LRANGE, getName(), 0, -1); return commandExecutor.readAsync(getName(), LRANGE, getName(), 0, -1);
} }


Expand Down
11 changes: 9 additions & 2 deletions src/main/java/org/redisson/RedissonObject.java
Expand Up @@ -15,6 +15,7 @@
*/ */
package org.redisson; package org.redisson;


import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisCommands;
import org.redisson.core.RObject; import org.redisson.core.RObject;


Expand All @@ -31,10 +32,16 @@ abstract class RedissonObject implements RObject {


final CommandExecutor commandExecutor; final CommandExecutor commandExecutor;
private final String name; private final String name;
final Codec codec;


public RedissonObject(CommandExecutor commandExecutor, String name) { public RedissonObject(Codec codec, CommandExecutor commandExecutor, String name) {
this.commandExecutor = commandExecutor; this.codec = codec;
this.name = name; this.name = name;
this.commandExecutor = commandExecutor;
}

public RedissonObject(CommandExecutor commandExecutor, String name) {
this(commandExecutor.getConnectionManager().getCodec(), commandExecutor, name);
} }


protected <V> V get(Future<V> future) { protected <V> V get(Future<V> future) {
Expand Down

0 comments on commit 1bfc3a7

Please sign in to comment.