Skip to content

Commit a277aea

Browse files
committed
Some cleanup and comments
1 parent c5d71f7 commit a277aea

File tree

3 files changed

+19
-35
lines changed

3 files changed

+19
-35
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
* defined in JSON specification (RFC-4627 or newer), except
1818
* for BOM handling, which is a property of underlying
1919
* streams.
20+
*
21+
* This class is copied from {@link com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper},
22+
* just modified so that {@code constructParser()} returns an
23+
* instance of {@link ESUTF8StreamJsonParser}.
2024
*/
2125
@SuppressWarnings("all")
2226
public final class ESByteSourceJsonBootstrapper {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public ESUTF8StreamJsonParser(
3636
super(ctxt, features, in, codec, sym, inputBuffer, start, end, bytesPreProcessed, bufferRecyclable);
3737
}
3838

39+
/**
40+
* Method that will try to get underlying UTF-8 encoded bytes of the current string token.
41+
* This is only a best-effort attempt; if there is some reason the bytes cannot be retrieved, this method will return null.
42+
* Currently, this is only implemented for ascii-only strings that do not contain escaped characters.
43+
*/
3944
public ESBytesRef getValueAsByteRef() throws IOException {
4045
if (_currToken == JsonToken.VALUE_STRING && _tokenIncomplete) {
4146
var value = _finishAndReturnByteRef();

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

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,36 +1104,22 @@ public String getOffsetFieldName() {
11041104
return offsetsFieldName;
11051105
}
11061106

1107+
/**
1108+
* Class that holds either a UTF-16 String or a UTF-8 BytesRef, and lazily converts between the two.
1109+
*/
11071110
private static class RawString {
11081111
private BytesRef bytesValue;
11091112
private String stringValue;
1110-
private boolean isNull;
11111113

11121114
RawString(BytesRef bytesValue) {
1113-
if (bytesValue == null) {
1114-
throw new IllegalArgumentException();
1115-
}
1116-
this.bytesValue = bytesValue;
1117-
this.isNull = false;
1115+
this.bytesValue = Objects.requireNonNull(bytesValue);
11181116
}
11191117

11201118
RawString(String stringValue) {
1121-
if (stringValue == null) {
1122-
throw new IllegalArgumentException();
1123-
}
1124-
this.stringValue = stringValue;
1125-
this.isNull = false;
1126-
}
1127-
1128-
RawString() {
1129-
this.isNull = true;
1119+
this.stringValue = Objects.requireNonNull(stringValue);
11301120
}
11311121

11321122
BytesRef bytesValue() {
1133-
if (isNull) {
1134-
return null;
1135-
}
1136-
11371123
if (bytesValue != null) {
11381124
return bytesValue;
11391125
}
@@ -1143,10 +1129,6 @@ BytesRef bytesValue() {
11431129
}
11441130

11451131
String stringValue() {
1146-
if (isNull) {
1147-
return null;
1148-
}
1149-
11501132
if (stringValue != null) {
11511133
return stringValue;
11521134
}
@@ -1155,18 +1137,11 @@ String stringValue() {
11551137
return stringValue;
11561138
}
11571139

1158-
boolean isNull() {
1159-
return isNull;
1160-
}
1161-
11621140
int length() {
1163-
if (isNull) {
1164-
throw new UnsupportedOperationException();
1165-
}
1166-
11671141
if (stringValue != null) {
11681142
return stringValue.length();
11691143
} else {
1144+
// This works because we currently use raw utf-8 encoding only for ascii-only strings.
11701145
return bytesValue.length;
11711146
}
11721147
}
@@ -1182,19 +1157,19 @@ protected void parseCreateField(DocumentParserContext context) throws IOExceptio
11821157
if (stringValue != null) {
11831158
value = new RawString(stringValue);
11841159
} else {
1185-
value = new RawString();
1160+
value = null;
11861161
}
11871162
}
11881163

1189-
if (value.isNull() && fieldType().nullValue != null) {
1164+
if (value == null && fieldType().nullValue != null) {
11901165
value = new RawString(fieldType().nullValue);
11911166
}
11921167

11931168
boolean indexed = indexValue(context, value);
11941169
if (offsetsFieldName != null && context.isImmediateParentAnArray() && context.canAddIgnoredField()) {
11951170
if (indexed) {
11961171
context.getOffSetContext().recordOffset(offsetsFieldName, value.stringValue());
1197-
} else if (value.isNull()) {
1172+
} else if (value == null) {
11981173
context.getOffSetContext().recordNull(offsetsFieldName);
11991174
}
12001175
}
@@ -1219,7 +1194,7 @@ private boolean indexValue(DocumentParserContext context, String value) {
12191194
}
12201195

12211196
private boolean indexValue(DocumentParserContext context, RawString value) throws IOException {
1222-
if (value.isNull()) {
1197+
if (value == null) {
12231198
return false;
12241199
}
12251200

0 commit comments

Comments
 (0)