Skip to content

Commit 84921aa

Browse files
committed
Use Text instead of XContentString
1 parent a40bee3 commit 84921aa

File tree

13 files changed

+70
-130
lines changed

13 files changed

+70
-130
lines changed

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/ESUTF8StreamJsonParser.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
import com.fasterxml.jackson.core.json.UTF8StreamJsonParser;
1717
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;
1818

19-
import org.elasticsearch.xcontent.XContentString;
19+
import org.elasticsearch.xcontent.Text;
2020

2121
import java.io.IOException;
2222
import java.io.InputStream;
23+
import java.nio.ByteBuffer;
2324

2425
public class ESUTF8StreamJsonParser extends UTF8StreamJsonParser {
2526
protected int stringEnd = -1;
@@ -44,20 +45,20 @@ public ESUTF8StreamJsonParser(
4445
* This is only a best-effort attempt; if there is some reason the bytes cannot be retrieved, this method will return null.
4546
* Currently, this is only implemented for ascii-only strings that do not contain escaped characters.
4647
*/
47-
public XContentString getValueAsByteRef() throws IOException {
48+
public Text getValueAsText() throws IOException {
4849
if (_currToken == JsonToken.VALUE_STRING && _tokenIncomplete) {
4950
if (stringEnd > 0) {
5051
final int len = stringEnd - 1 - _inputPtr;
5152
// For now, we can use `len` for `charCount` because we only support ascii-encoded unescaped strings,
5253
// which means each character uses exactly 1 byte.
53-
return new XContentString(new XContentString.ByteRef(_inputBuffer, _inputPtr, len), len);
54+
return new Text(ByteBuffer.wrap(_inputBuffer, _inputPtr, len), len);
5455
}
55-
return _finishAndReturnByteRef();
56+
return _finishAndReturnText();
5657
}
5758
return null;
5859
}
5960

60-
protected XContentString _finishAndReturnByteRef() throws IOException {
61+
protected Text _finishAndReturnText() throws IOException {
6162
int ptr = _inputPtr;
6263
if (ptr >= _inputEnd) {
6364
_loadMoreGuaranteed();
@@ -76,7 +77,7 @@ protected XContentString _finishAndReturnByteRef() throws IOException {
7677
final int len = ptr - startPtr;
7778
// For now, we can use `len` for `charCount` because we only support ascii-encoded unescaped strings,
7879
// which means each character uses exactly 1 byte.
79-
return new XContentString(new XContentString.ByteRef(inputBuffer, startPtr, len), len);
80+
return new Text(ByteBuffer.wrap(inputBuffer, startPtr, len), len);
8081
}
8182
return null;
8283
}

libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
import com.fasterxml.jackson.core.io.JsonEOFException;
1919

2020
import org.elasticsearch.core.IOUtils;
21+
import org.elasticsearch.xcontent.Text;
2122
import org.elasticsearch.xcontent.XContentEOFException;
2223
import org.elasticsearch.xcontent.XContentLocation;
2324
import org.elasticsearch.xcontent.XContentParseException;
2425
import org.elasticsearch.xcontent.XContentParserConfiguration;
25-
import org.elasticsearch.xcontent.XContentString;
2626
import org.elasticsearch.xcontent.XContentType;
2727
import org.elasticsearch.xcontent.provider.XContentParserConfigurationImpl;
2828
import org.elasticsearch.xcontent.support.AbstractXContentParser;
@@ -117,17 +117,17 @@ public String text() throws IOException {
117117
}
118118

119119
@Override
120-
public XContentString xContentText() throws IOException {
120+
public Text optimizedText() throws IOException {
121121
if (currentToken().isValue() == false) {
122122
throwOnNoText();
123123
}
124124
if (parser instanceof ESUTF8StreamJsonParser esParser) {
125-
var bytesRef = esParser.getValueAsByteRef();
125+
var bytesRef = esParser.getValueAsText();
126126
if (bytesRef != null) {
127127
return bytesRef;
128128
}
129129
}
130-
return new XContentString(text());
130+
return new Text(text());
131131
}
132132

133133
private void throwOnNoText() {

libs/x-content/impl/src/test/java/org/elasticsearch/xcontent/provider/json/ESUTF8StreamJsonParserTests.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
import org.elasticsearch.core.CheckedConsumer;
1818
import org.elasticsearch.test.ESTestCase;
1919
import org.elasticsearch.xcontent.XContentBuilder;
20-
import org.elasticsearch.xcontent.XContentString;
2120
import org.elasticsearch.xcontent.json.JsonXContent;
2221
import org.hamcrest.Matchers;
2322

2423
import java.io.IOException;
24+
import java.nio.ByteBuffer;
2525
import java.nio.charset.StandardCharsets;
26-
import java.util.Arrays;
2726

2827
public class ESUTF8StreamJsonParserTests extends ESTestCase {
2928

@@ -36,25 +35,24 @@ private void testParseJson(String input, CheckedConsumer<ESUTF8StreamJsonParser,
3635
test.accept((ESUTF8StreamJsonParser) parser);
3736
}
3837

39-
private void assertTextRef(XContentString.ByteRef textRef, String expectedValue) {
40-
var data = Arrays.copyOfRange(textRef.bytes(), textRef.offset(), textRef.offset() + textRef.length());
41-
assertThat(data, Matchers.equalTo(StandardCharsets.UTF_8.encode(expectedValue).array()));
38+
private void assertTextRef(ByteBuffer textRef, String expectedValue) {
39+
assertThat(textRef, Matchers.equalTo(StandardCharsets.UTF_8.encode(expectedValue)));
4240
}
4341

44-
public void testGetValueAsByteRef() throws IOException {
42+
public void testGetValueAsText() throws IOException {
4543
testParseJson("{\"foo\": \"bar\"}", parser -> {
4644
assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.START_OBJECT));
4745
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
4846
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
4947

50-
XContentString.ByteRef textRef = parser.getValueAsByteRef().getBytes();
48+
ByteBuffer textRef = parser.getValueAsText().bytes();
5149
assertThat(textRef, Matchers.notNullValue());
52-
assertThat(textRef.offset(), Matchers.equalTo(9));
53-
assertThat(textRef.length(), Matchers.equalTo(3));
50+
assertThat(textRef.arrayOffset() + textRef.position(), Matchers.equalTo(9));
51+
assertThat(textRef.limit(), Matchers.equalTo(12));
5452
assertTextRef(textRef, "bar");
5553

5654
assertThat(parser.getValueAsString(), Matchers.equalTo("bar"));
57-
assertThat(parser.getValueAsByteRef(), Matchers.nullValue());
55+
assertThat(parser.getValueAsText(), Matchers.nullValue());
5856

5957
assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.END_OBJECT));
6058
});
@@ -64,7 +62,7 @@ public void testGetValueAsByteRef() throws IOException {
6462
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
6563
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
6664

67-
assertThat(parser.getValueAsByteRef(), Matchers.nullValue());
65+
assertThat(parser.getValueAsText(), Matchers.nullValue());
6866
assertThat(parser.getValueAsString(), Matchers.equalTo("bar\"baz\""));
6967
});
7068

@@ -73,7 +71,7 @@ public void testGetValueAsByteRef() throws IOException {
7371
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
7472
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
7573

76-
assertThat(parser.getValueAsByteRef(), Matchers.nullValue());
74+
assertThat(parser.getValueAsText(), Matchers.nullValue());
7775
assertThat(parser.getValueAsString(), Matchers.equalTo("bår"));
7876
});
7977

@@ -84,28 +82,28 @@ public void testGetValueAsByteRef() throws IOException {
8482

8583
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
8684
{
87-
XContentString.ByteRef textRef = parser.getValueAsByteRef().getBytes();
85+
ByteBuffer textRef = parser.getValueAsText().bytes();
8886
assertThat(textRef, Matchers.notNullValue());
89-
assertThat(textRef.offset(), Matchers.equalTo(10));
90-
assertThat(textRef.length(), Matchers.equalTo(5));
87+
assertThat(textRef.arrayOffset() + textRef.position(), Matchers.equalTo(10));
88+
assertThat(textRef.limit(), Matchers.equalTo(15));
9189
assertTextRef(textRef, "lorem");
9290
}
9391

9492
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
9593
{
96-
XContentString.ByteRef textRef = parser.getValueAsByteRef().getBytes();
94+
ByteBuffer textRef = parser.getValueAsText().bytes();
9795
assertThat(textRef, Matchers.notNullValue());
98-
assertThat(textRef.offset(), Matchers.equalTo(19));
99-
assertThat(textRef.length(), Matchers.equalTo(5));
96+
assertThat(textRef.arrayOffset() + textRef.position(), Matchers.equalTo(19));
97+
assertThat(textRef.limit(), Matchers.equalTo(24));
10098
assertTextRef(textRef, "ipsum");
10199
}
102100

103101
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
104102
{
105-
XContentString.ByteRef textRef = parser.getValueAsByteRef().getBytes();
103+
ByteBuffer textRef = parser.getValueAsText().bytes();
106104
assertThat(textRef, Matchers.notNullValue());
107-
assertThat(textRef.offset(), Matchers.equalTo(28));
108-
assertThat(textRef.length(), Matchers.equalTo(5));
105+
assertThat(textRef.arrayOffset() + textRef.position(), Matchers.equalTo(28));
106+
assertThat(textRef.limit(), Matchers.equalTo(33));
109107
assertTextRef(textRef, "dolor");
110108
}
111109

@@ -151,9 +149,9 @@ public void testGetValueRandomized() throws IOException {
151149

152150
String currVal = values[i];
153151
if (validForTextRef(currVal)) {
154-
assertTextRef(parser.getValueAsByteRef().getBytes(), currVal);
152+
assertTextRef(parser.getValueAsText().bytes(), currVal);
155153
} else {
156-
assertThat(parser.getValueAsByteRef(), Matchers.nullValue());
154+
assertThat(parser.getValueAsText(), Matchers.nullValue());
157155
assertThat(parser.getValueAsString(), Matchers.equalTo(currVal));
158156
}
159157
}

libs/x-content/src/main/java/org/elasticsearch/xcontent/FilterXContentParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ public String textOrNull() throws IOException {
100100
return delegate().textOrNull();
101101
}
102102

103-
public XContentString xContentText() throws IOException {
104-
return delegate().xContentText();
103+
public Text optimizedText() throws IOException {
104+
return delegate().optimizedText();
105105
}
106106

107-
public XContentString xContentTextOrNull() throws IOException {
108-
return delegate().xContentTextOrNull();
107+
public Text optimizedTextOrNull() throws IOException {
108+
return delegate().optimizedTextOrNull();
109109
}
110110

111111
@Override

libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ <T> Map<String, T> map(Supplier<Map<String, T>> mapFactory, CheckedFunction<XCon
109109

110110
String textOrNull() throws IOException;
111111

112-
XContentString xContentText() throws IOException;
112+
Text optimizedText() throws IOException;
113113

114-
XContentString xContentTextOrNull() throws IOException;
114+
Text optimizedTextOrNull() throws IOException;
115115

116116
CharBuffer charBufferOrNull() throws IOException;
117117

libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentString.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import org.elasticsearch.core.RestApiVersion;
1616
import org.elasticsearch.xcontent.DeprecationHandler;
1717
import org.elasticsearch.xcontent.NamedXContentRegistry;
18+
import org.elasticsearch.xcontent.Text;
1819
import org.elasticsearch.xcontent.XContentParseException;
1920
import org.elasticsearch.xcontent.XContentParser;
20-
import org.elasticsearch.xcontent.XContentString;
2121

2222
import java.io.IOException;
2323
import java.math.BigDecimal;
@@ -260,16 +260,16 @@ public final String textOrNull() throws IOException {
260260
}
261261

262262
@Override
263-
public XContentString xContentText() throws IOException {
264-
return new XContentString(text());
263+
public Text optimizedText() throws IOException {
264+
return new Text(text());
265265
}
266266

267267
@Override
268-
public final XContentString xContentTextOrNull() throws IOException {
268+
public final Text optimizedTextOrNull() throws IOException {
269269
if (currentToken() == Token.VALUE_NULL) {
270270
return null;
271271
}
272-
return xContentText();
272+
return optimizedText();
273273
}
274274

275275
@Override

server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
import org.elasticsearch.xcontent.DeprecationHandler;
3333
import org.elasticsearch.xcontent.FilterXContentParser;
3434
import org.elasticsearch.xcontent.NamedXContentRegistry;
35+
import org.elasticsearch.xcontent.Text;
3536
import org.elasticsearch.xcontent.ToXContent;
3637
import org.elasticsearch.xcontent.XContentLocation;
3738
import org.elasticsearch.xcontent.XContentParser;
3839
import org.elasticsearch.xcontent.XContentParser.NumberType;
3940
import org.elasticsearch.xcontent.XContentParser.Token;
40-
import org.elasticsearch.xcontent.XContentString;
4141
import org.elasticsearch.xcontent.XContentType;
4242
import org.elasticsearch.xcontent.support.MapXContentParser;
4343

@@ -709,11 +709,11 @@ public Token currentToken() {
709709
}
710710

711711
@Override
712-
public XContentString xContentTextOrNull() throws IOException {
712+
public Text optimizedTextOrNull() throws IOException {
713713
if (parsingObject == false) {
714-
return new XContentString(textValue);
714+
return new Text(textValue);
715715
}
716-
return super.xContentTextOrNull();
716+
return super.optimizedTextOrNull();
717717
}
718718

719719
@Override

server/src/main/java/org/elasticsearch/index/mapper/DotExpandingXContentParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import org.elasticsearch.core.CheckedFunction;
1313
import org.elasticsearch.xcontent.FilterXContentParser;
1414
import org.elasticsearch.xcontent.FilterXContentParserWrapper;
15+
import org.elasticsearch.xcontent.Text;
1516
import org.elasticsearch.xcontent.XContentLocation;
1617
import org.elasticsearch.xcontent.XContentParser;
17-
import org.elasticsearch.xcontent.XContentString;
1818
import org.elasticsearch.xcontent.XContentSubParser;
1919

2020
import java.io.IOException;
@@ -381,11 +381,11 @@ public void skipChildren() throws IOException {
381381
}
382382

383383
@Override
384-
public XContentString xContentTextOrNull() throws IOException {
384+
public Text optimizedTextOrNull() throws IOException {
385385
if (state == State.EXPANDING_START_OBJECT) {
386386
throw new IllegalStateException("Can't get text on a " + currentToken() + " at " + getTokenLocation());
387387
}
388-
return super.xContentTextOrNull();
388+
return super.optimizedTextOrNull();
389389
}
390390

391391
@Override

server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@
5454
import org.elasticsearch.search.runtime.GeoPointScriptFieldDistanceFeatureQuery;
5555
import org.elasticsearch.xcontent.CopyingXContentParser;
5656
import org.elasticsearch.xcontent.FilterXContentParserWrapper;
57+
import org.elasticsearch.xcontent.Text;
5758
import org.elasticsearch.xcontent.ToXContent;
5859
import org.elasticsearch.xcontent.XContentBuilder;
5960
import org.elasticsearch.xcontent.XContentParser;
60-
import org.elasticsearch.xcontent.XContentString;
6161

6262
import java.io.IOException;
6363
import java.io.UncheckedIOException;
@@ -329,8 +329,8 @@ static class GeoHashMultiFieldParser extends FilterXContentParserWrapper {
329329
}
330330

331331
@Override
332-
public XContentString xContentTextOrNull() throws IOException {
333-
return new XContentString(value);
332+
public Text optimizedTextOrNull() throws IOException {
333+
return new Text(value);
334334
}
335335

336336
@Override

0 commit comments

Comments
 (0)