Skip to content

Commit

Permalink
Fix zset score parsing for infinite scores #528
Browse files Browse the repository at this point in the history
Lettuce now parses positive and negative infinite scores correctly. Infinite scores are received as inf and -inf that require appropriate transformation instead of parsing the floating point value.

Previously parsing caused NumberFormatException.
  • Loading branch information
mp911de committed Apr 29, 2017
1 parent 2e6f1e9 commit 2754060
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 47 deletions.
40 changes: 31 additions & 9 deletions src/main/java/com/lambdaworks/redis/LettuceStrings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@

/**
* Helper for {@link String} checks. This class is part of the internal API and may change without further notice.
*
*
* @author Mark Paluch
* @since 3.0
*/
Expand All @@ -38,7 +38,7 @@ private LettuceStrings() {

/**
* Checks if a CharSequence is empty ("") or null.
*
*
* @param cs the char sequence
* @return true if empty
*/
Expand All @@ -48,18 +48,19 @@ public static boolean isEmpty(final CharSequence cs) {

/**
* Checks if a CharSequence is not empty ("") and not null.
*
*
* @param cs the char sequence
* @return true if not empty
*
*
*/
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}

/**
* Convert double to string. If double is infinite, returns positive/negative infinity {@code +inf} and {@code -inf}.
*
* Convert {@code double} to {@link String}. If {@code n} is infinite, returns positive/negative infinity {@code +inf} and
* {@code -inf}.
*
* @param n the double.
* @return string representation of {@code n}
*/
Expand All @@ -70,9 +71,30 @@ public static String string(double n) {
return Double.toString(n);
}

/**
* Convert {@link String} to {@code double}. If {@code s} is {@literal +inf}/{@literal -inf}, returns positive/negative
* infinity.
*
* @param s string representation of the number
* @return the {@code double} value.
* @since 4.3.3
*/
public static double toDouble(String s) {

if ("+inf".equals(s) || "inf".equals(s)) {
return Double.POSITIVE_INFINITY;
}

if ("-inf".equals(s)) {
return Double.NEGATIVE_INFINITY;
}

return Double.parseDouble(s);
}

/**
* Create SHA1 digest from Lua script.
*
*
* @param script the script
* @return the Base16 encoded SHA1 value
*/
Expand All @@ -82,7 +104,7 @@ public static String digest(byte[] script) {

/**
* Create SHA1 digest from Lua script.
*
*
* @param script the script
* @return the Base16 encoded SHA1 value
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;

import com.lambdaworks.redis.LettuceStrings;
import com.lambdaworks.redis.ScoredValue;
import com.lambdaworks.redis.codec.RedisCodec;
import com.lambdaworks.redis.internal.LettuceAssert;
Expand All @@ -30,8 +31,9 @@
* @param <V> Value type.
* @author Will Glozer
*/
public class ScoredValueListOutput<K, V> extends CommandOutput<K, V, List<ScoredValue<V>>>
implements StreamingOutput<ScoredValue<V>> {
public class ScoredValueListOutput<K, V> extends CommandOutput<K, V, List<ScoredValue<V>>> implements
StreamingOutput<ScoredValue<V>> {

private V value;
private Subscriber<ScoredValue<V>> subscriber;

Expand All @@ -47,7 +49,7 @@ public void set(ByteBuffer bytes) {
return;
}

double score = Double.parseDouble(decodeAscii(bytes));
double score = LettuceStrings.toDouble(decodeAscii(bytes));
subscriber.onNext(new ScoredValue<>(score, value));
value = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,13 +17,14 @@

import java.nio.ByteBuffer;

import com.lambdaworks.redis.LettuceStrings;
import com.lambdaworks.redis.ScoredValue;
import com.lambdaworks.redis.ScoredValueScanCursor;
import com.lambdaworks.redis.codec.RedisCodec;

/**
* {@link com.lambdaworks.redis.ScoredValueScanCursor} for scan cursor output.
*
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch
Expand All @@ -44,7 +45,7 @@ protected void setOutput(ByteBuffer bytes) {
return;
}

double score = Double.parseDouble(decodeAscii(bytes));
double score = LettuceStrings.toDouble(decodeAscii(bytes));
output.getValues().add(new ScoredValue<V>(score, value));
value = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,13 +17,14 @@

import java.nio.ByteBuffer;

import com.lambdaworks.redis.LettuceStrings;
import com.lambdaworks.redis.ScoredValue;
import com.lambdaworks.redis.StreamScanCursor;
import com.lambdaworks.redis.codec.RedisCodec;

/**
* Streaming-Output of of values and their associated scores. Returns the count of all values (including null).
*
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch
Expand All @@ -45,7 +46,7 @@ protected void setOutput(ByteBuffer bytes) {
return;
}

double score = Double.parseDouble(decodeAscii(bytes));
double score = LettuceStrings.toDouble(decodeAscii(bytes));
channel.onValue(new ScoredValue<V>(score, value));
value = null;
output.setCount(output.getCount() + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2016 the original author or authors.
* Copyright 2011-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,12 +17,13 @@

import java.nio.ByteBuffer;

import com.lambdaworks.redis.LettuceStrings;
import com.lambdaworks.redis.ScoredValue;
import com.lambdaworks.redis.codec.RedisCodec;

/**
* Streaming-Output of of values and their associated scores. Returns the count of all values (including null).
*
*
* @author Mark Paluch
* @param <K> Key type.
* @param <V> Value type.
Expand All @@ -44,7 +45,7 @@ public void set(ByteBuffer bytes) {
return;
}

double score = Double.parseDouble(decodeAscii(bytes));
double score = LettuceStrings.toDouble(decodeAscii(bytes));
channel.onValue(new ScoredValue<V>(score, value));
value = null;
output = output.longValue() + 1;
Expand Down
Loading

0 comments on commit 2754060

Please sign in to comment.