Skip to content

Commit

Permalink
add support for tera and peta units
Browse files Browse the repository at this point in the history
  • Loading branch information
costin committed Jan 8, 2014
1 parent b2a9946 commit 8da308b
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 29 deletions.
124 changes: 112 additions & 12 deletions src/main/java/org/elasticsearch/common/unit/ByteSizeUnit.java
Expand Up @@ -49,6 +49,16 @@ public long toMB(long size) {
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
Expand All @@ -70,6 +80,16 @@ public long toMB(long size) {
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
Expand All @@ -91,6 +111,16 @@ public long toMB(long size) {
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
Expand All @@ -112,12 +142,86 @@ public long toMB(long size) {
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;

Expand All @@ -132,19 +236,15 @@ static long x(long d, long m, long over) {
}


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);
}
48 changes: 47 additions & 1 deletion src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java
Expand Up @@ -92,6 +92,22 @@ public long getGb() {
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;
}
Expand All @@ -116,12 +132,34 @@ public double getGbFrac() {
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) {
Expand Down Expand Up @@ -157,6 +195,14 @@ public static ByteSizeValue parseBytesSizeValue(String sValue, ByteSizeValue def
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 {
Expand Down

0 comments on commit 8da308b

Please sign in to comment.