RScoredSortedSet.firstScore, lastScore methods added. #811

pull/903/head
Nikita 8 years ago
parent 51fc7ad6c0
commit 15b18abe74

@ -143,6 +143,27 @@ public class RedissonScoredSortedSet<V> extends RedissonExpirable implements RSc
public RFuture<V> lastAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGE_SINGLE, getName(), -1, -1);
}
@Override
public Double firstScore() {
return get(firstScoreAsync());
}
@Override
public RFuture<Double> firstScoreAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGE_SINGLE_SCORE, getName(), 0, 0, "WITHSCORES");
}
@Override
public Double lastScore() {
return get(lastScoreAsync());
}
@Override
public RFuture<Double> lastScoreAsync() {
return commandExecutor.readAsync(getName(), codec, RedisCommands.ZRANGE_SINGLE_SCORE, getName(), -1, -1, "WITHSCORES");
}
@Override
public RFuture<Boolean> addAsync(double score, V object) {

@ -52,6 +52,10 @@ public interface RScoredSortedSet<V> extends RScoredSortedSetAsync<V>, Iterable<
V first();
V last();
Double firstScore();
Double lastScore();
Long addAll(Map<V, Double> objects);

@ -37,6 +37,10 @@ public interface RScoredSortedSetAsync<V> extends RExpirableAsync, RSortableAsyn
RFuture<V> firstAsync();
RFuture<V> lastAsync();
RFuture<Double> firstScoreAsync();
RFuture<Double> lastScoreAsync();
RFuture<Long> addAllAsync(Map<V, Double> objects);

@ -46,11 +46,11 @@ import org.redisson.client.protocol.decoder.ListResultReplayDecoder;
import org.redisson.client.protocol.decoder.ListScanResult;
import org.redisson.client.protocol.decoder.ListScanResultReplayDecoder;
import org.redisson.client.protocol.decoder.Long2MultiDecoder;
import org.redisson.client.protocol.decoder.LongMultiDecoder;
import org.redisson.client.protocol.decoder.MapScanResult;
import org.redisson.client.protocol.decoder.MapScanResultReplayDecoder;
import org.redisson.client.protocol.decoder.NestedMultiDecoder;
import org.redisson.client.protocol.decoder.ObjectFirstResultReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectFirstScoreReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectListReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectMapEntryReplayDecoder;
import org.redisson.client.protocol.decoder.ObjectMapReplayDecoder;
@ -115,6 +115,7 @@ public interface RedisCommands {
RedisCommand<Integer> ZREVRANK_INT = new RedisCommand<Integer>("ZREVRANK", new IntegerReplayConvertor(), 2);
RedisStrictCommand<Long> ZRANK = new RedisStrictCommand<Long>("ZRANK", 2);
RedisCommand<Object> ZRANGE_SINGLE = new RedisCommand<Object>("ZRANGE", new ObjectFirstResultReplayDecoder<Object>());
RedisStrictCommand<Double> ZRANGE_SINGLE_SCORE = new RedisStrictCommand<Double>("ZRANGE", new ObjectFirstScoreReplayDecoder());
RedisCommand<List<Object>> ZRANGE = new RedisCommand<List<Object>>("ZRANGE", new ObjectListReplayDecoder<Object>());
RedisStrictCommand<Integer> ZREMRANGEBYRANK = new RedisStrictCommand<Integer>("ZREMRANGEBYRANK", new IntegerReplayConvertor());
RedisStrictCommand<Integer> ZREMRANGEBYSCORE = new RedisStrictCommand<Integer>("ZREMRANGEBYSCORE", new IntegerReplayConvertor());

@ -0,0 +1,49 @@
/**
* Copyright 2016 Nikita Koksharov
*
* 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.protocol.decoder;
import java.math.BigDecimal;
import java.util.List;
import org.redisson.client.handler.State;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
/**
*
* @author Nikita Koksharov
*
*
*/
public class ObjectFirstScoreReplayDecoder implements MultiDecoder<Double> {
@Override
public Object decode(ByteBuf buf, State state) {
return new BigDecimal(buf.toString(CharsetUtil.UTF_8)).doubleValue();
}
@Override
public Double decode(List<Object> parts, State state) {
return (Double) parts.get(1);
}
@Override
public boolean isApplicable(int paramNum, State state) {
return paramNum % 2 != 0;
}
}

@ -274,7 +274,18 @@ public class RedissonScoredSortedSetTest extends BaseTest {
Assert.assertEquals("a", set.first());
Assert.assertEquals("d", set.last());
}
@Test
public void testFirstLastScore() {
RScoredSortedSet<String> set = redisson.getScoredSortedSet("simple");
set.add(0.1, "a");
set.add(0.2, "b");
set.add(0.3, "c");
set.add(0.4, "d");
assertThat(set.firstScore()).isEqualTo(0.1);
assertThat(set.lastScore()).isEqualTo(0.4);
}
@Test
public void testRemoveRangeByScore() {

Loading…
Cancel
Save