DATAREDIS-293 - JedisConnection, allow bulk addition with same score.

Added version check for jedis library as the operation had been introduced in 2.4.

Original Pull Request: #63
This commit is contained in:
icedrake
2014-04-02 16:58:29 -06:00
committed by Thomas Darimont
parent db2da6dd50
commit 6d7c5ce9cd
7 changed files with 270 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 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.
@@ -15,20 +15,16 @@
*/
package org.springframework.data.redis;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.data.redis.connection.RedisConnection;
/**
* Utilities for examining the Redis version
*
* @author Jennifer Hickey
* @author Christoph Strobl
*/
public abstract class RedisVersionUtils {
private static final Pattern VERSION_MATCHER = Pattern.compile("([0-9]+)\\.([0-9]+)(\\.([0-9]+))?");
public static Version getRedisVersion(RedisConnection connection) {
return parseVersion((String) connection.info().get("redis_version"));
}
@@ -42,14 +38,11 @@ public abstract class RedisVersionUtils {
}
public static Version parseVersion(String version) {
Matcher matcher = VERSION_MATCHER.matcher(version);
if (matcher.matches()) {
String major = matcher.group(1);
String minor = matcher.group(2);
String patch = matcher.group(4);
return new Version(Integer.parseInt(major), minor != null ? Integer.parseInt(minor) : 0,
patch != null ? Integer.parseInt(patch) : 0);
Version resolvedVersion = VersionParser.parseVersion(version);
if (Version.UNKNOWN.equals(resolvedVersion)) {
throw new IllegalArgumentException("Specified version cannot be parsed");
}
throw new IllegalArgumentException("Specified version cannot be parsed");
return resolvedVersion;
}
}

View File

@@ -1,51 +0,0 @@
/*
* Copyright 2011-2013 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.
* 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.springframework.data.redis;
/**
* A {@link Comparable} software version
*
* @author Jennifer Hickey
*/
public class Version implements Comparable<Version> {
Integer major;
Integer minor;
Integer patch;
public Version(int major, int minor, int patch) {
this.major = major;
this.minor = minor;
this.patch = patch;
}
public int compareTo(Version o) {
if (this.major != o.major) {
return this.major.compareTo(o.major);
}
if (this.minor != o.minor) {
return this.minor.compareTo(o.minor);
}
if (this.patch != o.patch) {
return this.patch.compareTo(o.patch);
}
return 0;
}
@Override
public String toString() {
return "" + major + "." + minor + "." + patch;
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2014 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.
* 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.springframework.data.redis;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* @author Christoph Strobl
*/
public class VersionParserUnitTests {
@Test
public void shouldParseNullToUnknown() {
assertThat(VersionParser.parseVersion(null), equalTo(Version.UNKNOWN));
}
@Test
public void shouldParseEmptyVersionStringToUnknown() {
assertThat(VersionParser.parseVersion(""), equalTo(Version.UNKNOWN));
}
@Test
public void shouldParseInvalidVersionStringToUnknown() {
assertThat(VersionParser.parseVersion("ThisIsNoValidVersion"), equalTo(Version.UNKNOWN));
}
@Test
public void shouldParseMajorMinorWithoutPatchCorrectly() {
assertThat(VersionParser.parseVersion("1.2"), equalTo(new Version(1, 2, 0)));
}
@Test
public void shouldParseMajorMinorPatchCorrectly() {
assertThat(VersionParser.parseVersion("1.2.3"), equalTo(new Version(1, 2, 3)));
}
@Test
public void shouldParseMajorWithoutMinorPatchCorrectly() {
assertThat(VersionParser.parseVersion("1.2.3.a"), equalTo(new Version(1, 2, 3)));
}
}

View File

@@ -117,12 +117,13 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
factory2.destroy();
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void testZAddSameScores() {
Set<StringTuple> strTuples = new HashSet<StringTuple>();
strTuples.add(new DefaultStringTuple("Bob".getBytes(), "Bob", 2.0));
strTuples.add(new DefaultStringTuple("James".getBytes(), "James", 2.0));
connection.zAdd("myset", strTuples);
Long added = connection.zAdd("myset", strTuples);
assertEquals(2L, added.longValue());
}
@Test(expected = InvalidDataAccessApiUsageException.class)