Made parsing of ByteSizeValue case independent

This allows to parse '12GB' as well as '12gb'

Closes #4442
This commit is contained in:
Alexander Reelsen 2014-01-02 13:00:41 +01:00
parent f4bf0d5112
commit 8d4be46e59
2 changed files with 32 additions and 7 deletions

View File

@ -28,6 +28,7 @@ import org.elasticsearch.common.io.stream.Streamable;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.Locale;
/** /**
* *
@ -143,19 +144,20 @@ public class ByteSizeValue implements Serializable, Streamable {
} }
long bytes; long bytes;
try { try {
if (sValue.endsWith("k") || sValue.endsWith("K")) { String lastTwoChars = sValue.substring(sValue.length() - Math.min(2, sValue.length())).toLowerCase(Locale.ROOT);
if (lastTwoChars.endsWith("k")) {
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C1); bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C1);
} else if (sValue.endsWith("kb")) { } else if (lastTwoChars.endsWith("kb")) {
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C1); bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C1);
} else if (sValue.endsWith("m") || sValue.endsWith("M")) { } else if (lastTwoChars.endsWith("m")) {
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C2); bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C2);
} else if (sValue.endsWith("mb")) { } else if (lastTwoChars.endsWith("mb")) {
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C2); bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C2);
} else if (sValue.endsWith("g") || sValue.endsWith("G")) { } else if (lastTwoChars.endsWith("g")) {
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C3); bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C3);
} else if (sValue.endsWith("gb")) { } else if (lastTwoChars.endsWith("gb")) {
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C3); bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C3);
} else if (sValue.endsWith("b")) { } else if (lastTwoChars.endsWith("b")) {
bytes = Long.parseLong(sValue.substring(0, sValue.length() - 1)); bytes = Long.parseLong(sValue.substring(0, sValue.length() - 1));
} else { } else {
bytes = Long.parseLong(sValue); bytes = Long.parseLong(sValue);

View File

@ -19,6 +19,7 @@
package org.elasticsearch.common.unit; package org.elasticsearch.common.unit;
import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.test.ElasticsearchTestCase; import org.elasticsearch.test.ElasticsearchTestCase;
import org.hamcrest.MatcherAssert; import org.hamcrest.MatcherAssert;
import org.junit.Test; import org.junit.Test;
@ -53,4 +54,26 @@ public class ByteSizeValueTests extends ElasticsearchTestCase {
assertThat("1.5gb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.MB).toString())); assertThat("1.5gb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.MB).toString()));
assertThat("1536gb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.GB).toString())); assertThat("1536gb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.GB).toString()));
} }
@Test
public void testParsing() {
assertThat(ByteSizeValue.parseBytesSizeValue("12gb").toString(), is("12gb"));
assertThat(ByteSizeValue.parseBytesSizeValue("12G").toString(), is("12gb"));
assertThat(ByteSizeValue.parseBytesSizeValue("12GB").toString(), is("12gb"));
assertThat(ByteSizeValue.parseBytesSizeValue("12M").toString(), is("12mb"));
assertThat(ByteSizeValue.parseBytesSizeValue("1b").toString(), is("1b"));
assertThat(ByteSizeValue.parseBytesSizeValue("23kb").toString(), is("23kb"));
assertThat(ByteSizeValue.parseBytesSizeValue("23k").toString(), is("23kb"));
assertThat(ByteSizeValue.parseBytesSizeValue("23").toString(), is("23b"));
}
@Test(expected = ElasticSearchParseException.class)
public void testFailOnEmptyParsing() {
assertThat(ByteSizeValue.parseBytesSizeValue("").toString(), is("23kb"));
}
@Test(expected = ElasticSearchParseException.class)
public void testFailOnEmptyNumberParsing() {
assertThat(ByteSizeValue.parseBytesSizeValue("g").toString(), is("23b"));
}
} }