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

@@ -0,0 +1,102 @@
/*
* 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.
* 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
* @author Christoph Strobl
*/
public class Version implements Comparable<Version> {
public static final Version UNKNOWN = new Version(0, 0, 0);
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;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((major == null) ? 0 : major.hashCode());
result = prime * result + ((minor == null) ? 0 : minor.hashCode());
result = prime * result + ((patch == null) ? 0 : patch.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Version)) {
return false;
}
Version other = (Version) obj;
if (major == null) {
if (other.major != null) {
return false;
}
} else if (!major.equals(other.major)) {
return false;
}
if (minor == null) {
if (other.minor != null) {
return false;
}
} else if (!minor.equals(other.minor)) {
return false;
}
if (patch == null) {
if (other.patch != null) {
return false;
}
} else if (!patch.equals(other.patch)) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,53 @@
/*
* 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 java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Central class for reading version string (eg. {@literal 1.3.1}) into {@link Version}.
*
* @author Christoph Strobl
* @since 1.3
*/
public class VersionParser {
private static final Pattern VERSION_MATCHER = Pattern.compile("([0-9]+)\\.([0-9]+)(\\.([0-9]+))?(.*)");
/**
* Parse version string {@literal eg. 1.1.1} to {@link Version}.
*
* @param version
* @return
*/
public static Version parseVersion(String version) {
if (version == null) {
return Version.UNKNOWN;
}
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);
}
return Version.UNKNOWN;
}
}

View File

@@ -2923,7 +2923,7 @@ public class JedisConnection implements RedisConnection {
Map<byte[], Double> args = new HashMap<byte[], Double>();
for (Tuple tuple : tuples) {
if (args.containsValue(tuple.getScore())) {
if (args.containsValue(tuple.getScore()) && !JedisVersionUtil.atLeastJedis24()) {
throw new UnsupportedOperationException(
"Bulk add of multiple elements with the same score is not supported. Add the elements individually.");
}

View File

@@ -0,0 +1,97 @@
/*
* 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.connection.jedis;
import java.io.IOException;
import java.util.Properties;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.data.redis.Version;
import org.springframework.data.redis.VersionParser;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
/**
* @author Christoph Strobl
* @since 1.3
*/
public class JedisVersionUtil {
private static Version jedisVersion = parseVersion(resolveJedisVersion());
/**
* @return current {@link redis.clients.jedis.Jedis} version.
*/
public static Version jedisVersion() {
return jedisVersion;
}
/**
* Parse version string {@literal eg. 1.1.1} to {@link Version}.
*
* @param version
* @return
*/
static Version parseVersion(String version) {
return VersionParser.parseVersion(version);
}
/**
* @return true if used jedis version is at minimum {@literal 2.4}.
*/
public static boolean atLeastJedis24() {
return atLeast("2.4");
}
private static String resolveJedisVersion() {
String version = Jedis.class.getPackage().getImplementationVersion();
if (!StringUtils.hasText(version)) {
try {
Properties props = PropertiesLoaderUtils.loadAllProperties("META-INF/maven/redis.clients/jedis/pom.properties");
if (props.containsKey("version")) {
version = props.getProperty("version");
}
} catch (IOException e) {
// ignore this one
}
}
return version;
}
/**
* Compares given version string against current jedis version.
*
* @param version
* @return true in case given version is greater than equal to current one.
*/
public static boolean atLeast(String version) {
return jedisVersion.compareTo(parseVersion(version)) >= 0;
}
/**
* Compares given version string against current jedis version.
*
* @param version
* @return true in case given version is less than equal to current one.
*/
public static boolean atMost(String version) {
return jedisVersion.compareTo(parseVersion(version)) <= 0;
}
}