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

Initial implementation of Ion 1.1 raw binary writer #660

Merged
merged 3 commits into from
Dec 1, 2023

Conversation

popematt
Copy link
Contributor

Issue #, if available:

None.

Description of changes:

  • Adds (incomplete) implementation of IonWriter_1_1 with support for nulls, bools, and lists.
  • Extracts core FlexInt and FlexUInt logic from WriteBuffer to FlexInt.
  • Updates WriteBuffer so that FlexInt and FlexUInt are written directly to the data array of the block instead of being indirected through writeByte().
  • Extracts PatchPoint to be a top level class so that it can be shared between writers.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Comment on lines +76 to +77
expected, actual,
"Bytes don't match!\nEXPECTED:\n" + hexDump(expected) + "\nACTUAL:\n" + hexDump(actual) + "\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because of the change from org.junit.Assert to org.junit.jupiter.api.Assertions.

fun writeFlexIntOrUIntInto(data: ByteArray, offset: Int, value: Long, numBytes: Int) {
var i = offset

when (numBytes) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I discovered that using when to compare directly against some constants would allow the Kotlin compiler to generate a jump table, so I figured I'd just manually unroll the old loop based logic. Now the only branching in this method is the jump table for the when, compared to the chained if/else falling back to the loop in the previous implementation.

@popematt popematt requested a review from tgregg November 30, 2023 19:12
src/main/java/com/amazon/ion/impl/bin/WriteBuffer.java Outdated Show resolved Hide resolved
Comment on lines +78 to +79
// Just checking that data is written, not asserting the content.
assertTrue(actual.isNotBlank())
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: it would also be correct for the output to be blank in this particular case, since no user values were written. This is true for all the tests that write only an IVM before finish/close.

Copy link
Contributor Author

@popematt popematt Dec 1, 2023

Choose a reason for hiding this comment

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

Is that the behavior we want for Ion 1.1, though? What if someone calls writeIVM() and then finish() (maybe they have logic that writes after every top level piece of "data") and then try to write Ion 1.1 data? I know it's a niche case, but that would cause invalid data to be written.

In addition, because emitting an IVM resets the encoding environment, I think the decision of when to write or not write the IVM belongs in a higher level writer that is aware of the encoding environment.

Copy link
Contributor

Choose a reason for hiding this comment

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

writeIVM() should be a raw-level API, not accessible to users of an application-level writer. Those users get IVMs written as a side effect of writing user values. The IVM that is written depends on the kind of writer the user selected (Ion 1.0 or Ion 1.1).

So, I think I'm saying I agree that for the purpose of these raw tests, the assertions of IVM writing are fine. The raw writer should always write an IVM when requested, but the user-level writer should only request one before writing a new encoding environment that precedes user values.

@popematt popematt merged commit 79c8e5c into amazon-ion:ion-11-encoding Dec 1, 2023
13 of 27 checks passed
@popematt popematt deleted the raw11writer branch December 1, 2023 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants