Skip to content

Commit b222fc0

Browse files
committed
Put back ByteSizeValue.toString
1 parent 68458c3 commit b222fc0

File tree

2 files changed

+20
-66
lines changed

2 files changed

+20
-66
lines changed

server/src/main/java/org/elasticsearch/common/unit/ByteSizeValue.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010
package org.elasticsearch.common.unit;
1111

1212
import org.elasticsearch.ElasticsearchParseException;
13+
import org.elasticsearch.common.Strings;
1314
import org.elasticsearch.common.io.stream.StreamInput;
1415
import org.elasticsearch.common.io.stream.StreamOutput;
1516
import org.elasticsearch.common.io.stream.Writeable;
1617
import org.elasticsearch.common.logging.DeprecationLogger;
17-
import org.elasticsearch.core.SuppressForbidden;
1818
import org.elasticsearch.xcontent.ToXContentFragment;
1919
import org.elasticsearch.xcontent.XContentBuilder;
2020

2121
import java.io.IOException;
2222
import java.math.BigDecimal;
23-
import java.text.DecimalFormat;
2423
import java.util.Locale;
2524
import java.util.Objects;
2625

@@ -233,33 +232,26 @@ public String getStringRep() {
233232

234233
@Override
235234
public String toString() {
236-
return format2Decimals(sizeInBytes, preferredUnit.toBytes(1)) + preferredUnit.getSuffix();
237-
}
238-
239-
/**
240-
* Strictly speaking, this is only <em>guaranteed</em> to return the exact right string
241-
* when {@code numerator} is the rounded value of some multiple of 1% of {@code denominator}.
242-
* Other arbitrary values could yield surprising results; for example, you might see
243-
* {@code 123.0} or even {@code 1221.00} in place of {@code 123}.
244-
*/
245-
static String format2Decimals(long numerator, long denominator) {
246-
// If numerator >> denominator, we risk exceeding the precision of a double, so we enlist
247-
// the help of MAX_TWO_DECIMALS only for the fractional portion, and we handle the whole-number
248-
// portion ourselves.
249-
long wholeNumberPortion = numerator / denominator;
250-
long remainder = numerator % denominator;
251-
if (remainder == 0) {
252-
return Long.toString(wholeNumberPortion);
253-
} else {
254-
return wholeNumberPortion + MAX_TWO_DECIMALS.format(remainder / (double) denominator);
235+
long bytes = getBytes();
236+
double value = bytes;
237+
String suffix = ByteSizeUnit.BYTES.getSuffix();
238+
if (bytes >= ByteSizeUnit.C5) {
239+
value = getPbFrac();
240+
suffix = ByteSizeUnit.PB.getSuffix();
241+
} else if (bytes >= ByteSizeUnit.C4) {
242+
value = getTbFrac();
243+
suffix = ByteSizeUnit.TB.getSuffix();
244+
} else if (bytes >= ByteSizeUnit.C3) {
245+
value = getGbFrac();
246+
suffix = ByteSizeUnit.GB.getSuffix();
247+
} else if (bytes >= ByteSizeUnit.C2) {
248+
value = getMbFrac();
249+
suffix = ByteSizeUnit.MB.getSuffix();
250+
} else if (bytes >= ByteSizeUnit.C1) {
251+
value = getKbFrac();
252+
suffix = ByteSizeUnit.KB.getSuffix();
255253
}
256-
}
257-
258-
private static final DecimalFormat MAX_TWO_DECIMALS = maxTwoDecimalsFormat();
259-
260-
@SuppressForbidden(reason = "We truly just want two decimals; Locale settings don't matter here")
261-
private static DecimalFormat maxTwoDecimalsFormat() {
262-
return new DecimalFormat(".##");
254+
return Strings.format1Decimals(value, suffix);
263255
}
264256

265257
public static ByteSizeValue parseBytesSizeValue(String sValue, String settingName) throws ElasticsearchParseException {

server/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static org.elasticsearch.common.unit.ByteSizeUnit.MB;
2626
import static org.elasticsearch.common.unit.ByteSizeUnit.PB;
2727
import static org.elasticsearch.common.unit.ByteSizeUnit.TB;
28-
import static org.elasticsearch.common.unit.ByteSizeValue.format2Decimals;
2928
import static org.hamcrest.Matchers.containsString;
3029
import static org.hamcrest.Matchers.equalTo;
3130
import static org.hamcrest.Matchers.is;
@@ -518,43 +517,6 @@ public void testParseFractionsExhaustively() {
518517
}
519518
}
520519

521-
public void testFormat2DecimalsExhaustively() {
522-
for (var unit : ByteSizeUnit.values()) {
523-
if (unit == BYTES) {
524-
// Can't specify fractions of a byte
525-
continue;
526-
}
527-
var granularity = unit.toBytes(1);
528-
long wholeUnits = Long.MAX_VALUE / granularity - 1; // Try to provoke a failure by exceeding double precision
529-
assertEquals(
530-
"Integers should not have decimals",
531-
Long.toString(wholeUnits),
532-
format2Decimals(wholeUnits * granularity, granularity)
533-
);
534-
for (var percent = 1; percent < 100; percent++) {
535-
long numerator = wholeUnits * granularity + (long) (percent * 0.01 * granularity);
536-
String expected = wholeUnits + percentToDecimal(percent);
537-
assertEquals(
538-
"format2Decimals on " + percent + "% of one " + unit.getSuffix(),
539-
expected,
540-
format2Decimals(numerator, granularity)
541-
);
542-
}
543-
}
544-
}
545-
546-
public void testFormat2DecimalsForHugeValues() {
547-
// The largest magnitude we can get with fractions is if we specify a value very close to Long.MAX_VALUE bytes using KB.
548-
long granularity = KB.toBytes(1);
549-
long wholeUnits = Long.MAX_VALUE / granularity;
550-
for (var percent = 1; percent < 100; percent++) {
551-
long fractionalPart = (long) (0.01 * percent * granularity);
552-
long numerator = wholeUnits * granularity + fractionalPart;
553-
String expected = wholeUnits + percentToDecimal(percent);
554-
assertEquals("String should match for " + percent + "%", expected, format2Decimals(numerator, granularity));
555-
}
556-
}
557-
558520
private static String percentToDecimal(int percent) {
559521
if (percent <= 9) {
560522
return ".0" + percent;

0 commit comments

Comments
 (0)