Skip to content

Commit d7a9fcd

Browse files
committed
Remove BytesRef usage from XContentParser and its subclasses (#28792)
* Remove BytesRef usage from XContentParser and its subclasses This removes all the BytesRef usage from XContentParser in favor of directly returning a CharBuffer (this was originally what was returned, it was just immediately wraped in a BytesRef). Relates to #28504 * Rename method after Ryan's feedback
1 parent e28fd9f commit d7a9fcd

16 files changed

+100
-82
lines changed

server/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ public XContentBuilder field(String name, BytesRef value) throws IOException {
593593
/**
594594
* Writes the binary content of the given {@link BytesRef} as UTF-8 bytes.
595595
*
596-
* Use {@link XContentParser#utf8Bytes()} to read the value back
596+
* Use {@link XContentParser#charBuffer()} to read the value back
597597
*/
598598
public XContentBuilder utf8Field(String name, BytesRef value) throws IOException {
599599
return field(name).utf8Value(value);
@@ -615,7 +615,7 @@ public XContentBuilder binaryValue(BytesRef value) throws IOException {
615615
/**
616616
* Writes the binary content of the given {@link BytesRef} as UTF-8 bytes.
617617
*
618-
* Use {@link XContentParser#utf8Bytes()} to read the value back
618+
* Use {@link XContentParser#charBuffer()} to read the value back
619619
*/
620620
public XContentBuilder utf8Value(BytesRef value) throws IOException {
621621
if (value == null) {

server/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
package org.elasticsearch.common.xcontent;
2121

22-
import org.apache.lucene.util.BytesRef;
2322
import org.elasticsearch.common.lease.Releasable;
2423

2524
import java.io.IOException;
25+
import java.nio.CharBuffer;
2626
import java.util.List;
2727
import java.util.Map;
2828

@@ -144,17 +144,13 @@ enum NumberType {
144144

145145
String textOrNull() throws IOException;
146146

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

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

159155
Object objectText() throws IOException;
160156

@@ -248,8 +244,6 @@ enum NumberType {
248244
*
249245
* these methods write UTF-8 encoded strings and must be read through:
250246
* <ul>
251-
* <li>{@link XContentParser#utf8Bytes()}</li>
252-
* <li>{@link XContentParser#utf8BytesOrNull()}}</li>
253247
* <li>{@link XContentParser#text()} ()}</li>
254248
* <li>{@link XContentParser#textOrNull()} ()}</li>
255249
* <li>{@link XContentParser#textCharacters()} ()}}</li>

server/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentParser.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.fasterxml.jackson.core.JsonParser;
2424
import com.fasterxml.jackson.core.JsonToken;
2525

26-
import org.apache.lucene.util.BytesRef;
2726
import org.apache.lucene.util.IOUtils;
2827
import org.elasticsearch.common.xcontent.DeprecationHandler;
2928
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
@@ -88,8 +87,8 @@ public String text() throws IOException {
8887
}
8988

9089
@Override
91-
public BytesRef utf8Bytes() throws IOException {
92-
return new BytesRef(CharBuffer.wrap(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength()));
90+
public CharBuffer charBuffer() throws IOException {
91+
return CharBuffer.wrap(parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
9392
}
9493

9594
@Override
@@ -114,7 +113,7 @@ public Object objectText() throws IOException {
114113
public Object objectBytes() throws IOException {
115114
JsonToken currentToken = parser.getCurrentToken();
116115
if (currentToken == JsonToken.VALUE_STRING) {
117-
return utf8Bytes();
116+
return charBuffer();
118117
} else if (currentToken == JsonToken.VALUE_NUMBER_INT || currentToken == JsonToken.VALUE_NUMBER_FLOAT) {
119118
return parser.getNumberValue();
120119
} else if (currentToken == JsonToken.VALUE_TRUE) {
@@ -124,8 +123,7 @@ public Object objectBytes() throws IOException {
124123
} else if (currentToken == JsonToken.VALUE_NULL) {
125124
return null;
126125
} else {
127-
//TODO should this really do UTF-8 conversion?
128-
return utf8Bytes();
126+
return charBuffer();
129127
}
130128
}
131129

server/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.common.xcontent.support;
2121

22-
import org.apache.lucene.util.BytesRef;
2322
import org.elasticsearch.ElasticsearchParseException;
2423
import org.elasticsearch.common.Booleans;
2524
import org.elasticsearch.common.Numbers;
@@ -28,6 +27,7 @@
2827
import org.elasticsearch.common.xcontent.XContentParser;
2928

3029
import java.io.IOException;
30+
import java.nio.CharBuffer;
3131
import java.util.ArrayList;
3232
import java.util.HashMap;
3333
import java.util.LinkedHashMap;
@@ -240,13 +240,12 @@ public final String textOrNull() throws IOException {
240240
return text();
241241
}
242242

243-
244243
@Override
245-
public BytesRef utf8BytesOrNull() throws IOException {
244+
public CharBuffer charBufferOrNull() throws IOException {
246245
if (currentToken() == Token.VALUE_NULL) {
247246
return null;
248247
}
249-
return utf8Bytes();
248+
return charBuffer();
250249
}
251250

252251
@Override

server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.elasticsearch.common.xcontent.XContentParser;
3838

3939
import java.io.IOException;
40+
import java.nio.CharBuffer;
4041
import java.util.ArrayList;
4142
import java.util.Collection;
4243
import java.util.List;
@@ -194,27 +195,31 @@ public final int hashCode() {
194195
protected abstract int doHashCode();
195196

196197
/**
197-
* This helper method checks if the object passed in is a string, if so it
198-
* converts it to a {@link BytesRef}.
198+
* This helper method checks if the object passed in is a string or {@link CharBuffer},
199+
* if so it converts it to a {@link BytesRef}.
199200
* @param obj the input object
200201
* @return the same input object or a {@link BytesRef} representation if input was of type string
201202
*/
202-
static Object convertToBytesRefIfString(Object obj) {
203+
static Object maybeConvertToBytesRef(Object obj) {
203204
if (obj instanceof String) {
204205
return BytesRefs.toBytesRef(obj);
206+
} else if (obj instanceof CharBuffer) {
207+
return new BytesRef((CharBuffer) obj);
205208
}
206209
return obj;
207210
}
208211

209212
/**
210-
* This helper method checks if the object passed in is a {@link BytesRef}, if so it
211-
* converts it to a utf8 string.
213+
* This helper method checks if the object passed in is a {@link BytesRef} or {@link CharBuffer},
214+
* if so it converts it to a utf8 string.
212215
* @param obj the input object
213-
* @return the same input object or a utf8 string if input was of type {@link BytesRef}
216+
* @return the same input object or a utf8 string if input was of type {@link BytesRef} or {@link CharBuffer}
214217
*/
215-
static Object convertToStringIfBytesRef(Object obj) {
218+
static Object maybeConvertToString(Object obj) {
216219
if (obj instanceof BytesRef) {
217220
return ((BytesRef) obj).utf8ToString();
221+
} else if (obj instanceof CharBuffer) {
222+
return new BytesRef((CharBuffer) obj).utf8ToString();
218223
}
219224
return obj;
220225
}

server/src/main/java/org/elasticsearch/index/query/BaseTermQueryBuilder.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public BaseTermQueryBuilder(String fieldName, Object value) {
116116
throw new IllegalArgumentException("value cannot be null");
117117
}
118118
this.fieldName = fieldName;
119-
this.value = convertToBytesRefIfString(value);
119+
this.value = maybeConvertToBytesRef(value);
120120
}
121121

122122
/**
@@ -144,14 +144,14 @@ public String fieldName() {
144144
* If necessary, converts internal {@link BytesRef} representation back to string.
145145
*/
146146
public Object value() {
147-
return convertToStringIfBytesRef(this.value);
147+
return maybeConvertToString(this.value);
148148
}
149149

150150
@Override
151151
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
152152
builder.startObject(getName());
153153
builder.startObject(fieldName);
154-
builder.field(VALUE_FIELD.getPreferredName(), convertToStringIfBytesRef(this.value));
154+
builder.field(VALUE_FIELD.getPreferredName(), maybeConvertToString(this.value));
155155
printBoostAndQueryName(builder);
156156
builder.endObject();
157157
builder.endObject();

server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public FuzzyQueryBuilder(String fieldName, Object value) {
154154
throw new IllegalArgumentException("query value cannot be null");
155155
}
156156
this.fieldName = fieldName;
157-
this.value = convertToBytesRefIfString(value);
157+
this.value = maybeConvertToBytesRef(value);
158158
}
159159

160160
/**
@@ -187,7 +187,7 @@ public String fieldName() {
187187
}
188188

189189
public Object value() {
190-
return convertToStringIfBytesRef(this.value);
190+
return maybeConvertToString(this.value);
191191
}
192192

193193
public FuzzyQueryBuilder fuzziness(Fuzziness fuzziness) {
@@ -239,7 +239,7 @@ public String rewrite() {
239239
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
240240
builder.startObject(NAME);
241241
builder.startObject(fieldName);
242-
builder.field(VALUE_FIELD.getPreferredName(), convertToStringIfBytesRef(this.value));
242+
builder.field(VALUE_FIELD.getPreferredName(), maybeConvertToString(this.value));
243243
fuzziness.toXContent(builder, params);
244244
builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength);
245245
builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions);
@@ -275,9 +275,9 @@ public static FuzzyQueryBuilder fromXContent(XContentParser parser) throws IOExc
275275
currentFieldName = parser.currentName();
276276
} else if (token.isValue()) {
277277
if (TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
278-
value = parser.objectBytes();
278+
value = maybeConvertToBytesRef(parser.objectBytes());
279279
} else if (VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
280-
value = parser.objectBytes();
280+
value = maybeConvertToBytesRef(parser.objectBytes());
281281
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
282282
boost = parser.floatValue();
283283
} else if (Fuzziness.FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
@@ -304,7 +304,7 @@ public static FuzzyQueryBuilder fromXContent(XContentParser parser) throws IOExc
304304
} else {
305305
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
306306
fieldName = parser.currentName();
307-
value = parser.objectBytes();
307+
value = maybeConvertToBytesRef(parser.objectBytes());
308308
}
309309
}
310310
return new FuzzyQueryBuilder(fieldName, value)

server/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public String fieldName() {
162162
* of query to be equal regardless of whether it was created from XContent or via Java API.
163163
*/
164164
public RangeQueryBuilder from(Object from, boolean includeLower) {
165-
this.from = convertToBytesRefIfString(from);
165+
this.from = maybeConvertToBytesRef(from);
166166
this.includeLower = includeLower;
167167
return this;
168168
}
@@ -178,7 +178,7 @@ public RangeQueryBuilder from(Object from) {
178178
* Gets the lower range value for this query.
179179
*/
180180
public Object from() {
181-
return convertToStringIfBytesRef(this.from);
181+
return maybeConvertToString(this.from);
182182
}
183183

184184
/**
@@ -199,7 +199,7 @@ public RangeQueryBuilder gte(Object from) {
199199
* The to part of the range query. Null indicates unbounded.
200200
*/
201201
public RangeQueryBuilder to(Object to, boolean includeUpper) {
202-
this.to = convertToBytesRefIfString(to);
202+
this.to = maybeConvertToBytesRef(to);
203203
this.includeUpper = includeUpper;
204204
return this;
205205
}
@@ -218,7 +218,7 @@ public RangeQueryBuilder to(Object to) {
218218
* of query to be equal regardless of whether it was created from XContent or via Java API.
219219
*/
220220
public Object to() {
221-
return convertToStringIfBytesRef(this.to);
221+
return maybeConvertToString(this.to);
222222
}
223223

224224
/**
@@ -334,8 +334,8 @@ public RangeQueryBuilder relation(String relation) {
334334
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
335335
builder.startObject(NAME);
336336
builder.startObject(fieldName);
337-
builder.field(FROM_FIELD.getPreferredName(), convertToStringIfBytesRef(this.from));
338-
builder.field(TO_FIELD.getPreferredName(), convertToStringIfBytesRef(this.to));
337+
builder.field(FROM_FIELD.getPreferredName(), maybeConvertToString(this.from));
338+
builder.field(TO_FIELD.getPreferredName(), maybeConvertToString(this.to));
339339
builder.field(INCLUDE_LOWER_FIELD.getPreferredName(), includeLower);
340340
builder.field(INCLUDE_UPPER_FIELD.getPreferredName(), includeUpper);
341341
if (timeZone != null) {
@@ -377,26 +377,26 @@ public static RangeQueryBuilder fromXContent(XContentParser parser) throws IOExc
377377
currentFieldName = parser.currentName();
378378
} else {
379379
if (FROM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
380-
from = parser.objectBytes();
380+
from = maybeConvertToBytesRef(parser.objectBytes());
381381
} else if (TO_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
382-
to = parser.objectBytes();
382+
to = maybeConvertToBytesRef(parser.objectBytes());
383383
} else if (INCLUDE_LOWER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
384384
includeLower = parser.booleanValue();
385385
} else if (INCLUDE_UPPER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
386386
includeUpper = parser.booleanValue();
387387
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
388388
boost = parser.floatValue();
389389
} else if (GT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
390-
from = parser.objectBytes();
390+
from = maybeConvertToBytesRef(parser.objectBytes());
391391
includeLower = false;
392392
} else if (GTE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
393-
from = parser.objectBytes();
393+
from = maybeConvertToBytesRef(parser.objectBytes());
394394
includeLower = true;
395395
} else if (LT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
396-
to = parser.objectBytes();
396+
to = maybeConvertToBytesRef(parser.objectBytes());
397397
includeUpper = false;
398398
} else if (LTE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
399-
to = parser.objectBytes();
399+
to = maybeConvertToBytesRef(parser.objectBytes());
400400
includeUpper = true;
401401
} else if (TIME_ZONE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
402402
timeZone = parser.text();

server/src/main/java/org/elasticsearch/index/query/SpanTermQueryBuilder.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ public static SpanTermQueryBuilder fromXContent(XContentParser parser) throws IO
109109
currentFieldName = parser.currentName();
110110
} else {
111111
if (TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
112-
value = parser.objectBytes();
112+
value = maybeConvertToBytesRef(parser.objectBytes());
113113
} else if (BaseTermQueryBuilder.VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
114-
value = parser.objectBytes();
114+
value = maybeConvertToBytesRef(parser.objectBytes());
115115
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
116116
boost = parser.floatValue();
117117
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
@@ -125,7 +125,7 @@ public static SpanTermQueryBuilder fromXContent(XContentParser parser) throws IO
125125
} else {
126126
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
127127
fieldName = parser.currentName();
128-
value = parser.objectBytes();
128+
value = maybeConvertToBytesRef(parser.objectBytes());
129129
}
130130
}
131131

server/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ public static TermQueryBuilder fromXContent(XContentParser parser) throws IOExce
100100
currentFieldName = parser.currentName();
101101
} else {
102102
if (TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
103-
value = parser.objectBytes();
103+
value = maybeConvertToBytesRef(parser.objectBytes());
104104
} else if (VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
105-
value = parser.objectBytes();
105+
value = maybeConvertToBytesRef(parser.objectBytes());
106106
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
107107
queryName = parser.text();
108108
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
@@ -116,7 +116,7 @@ public static TermQueryBuilder fromXContent(XContentParser parser) throws IOExce
116116
} else if (token.isValue()) {
117117
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
118118
fieldName = currentFieldName;
119-
value = parser.objectBytes();
119+
value = maybeConvertToBytesRef(parser.objectBytes());
120120
} else if (token == XContentParser.Token.START_ARRAY) {
121121
throw new ParsingException(parser.getTokenLocation(), "[term] query does not support array of values");
122122
}

0 commit comments

Comments
 (0)