improve LongEncoding.decodeSortable() (#34)
Change-Id: I7962ffe9f55a2ef33752a414262e89d5a7918cc1
diff --git a/pom.xml b/pom.xml
index fc6ac29..0bb4694 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
- <version>1.6.10</version>
+ <version>1.6.11</version>
<name>hugegraph-common</name>
<url>https://github.com/hugegraph/hugegraph-common</url>
@@ -212,7 +212,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
- <Implementation-Version>1.6.10.0</Implementation-Version>
+ <Implementation-Version>1.6.11.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
diff --git a/src/main/java/com/baidu/hugegraph/util/LongEncoding.java b/src/main/java/com/baidu/hugegraph/util/LongEncoding.java
index 87b1366..4456cf5 100644
--- a/src/main/java/com/baidu/hugegraph/util/LongEncoding.java
+++ b/src/main/java/com/baidu/hugegraph/util/LongEncoding.java
@@ -70,11 +70,15 @@
E.checkArgument(str.length() >= 2,
"Length of sortable encoded string must be >=2");
boolean negative = str.charAt(0) == NEG;
- int prefix = 1;
+ int lengthPos = 0;
if (negative) {
- prefix = 2;
+ lengthPos = 1;
}
- String encoded = str.substring(prefix);
+ int length = BASE_SYMBOLS.indexOf(str.charAt(lengthPos));
+ E.checkArgument(length == str.length() - lengthPos - 1,
+ "Can't decode illegal string '%s' with wrong length",
+ str);
+ String encoded = str.substring(lengthPos + 1);
long value = decode(encoded);
if (negative) {
value -= FULL_LONG;
@@ -82,6 +86,10 @@
return value;
}
+ public static boolean validSortableChar(char c) {
+ return BASE_SYMBOLS.indexOf(c) != -1;
+ }
+
public static String encode(long num) {
return encode(num, BASE_SYMBOLS);
}
diff --git a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java
index a54b418..1a8bdde 100644
--- a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java
+++ b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java
@@ -27,5 +27,5 @@
// The second parameter of Version.of() is for all-in-one JAR
public static final Version VERSION = Version.of(CommonVersion.class,
- "1.6.10");
+ "1.6.11");
}
diff --git a/src/test/java/com/baidu/hugegraph/unit/util/LongEncodingTest.java b/src/test/java/com/baidu/hugegraph/unit/util/LongEncodingTest.java
index 13ca425..3004d85 100644
--- a/src/test/java/com/baidu/hugegraph/unit/util/LongEncodingTest.java
+++ b/src/test/java/com/baidu/hugegraph/unit/util/LongEncodingTest.java
@@ -279,6 +279,29 @@
}
@Test
+ public void testDecodeIllegalSortable() {
+ // Length is 1, actual length is 0
+ Assert.assertThrows(IllegalArgumentException.class, () -> {
+ LongEncoding.decodeSortable("1");
+ });
+
+ // Length is 1, actual length is 2
+ Assert.assertThrows(IllegalArgumentException.class, () -> {
+ LongEncoding.decodeSortable("123");
+ });
+
+ // Length is 1, actual length is 0
+ Assert.assertThrows(IllegalArgumentException.class, () -> {
+ LongEncoding.decodeSortable("01");
+ });
+
+ // Length is 1, actual length is 2
+ Assert.assertThrows(IllegalArgumentException.class, () -> {
+ LongEncoding.decodeSortable("0123");
+ });
+ }
+
+ @Test
public void testEncodeNumber() throws ParseException {
String l1234 = LongEncoding.encodeNumber(1234);
Assert.assertEquals("2JI", l1234);