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

Decouple XContentBuilder from BytesReference #28972

Merged
merged 9 commits into from
Mar 14, 2018

Conversation

dakrone
Copy link
Member

@dakrone dakrone commented Mar 9, 2018

This commit removes all mentions of BytesReference from XContentBuilder.
This is needed so that we can completely decouple the XContent code and move it
into its own dependency.

While this change appears large, it is due to two main changes, moving
.bytes() and .string() out of XContentBuilder itself into static methods
BytesReference.bytes and Strings.toString respectively. The rest of the
change is code reacting to these changes (the majority of it in tests).

Relates to #28504

This commit removes all mentions of `BytesReference` from `XContentBuilder`.
This is needed so that we can completely decouple the XContent code and move it
into its own dependency.

While this change appears large, it is due to two main changes, moving
`.bytes()` and `.string()` out of XContentBuilder itself into static methods
`BytesReference.bytes` and `Strings.toString` respectively. The rest of the
change is code reacting to these changes (the majority of it in tests).

Relates to elastic#28504
@dakrone dakrone added >non-issue :Core/Infra/Core Core issues without another label v7.0.0 v6.3.0 labels Mar 9, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

dakrone added 2 commits March 9, 2018 16:07
…ields

We assert right below it that the XContent equivalent are equal, and the byte
array instances are not going to be the same exact thing regardless.
@@ -38,6 +41,16 @@

private Integer hash = null; // we cache the hash of this reference since it can be quite costly to re-calculated it

