Skip to content

Commit 0af1611

Browse files
committed
Rename to UTF8Bytes and clean up javadoc
1 parent 7bb2f17 commit 0af1611

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.nio.charset.StandardCharsets;
1414

1515
/**
16-
* Both {@link String} and {@link EncodedBytes} representation of the text. Starts with one of those, and if
16+
* Both {@link String} and {@link UTF8Bytes} representation of the text. Starts with one of those, and if
1717
* the other is requested, caches the other one in a local reference so no additional conversion will be needed.
1818
*/
1919
public final class Text implements XContentString, Comparable<Text>, ToXContentFragment {
@@ -31,45 +31,46 @@ public static Text[] convertFromStringArray(String[] strings) {
3131
return texts;
3232
}
3333

34-
private EncodedBytes bytes;
35-
private String text;
34+
private UTF8Bytes bytes;
35+
private String string;
3636
private int hash;
3737
private int stringLength = -1;
3838

3939
/**
40-
* Construct a Text from a UTF-8 encoded ByteBuffer. Since no string length is specified, {@link #stringLength()}
40+
* Construct a Text from encoded UTF8Bytes. Since no string length is specified, {@link #stringLength()}
4141
* will perform a string conversion to measure the string length.
4242
*/
43-
public Text(EncodedBytes bytes) {
43+
public Text(UTF8Bytes bytes) {
4444
this.bytes = bytes;
4545
}
4646

4747
/**
48-
* Construct a Text from a UTF-8 encoded ByteBuffer and an explicit string length. Used to avoid string conversion
49-
* in {@link #stringLength()}.
48+
* Construct a Text from encoded UTF8Bytes and an explicit string length. Used to avoid string conversion
49+
* in {@link #stringLength()}. The provided stringLength should match the value that would
50+
* be calculated by {@link Text#Text(UTF8Bytes)}.
5051
*/
51-
public Text(EncodedBytes bytes, int stringLength) {
52+
public Text(UTF8Bytes bytes, int stringLength) {
5253
this.bytes = bytes;
5354
this.stringLength = stringLength;
5455
}
5556

56-
public Text(String text) {
57-
this.text = text;
57+
public Text(String string) {
58+
this.string = string;
5859
}
5960

6061
/**
61-
* Whether an {@link EncodedBytes} view of the data is already materialized.
62+
* Whether an {@link UTF8Bytes} view of the data is already materialized.
6263
*/
6364
public boolean hasBytes() {
6465
return bytes != null;
6566
}
6667

6768
@Override
68-
public EncodedBytes bytes() {
69+
public UTF8Bytes bytes() {
6970
if (bytes == null) {
70-
var byteBuff = StandardCharsets.UTF_8.encode(text);
71+
var byteBuff = StandardCharsets.UTF_8.encode(string);
7172
assert byteBuff.hasArray();
72-
bytes = new EncodedBytes(byteBuff.array(), byteBuff.arrayOffset() + byteBuff.position(), byteBuff.remaining());
73+
bytes = new UTF8Bytes(byteBuff.array(), byteBuff.arrayOffset() + byteBuff.position(), byteBuff.remaining());
7374
}
7475
return bytes;
7576
}
@@ -78,16 +79,17 @@ public EncodedBytes bytes() {
7879
* Whether a {@link String} view of the data is already materialized.
7980
*/
8081
public boolean hasString() {
81-
return text != null;
82+
return string != null;
8283
}
8384

8485
@Override
8586
public String string() {
86-
if (text == null) {
87+
if (string == null) {
8788
var byteBuff = ByteBuffer.wrap(bytes.bytes(), bytes.offset(), bytes.length());
88-
text = StandardCharsets.UTF_8.decode(byteBuff).toString();
89+
string = StandardCharsets.UTF_8.decode(byteBuff).toString();
90+
assert (stringLength < 0) || (string.length() == stringLength);
8991
}
90-
return text;
92+
return string;
9193
}
9294

9395
@Override

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
import java.nio.ByteBuffer;
1313

1414
public interface XContentString {
15-
record EncodedBytes(byte[] bytes, int offset, int length) implements Comparable<EncodedBytes> {
16-
public EncodedBytes(byte[] bytes) {
15+
record UTF8Bytes(byte[] bytes, int offset, int length) implements Comparable<UTF8Bytes> {
16+
public UTF8Bytes(byte[] bytes) {
1717
this(bytes, 0, bytes.length);
1818
}
1919

2020
@Override
21-
public int compareTo(EncodedBytes o) {
21+
public int compareTo(UTF8Bytes o) {
22+
if (this.bytes == o.bytes && this.offset == o.offset && this.length == o.length) {
23+
return 0;
24+
}
25+
2226
return ByteBuffer.wrap(bytes, offset, length).compareTo(ByteBuffer.wrap(o.bytes, o.offset, o.length));
2327
}
2428

@@ -31,7 +35,7 @@ public boolean equals(Object o) {
3135
return false;
3236
}
3337

34-
return this.compareTo((EncodedBytes) o) == 0;
38+
return this.compareTo((UTF8Bytes) o) == 0;
3539
}
3640

3741
@Override
@@ -46,9 +50,9 @@ public int hashCode() {
4650
String string();
4751

4852
/**
49-
* Returns a UTF8-encoded {@link ByteBuffer} view of the data.
53+
* Returns an encoded {@link UTF8Bytes} view of the data.
5054
*/
51-
EncodedBytes bytes();
55+
UTF8Bytes bytes();
5256

5357
/**
5458
* Returns the number of characters in the represented string.

libs/x-content/src/test/java/org/elasticsearch/xcontent/TextTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class TextTests extends ESTestCase {
1717
public void testConvertToBytes() {
1818
String value = randomUnicodeOfLength(randomInt(128));
1919
byte[] encodedArr = value.getBytes(StandardCharsets.UTF_8);
20-
var encoded = new XContentString.EncodedBytes(encodedArr);
20+
var encoded = new XContentString.UTF8Bytes(encodedArr);
2121

2222
var text = new Text(value);
2323
assertTrue(text.hasString());
@@ -39,7 +39,7 @@ public void testConvertToBytes() {
3939
public void testConvertToString() {
4040
String value = randomUnicodeOfLength(randomInt(128));
4141
byte[] encodedArr = value.getBytes(StandardCharsets.UTF_8);
42-
var encoded = new XContentString.EncodedBytes(encodedArr);
42+
var encoded = new XContentString.UTF8Bytes(encodedArr);
4343

4444
var text = new Text(encoded);
4545
assertFalse(text.hasString());
@@ -62,7 +62,7 @@ public void testStringLength() {
6262
int stringLength = randomInt(128);
6363
String value = randomUnicodeOfLength(stringLength);
6464
byte[] encodedArr = value.getBytes(StandardCharsets.UTF_8);
65-
var encoded = new XContentString.EncodedBytes(encodedArr);
65+
var encoded = new XContentString.UTF8Bytes(encodedArr);
6666

6767
{
6868
var text = new Text(value);
@@ -88,7 +88,7 @@ public void testStringLength() {
8888
public void testEquals() {
8989
String value = randomUnicodeOfLength(randomInt(128));
9090
byte[] encodedArr = value.getBytes(StandardCharsets.UTF_8);
91-
var encoded = new XContentString.EncodedBytes(encodedArr);
91+
var encoded = new XContentString.UTF8Bytes(encodedArr);
9292

9393
{
9494
var text1 = new Text(value);
@@ -112,7 +112,7 @@ public void testEquals() {
112112
public void testCompareTo() {
113113
String value1 = randomUnicodeOfLength(randomInt(128));
114114
byte[] encodedArr1 = value1.getBytes(StandardCharsets.UTF_8);
115-
var encoded1 = new XContentString.EncodedBytes(encodedArr1);
115+
var encoded1 = new XContentString.UTF8Bytes(encodedArr1);
116116

117117
{
118118
var text1 = new Text(value1);
@@ -134,7 +134,7 @@ public void testCompareTo() {
134134

135135
String value2 = randomUnicodeOfLength(randomInt(128));
136136
byte[] encodedArr2 = value2.getBytes(StandardCharsets.UTF_8);
137-
var encoded2 = new XContentString.EncodedBytes(encodedArr2);
137+
var encoded2 = new XContentString.UTF8Bytes(encodedArr2);
138138

139139
int compSign = (int) Math.signum(encoded1.compareTo(encoded2));
140140

server/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ private Text readText(int length) throws IOException {
391391
if (length > 0) {
392392
readBytes(bytes, 0, length);
393393
}
394-
var encoded = new XContentString.EncodedBytes(bytes);
394+
var encoded = new XContentString.UTF8Bytes(bytes);
395395
return new Text(encoded);
396396
}
397397

server/src/main/java/org/elasticsearch/search/suggest/term/TermSuggester.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public TermSuggestion innerExecute(String name, TermSuggestionContext suggestion
4848
suggestion.getDirectSpellCheckerSettings().suggestMode()
4949
);
5050
var termBytes = token.term.bytes();
51-
var termEncoded = new XContentString.EncodedBytes(termBytes.bytes, termBytes.offset, termBytes.length);
51+
var termEncoded = new XContentString.UTF8Bytes(termBytes.bytes, termBytes.offset, termBytes.length);
5252
Text key = new Text(termEncoded);
5353
TermSuggestion.Entry resultEntry = new TermSuggestion.Entry(key, token.startOffset, token.endOffset - token.startOffset);
5454
for (SuggestWord suggestWord : suggestedWords) {
@@ -99,7 +99,7 @@ protected TermSuggestion emptySuggestion(String name, TermSuggestionContext sugg
9999
List<Token> tokens = queryTerms(suggestion, spare);
100100
for (Token token : tokens) {
101101
var termBytes = token.term.bytes();
102-
var termEncoded = new XContentString.EncodedBytes(termBytes.bytes, termBytes.offset, termBytes.length);
102+
var termEncoded = new XContentString.UTF8Bytes(termBytes.bytes, termBytes.offset, termBytes.length);
103103
Text key = new Text(termEncoded);
104104
TermSuggestion.Entry resultEntry = new TermSuggestion.Entry(key, token.startOffset, token.endOffset - token.startOffset);
105105
termSuggestion.addTerm(resultEntry);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public void testText() throws Exception {
379379
assertResult("{'text':'foo bar'}", () -> builder().startObject().field("text", new Text("foo bar")).endObject());
380380

381381
final var random = randomBytes();
382-
XContentBuilder builder = builder().startObject().field("text", new Text(new XContentString.EncodedBytes(random))).endObject();
382+
XContentBuilder builder = builder().startObject().field("text", new Text(new XContentString.UTF8Bytes(random))).endObject();
383383

384384
try (XContentParser parser = createParser(xcontentType().xContent(), BytesReference.bytes(builder))) {
385385
assertSame(parser.nextToken(), Token.START_OBJECT);
@@ -597,7 +597,7 @@ public void testObjects() throws Exception {
597597
objects.put("{'objects':['a','b','c']}", new Object[] { "a", "b", "c" });
598598
objects.put(
599599
"{'objects':['a','b','c']}",
600-
new Object[] { new Text("a"), new Text(new XContentString.EncodedBytes("b".getBytes(StandardCharsets.UTF_8))), new Text("c") }
600+
new Object[] { new Text("a"), new Text(new XContentString.UTF8Bytes("b".getBytes(StandardCharsets.UTF_8))), new Text("c") }
601601
);
602602
objects.put("{'objects':null}", null);
603603
objects.put("{'objects':[null,null,null]}", new Object[] { null, null, null });
@@ -644,7 +644,7 @@ public void testObject() throws Exception {
644644
object.put("{'object':1}", (short) 1);
645645
object.put("{'object':'string'}", "string");
646646
object.put("{'object':'a'}", new Text("a"));
647-
object.put("{'object':'b'}", new Text(new XContentString.EncodedBytes("b".getBytes(StandardCharsets.UTF_8))));
647+
object.put("{'object':'b'}", new Text(new XContentString.UTF8Bytes("b".getBytes(StandardCharsets.UTF_8))));
648648
object.put("{'object':null}", null);
649649
object.put("{'object':'OPEN'}", IndexMetadata.State.OPEN);
650650
object.put("{'object':'NM'}", DistanceUnit.NAUTICALMILES);

0 commit comments

Comments
 (0)