Skip to content

Commit

Permalink
Made parsing of ByteSizeValue case independent
Browse files Browse the repository at this point in the history
This allows to parse '12GB' as well as '12gb'

Closes elastic#4442
  • Loading branch information
spinscale committed Jan 2, 2014
1 parent 5a70e4d commit c223e18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.io.IOException;
import java.io.Serializable;
import java.util.Locale;

/**
*
Expand Down Expand Up @@ -143,19 +144,20 @@ public static ByteSizeValue parseBytesSizeValue(String sValue, ByteSizeValue def
}
long bytes;
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);
} else if (sValue.endsWith("kb")) {
} else if (lastTwoChars.endsWith("kb")) {
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);
} else if (sValue.endsWith("mb")) {
} else if (lastTwoChars.endsWith("mb")) {
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);
} else if (sValue.endsWith("gb")) {
} else if (lastTwoChars.endsWith("gb")) {
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));
} else {
bytes = Long.parseLong(sValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.common.unit;

import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
Expand Down Expand Up @@ -53,4 +54,26 @@ public void testToString() {
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()));
}

@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"));
}
}

0 comments on commit c223e18

Please sign in to comment.