Skip to content

Commit 681ce38

Browse files
committed
Use new BaseText and BaseBytesReference types
1 parent deefb81 commit 681ce38

File tree

17 files changed

+123
-127
lines changed

17 files changed

+123
-127
lines changed

libs/core/src/main/java/org/elasticsearch/core/BaseText.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
public class BaseText {
2222
protected BaseBytesReference bytes;
2323
private String text;
24+
private int length = -1;
2425

2526
public BaseText(BaseBytesReference bytes) {
2627
this.bytes = bytes;
2728
}
2829

30+
public BaseText(BaseBytesReference bytes, int length) {
31+
this.bytes = bytes;
32+
this.length = length;
33+
}
34+
2935
public BaseText(String text) {
3036
this.text = text;
3137
}
@@ -69,4 +75,10 @@ public String toString() {
6975
return string();
7076
}
7177

78+
public int length() {
79+
if (length < 0) {
80+
length = string().length();
81+
}
82+
return length;
83+
}
7284
}

libs/core/src/main/java/org/elasticsearch/core/bytes/BaseBytesArray.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public class BaseBytesArray implements BaseBytesReference {
1414
private final int offset;
1515
private final int length;
1616

17+
int hash = 0;
18+
boolean hashIsZero = false;
19+
1720
public BaseBytesArray(byte[] bytes) {
1821
this(bytes, 0, bytes.length);
1922
}
@@ -48,4 +51,36 @@ public byte[] array() {
4851
public int arrayOffset() {
4952
return offset;
5053
}
54+
55+
@Override
56+
public boolean equals(Object other) {
57+
if (this == other) {
58+
return true;
59+
}
60+
if (other instanceof final BaseBytesReference otherRef) {
61+
if (length != otherRef.length()) {
62+
return false;
63+
}
64+
for (int i = 0; i < length; i++) {
65+
if (get(i) != otherRef.get(i)) {
66+
return false;
67+
}
68+
}
69+
}
70+
return true;
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
if (hash == 0 && hashIsZero == false) {
76+
hash = 1;
77+
for (int i = offset; i < length; i++) {
78+
hash = 31 * hash + bytes[i];
79+
}
80+
if (hash == 0) {
81+
hashIsZero = true;
82+
}
83+
}
84+
return hash;
85+
}
5186
}

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,7 +16,8 @@
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.core.BaseText;
20+
import org.elasticsearch.core.bytes.BaseBytesArray;
2021

2122
import java.io.IOException;
2223
import java.io.InputStream;
@@ -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 BaseText 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 BaseText(new BaseBytesArray(_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 BaseText _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 BaseText(new BaseBytesArray(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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
import com.fasterxml.jackson.core.exc.InputCoercionException;
1818
import com.fasterxml.jackson.core.io.JsonEOFException;
1919

20+
import org.elasticsearch.core.BaseText;
2021
import org.elasticsearch.core.IOUtils;
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 BaseText xContentText() throws IOException {
121121
if (currentToken().isValue() == false) {
122122
throwOnNoText();
123123
}
124124
if (parser instanceof ESUTF8StreamJsonParser esParser) {
125-
var bytesRef = esParser.getValueAsByteRef();
126-
if (bytesRef != null) {
127-
return bytesRef;
125+
var text = esParser.getValueAsText();
126+
if (text != null) {
127+
return text;
128128
}
129129
}
130-
return new XContentString(text());
130+
return new BaseText(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 & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
import org.elasticsearch.common.Strings;
1717
import org.elasticsearch.core.CheckedConsumer;
18+
import org.elasticsearch.core.bytes.BaseBytesArray;
19+
import org.elasticsearch.core.bytes.BaseBytesReference;
1820
import org.elasticsearch.test.ESTestCase;
1921
import org.elasticsearch.xcontent.XContentBuilder;
20-
import org.elasticsearch.xcontent.XContentString;
2122
import org.elasticsearch.xcontent.json.JsonXContent;
2223
import org.hamcrest.Matchers;
2324

2425
import java.io.IOException;
2526
import java.nio.charset.StandardCharsets;
26-
import java.util.Arrays;
2727

2828
public class ESUTF8StreamJsonParserTests extends ESTestCase {
2929

@@ -36,9 +36,8 @@ private void testParseJson(String input, CheckedConsumer<ESUTF8StreamJsonParser,
3636
test.accept((ESUTF8StreamJsonParser) parser);
3737
}
3838

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()));
39+
private void assertTextBytes(BaseBytesReference textRef, String expectedValue) {
40+
assertThat(textRef, Matchers.equalTo(new BaseBytesArray(expectedValue.getBytes(StandardCharsets.UTF_8))));
4241
}
4342

4443
public void testGetValueAsByteRef() throws IOException {
@@ -47,14 +46,14 @@ public void testGetValueAsByteRef() throws IOException {
4746
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
4847
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
4948

50-
XContentString.ByteRef textRef = parser.getValueAsByteRef().getBytes();
49+
var textRef = parser.getValueAsText().bytes();
5150
assertThat(textRef, Matchers.notNullValue());
52-
assertThat(textRef.offset(), Matchers.equalTo(9));
51+
assertThat(textRef.arrayOffset(), Matchers.equalTo(9));
5352
assertThat(textRef.length(), Matchers.equalTo(3));
54-
assertTextRef(textRef, "bar");
53+
assertTextBytes(textRef, "bar");
5554

5655
assertThat(parser.getValueAsString(), Matchers.equalTo("bar"));
57-
assertThat(parser.getValueAsByteRef(), Matchers.nullValue());
56+
assertThat(parser.getValueAsText(), Matchers.nullValue());
5857

5958
assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.END_OBJECT));
6059
});
@@ -64,7 +63,7 @@ public void testGetValueAsByteRef() throws IOException {
6463
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
6564
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
6665

67-
assertThat(parser.getValueAsByteRef(), Matchers.nullValue());
66+
assertThat(parser.getValueAsText(), Matchers.nullValue());
6867
assertThat(parser.getValueAsString(), Matchers.equalTo("bar\"baz\""));
6968
});
7069

@@ -73,7 +72,7 @@ public void testGetValueAsByteRef() throws IOException {
7372
assertThat(parser.nextFieldName(), Matchers.equalTo("foo"));
7473
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
7574

76-
assertThat(parser.getValueAsByteRef(), Matchers.nullValue());
75+
assertThat(parser.getValueAsText(), Matchers.nullValue());
7776
assertThat(parser.getValueAsString(), Matchers.equalTo("bår"));
7877
});
7978

@@ -84,29 +83,29 @@ public void testGetValueAsByteRef() throws IOException {
8483

8584
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
8685
{
87-
XContentString.ByteRef textRef = parser.getValueAsByteRef().getBytes();
86+
var textRef = parser.getValueAsText().bytes();
8887
assertThat(textRef, Matchers.notNullValue());
89-
assertThat(textRef.offset(), Matchers.equalTo(10));
88+
assertThat(textRef.arrayOffset(), Matchers.equalTo(10));
9089
assertThat(textRef.length(), Matchers.equalTo(5));
91-
assertTextRef(textRef, "lorem");
90+
assertTextBytes(textRef, "lorem");
9291
}
9392

9493
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
9594
{
96-
XContentString.ByteRef textRef = parser.getValueAsByteRef().getBytes();
95+
var textRef = parser.getValueAsText().bytes();
9796
assertThat(textRef, Matchers.notNullValue());
98-
assertThat(textRef.offset(), Matchers.equalTo(19));
97+
assertThat(textRef.arrayOffset(), Matchers.equalTo(19));
9998
assertThat(textRef.length(), Matchers.equalTo(5));
100-
assertTextRef(textRef, "ipsum");
99+
assertTextBytes(textRef, "ipsum");
101100
}
102101

103102
assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING));
104103
{
105-
XContentString.ByteRef textRef = parser.getValueAsByteRef().getBytes();
104+
var textRef = parser.getValueAsText().bytes();
106105
assertThat(textRef, Matchers.notNullValue());
107-
assertThat(textRef.offset(), Matchers.equalTo(28));
106+
assertThat(textRef.arrayOffset(), Matchers.equalTo(28));
108107
assertThat(textRef.length(), Matchers.equalTo(5));
109-
assertTextRef(textRef, "dolor");
108+
assertTextBytes(textRef, "dolor");
110109
}
111110

112111
assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.END_ARRAY));
@@ -151,9 +150,9 @@ public void testGetValueRandomized() throws IOException {
151150

152151
String currVal = values[i];
153152
if (validForTextRef(currVal)) {
154-
assertTextRef(parser.getValueAsByteRef().getBytes(), currVal);
153+
assertTextBytes(parser.getValueAsText().bytes(), currVal);
155154
} else {
156-
assertThat(parser.getValueAsByteRef(), Matchers.nullValue());
155+
assertThat(parser.getValueAsText(), Matchers.nullValue());
157156
assertThat(parser.getValueAsString(), Matchers.equalTo(currVal));
158157
}
159158
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.xcontent;
1111

12+
import org.elasticsearch.core.BaseText;
1213
import org.elasticsearch.core.CheckedFunction;
1314
import org.elasticsearch.core.RestApiVersion;
1415

@@ -100,11 +101,11 @@ public String textOrNull() throws IOException {
100101
return delegate().textOrNull();
101102
}
102103

103-
public XContentString xContentText() throws IOException {
104+
public BaseText xContentText() throws IOException {
104105
return delegate().xContentText();
105106
}
106107

107-
public XContentString xContentTextOrNull() throws IOException {
108+
public BaseText xContentTextOrNull() throws IOException {
108109
return delegate().xContentTextOrNull();
109110
}
110111

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.xcontent;
1111

12+
import org.elasticsearch.core.BaseText;
1213
import org.elasticsearch.core.CheckedFunction;
1314
import org.elasticsearch.core.Nullable;
1415
import org.elasticsearch.core.RestApiVersion;
@@ -109,9 +110,9 @@ <T> Map<String, T> map(Supplier<Map<String, T>> mapFactory, CheckedFunction<XCon
109110

110111
String textOrNull() throws IOException;
111112

112-
XContentString xContentText() throws IOException;
113+
BaseText xContentText() throws IOException;
113114

114-
XContentString xContentTextOrNull() throws IOException;
115+
BaseText xContentTextOrNull() throws IOException;
115116

116117
CharBuffer charBufferOrNull() throws IOException;
117118

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

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

0 commit comments

Comments
 (0)