add support for tera and peta units
This commit is contained in:
parent
bec6527312
commit
7ae4d101ab
|
@ -49,6 +49,16 @@ public enum ByteSizeUnit {
|
|||
public long toGB(long size) {
|
||||
return size / (C3 / C0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTB(long size) {
|
||||
return size / (C4 / C0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPB(long size) {
|
||||
return size / (C5 / C0);
|
||||
}
|
||||
},
|
||||
KB {
|
||||
@Override
|
||||
|
@ -70,6 +80,16 @@ public enum ByteSizeUnit {
|
|||
public long toGB(long size) {
|
||||
return size / (C3 / C1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTB(long size) {
|
||||
return size / (C4 / C1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPB(long size) {
|
||||
return size / (C5 / C1);
|
||||
}
|
||||
},
|
||||
MB {
|
||||
@Override
|
||||
|
@ -91,6 +111,16 @@ public enum ByteSizeUnit {
|
|||
public long toGB(long size) {
|
||||
return size / (C3 / C2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTB(long size) {
|
||||
return size / (C4 / C2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPB(long size) {
|
||||
return size / (C5 / C2);
|
||||
}
|
||||
},
|
||||
GB {
|
||||
@Override
|
||||
|
@ -112,12 +142,86 @@ public enum ByteSizeUnit {
|
|||
public long toGB(long size) {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTB(long size) {
|
||||
return size / (C4 / C3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPB(long size) {
|
||||
return size / (C5 / C3);
|
||||
}
|
||||
},
|
||||
TB {
|
||||
@Override
|
||||
public long toBytes(long size) {
|
||||
return x(size, C4 / C0, MAX / (C4 / C0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toKB(long size) {
|
||||
return x(size, C4 / C1, MAX / (C4 / C1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toMB(long size) {
|
||||
return x(size, C4 / C2, MAX / (C4 / C2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toGB(long size) {
|
||||
return x(size, C4 / C3, MAX / (C4 / C3));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTB(long size) {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPB(long size) {
|
||||
return size / (C5 / C4);
|
||||
}
|
||||
},
|
||||
PB {
|
||||
@Override
|
||||
public long toBytes(long size) {
|
||||
return x(size, C5 / C0, MAX / (C5 / C0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toKB(long size) {
|
||||
return x(size, C5 / C1, MAX / (C5 / C1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toMB(long size) {
|
||||
return x(size, C5 / C2, MAX / (C5 / C2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toGB(long size) {
|
||||
return x(size, C5 / C3, MAX / (C5 / C3));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTB(long size) {
|
||||
return x(size, C5 / C4, MAX / (C5 / C4));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPB(long size) {
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
static final long C0 = 1L;
|
||||
static final long C1 = C0 * 1024L;
|
||||
static final long C2 = C1 * 1024L;
|
||||
static final long C3 = C2 * 1024L;
|
||||
static final long C4 = C3 * 1024L;
|
||||
static final long C5 = C4 * 1024L;
|
||||
|
||||
static final long MAX = Long.MAX_VALUE;
|
||||
|
||||
|
@ -132,19 +236,15 @@ public enum ByteSizeUnit {
|
|||
}
|
||||
|
||||
|
||||
public long toBytes(long size) {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
public abstract long toBytes(long size);
|
||||
|
||||
public long toKB(long size) {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
public abstract long toKB(long size);
|
||||
|
||||
public long toMB(long size) {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
public abstract long toMB(long size);
|
||||
|
||||
public long toGB(long size) {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
}
|
||||
public abstract long toGB(long size);
|
||||
|
||||
public abstract long toTB(long size);
|
||||
|
||||
public abstract long toPB(long size);
|
||||
}
|
|
@ -92,6 +92,22 @@ public class ByteSizeValue implements Serializable, Streamable {
|
|||
return gb();
|
||||
}
|
||||
|
||||
public long tb() {
|
||||
return sizeUnit.toTB(size);
|
||||
}
|
||||
|
||||
public long getTb() {
|
||||
return tb();
|
||||
}
|
||||
|
||||
public long pb() {
|
||||
return sizeUnit.toPB(size);
|
||||
}
|
||||
|
||||
public long getPb() {
|
||||
return pb();
|
||||
}
|
||||
|
||||
public double kbFrac() {
|
||||
return ((double) bytes()) / ByteSizeUnit.C1;
|
||||
}
|
||||
|
@ -116,12 +132,34 @@ public class ByteSizeValue implements Serializable, Streamable {
|
|||
return gbFrac();
|
||||
}
|
||||
|
||||
public double tbFrac() {
|
||||
return ((double) bytes()) / ByteSizeUnit.C4;
|
||||
}
|
||||
|
||||
public double getTbFrac() {
|
||||
return tbFrac();
|
||||
}
|
||||
|
||||
public double pbFrac() {
|
||||
return ((double) bytes()) / ByteSizeUnit.C5;
|
||||
}
|
||||
|
||||
public double getPbFrac() {
|
||||
return pbFrac();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
long bytes = bytes();
|
||||
double value = bytes;
|
||||
String suffix = "b";
|
||||
if (bytes >= ByteSizeUnit.C3) {
|
||||
if (bytes >= ByteSizeUnit.C5) {
|
||||
value = pbFrac();
|
||||
suffix = "pb";
|
||||
} else if (bytes >= ByteSizeUnit.C4) {
|
||||
value = tbFrac();
|
||||
suffix = "tb";
|
||||
} else if (bytes >= ByteSizeUnit.C3) {
|
||||
value = gbFrac();
|
||||
suffix = "gb";
|
||||
} else if (bytes >= ByteSizeUnit.C2) {
|
||||
|
@ -157,6 +195,14 @@ public class ByteSizeValue implements Serializable, Streamable {
|
|||
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C3);
|
||||
} else if (lastTwoChars.endsWith("gb")) {
|
||||
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C3);
|
||||
} else if (lastTwoChars.endsWith("t")) {
|
||||
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C4);
|
||||
} else if (lastTwoChars.endsWith("tb")) {
|
||||
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C4);
|
||||
} else if (lastTwoChars.endsWith("p")) {
|
||||
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * ByteSizeUnit.C5);
|
||||
} else if (lastTwoChars.endsWith("pb")) {
|
||||
bytes = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 2)) * ByteSizeUnit.C5);
|
||||
} else if (lastTwoChars.endsWith("b")) {
|
||||
bytes = Long.parseLong(sValue.substring(0, sValue.length() - 1));
|
||||
} else {
|
||||
|
|
|
@ -43,6 +43,16 @@ public enum SizeUnit {
|
|||
public long toGiga(long size) {
|
||||
return size / (C3 / C0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTera(long size) {
|
||||
return size / (C4 / C0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPeta(long size) {
|
||||
return size / (C5 / C0);
|
||||
}
|
||||
},
|
||||
KILO {
|
||||
@Override
|
||||
|
@ -64,6 +74,16 @@ public enum SizeUnit {
|
|||
public long toGiga(long size) {
|
||||
return size / (C3 / C1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTera(long size) {
|
||||
return size / (C4 / C1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPeta(long size) {
|
||||
return size / (C5 / C1);
|
||||
}
|
||||
},
|
||||
MEGA {
|
||||
@Override
|
||||
|
@ -85,6 +105,16 @@ public enum SizeUnit {
|
|||
public long toGiga(long size) {
|
||||
return size / (C3 / C2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTera(long size) {
|
||||
return size / (C4 / C2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPeta(long size) {
|
||||
return size / (C5 / C2);
|
||||
}
|
||||
},
|
||||
GIGA {
|
||||
@Override
|
||||
|
@ -106,12 +136,86 @@ public enum SizeUnit {
|
|||
public long toGiga(long size) {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTera(long size) {
|
||||
return size / (C4 / C3);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPeta(long size) {
|
||||
return size / (C5 / C3);
|
||||
}
|
||||
},
|
||||
TERA {
|
||||
@Override
|
||||
public long toSingles(long size) {
|
||||
return x(size, C4 / C0, MAX / (C4 / C0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toKilo(long size) {
|
||||
return x(size, C4 / C1, MAX / (C4 / C1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toMega(long size) {
|
||||
return x(size, C4 / C2, MAX / (C4 / C2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toGiga(long size) {
|
||||
return x(size, C4 / C3, MAX / (C4 / C3));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTera(long size) {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPeta(long size) {
|
||||
return size / (C5 / C0);
|
||||
}
|
||||
},
|
||||
PETA {
|
||||
@Override
|
||||
public long toSingles(long size) {
|
||||
return x(size, C5 / C0, MAX / (C5 / C0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toKilo(long size) {
|
||||
return x(size, C5 / C1, MAX / (C5 / C1));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toMega(long size) {
|
||||
return x(size, C5 / C2, MAX / (C5 / C2));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toGiga(long size) {
|
||||
return x(size, C5 / C3, MAX / (C5 / C3));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toTera(long size) {
|
||||
return x(size, C5 / C4, MAX / (C5 / C4));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long toPeta(long size) {
|
||||
return size;
|
||||
}
|
||||
};
|
||||
|
||||
static final long C0 = 1L;
|
||||
static final long C1 = C0 * 1000L;
|
||||
static final long C2 = C1 * 1000L;
|
||||
static final long C3 = C2 * 1000L;
|
||||
static final long C4 = C3 * 1000L;
|
||||
static final long C5 = C4 * 1000L;
|
||||
|
||||
static final long MAX = Long.MAX_VALUE;
|
||||
|
||||
|
@ -126,19 +230,15 @@ public enum SizeUnit {
|
|||
}
|
||||
|
||||
|
||||
public long toSingles(long size) {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
public abstract long toSingles(long size);
|
||||
|
||||
public long toKilo(long size) {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
public abstract long toKilo(long size);
|
||||
|
||||
public long toMega(long size) {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
public abstract long toMega(long size);
|
||||
|
||||
public long toGiga(long size) {
|
||||
throw new AbstractMethodError();
|
||||
}
|
||||
public abstract long toGiga(long size);
|
||||
|
||||
public abstract long toTera(long size);
|
||||
|
||||
public abstract long toPeta(long size);
|
||||
}
|
|
@ -82,6 +82,22 @@ public class SizeValue implements Serializable, Streamable {
|
|||
return giga();
|
||||
}
|
||||
|
||||
public long tera() {
|
||||
return sizeUnit.toTera(size);
|
||||
}
|
||||
|
||||
public long getTera() {
|
||||
return tera();
|
||||
}
|
||||
|
||||
public long peta() {
|
||||
return sizeUnit.toPeta(size);
|
||||
}
|
||||
|
||||
public long getPeta() {
|
||||
return peta();
|
||||
}
|
||||
|
||||
public double kiloFrac() {
|
||||
return ((double) singles()) / SizeUnit.C1;
|
||||
}
|
||||
|
@ -106,12 +122,34 @@ public class SizeValue implements Serializable, Streamable {
|
|||
return gigaFrac();
|
||||
}
|
||||
|
||||
public double teraFrac() {
|
||||
return ((double) singles()) / SizeUnit.C4;
|
||||
}
|
||||
|
||||
public double getTeraFrac() {
|
||||
return teraFrac();
|
||||
}
|
||||
|
||||
public double petaFrac() {
|
||||
return ((double) singles()) / SizeUnit.C5;
|
||||
}
|
||||
|
||||
public double getPetaFrac() {
|
||||
return petaFrac();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
long singles = singles();
|
||||
double value = singles;
|
||||
String suffix = "";
|
||||
if (singles >= SizeUnit.C3) {
|
||||
if (singles >= SizeUnit.C5) {
|
||||
value = petaFrac();
|
||||
suffix = "p";
|
||||
} else if (singles >= SizeUnit.C4) {
|
||||
value = teraFrac();
|
||||
suffix = "t";
|
||||
} else if (singles >= SizeUnit.C3) {
|
||||
value = gigaFrac();
|
||||
suffix = "g";
|
||||
} else if (singles >= SizeUnit.C2) {
|
||||
|
@ -121,6 +159,7 @@ public class SizeValue implements Serializable, Streamable {
|
|||
value = kiloFrac();
|
||||
suffix = "k";
|
||||
}
|
||||
|
||||
return Strings.format1Decimals(value, suffix);
|
||||
}
|
||||
|
||||
|
@ -142,6 +181,10 @@ public class SizeValue implements Serializable, Streamable {
|
|||
singles = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * SizeUnit.C2);
|
||||
} else if (sValue.endsWith("g") || sValue.endsWith("G")) {
|
||||
singles = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * SizeUnit.C3);
|
||||
} else if (sValue.endsWith("t") || sValue.endsWith("T")) {
|
||||
singles = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * SizeUnit.C4);
|
||||
} else if (sValue.endsWith("p") || sValue.endsWith("P")) {
|
||||
singles = (long) (Double.parseDouble(sValue.substring(0, sValue.length() - 1)) * SizeUnit.C5);
|
||||
} else {
|
||||
singles = Long.parseLong(sValue);
|
||||
}
|
||||
|
|
|
@ -23,7 +23,6 @@ import org.elasticsearch.test.ElasticsearchTestCase;
|
|||
import org.junit.Test;
|
||||
|
||||
import static org.elasticsearch.common.unit.ByteSizeUnit.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
|
@ -62,4 +61,23 @@ public class ByteSizeUnitTests extends ElasticsearchTestCase {
|
|||
assertThat(GB.toMB(1), equalTo(1024l));
|
||||
assertThat(GB.toGB(1), equalTo(1l));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTB() {
|
||||
assertThat(TB.toBytes(1), equalTo(1024l * 1024 * 1024 * 1024));
|
||||
assertThat(TB.toKB(1), equalTo(1024l * 1024 * 1024));
|
||||
assertThat(TB.toMB(1), equalTo(1024l * 1024));
|
||||
assertThat(TB.toGB(1), equalTo(1024l));
|
||||
assertThat(TB.toTB(1), equalTo(1l));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPB() {
|
||||
assertThat(PB.toBytes(1), equalTo(1024l * 1024 * 1024 * 1024 * 1024));
|
||||
assertThat(PB.toKB(1), equalTo(1024l * 1024 * 1024 * 1024));
|
||||
assertThat(PB.toMB(1), equalTo(1024l * 1024 * 1024));
|
||||
assertThat(PB.toGB(1), equalTo(1024l * 1024));
|
||||
assertThat(PB.toTB(1), equalTo(1024l));
|
||||
assertThat(PB.toPB(1), equalTo(1l));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ import org.elasticsearch.test.ElasticsearchTestCase;
|
|||
import org.hamcrest.MatcherAssert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
|
||||
|
@ -33,6 +32,16 @@ import static org.hamcrest.Matchers.is;
|
|||
*/
|
||||
public class ByteSizeValueTests extends ElasticsearchTestCase {
|
||||
|
||||
@Test
|
||||
public void testActualPeta() {
|
||||
MatcherAssert.assertThat(new ByteSizeValue(4, ByteSizeUnit.PB).bytes(), equalTo(4503599627370496l));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActualTera() {
|
||||
MatcherAssert.assertThat(new ByteSizeValue(4, ByteSizeUnit.TB).bytes(), equalTo(4398046511104l));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActual() {
|
||||
MatcherAssert.assertThat(new ByteSizeValue(4, ByteSizeUnit.GB).bytes(), equalTo(4294967296l));
|
||||
|
@ -44,6 +53,8 @@ public class ByteSizeValueTests extends ElasticsearchTestCase {
|
|||
assertThat(ByteSizeUnit.KB.toKB(10), is(new ByteSizeValue(10, ByteSizeUnit.KB).kb()));
|
||||
assertThat(ByteSizeUnit.MB.toMB(10), is(new ByteSizeValue(10, ByteSizeUnit.MB).mb()));
|
||||
assertThat(ByteSizeUnit.GB.toGB(10), is(new ByteSizeValue(10, ByteSizeUnit.GB).gb()));
|
||||
assertThat(ByteSizeUnit.TB.toTB(10), is(new ByteSizeValue(10, ByteSizeUnit.TB).tb()));
|
||||
assertThat(ByteSizeUnit.PB.toPB(10), is(new ByteSizeValue(10, ByteSizeUnit.PB).pb()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -52,11 +63,19 @@ public class ByteSizeValueTests extends ElasticsearchTestCase {
|
|||
assertThat("1.5kb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.BYTES).toString()));
|
||||
assertThat("1.5mb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.KB).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("1.5tb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.GB).toString()));
|
||||
assertThat("1.5pb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.TB).toString()));
|
||||
assertThat("1536pb", is(new ByteSizeValue((long) (1024 * 1.5), ByteSizeUnit.PB).toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsing() {
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("42pb").toString(), is("42pb"));
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("42P").toString(), is("42pb"));
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("42PB").toString(), is("42pb"));
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("54tb").toString(), is("54tb"));
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("54T").toString(), is("54tb"));
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("54TB").toString(), is("54tb"));
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("12gb").toString(), is("12gb"));
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("12G").toString(), is("12gb"));
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("12GB").toString(), is("12gb"));
|
||||
|
@ -76,4 +95,4 @@ public class ByteSizeValueTests extends ElasticsearchTestCase {
|
|||
public void testFailOnEmptyNumberParsing() {
|
||||
assertThat(ByteSizeValue.parseBytesSizeValue("g").toString(), is("23b"));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue