Skip to content

Commit 58b0fbc

Browse files
[8.19] Revert changes to Text class (elastic#128483) (elastic#128486)
* Revert changes to Text class (elastic#128483) * Revert "Fix the Text class package change in example plugins (elastic#128316)" This reverts commit cc48648. * Revert "Update Text class to use native java ByteBuffer (elastic#127666)" This reverts commit db0c3c7. * [CI] Auto commit changes from spotless --------- Co-authored-by: elasticsearchmachine <[email protected]>
1 parent 0a6019b commit 58b0fbc

File tree

41 files changed

+73
-135
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+73
-135
lines changed

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

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

plugins/examples/custom-suggester/src/main/java/org/elasticsearch/example/customsuggester/CustomSuggester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import org.apache.lucene.search.IndexSearcher;
1313
import org.apache.lucene.util.CharsRefBuilder;
14-
import org.elasticsearch.xcontent.Text;
14+
import org.elasticsearch.common.text.Text;
1515
import org.elasticsearch.search.suggest.Suggest;
1616
import org.elasticsearch.search.suggest.Suggester;
1717

plugins/examples/custom-suggester/src/main/java/org/elasticsearch/example/customsuggester/CustomSuggestion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
14-
import org.elasticsearch.xcontent.Text;
14+
import org.elasticsearch.common.text.Text;
1515
import org.elasticsearch.search.suggest.Suggest;
1616
import org.elasticsearch.xcontent.ParseField;
1717
import org.elasticsearch.xcontent.XContentBuilder;

server/src/main/java/org/elasticsearch/cluster/service/MasterService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.common.settings.ClusterSettings;
3636
import org.elasticsearch.common.settings.Setting;
3737
import org.elasticsearch.common.settings.Settings;
38+
import org.elasticsearch.common.text.Text;
3839
import org.elasticsearch.common.util.CollectionUtils;
3940
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
4041
import org.elasticsearch.common.util.concurrent.CountDown;
@@ -53,7 +54,6 @@
5354
import org.elasticsearch.tasks.TaskManager;
5455
import org.elasticsearch.threadpool.Scheduler;
5556
import org.elasticsearch.threadpool.ThreadPool;
56-
import org.elasticsearch.xcontent.Text;
5757

5858
import java.util.ArrayList;
5959
import java.util.Collections;

server/src/main/java/org/elasticsearch/cluster/service/PendingClusterTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import org.elasticsearch.common.io.stream.StreamInput;
1414
import org.elasticsearch.common.io.stream.StreamOutput;
1515
import org.elasticsearch.common.io.stream.Writeable;
16+
import org.elasticsearch.common.text.Text;
1617
import org.elasticsearch.core.TimeValue;
17-
import org.elasticsearch.xcontent.Text;
1818

1919
import java.io.IOException;
2020

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import org.elasticsearch.common.collect.ImmutableOpenMap;
2424
import org.elasticsearch.common.geo.GeoPoint;
2525
import org.elasticsearch.common.settings.SecureString;
26+
import org.elasticsearch.common.text.Text;
2627
import org.elasticsearch.common.util.Maps;
2728
import org.elasticsearch.common.util.set.Sets;
2829
import org.elasticsearch.core.CharArrays;
2930
import org.elasticsearch.core.Nullable;
3031
import org.elasticsearch.core.TimeValue;
31-
import org.elasticsearch.xcontent.Text;
3232

3333
import java.io.EOFException;
3434
import java.io.FilterInputStream;
@@ -379,23 +379,13 @@ public Text readOptionalText() throws IOException {
379379
if (length == -1) {
380380
return null;
381381
}
382-
byte[] bytes = new byte[length];
383-
if (length > 0) {
384-
readBytes(bytes, 0, length);
385-
}
386-
var byteBuff = ByteBuffer.wrap(bytes);
387-
return new Text(byteBuff);
382+
return new Text(readBytesReference(length));
388383
}
389384

390385
public Text readText() throws IOException {
391-
// use Text so we can cache the string if it's ever converted to it
386+
// use StringAndBytes so we can cache the string if it's ever converted to it
392387
int length = readInt();
393-
byte[] bytes = new byte[length];
394-
if (length > 0) {
395-
readBytes(bytes, 0, length);
396-
}
397-
var byteBuff = ByteBuffer.wrap(bytes);
398-
return new Text(byteBuff);
388+
return new Text(readBytesReference(length));
399389
}
400390

401391
@Nullable

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import org.elasticsearch.common.geo.GeoPoint;
2222
import org.elasticsearch.common.io.stream.Writeable.Writer;
2323
import org.elasticsearch.common.settings.SecureString;
24+
import org.elasticsearch.common.text.Text;
2425
import org.elasticsearch.common.util.ByteUtils;
2526
import org.elasticsearch.core.CharArrays;
2627
import org.elasticsearch.core.Nullable;
2728
import org.elasticsearch.core.TimeValue;
28-
import org.elasticsearch.xcontent.Text;
2929
import org.elasticsearch.xcontent.XContentType;
3030

3131
import java.io.IOException;
@@ -399,7 +399,7 @@ public void writeText(Text text) throws IOException {
399399
writeInt(spare.length());
400400
write(spare.bytes(), 0, spare.length());
401401
} else {
402-
BytesReference bytes = BytesReference.fromByteBuffer(text.bytes());
402+
BytesReference bytes = text.bytes();
403403
writeInt(bytes.length());
404404
bytes.writeTo(this);
405405
}

libs/x-content/src/main/java/org/elasticsearch/xcontent/Text.java renamed to server/src/main/java/org/elasticsearch/common/text/Text.java

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@
66
* your election, the "Elastic License 2.0", the "GNU Affero General Public
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
9-
package org.elasticsearch.xcontent;
9+
package org.elasticsearch.common.text;
10+
11+
import org.apache.lucene.util.BytesRef;
12+
import org.elasticsearch.common.bytes.BytesArray;
13+
import org.elasticsearch.common.bytes.BytesReference;
14+
import org.elasticsearch.xcontent.ToXContentFragment;
15+
import org.elasticsearch.xcontent.XContentBuilder;
1016

1117
import java.io.IOException;
12-
import java.nio.ByteBuffer;
1318
import java.nio.charset.StandardCharsets;
1419

1520
/**
16-
* Both {@link String} and {@link ByteBuffer} representation of the text. Starts with one of those, and if
21+
* Both {@link String} and {@link BytesReference} representation of the text. Starts with one of those, and if
1722
* the other is requests, caches the other one in a local reference so no additional conversion will be needed.
1823
*/
19-
public final class Text implements XContentString, Comparable<Text>, ToXContentFragment {
24+
public final class Text implements Comparable<Text>, ToXContentFragment {
2025

2126
public static final Text[] EMPTY_ARRAY = new Text[0];
2227

@@ -31,43 +36,31 @@ public static Text[] convertFromStringArray(String[] strings) {
3136
return texts;
3237
}
3338

34-
private ByteBuffer bytes;
39+
private BytesReference bytes;
3540
private String text;
3641
private int hash;
37-
private int stringLength = -1;
38-
39-
/**
40-
* Construct a Text from a UTF-8 encoded ByteBuffer. Since no string length is specified, {@link #stringLength()}
41-
* will perform a string conversion to measure the string length.
42-
*/
43-
public Text(ByteBuffer bytes) {
44-
this.bytes = bytes;
45-
}
4642

47-
/**
48-
* Construct a Text from a UTF-8 encoded ByteBuffer and an explicit string length. Used to avoid string conversion
49-
* in {@link #stringLength()}.
50-
*/
51-
public Text(ByteBuffer bytes, int stringLength) {
43+
public Text(BytesReference bytes) {
5244
this.bytes = bytes;
53-
this.stringLength = stringLength;
5445
}
5546

5647
public Text(String text) {
5748
this.text = text;
5849
}
5950

6051
/**
61-
* Whether a {@link ByteBuffer} view of the data is already materialized.
52+
* Whether a {@link BytesReference} view of the data is already materialized.
6253
*/
6354
public boolean hasBytes() {
6455
return bytes != null;
6556
}
6657

67-
@Override
68-
public ByteBuffer bytes() {
58+
/**
59+
* Returns a {@link BytesReference} view of the data.
60+
*/
61+
public BytesReference bytes() {
6962
if (bytes == null) {
70-
bytes = StandardCharsets.UTF_8.encode(text);
63+
bytes = new BytesArray(text.getBytes(StandardCharsets.UTF_8));
7164
}
7265
return bytes;
7366
}
@@ -79,20 +72,11 @@ public boolean hasString() {
7972
return text != null;
8073
}
8174

82-
@Override
75+
/**
76+
* Returns a {@link String} view of the data.
77+
*/
8378
public String string() {
84-
if (text == null) {
85-
text = StandardCharsets.UTF_8.decode(bytes).toString();
86-
}
87-
return text;
88-
}
89-
90-
@Override
91-
public int stringLength() {
92-
if (stringLength < 0) {
93-
stringLength = string().length();
94-
}
95-
return stringLength;
79+
return text == null ? bytes.utf8ToString() : text;
9680
}
9781

9882
@Override
@@ -131,8 +115,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
131115
} else {
132116
// TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a
133117
// request to jackson to support InputStream as well?
134-
assert bytes.hasArray();
135-
return builder.utf8Value(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.remaining());
118+
BytesRef br = this.bytes().toBytesRef();
119+
return builder.utf8Value(br.bytes, br.offset, br.length);
136120
}
137121
}
138122
}

server/src/main/java/org/elasticsearch/search/SearchHit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.common.io.stream.StreamInput;
2121
import org.elasticsearch.common.io.stream.StreamOutput;
2222
import org.elasticsearch.common.io.stream.Writeable;
23+
import org.elasticsearch.common.text.Text;
2324
import org.elasticsearch.common.util.Maps;
2425
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2526
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -38,7 +39,6 @@
3839
import org.elasticsearch.search.lookup.Source;
3940
import org.elasticsearch.transport.LeakTracker;
4041
import org.elasticsearch.transport.RemoteClusterAware;
41-
import org.elasticsearch.xcontent.Text;
4242
import org.elasticsearch.xcontent.ToXContentFragment;
4343
import org.elasticsearch.xcontent.ToXContentObject;
4444
import org.elasticsearch.xcontent.XContentBuilder;

server/src/main/java/org/elasticsearch/search/SearchShardTarget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
1414
import org.elasticsearch.common.io.stream.Writeable;
15+
import org.elasticsearch.common.text.Text;
1516
import org.elasticsearch.core.Nullable;
1617
import org.elasticsearch.index.shard.ShardId;
1718
import org.elasticsearch.transport.RemoteClusterAware;
18-
import org.elasticsearch.xcontent.Text;
1919

2020
import java.io.IOException;
2121
import java.util.Objects;

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/DefaultHighlighter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.common.CheckedSupplier;
2222
import org.elasticsearch.common.Strings;
2323
import org.elasticsearch.common.lucene.Lucene;
24+
import org.elasticsearch.common.text.Text;
2425
import org.elasticsearch.features.NodeFeature;
2526
import org.elasticsearch.index.IndexSettings;
2627
import org.elasticsearch.index.mapper.IdFieldMapper;
@@ -33,7 +34,6 @@
3334
import org.elasticsearch.lucene.search.uhighlight.Snippet;
3435
import org.elasticsearch.search.fetch.FetchContext;
3536
import org.elasticsearch.search.fetch.FetchSubPhase;
36-
import org.elasticsearch.xcontent.Text;
3737

3838
import java.io.IOException;
3939
import java.text.BreakIterator;

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/FastVectorHighlighter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.lucene.search.vectorhighlight.SingleFragListBuilder;
2424
import org.elasticsearch.common.settings.Setting;
2525
import org.elasticsearch.common.settings.Settings;
26+
import org.elasticsearch.common.text.Text;
2627
import org.elasticsearch.common.util.CollectionUtils;
2728
import org.elasticsearch.index.mapper.MappedFieldType;
2829
import org.elasticsearch.index.mapper.TextSearchInfo;
@@ -32,7 +33,6 @@
3233
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext.Field;
3334
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions;
3435
import org.elasticsearch.search.lookup.Source;
35-
import org.elasticsearch.xcontent.Text;
3636

3737
import java.io.IOException;
3838
import java.text.BreakIterator;

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.elasticsearch.common.io.stream.StreamInput;
1414
import org.elasticsearch.common.io.stream.StreamOutput;
1515
import org.elasticsearch.common.io.stream.Writeable;
16-
import org.elasticsearch.xcontent.Text;
16+
import org.elasticsearch.common.text.Text;
1717
import org.elasticsearch.xcontent.ToXContentFragment;
1818
import org.elasticsearch.xcontent.XContentBuilder;
1919
import org.elasticsearch.xcontent.XContentParser;

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/PlainHighlighter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import org.apache.lucene.search.highlight.TextFragment;
2525
import org.apache.lucene.util.BytesRefHash;
2626
import org.elasticsearch.common.lucene.Lucene;
27+
import org.elasticsearch.common.text.Text;
2728
import org.elasticsearch.index.IndexSettings;
2829
import org.elasticsearch.index.mapper.MappedFieldType;
2930
import org.elasticsearch.search.fetch.FetchContext;
3031
import org.elasticsearch.search.fetch.FetchSubPhase;
31-
import org.elasticsearch.xcontent.Text;
3232

3333
import java.io.IOException;
3434
import java.util.ArrayList;

server/src/main/java/org/elasticsearch/search/searchafter/SearchAfterBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
import org.elasticsearch.common.io.stream.StreamInput;
2020
import org.elasticsearch.common.io.stream.StreamOutput;
2121
import org.elasticsearch.common.io.stream.Writeable;
22+
import org.elasticsearch.common.text.Text;
2223
import org.elasticsearch.core.Nullable;
2324
import org.elasticsearch.index.fielddata.IndexFieldData;
2425
import org.elasticsearch.search.DocValueFormat;
2526
import org.elasticsearch.search.sort.SortAndFormats;
2627
import org.elasticsearch.xcontent.ParseField;
27-
import org.elasticsearch.xcontent.Text;
2828
import org.elasticsearch.xcontent.ToXContentObject;
2929
import org.elasticsearch.xcontent.XContentBuilder;
3030
import org.elasticsearch.xcontent.XContentParser;

server/src/main/java/org/elasticsearch/search/suggest/Suggest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
import org.elasticsearch.common.io.stream.StreamInput;
1515
import org.elasticsearch.common.io.stream.StreamOutput;
1616
import org.elasticsearch.common.io.stream.Writeable;
17+
import org.elasticsearch.common.text.Text;
1718
import org.elasticsearch.rest.action.search.RestSearchAction;
1819
import org.elasticsearch.search.aggregations.Aggregation;
1920
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry;
2021
import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
2122
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
2223
import org.elasticsearch.xcontent.ParseField;
23-
import org.elasticsearch.xcontent.Text;
2424
import org.elasticsearch.xcontent.ToXContentFragment;
2525
import org.elasticsearch.xcontent.XContentBuilder;
2626

server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
import org.apache.lucene.search.suggest.document.TopSuggestDocs;
1919
import org.apache.lucene.search.suggest.document.TopSuggestDocsCollector;
2020
import org.apache.lucene.util.CharsRefBuilder;
21+
import org.elasticsearch.common.text.Text;
2122
import org.elasticsearch.index.mapper.CompletionFieldMapper;
2223
import org.elasticsearch.search.suggest.Suggest;
2324
import org.elasticsearch.search.suggest.Suggester;
24-
import org.elasticsearch.xcontent.Text;
2525

2626
import java.io.IOException;
2727
import java.util.Collections;

server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
import org.elasticsearch.common.io.stream.StreamInput;
1515
import org.elasticsearch.common.io.stream.StreamOutput;
1616
import org.elasticsearch.common.lucene.Lucene;
17+
import org.elasticsearch.common.text.Text;
1718
import org.elasticsearch.common.util.Maps;
1819
import org.elasticsearch.common.util.set.Sets;
1920
import org.elasticsearch.search.SearchHit;
2021
import org.elasticsearch.search.suggest.Suggest;
2122
import org.elasticsearch.xcontent.ParseField;
22-
import org.elasticsearch.xcontent.Text;
2323
import org.elasticsearch.xcontent.XContentBuilder;
2424

2525
import java.io.IOException;

0 commit comments

Comments
 (0)