public static BytesReference bytes(XContentBuilder xContentBuilder) {
Copy link
Member

Choose a reason for hiding this comment

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

Probably worth Javadoc.

Copy link
Member

Choose a reason for hiding this comment

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

Including the warning that it closes the builder.

@@ -173,6 +170,13 @@ public XContentType contentType() {
return generator.contentType();
}

/**
* @return the output stream to which the built object is being written
Copy link
Member

Choose a reason for hiding this comment

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

I think it'd be worth warning that it is dangerous to modify the steam and that it might not be complete unless the builder is closed. Actually, I wonder if you can have this close the builder too?

Copy link
Member

Choose a reason for hiding this comment

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

I mean, that is kind of weird, but maybe not too weird. Because this is only useful for byte array style output streams. Maybe if this doesn't close the stream it can assert that it is closed? I'm not sure how hard that is.

Copy link
Member Author

Choose a reason for hiding this comment

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

There isn't a way that I see to assert that the OutputStream is already closed (other than closing it). I'll add documentation about the modification though.

@@ -81,7 +81,6 @@ private void doFromXContentTestWithRandomFields(boolean addRandomFields) throws
parsedGetResponse = GetResponse.fromXContent(parser);
assertNull(parser.nextToken());
}
assertEquals(expectedGetResponse, parsedGetResponse);
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why we need this line?

Copy link
Member

Choose a reason for hiding this comment

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

Or, rather, need to remove it?

Copy link
Member Author

Choose a reason for hiding this comment

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

That was a mistake, I will re-add this.

xContentBuilder.field("test1", "value1");
xContentBuilder.endObject();
assertThat(xContentBuilder.bytes().utf8ToString(), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"test1\":\"value1\"}"));
assertThat(BytesReference.bytes(xContentBuilder).utf8ToString(), equalTo("{\"test\":\"value\",\"foo\":{\"test\":\"value\"},\"test1\":\"value1\"}"));
Copy link
Member

Choose a reason for hiding this comment

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

These should probably move into xcontent's tests rather than server's tests so it won't be able to use this.

But that can be another PR :).

It does make me wonder if string should be implemented in the xcontent artifact without Lucene.

Copy link
Member

Choose a reason for hiding this comment

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

I think these should use Strings.toString(xContentBuilder) at least.

@@ -69,18 +70,18 @@ public void testSingleFieldObject() throws IOException {
}

static void assertXContentBuilderAsString(final XContentBuilder expected, final XContentBuilder actual) {
assertThat(actual.bytes().utf8ToString(), is(expected.bytes().utf8ToString()));
assertThat(BytesReference.bytes(actual).utf8ToString(), is(BytesReference.bytes(expected).utf8ToString()));
Copy link
Member

Choose a reason for hiding this comment

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

I think these should use Strings.toString(actual)

@dakrone
Copy link
Member Author

dakrone commented Mar 13, 2018

Thanks for taking a look @nik9000, I pushed commits addressing your comments

@dakrone dakrone merged commit 8e8fdc4 into elastic:master Mar 14, 2018
dakrone added a commit that referenced this pull request Mar 14, 2018
* Decouple XContentBuilder from BytesReference

This commit removes all mentions of `BytesReference` from `XContentBuilder`.
This is needed so that we can completely decouple the XContent code and move it
into its own dependency.

While this change appears large, it is due to two main changes, moving
`.bytes()` and `.string()` out of XContentBuilder itself into static methods
`BytesReference.bytes` and `Strings.toString` respectively. The rest of the
change is code reacting to these changes (the majority of it in tests).

Relates to #28504
cbuescher pushed a commit to cbuescher/elasticsearch that referenced this pull request Mar 15, 2018
Changes made in elastic#28972 seems to have changed some assumptions about how
SMILE and CBOR write byte[] values and how this is tested. This changes
the generation of the randomized DocumentField values back to BytesArray
while expecting the JSON and YAML deserialisation to produce Base64
encoded strings and SMILE and CBOR to parse back BytesArray instances.

Closes elastic#29080
cbuescher pushed a commit that referenced this pull request Mar 15, 2018
Changes made in #28972 seems to have changed some assumptions about how
SMILE and CBOR write byte[] values and how this is tested. This changes
the generation of the randomized DocumentField values back to BytesArray
while expecting the JSON and YAML deserialisation to produce Base64
encoded strings and SMILE and CBOR to parse back BytesArray instances.

Closes #29080
cbuescher pushed a commit that referenced this pull request Mar 15, 2018
Changes made in #28972 seems to have changed some assumptions about how
SMILE and CBOR write byte[] values and how this is tested. This changes
the generation of the randomized DocumentField values back to BytesArray
while expecting the JSON and YAML deserialisation to produce Base64
encoded strings and SMILE and CBOR to parse back BytesArray instances.

Closes #29080
martijnvg added a commit that referenced this pull request Mar 16, 2018
* es/6.x: (89 commits)
  Clarify requirements of strict date formats. (#29090)
  Clarify that dates are always rendered as strings. (#29093)
  [Docs] Fix link to Grok patterns (#29088)
  Fix starting on Windows from another drive (#29086)
  Use removeTask instead of finishTask in PersistentTasksClusterService (#29055)
  Added minimal docs for reindex api in java-api docs
  Allow overriding JVM options in Windows service (#29044)
  Clarify how to set compiler and runtime JDKs (#29101)
  Fix Parsing Bug with Update By Query for Stored Scripts (#29039)
  TEST: write ops should execute under shard permit (#28966)
  [DOCS] Add X-Pack upgrade details (#29038)
  Revert "Improve error message for installing plugin (#28298)"
  Docs: HighLevelRestClient#exists (#29073)
  Validate regular expressions in dynamic templates. (#29013)
  [Tests] Fix GetResultTests and DocumentFieldTests failures (#29083)
  Reenable LiveVersionMapTests.testRamBytesUsed on Java 9. (#29063)
  Mute failing GetResultTests and DocumentFieldTests
  [Docs] Fix Java Api index administration usage (#28260)
  Improve error message for installing plugin (#28298)
  Decouple XContentBuilder from BytesReference (#28972)
  ...
@dakrone dakrone deleted the de-bytesreference-xcontentbuilder branch April 19, 2018 14:45
@cbuescher cbuescher mentioned this pull request Sep 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants