Skip to content

Commit d75f180

Browse files
committed
Use new UTF8Bytes class
1 parent de33cb6 commit d75f180

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer;
1818

1919
import org.elasticsearch.xcontent.Text;
20+
import org.elasticsearch.xcontent.XContentString;
2021

2122
import java.io.IOException;
2223
import java.io.InputStream;
23-
import java.nio.ByteBuffer;
2424

2525
public class ESUTF8StreamJsonParser extends UTF8StreamJsonParser {
2626
protected int stringEnd = -1;
@@ -49,9 +49,9 @@ public Text getValueAsText() throws IOException {
4949
if (_currToken == JsonToken.VALUE_STRING && _tokenIncomplete) {
5050
if (stringEnd > 0) {
5151
final int len = stringEnd - 1 - _inputPtr;
52-
// For now, we can use `len` for `charCount` because we only support ascii-encoded unescaped strings,
52+
// For now, we can use `len` for `stringLength` because we only support ascii-encoded unescaped strings,
5353
// which means each character uses exactly 1 byte.
54-
return new Text(ByteBuffer.wrap(_inputBuffer, _inputPtr, len), len);
54+
return new Text(new XContentString.UTF8Bytes(_inputBuffer, _inputPtr, len), len);
5555
}
5656
return _finishAndReturnText();
5757
}
@@ -75,9 +75,9 @@ protected Text _finishAndReturnText() throws IOException {
7575
if (c == INT_QUOTE) {
7676
stringEnd = ptr + 1;
7777
final int len = ptr - startPtr;
78-
// For now, we can use `len` for `charCount` because we only support ascii-encoded unescaped strings,
78+
// For now, we can use `len` for `stringLength` because we only support ascii-encoded unescaped strings,
7979
// which means each character uses exactly 1 byte.
80-
return new Text(ByteBuffer.wrap(inputBuffer, startPtr, len), len);
80+
return new Text(new XContentString.UTF8Bytes(inputBuffer, startPtr, len), len);
8181
}
8282
return null;
8383
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import org.elasticsearch.core.CheckedConsumer;
1818
import org.elasticsearch.test.ESTestCase;
1919
import org.elasticsearch.xcontent.XContentBuilder;
20+
import org.elasticsearch.xcontent.XContentString;
2021
import org.elasticsearch.xcontent.json.JsonXContent;
2122
import org.hamcrest.Matchers;
2223

2324
import java.io.IOException;
24-
import java.nio.ByteBuffer;
2525
import java.nio.charset.StandardCharsets;
2626

2727
public class ESUTF8StreamJsonParserTests extends ESTestCase {
@@ -35,8 +35,8 @@ private void testParseJson(String input, CheckedConsumer<ESUTF8StreamJsonParser,
3535
test.accept((ESUTF8StreamJsonParser) parser);
3636
}
3737

38-
private void assertTextRef(ByteBuffer textRef, String expectedValue) {
39-
assertThat(textRef, Matchers.equalTo(StandardCharsets.UTF_8.encode(expectedValue)));
38+
private void assertTextRef(XContentString.UTF8Bytes textRef, String expectedValue) {
39+
assertThat(textRef, Matchers.equalTo(new XContentString.UTF8Bytes(expectedValue.getBytes(StandardCharsets.UTF_8))));
4040
}
4141

4242
public void testGetValueAsText() throws IOException {
@@ -45,10 +45,10 @@ public void testGetValueAsText() throws IOException {
4545
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
4646
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
4747

48-
ByteBuffer textRef = parser.getValueAsText().bytes();
48+
var textRef = parser.getValueAsText().bytes();
4949
assertThat(textRef, Matchers.notNullValue());
50-
assertThat(textRef.arrayOffset() + textRef.position(), Matchers.equalTo(9));
51-
assertThat(textRef.limit(), Matchers.equalTo(12));
50+
assertThat(textRef.offset(), Matchers.equalTo(9));
51+
assertThat(textRef.offset() + textRef.length(), Matchers.equalTo(12));
5252
assertTextRef(textRef, "bar");
5353

5454
assertThat(parser.getValueAsString(), Matchers.equalTo("bar"));
@@ -82,28 +82,28 @@ public void testGetValueAsText() throws IOException {
8282

8383
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
8484
{
85-
ByteBuffer textRef = parser.getValueAsText().bytes();
85+
var textRef = parser.getValueAsText().bytes();
8686
assertThat(textRef, Matchers.notNullValue());
87-
assertThat(textRef.arrayOffset() + textRef.position(), Matchers.equalTo(10));
88-
assertThat(textRef.limit(), Matchers.equalTo(15));
87+
assertThat(textRef.offset(), Matchers.equalTo(10));
88+
assertThat(textRef.offset() + textRef.length(), Matchers.equalTo(15));
8989
assertTextRef(textRef, "lorem");
9090
}
9191

9292
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
9393
{
94-
ByteBuffer textRef = parser.getValueAsText().bytes();
94+
var textRef = parser.getValueAsText().bytes();
9595
assertThat(textRef, Matchers.notNullValue());
96-
assertThat(textRef.arrayOffset() + textRef.position(), Matchers.equalTo(19));
97-
assertThat(textRef.limit(), Matchers.equalTo(24));
96+
assertThat(textRef.offset(), Matchers.equalTo(19));
97+
assertThat(textRef.offset() + textRef.length(), Matchers.equalTo(24));
9898
assertTextRef(textRef, "ipsum");
9999
}
100100

101101
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
102102
{
103-
ByteBuffer textRef = parser.getValueAsText().bytes();
103+
var textRef = parser.getValueAsText().bytes();
104104
assertThat(textRef, Matchers.notNullValue());
105-
assertThat(textRef.arrayOffset() + textRef.position(), Matchers.equalTo(28));
106-
assertThat(textRef.limit(), Matchers.equalTo(33));
105+
assertThat(textRef.offset(), Matchers.equalTo(28));
106+
assertThat(textRef.offset() + textRef.length(), Matchers.equalTo(33));
107107
assertTextRef(textRef, "dolor");
108108
}
109109

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.apache.lucene.util.automaton.CompiledAutomaton;
3535
import org.apache.lucene.util.automaton.CompiledAutomaton.AUTOMATON_TYPE;
3636
import org.apache.lucene.util.automaton.Operations;
37-
import org.elasticsearch.common.bytes.BytesReference;
3837
import org.elasticsearch.common.lucene.BytesRefs;
3938
import org.elasticsearch.common.lucene.Lucene;
4039
import org.elasticsearch.common.lucene.search.AutomatonQueries;
@@ -1153,7 +1152,9 @@ private boolean indexValue(DocumentParserContext context, XContentString value)
11531152
context.addIgnoredField(fullPath());
11541153
if (isSyntheticSource) {
11551154
// Save a copy of the field so synthetic source can load it
1156-
context.doc().add(new StoredField(originalName(), BytesReference.fromByteBuffer(value.bytes()).toBytesRef()));
1155+
var utfBytes = value.bytes();
1156+
var bytesRef = new BytesRef(utfBytes.bytes(), utfBytes.offset(), utfBytes.length());
1157+
context.doc().add(new StoredField(originalName(), bytesRef));
11571158
}
11581159
return false;
11591160
}
@@ -1163,7 +1164,8 @@ private boolean indexValue(DocumentParserContext context, XContentString value)
11631164
value = new Text(normalizedString);
11641165
}
11651166

1166-
var binaryValue = BytesReference.fromByteBuffer(value.bytes()).toBytesRef();
1167+
var utfBytes = value.bytes();
1168+
var binaryValue = new BytesRef(utfBytes.bytes(), utfBytes.offset(), utfBytes.length());
11671169
if (fieldType().isDimension()) {
11681170
context.getRoutingFields().addString(fieldType().name(), binaryValue);
11691171
}

server/src/test/java/org/elasticsearch/common/xcontent/BaseXContentTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ public void testText() throws Exception {
396396
}
397397

398398
public void testOptimizedText() throws Exception {
399-
final var random = ByteBuffer.wrap(randomBytes());
399+
final var random = new XContentString.UTF8Bytes(randomBytes());
400400
XContentBuilder builder = builder().startObject().field("text", new Text(random)).endObject();
401401

402402
try (XContentParser parser = createParser(xcontentType().xContent(), BytesReference.bytes(builder))) {

0 commit comments

Comments
 (0)