Skip to content

Commit a40bee3

Browse files
committed
Move Text to :libs:x-content
1 parent b3f4e04 commit a40bee3

38 files changed

+91
-67
lines changed

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

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@
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.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;
9+
package org.elasticsearch.xcontent;
1610

1711
import java.io.IOException;
12+
import java.nio.ByteBuffer;
1813
import java.nio.charset.StandardCharsets;
1914

2015
/**
21-
* Both {@link String} and {@link BytesReference} representation of the text. Starts with one of those, and if
16+
* Both {@link String} and {@link ByteBuffer} representation of the text. Starts with one of those, and if
2217
* the other is requests, caches the other one in a local reference so no additional conversion will be needed.
2318
*/
2419
public final class Text implements Comparable<Text>, ToXContentFragment {
@@ -36,31 +31,37 @@ public static Text[] convertFromStringArray(String[] strings) {
3631
return texts;
3732
}
3833

39-
private BytesReference bytes;
34+
private ByteBuffer bytes;
4035
private String text;
4136
private int hash;
37+
private int length = -1;
4238

43-
public Text(BytesReference bytes) {
39+
public Text(ByteBuffer bytes) {
4440
this.bytes = bytes;
4541
}
4642

43+
public Text(ByteBuffer bytes, int length) {
44+
this.bytes = bytes;
45+
this.length = length;
46+
}
47+
4748
public Text(String text) {
4849
this.text = text;
4950
}
5051

5152
/**
52-
* Whether a {@link BytesReference} view of the data is already materialized.
53+
* Whether a {@link ByteBuffer} view of the data is already materialized.
5354
*/
5455
public boolean hasBytes() {
5556
return bytes != null;
5657
}
5758

5859
/**
59-
* Returns a {@link BytesReference} view of the data.
60+
* Returns a {@link ByteBuffer} view of the data.
6061
*/
61-
public BytesReference bytes() {
62+
public ByteBuffer bytes() {
6263
if (bytes == null) {
63-
bytes = new BytesArray(text.getBytes(StandardCharsets.UTF_8));
64+
bytes = StandardCharsets.UTF_8.encode(text);
6465
}
6566
return bytes;
6667
}
@@ -76,7 +77,20 @@ public boolean hasString() {
7677
* Returns a {@link String} view of the data.
7778
*/
7879
public String string() {
79-
return text == null ? bytes.utf8ToString() : text;
80+
if (text == null) {
81+
text = StandardCharsets.UTF_8.decode(bytes).toString();
82+
}
83+
return text;
84+
}
85+
86+
/**
87+
* Returns the number of characters in the represented string
88+
*/
89+
public int length() {
90+
if (length < 0) {
91+
length = string().length();
92+
}
93+
return length;
8094
}
8195

8296
@Override
@@ -115,8 +129,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
115129
} else {
116130
// TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a
117131
// request to jackson to support InputStream as well?
118-
BytesRef br = this.bytes().toBytesRef();
119-
return builder.utf8Value(br.bytes, br.offset, br.length);
132+
assert bytes.hasArray();
133+
return builder.utf8Value(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.remaining());
120134
}
121135
}
122136
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.elasticsearch.common.settings.ClusterSettings;
3737
import org.elasticsearch.common.settings.Setting;
3838
import org.elasticsearch.common.settings.Settings;
39-
import org.elasticsearch.common.text.Text;
4039
import org.elasticsearch.common.util.CollectionUtils;
4140
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
4241
import org.elasticsearch.common.util.concurrent.CountDown;
@@ -56,6 +55,7 @@
5655
import org.elasticsearch.tasks.TaskManager;
5756
import org.elasticsearch.threadpool.Scheduler;
5857
import org.elasticsearch.threadpool.ThreadPool;
58+
import org.elasticsearch.xcontent.Text;
5959

6060
import java.util.ArrayList;
6161
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;
1716
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: 8 additions & 4 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;
2726
import org.elasticsearch.common.util.Maps;
2827
import org.elasticsearch.common.util.set.Sets;
2928
import org.elasticsearch.core.CharArrays;
3029
import org.elasticsearch.core.Nullable;
3130
import org.elasticsearch.core.TimeValue;
31+
import org.elasticsearch.xcontent.Text;
3232

3333
import java.io.EOFException;
3434
import java.io.FilterInputStream;
@@ -391,13 +391,17 @@ public Text readOptionalText() throws IOException {
391391
if (length == -1) {
392392
return null;
393393
}
394-
return new Text(readBytesReference(length));
394+
var byteBuffs = BytesReference.toByteBuffers(readBytesReference(length));
395+
assert byteBuffs.length == 1;
396+
return new Text(byteBuffs[0]);
395397
}
396398

397399
public Text readText() throws IOException {
398-
// use StringAndBytes so we can cache the string if it's ever converted to it
400+
// use Text so we can cache the string if it's ever converted to it
399401
int length = readInt();
400-
return new Text(readBytesReference(length));
402+
var byteBuffs = BytesReference.toByteBuffers(readBytesReference(length));
403+
assert byteBuffs.length == 1;
404+
return new Text(byteBuffs[0]);
401405
}
402406

403407
@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
@@ -22,11 +22,11 @@
2222
import org.elasticsearch.common.geo.GeoPoint;
2323
import org.elasticsearch.common.io.stream.Writeable.Writer;
2424
import org.elasticsearch.common.settings.SecureString;
25-
import org.elasticsearch.common.text.Text;
2625
import org.elasticsearch.common.util.ByteUtils;
2726
import org.elasticsearch.core.CharArrays;
2827
import org.elasticsearch.core.Nullable;
2928
import org.elasticsearch.core.TimeValue;
29+
import org.elasticsearch.xcontent.Text;
3030
import org.elasticsearch.xcontent.XContentType;
3131

3232
import java.io.IOException;
@@ -419,7 +419,7 @@ public void writeText(Text text) throws IOException {
419419
writeInt(spare.length());
420420
write(spare.bytes(), 0, spare.length());
421421
} else {
422-
BytesReference bytes = text.bytes();
422+
BytesReference bytes = BytesReference.fromByteBuffer(text.bytes());
423423
writeInt(bytes.length());
424424
bytes.writeTo(this);
425425
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
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;
2423
import org.elasticsearch.common.util.Maps;
2524
import org.elasticsearch.common.xcontent.ChunkedToXContent;
2625
import org.elasticsearch.common.xcontent.XContentHelper;
@@ -39,6 +38,7 @@
3938
import org.elasticsearch.search.lookup.Source;
4039
import org.elasticsearch.transport.LeakTracker;
4140
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;
1615
import org.elasticsearch.core.Nullable;
1716
import org.elasticsearch.index.shard.ShardId;
1817
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,7 +21,6 @@
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;
2524
import org.elasticsearch.index.IndexSettings;
2625
import org.elasticsearch.index.mapper.IdFieldMapper;
2726
import org.elasticsearch.index.mapper.MappedFieldType;
@@ -34,6 +33,7 @@
3433
import org.elasticsearch.lucene.search.uhighlight.Snippet;
3534
import org.elasticsearch.search.fetch.FetchContext;
3635
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,7 +23,6 @@
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;
2726
import org.elasticsearch.common.util.CollectionUtils;
2827
import org.elasticsearch.index.mapper.MappedFieldType;
2928
import org.elasticsearch.index.mapper.TextSearchInfo;
@@ -33,6 +32,7 @@
3332
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext.Field;
3433
import org.elasticsearch.search.fetch.subphase.highlight.SearchHighlightContext.FieldOptions;
3534
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
@@ -12,7 +12,7 @@
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;
15+
import org.elasticsearch.xcontent.Text;
1616
import org.elasticsearch.xcontent.ToXContentFragment;
1717
import org.elasticsearch.xcontent.XContentBuilder;
1818

0 commit comments

Comments
 (0)