Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensures that macro arguments are present in the binary reader's buffer before attempting to materialize them. #985

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,15 @@ public Event nextValue() {
return event;
}

@Override
public Event fillValue() {
if (isEvaluatingEExpression) {
event = Event.VALUE_READY;
return event;
}
return super.fillValue();
}

@Override
public Event stepIntoContainer() {
if (isEvaluatingEExpression) {
Expand Down
77 changes: 65 additions & 12 deletions src/main/java/com/amazon/ion/impl/macro/ReaderAdapterContinuable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,44 @@ internal class ReaderAdapterContinuable(val reader: IonReaderContinuableCore) :
return event != IonCursor.Event.NEEDS_DATA && event != IonCursor.Event.END_CONTAINER
}

override fun stringValue(): String = reader.stringValue()
/**
* Ensures that the value on which the reader is positioned is fully buffered.
*/
private fun prepareValue() {
// TODO performance: fill entire expression groups up-front so that the reader will usually not be in slow
// mode when this is called.
if (reader.fillValue() != IonCursor.Event.VALUE_READY) {
throw IonException("TODO: support continuable reading and oversize value handling via this adapter.")
}
}

override fun stringValue(): String {
prepareValue()
return reader.stringValue()
}

override fun intValue(): Int = reader.intValue()
override fun intValue(): Int {
prepareValue()
return reader.intValue()
}

override fun decimalValue(): BigDecimal = reader.decimalValue()
override fun decimalValue(): BigDecimal {
prepareValue()
return reader.decimalValue()
}

override fun doubleValue(): Double = reader.doubleValue()
override fun doubleValue(): Double {
prepareValue()
return reader.doubleValue()
}

override fun stepIntoContainer() {
// Note: the following line ensures the entire container is buffered. This improves performance when reading the
// container's elements because there is less work to do per element. However, very large containers would
// increase memory usage. The current implementation already assumes this risk by eagerly materializing
// macro invocation arguments. However, if that is changed, then removing the following line should also be
// considered.
prepareValue()
Comment on lines +60 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the context :)

reader.stepIntoContainer()
}

Expand All @@ -50,25 +79,49 @@ internal class ReaderAdapterContinuable(val reader: IonReaderContinuableCore) :
return annotations
}

override fun symbolValue(): SymbolToken = reader.symbolValue()
override fun symbolValue(): SymbolToken {
prepareValue()
return reader.symbolValue()
}

override fun getIntegerSize(): IntegerSize = reader.integerSize
override fun getIntegerSize(): IntegerSize {
prepareValue()
return reader.integerSize
}

override fun getFieldNameSymbol(): SymbolToken = reader.fieldNameSymbol

override fun isInStruct(): Boolean = reader.isInStruct

override fun newBytes(): ByteArray = reader.newBytes()
override fun newBytes(): ByteArray {
prepareValue()
return reader.newBytes()
}

override fun timestampValue(): Timestamp = reader.timestampValue()
override fun timestampValue(): Timestamp {
prepareValue()
return reader.timestampValue()
}

override fun bigIntegerValue(): BigInteger = reader.bigIntegerValue()
override fun bigIntegerValue(): BigInteger {
prepareValue()
return reader.bigIntegerValue()
}

override fun longValue(): Long = reader.longValue()
override fun longValue(): Long {
prepareValue()
return reader.longValue()
}

override fun isNullValue(): Boolean = reader.isNullValue

override fun booleanValue(): Boolean = reader.booleanValue()
override fun booleanValue(): Boolean {
prepareValue()
return reader.booleanValue()
}

override fun integerSize(): IntegerSize? = reader.integerSize
override fun integerSize(): IntegerSize? {
prepareValue()
return reader.integerSize
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6004,5 +6004,30 @@ public void addSymbolsSystemMacro(boolean constructFromBytes) throws Exception {
}
}

@Test
public void addSymbolsSystemMacroWhenNotEntirelyBuffered() throws Exception {
int[] data = new int[] {
/* 0 - 3 */ // 0xEA, 0x01, 0x01, 0xE0, // implicitly provided by BinaryIonAppender
/* 4 - 5 */ 0xEF, 0x0C, // system macro add_symbols
/* 6 - 7 */ 0x02, 0x01, // AEB: 0b------aa; a=10, FlexInt 0, a delimited expression group
/* 8 - 12 */ 0x93, 0x66, 0x6F, 0x72, // "for"
/* 13 - 16 */ 0x93, 0x66, 0x75, 0x72, // "fur"
/* 17 - 21 */ 0x94, 0x66, 0x6F, 0x75, 0x72, // "four"
/* 22 - 22 */ 0xF0, // delimited end... of expression group
/* 23 - 24 */ 0xE1, 0x41 + 3, // SID single byte ${usid 1} => "four"
};
byte[] bytes = new TestUtils.BinaryIonAppender(1).append(data).toByteArray();
totalBytesInStream = bytes.length;
readerBuilder = IonReaderBuilder.standard().withIncrementalReadingEnabled(true);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
reader = boundedReaderFor(inputStream, 16, Integer.MAX_VALUE, byteCountingHandler);
assertSequence(
next(IonType.SYMBOL),
stringValue("four"),
next(null)
);
closeAndCount();
}

// TODO Ion 1.1 symbol tables with all kinds of annotation encodings (opcodes E4 - E9, inline and SID)
}
Loading