Skip to content

Commit

Permalink
Adds the --ion-use-big-decimals option.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgregg committed Oct 9, 2020
1 parent 4447fa7 commit d37b6fd
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/com/amazon/ion/benchmark/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Constants {
static final String ION_USE_SYMBOL_TOKENS_NAME = "k";
static final String ION_FLOAT_WIDTH_NAME = "W";
static final String ION_USE_LOB_CHUNKS_NAME = "e";
static final String ION_USE_BIG_DECIMALS_NAME = "D";
static final String PATHS_NAME = "s";
static final String ION_WRITER_BLOCK_SIZE_NAME = "b";
static final String AUTO_VALUE = "auto";
Expand Down
6 changes: 5 additions & 1 deletion src/com/amazon/ion/benchmark/IonMeasurableReadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ private void consumeCurrentValue(IonReader reader, boolean isInStruct) {
reader.doubleValue();
break;
case DECIMAL:
reader.decimalValue();
if (options.useBigDecimals) {
reader.bigDecimalValue();
} else {
reader.decimalValue();
}
break;
case TIMESTAMP:
reader.timestampValue();
Expand Down
9 changes: 6 additions & 3 deletions src/com/amazon/ion/benchmark/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Main {
+ "[--ion-api <api>]... [--ion-imports-for-input <file>] [--ion-imports-for-benchmark <file>]... "
+ "[--ion-flush-period <int>]... [--ion-length-preallocation <int>]... [--ion-float-width <int>]... "
+ "[--ion-use-symbol-tokens <bool>]... [--paths <file>] [--ion-reader <type>]... "
+ "[--ion-use-lob-chunks <bool>]... <input_file>\n"
+ "[--ion-use-lob-chunks <bool>]... [--ion-use-big-decimals <bool>]... <input_file>\n"

+ " ion-java-benchmark --help\n"

Expand Down Expand Up @@ -66,8 +66,6 @@ public class Main {
+ "world to ensure the initialization cost is properly amortized.\n"
+ "\n";

// TODO add options for the following:
// TODO read decimals using bigDecimalValue() instead of decimalValue()
private static final String OPTIONS =
"Options:\n"

Expand Down Expand Up @@ -211,6 +209,11 @@ public class Main {
+ "is ion_binary or ion_text and --ion-api streaming is used. May be specified twice to compare both "
+ "settings. [default: false]\n"

+ " -D --ion-use-big-decimals <bool> When true, read Ion decimal values into BigDecimal instances. When "
+ "false, read decimal values into Decimal instances, which are capable of conveying negative zero. "
+ "Ignored unless one of the specified formats is ion_binary or ion_text and --ion-api streaming is used. "
+ "May be specified twice to compare both settings. [default: false]\n"

+ "\n";

private static final String EXAMPLES =
Expand Down
3 changes: 3 additions & 0 deletions src/com/amazon/ion/benchmark/ReadOptionsCombination.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;

import static com.amazon.ion.benchmark.Constants.ION_READER_NAME;
import static com.amazon.ion.benchmark.Constants.ION_USE_BIG_DECIMALS_NAME;
import static com.amazon.ion.benchmark.Constants.ION_USE_LOB_CHUNKS_NAME;
import static com.amazon.ion.benchmark.Constants.PATHS_NAME;

Expand All @@ -24,6 +25,7 @@ class ReadOptionsCombination extends OptionsCombinationBase {
final List<String> paths;
final IonReaderType readerType;
final boolean useLobChunks;
final boolean useBigDecimals;

/**
* @param serializedOptionsCombination text Ion representation of the options combination.
Expand Down Expand Up @@ -52,6 +54,7 @@ class ReadOptionsCombination extends OptionsCombinationBase {
IonReaderType.NON_BLOCKING
);
useLobChunks = getOrDefault(optionsCombinationStruct, ION_USE_LOB_CHUNKS_NAME, val -> ((IonBool) val).booleanValue(), false);
useBigDecimals = getOrDefault(optionsCombinationStruct, ION_USE_BIG_DECIMALS_NAME, val -> ((IonBool) val).booleanValue(), false);
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions src/com/amazon/ion/benchmark/ReadOptionsMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static com.amazon.ion.benchmark.Constants.ION_READER_NAME;
import static com.amazon.ion.benchmark.Constants.ION_SYSTEM;
import static com.amazon.ion.benchmark.Constants.ION_USE_BIG_DECIMALS_NAME;
import static com.amazon.ion.benchmark.Constants.ION_USE_LOB_CHUNKS_NAME;
import static com.amazon.ion.benchmark.Constants.PATHS_NAME;

Expand Down Expand Up @@ -46,6 +47,14 @@ void parseCommandSpecificOptions(Map<String, Object> optionsMatrix, List<IonStru
optionsCombinationStructs,
() -> ION_SYSTEM.newBool(false)
);
parseAndCombine(
optionsMatrix.get("--ion-use-big-decimals"),
ION_USE_BIG_DECIMALS_NAME,
OptionsMatrixBase::getTrueOrNull,
ION_SYSTEM::newBool,
optionsCombinationStructs,
() -> ION_SYSTEM.newBool(false)
);
}

}
39 changes: 39 additions & 0 deletions tst/com/amazon/ion/benchmark/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ private static class ExpectedReadOptionsCombination
List<String> paths = null;
IonReaderType readerType = IonReaderType.NON_BLOCKING;
boolean useLobChunks = false;
boolean useBigDecimals = false;

static ExpectedReadOptionsCombination defaultOptions() {
return new ExpectedReadOptionsCombination();
Expand All @@ -189,12 +190,18 @@ final ExpectedReadOptionsCombination useLobChunks(boolean useLobChunks) {
return this;
}

final ExpectedReadOptionsCombination useBigDecimals(boolean useBigDecimals) {
this.useBigDecimals = useBigDecimals;
return this;
}

@Override
void assertOptionsEqual(ReadOptionsCombination that) {
super.assertOptionsEqual(that);
assertEquals(paths, that.paths);
assertEquals(readerType, that.readerType);
assertEquals(useLobChunks, that.useLobChunks);
assertEquals(useBigDecimals, that.useBigDecimals);
}
}

Expand Down Expand Up @@ -1218,4 +1225,36 @@ public void readUsingLogChunks() throws Exception {
assertTrue(expectedCombinations.isEmpty());
}

@Test
public void readUsingBigDecimals() throws Exception {
List<ReadOptionsCombination> optionsCombinations = parseOptionsCombinations(
"read",
"--ion-use-big-decimals",
"true",
"--ion-use-big-decimals",
"false",
"--format",
"ion_text",
"--format",
"ion_binary",
"binaryLargeLobs.10n"
);
assertEquals(4, optionsCombinations.size());
List<ExpectedReadOptionsCombination> expectedCombinations = new ArrayList<>(4);

expectedCombinations.add(ExpectedReadOptionsCombination.defaultOptions().useBigDecimals(true).format(Format.ION_TEXT));
expectedCombinations.add(ExpectedReadOptionsCombination.defaultOptions().useBigDecimals(false).format(Format.ION_TEXT));
expectedCombinations.add(ExpectedReadOptionsCombination.defaultOptions().useBigDecimals(true).format(Format.ION_BINARY));
expectedCombinations.add(ExpectedReadOptionsCombination.defaultOptions().useBigDecimals(false).format(Format.ION_BINARY));

for (ReadOptionsCombination optionsCombination : optionsCombinations) {
expectedCombinations.removeIf(candidate -> candidate.useBigDecimals == optionsCombination.useBigDecimals && candidate.format == optionsCombination.format);

assertReadTaskExecutesCorrectly("binaryAllTypes.10n", optionsCombination, optionsCombination.format, optionsCombination.format == Format.ION_TEXT);
assertReadTaskExecutesCorrectly("textAllTypes.ion", optionsCombination, optionsCombination.format, optionsCombination.format == Format.ION_BINARY);
}

assertTrue(expectedCombinations.isEmpty());
}

}
Binary file modified tst/com/amazon/ion/benchmark/binaryAllTypes.10n
Binary file not shown.
1 change: 1 addition & 0 deletions tst/com/amazon/ion/benchmark/textAllTypes.ion
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ false
1.23e4
-1.23e-4
0.00001
-0d0
-1.0d23
2000-01-01T00:00:00Z
{{ aGVsbG8= }}
Expand Down

0 comments on commit d37b6fd

Please sign in to comment.