From c19ed0a64984bbafb4b54e14270ea616851fff04 Mon Sep 17 00:00:00 2001 From: Nikita Date: Sat, 7 Jun 2014 19:20:55 +0400 Subject: [PATCH] Sorted set scores collapsed to zeros fixed. #28 --- .../java/org/redisson/RedissonSortedSet.java | 85 ++++++++++++++++--- 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/redisson/RedissonSortedSet.java b/src/main/java/org/redisson/RedissonSortedSet.java index 6a40c8bd2..eee20db1b 100644 --- a/src/main/java/org/redisson/RedissonSortedSet.java +++ b/src/main/java/org/redisson/RedissonSortedSet.java @@ -18,7 +18,9 @@ package org.redisson; import java.io.ByteArrayOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.math.BigDecimal; import java.math.BigInteger; +import java.math.MathContext; import java.security.MessageDigest; import java.util.Collection; import java.util.Comparator; @@ -211,13 +213,30 @@ public class RedissonSortedSet extends RedissonObject implements RSortedSet iterator() { - double startScore; + Double startScore; RedisConnection connection = connectionManager.connectionReadOp(); try { startScore = getScoreAtIndex(0, connection); } finally { connectionManager.releaseRead(connection); } + if (startScore == null) { + return new Iterator() { + @Override + public boolean hasNext() { + return false; + } + + @Override + public V next() { + throw new NoSuchElementException(); + } + + @Override + public void remove() { + } + }; + } return iterator(startScore, Double.MAX_VALUE); } @@ -266,6 +285,7 @@ public class RedissonSortedSet extends RedissonObject implements RSortedSet val = values.get(0); value = val.value; +// System.out.println("value: " + value + " currentScore: " + currentScore); if (values.size() > 1) { ScoredValue nextVal = values.get(1); currentScore = nextVal.score; @@ -375,6 +395,7 @@ public class RedissonSortedSet extends RedissonObject implements RSortedSet extends RedissonObject implements RSortedSet connection) { + protected NewScore calcNewScore(int index, RedisConnection connection) { if (index >= 0) { throw new IllegalStateException("index should be negative, but value is " + index); } @@ -438,24 +470,51 @@ public class RedissonSortedSet extends RedissonObject implements RSortedSet 1) { + score = leftScore + 1; + } else if (s < 1) { + double inc = calcIncrement(s); + if (BigDecimal.valueOf(leftScore).add(BigDecimal.valueOf(inc)).compareTo(BigDecimal.valueOf(rightScore)) >= 0) { + // TODO check strange behavior +// if (leftScore + inc >= rightScore) { + if (inc == 0) { + inc = 0.1; + } + score = BigDecimal.valueOf(leftScore).add(BigDecimal.valueOf(inc).divide(BigDecimal.TEN)).doubleValue(); + } else { + score = BigDecimal.valueOf(leftScore).add(BigDecimal.valueOf(inc)).doubleValue(); + } + } } } +// System.out.println("score: " + score + " left: " + leftScore + " right: " + rightScore); return new NewScore(leftScore, rightScore, score); } @@ -624,10 +683,10 @@ public class RedissonSortedSet extends RedissonObject implements RSortedSet connection) { + private Double getScoreAtIndex(int index, RedisConnection connection) { List> res = connection.zrangeWithScores(getName(), index, index); if (res.isEmpty()) { - return -1; + return null; } return res.get(0).score; }