HBASE-20444 Improve comparison logic for HBase version strings
Signed-off-by: Sean Busbey <busbey@apache.org>
This commit is contained in:
parent
997747076d
commit
b77fb77b32
|
@ -21,6 +21,7 @@ package org.apache.hadoop.hbase.util;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.hadoop.hbase.Version;
|
import org.apache.hadoop.hbase.Version;
|
||||||
import org.apache.yetus.audience.InterfaceAudience;
|
import org.apache.yetus.audience.InterfaceAudience;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -117,50 +118,49 @@ public class VersionInfo {
|
||||||
if (v1.equals(v2)) {
|
if (v1.equals(v2)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
String[] v1Comps = getVersionComponents(v1);
|
||||||
|
String[] v2Comps = getVersionComponents(v2);
|
||||||
|
|
||||||
Object[] v1Comps = getVersionComponents(v1); //1.2.3-hotfix -> [1, 2, 3, hotfix]
|
int length = Math.max(v1Comps.length, v2Comps.length);
|
||||||
Object[] v2Comps = getVersionComponents(v2);
|
for (int i = 0; i < length; i++) {
|
||||||
int index = 0;
|
Integer va = i < v1Comps.length ? Integer.parseInt(v1Comps[i]) : 0;
|
||||||
while (index < v1Comps.length && index < v2Comps.length) {
|
Integer vb = i < v2Comps.length ? Integer.parseInt(v2Comps[i]) : 0;
|
||||||
int va = v1Comps[index] instanceof Integer ? (Integer)v1Comps[index] : VERY_LARGE_NUMBER;
|
int compare = va.compareTo(vb);
|
||||||
int vb = v2Comps[index] instanceof Integer ? (Integer)v2Comps[index] : VERY_LARGE_NUMBER;
|
if (compare != 0) {
|
||||||
|
return compare;
|
||||||
if (va != vb) {
|
|
||||||
return va - vb;
|
|
||||||
}
|
|
||||||
if (va == VERY_LARGE_NUMBER) {
|
|
||||||
// here, va and vb components must be same and Strings, compare as String
|
|
||||||
int c = ((String)v1Comps[index]).compareTo((String)v2Comps[index]);
|
|
||||||
if (c != 0) {
|
|
||||||
return c;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
index++;
|
return 0;
|
||||||
}
|
|
||||||
if (index < v1Comps.length) {
|
|
||||||
// v1 is longer
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
//v2 is longer
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the version components as Integer and String objects
|
* Returns the version components as String objects
|
||||||
* Examples: "1.2.3" returns [1, 2, 3], "4.5.6-SNAPSHOT" returns [4, 5, 6, "SNAPSHOT"]
|
* Examples: "1.2.3" returns ["1", "2", "3"], "4.5.6-SNAPSHOT" returns ["4", "5", "6", "-1"]
|
||||||
|
* "4.5.6-beta" returns ["4", "5", "6", "-2"], "4.5.6-alpha" returns ["4", "5", "6", "-3"]
|
||||||
|
* "4.5.6-UNKNOW" returns ["4", "5", "6", "-4"]
|
||||||
* @return the components of the version string
|
* @return the components of the version string
|
||||||
*/
|
*/
|
||||||
static Object[] getVersionComponents(final String version) {
|
static String[] getVersionComponents(final String version) {
|
||||||
assert(version != null);
|
assert(version != null);
|
||||||
Object[] strComps = version.split("[\\.-]");
|
String[] strComps = version.split("[\\.-]");
|
||||||
assert(strComps.length > 0);
|
assert(strComps.length > 0);
|
||||||
|
|
||||||
Object[] comps = new Object[strComps.length];
|
String[] comps = new String[strComps.length];
|
||||||
for (int i = 0; i < strComps.length; ++i) {
|
for (int i = 0; i < strComps.length; ++i) {
|
||||||
try {
|
if (StringUtils.isNumeric(strComps[i])) {
|
||||||
comps[i] = Integer.parseInt((String) strComps[i]);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
comps[i] = strComps[i];
|
comps[i] = strComps[i];
|
||||||
|
} else if (StringUtils.isEmpty(strComps[i])) {
|
||||||
|
comps[i] = String.valueOf(VERY_LARGE_NUMBER);
|
||||||
|
} else {
|
||||||
|
if("SNAPSHOT".equals(strComps[i])) {
|
||||||
|
comps[i] = "-1";
|
||||||
|
} else if("beta".equals(strComps[i])) {
|
||||||
|
comps[i] = "-2";
|
||||||
|
} else if("alpha".equals(strComps[i])) {
|
||||||
|
comps[i] = "-3";
|
||||||
|
} else {
|
||||||
|
comps[i] = "-4";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return comps;
|
return comps;
|
||||||
|
|
|
@ -37,6 +37,24 @@ public class TestVersionInfo {
|
||||||
assertTrue(VersionInfo.compareVersion("1.0.0", "0.98.11") > 0);
|
assertTrue(VersionInfo.compareVersion("1.0.0", "0.98.11") > 0);
|
||||||
assertTrue(VersionInfo.compareVersion("0.98.11", "1.0.1") < 0);
|
assertTrue(VersionInfo.compareVersion("0.98.11", "1.0.1") < 0);
|
||||||
assertTrue(VersionInfo.compareVersion("2.0.0", "1.4.0") > 0);
|
assertTrue(VersionInfo.compareVersion("2.0.0", "1.4.0") > 0);
|
||||||
assertTrue(VersionInfo.compareVersion("2.0.0", "2.0.0-SNAPSHOT") < 0);
|
assertTrue(VersionInfo.compareVersion("2.0.0", "2.0.0-SNAPSHOT") > 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("0.94.6.1", "0.96.1.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("0.96.1.1", "0.98.6.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("0.98.6.1", "0.98.10.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("0.98.10.1", "0.98.12.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("0.98.12.1", "0.98.16.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("0.98.16.1", "1.0.1.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("1.0.1.1", "1.1.0.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("2.0..1", "2.0.0") > 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("2.0.0", "2.0.0") == 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("1.99.14", "2.0.0-alpha-1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("2.0.0-alpha-1", "2.0.0-beta-3") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("2.0.0-beta-3", "2.0.0-SNAPSHOT") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("2.0.0-SNAPSHOT", "2.0") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("2.0", "2.0.0.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("2.0.0.1", "2.0.1") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("3.0.0-alpha-2", "3.0.0-alpha-11") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("3.0.0-beta-2", "3.0.0-beta-11") < 0);
|
||||||
|
assertTrue(VersionInfo.compareVersion("2.0.0-foobar", "2.0.0.1") < 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue