Skip to content

Commit

Permalink
Merge branch 'master' into package-create-keystore
Browse files Browse the repository at this point in the history
* master:
  [TEST] AwaitsFix QueryRescorerIT.testRescoreAfterCollapse
  Decouple XContentType from StreamInput/Output (elastic#28927)
  Remove BytesRef usage from XContentParser and its subclasses (elastic#28792)
  • Loading branch information
jasontedor committed Mar 7, 2018
2 parents 97058a7 + 9d4f09d commit 617fb35
Show file tree
Hide file tree
Showing 27 changed files with 131 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
}
if (documents.isEmpty() == false) {
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
documentXContentType = XContentType.readFrom(in);
documentXContentType = in.readEnum(XContentType.class);
} else {
documentXContentType = XContentFactory.xContentType(documents.iterator().next());
}
Expand Down Expand Up @@ -331,7 +331,7 @@ protected void doWriteTo(StreamOutput out) throws IOException {
out.writeOptionalBytesReference(doc);
}
if (documents.isEmpty() == false && out.getVersion().onOrAfter(Version.V_5_3_0)) {
documentXContentType.writeTo(out);
out.writeEnum(documentXContentType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void readFrom(StreamInput in) throws IOException {
id = in.readOptionalString();
content = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in);
xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(content);
}
Expand All @@ -145,7 +145,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(id);
out.writeBytesReference(content);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
out.writeEnum(xContentType);
}
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) {
out.writeOptionalString(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ public void readFrom(StreamInput in) throws IOException {
pipeline = in.readOptionalString();
isRetry = in.readBoolean();
autoGeneratedTimestamp = in.readLong();
contentType = in.readOptionalWriteable(XContentType::readFrom);
if (in.readBoolean()) {
contentType = in.readEnum(XContentType.class);
} else {
contentType = null;
}
}

@Override
Expand All @@ -566,7 +570,12 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalString(pipeline);
out.writeBoolean(isRetry);
out.writeLong(autoGeneratedTimestamp);
out.writeOptionalWriteable(contentType);
if (contentType != null) {
out.writeBoolean(true);
out.writeEnum(contentType);
} else {
out.writeBoolean(false);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void readFrom(StreamInput in) throws IOException {
id = in.readString();
source = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in);
xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(source);
}
Expand All @@ -93,7 +93,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeBytesReference(source);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
out.writeEnum(xContentType);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public SimulatePipelineRequest(BytesReference source, XContentType xContentType)
verbose = in.readBoolean();
source = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in);
xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(source);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(verbose);
out.writeBytesReference(source);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
out.writeEnum(xContentType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public void readFrom(StreamInput in) throws IOException {
if (in.readBoolean()) {
doc = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType = XContentType.readFrom(in);
xContentType = in.readEnum(XContentType.class);
} else {
xContentType = XContentFactory.xContentType(doc);
}
Expand Down Expand Up @@ -561,7 +561,7 @@ public void writeTo(StreamOutput out) throws IOException {
if (doc != null) {
out.writeBytesReference(doc);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
out.writeEnum(xContentType);
}
}
out.writeOptionalString(routing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ public XContentBuilder field(String name, BytesRef value) throws IOException {
/**
* Writes the binary content of the given {@link BytesRef} as UTF-8 bytes.
*
* Use {@link XContentParser#utf8Bytes()} to read the value back
* Use {@link XContentParser#charBuffer()} to read the value back
*/
public XContentBuilder utf8Field(String name, BytesRef value) throws IOException {
return field(name).utf8Value(value);
Expand All @@ -615,7 +615,7 @@ public XContentBuilder binaryValue(BytesRef value) throws IOException {
/**
* Writes the binary content of the given {@link BytesRef} as UTF-8 bytes.
*
* Use {@link XContentParser#utf8Bytes()} to read the value back
* Use {@link XContentParser#charBuffer()} to read the value back
*/
public XContentBuilder utf8Value(BytesRef value) throws IOException {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@

package org.elasticsearch.common.xcontent;

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lease.Releasable;

import java.io.Closeable;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.List;
import java.util.Map;

Expand All @@ -37,7 +36,7 @@
* NamedXContentRegistry.EMPTY, ParserField."{\"key\" : \"value\"}");
* </pre>
*/
public interface XContentParser extends Releasable {
public interface XContentParser extends Closeable {

enum Token {
START_OBJECT {
Expand Down Expand Up @@ -144,17 +143,13 @@ enum NumberType {

String textOrNull() throws IOException;

/**
* Returns a BytesRef holding UTF-8 bytes or null if a null value is {@link Token#VALUE_NULL}.
* This method should be used to read text only binary content should be read through {@link #binaryValue()}
*/
BytesRef utf8BytesOrNull() throws IOException;
CharBuffer charBufferOrNull() throws IOException;

/**
* Returns a BytesRef holding UTF-8 bytes.
* Returns a {@link CharBuffer} holding UTF-8 bytes.
* This method should be used to read text only binary content should be read through {@link #binaryValue()}
*/
BytesRef utf8Bytes() throws IOException;
CharBuffer charBuffer() throws IOException;

Object objectText() throws IOException;

Expand Down Expand Up @@ -248,8 +243,6 @@ enum NumberType {
*
* these methods write UTF-8 encoded strings and must be read through:
* <ul>
* <li>{@link XContentParser#utf8Bytes()}</li>
* <li>{@link XContentParser#utf8BytesOrNull()}}</li>
* <li>{@link XContentParser#text()} ()}</li>
* <li>{@link XContentParser#textOrNull()} ()}</li>
* <li>{@link XContentParser#textCharacters()} ()}}</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@

package org.elasticsearch.common.xcontent;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.cbor.CborXContent;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.common.xcontent.smile.SmileXContent;
import org.elasticsearch.common.xcontent.yaml.YamlXContent;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

/**
* The content type of {@link org.elasticsearch.common.xcontent.XContent}.
*/
public enum XContentType implements Writeable {
public enum XContentType {

/**
* A JSON based content type.
Expand Down Expand Up @@ -183,18 +179,4 @@ public String mediaType() {

public abstract String mediaTypeWithoutParameters();

public static XContentType readFrom(StreamInput in) throws IOException {
int index = in.readVInt();
for (XContentType contentType : values()) {
if (index == contentType.index) {
return contentType;
}
}
throw new IllegalStateException("Unknown XContentType with index [" + index + "]");
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
Expand Down Expand Up @@ -88,8 +87,8 @@ public String text() throws IOException {
}

@Override
public BytesRef utf8Bytes() throws IOException {
return new BytesRef(CharBuffer.wrap(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength()));
public CharBuffer charBuffer() throws IOException {
return CharBuffer.wrap(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
}

@Override
Expand All @@ -114,7 +113,7 @@ public Object objectText() throws IOException {
public Object objectBytes() throws IOException {
JsonToken currentToken = parser.getCurrentToken();
if (currentToken == JsonToken.VALUE_STRING) {
return utf8Bytes();
return charBuffer();
} else if (currentToken == JsonToken.VALUE_NUMBER_INT || currentToken == JsonToken.VALUE_NUMBER_FLOAT) {
return parser.getNumberValue();
} else if (currentToken == JsonToken.VALUE_TRUE) {
Expand All @@ -124,8 +123,7 @@ public Object objectBytes() throws IOException {
} else if (currentToken == JsonToken.VALUE_NULL) {
return null;
} else {
//TODO should this really do UTF-8 conversion?
return utf8Bytes();
return charBuffer();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.elasticsearch.common.xcontent.support;

import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Booleans;
import org.elasticsearch.common.Numbers;
Expand All @@ -28,6 +27,7 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -240,13 +240,12 @@ public final String textOrNull() throws IOException {
return text();
}


@Override
public BytesRef utf8BytesOrNull() throws IOException {
public CharBuffer charBufferOrNull() throws IOException {
if (currentToken() == Token.VALUE_NULL) {
return null;
}
return utf8Bytes();
return charBuffer();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -194,27 +195,31 @@ public final int hashCode() {
protected abstract int doHashCode();

/**
* This helper method checks if the object passed in is a string, if so it
* converts it to a {@link BytesRef}.
* This helper method checks if the object passed in is a string or {@link CharBuffer},
* if so it converts it to a {@link BytesRef}.
* @param obj the input object
* @return the same input object or a {@link BytesRef} representation if input was of type string
*/
static Object convertToBytesRefIfString(Object obj) {
static Object maybeConvertToBytesRef(Object obj) {
if (obj instanceof String) {
return BytesRefs.toBytesRef(obj);
} else if (obj instanceof CharBuffer) {
return new BytesRef((CharBuffer) obj);
}
return obj;
}

/**
* This helper method checks if the object passed in is a {@link BytesRef}, if so it
* converts it to a utf8 string.
* This helper method checks if the object passed in is a {@link BytesRef} or {@link CharBuffer},
* if so it converts it to a utf8 string.
* @param obj the input object
* @return the same input object or a utf8 string if input was of type {@link BytesRef}
* @return the same input object or a utf8 string if input was of type {@link BytesRef} or {@link CharBuffer}
*/
static Object convertToStringIfBytesRef(Object obj) {
static Object maybeConvertToString(Object obj) {
if (obj instanceof BytesRef) {
return ((BytesRef) obj).utf8ToString();
} else if (obj instanceof CharBuffer) {
return new BytesRef((CharBuffer) obj).utf8ToString();
}
return obj;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public BaseTermQueryBuilder(String fieldName, Object value) {
throw new IllegalArgumentException("value cannot be null");
}
this.fieldName = fieldName;
this.value = convertToBytesRefIfString(value);
this.value = maybeConvertToBytesRef(value);
}

/**
Expand Down Expand Up @@ -144,14 +144,14 @@ public String fieldName() {
* If necessary, converts internal {@link BytesRef} representation back to string.
*/
public Object value() {
return convertToStringIfBytesRef(this.value);
return maybeConvertToString(this.value);
}

@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(getName());
builder.startObject(fieldName);
builder.field(VALUE_FIELD.getPreferredName(), convertToStringIfBytesRef(this.value));
builder.field(VALUE_FIELD.getPreferredName(), maybeConvertToString(this.value));
printBoostAndQueryName(builder);
builder.endObject();
builder.endObject();
Expand Down
Loading

0 comments on commit 617fb35

Please sign in to comment.