Skip to content

Commit 6fb1afa

Browse files
committed
More
1 parent 11d900e commit 6fb1afa

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,66 @@ private void writeAdditionalPages(byte[] b, int offset, int length) {
111111
}
112112
}
113113

114+
public void writeUTF8String(String str) throws IOException {
115+
final int charCount = str.length();
116+
if (charCount == 0) {
117+
writeVInt(0);
118+
return;
119+
}
120+
121+
// Optimistically write length assuming all ASCII (1 byte per char)
122+
long startPosition = position();
123+
writeVInt(charCount);
124+
125+
// Ensure we have at least enough capacity for ASCII representation
126+
if (charCount > (pageSize - currentPageOffset)) {
127+
ensureCapacity(charCount);
128+
}
129+
130+
if (writeAsciiChars(str, charCount) == false) {
131+
seek(startPosition);
132+
handleNonAsciiString(str);
133+
}
134+
}
135+
136+
/**
137+
* Fast path for writing ASCII characters. Returns true if all characters
138+
* were ASCII, false if a non-ASCII character was encountered.
139+
*/
140+
private boolean writeAsciiChars(String str, int charCount) {
141+
int charIndex = 0;
142+
143+
while (charIndex < charCount) {
144+
int remainingInPage = pageSize - currentPageOffset;
145+
int charsToWrite = Math.min(remainingInPage, charCount - charIndex);
146+
147+
for (int i = 0; i < charsToWrite; i++) {
148+
char c = str.charAt(charIndex + i);
149+
if (c > 0x7F) {
150+
return false;
151+
}
152+
bytesRefBytes[bytesRefOffset + currentPageOffset + i] = (byte) c;
153+
}
154+
155+
// Update positions for what we wrote
156+
currentPageOffset += charsToWrite;
157+
charIndex += charsToWrite;
158+
159+
// Check if we need to move to next page AFTER writing
160+
if (currentPageOffset == pageSize && charIndex < charCount) {
161+
nextPage();
162+
}
163+
}
164+
165+
return true; // All characters were ASCII
166+
}
167+
168+
private void handleNonAsciiString(String str) throws IOException {
169+
byte[] utf8Bytes = str.getBytes(java.nio.charset.StandardCharsets.UTF_8);
170+
writeVInt(utf8Bytes.length);
171+
writeBytes(utf8Bytes, 0, utf8Bytes.length);
172+
}
173+
114174
@Override
115175
public void writeInt(int i) throws IOException {
116176
final int currentPageOffset = this.currentPageOffset;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package org.elasticsearch.index.mapper;
1111

1212
import org.elasticsearch.core.CheckedFunction;
13-
import org.elasticsearch.ingest.ESONXContentParser;
1413
import org.elasticsearch.xcontent.FilterXContentParser;
1514
import org.elasticsearch.xcontent.FilterXContentParserWrapper;
1615
import org.elasticsearch.xcontent.XContentLocation;
@@ -74,9 +73,7 @@ public Token nextToken() throws IOException {
7473
}
7574

7675
private static Token getNextToken(XContentParser parser) throws IOException {
77-
if (parser instanceof ESONXContentParser eson) {
78-
return eson.nextToken();
79-
} else if (parser instanceof DotExpandingXContentParser dot) {
76+
if (parser instanceof DotExpandingXContentParser dot) {
8077
return dot.nextToken();
8178
} else {
8279
return parser.nextToken();

server/src/main/java/org/elasticsearch/ingest/ESONFlat.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ public BytesReference getSerializedKeyBytes() {
7474
try (RecyclerBytesStreamOutput streamOutput = new RecyclerBytesStreamOutput(getBytesRefRecycler())) {
7575
streamOutput.writeVInt(keys.size());
7676
for (ESONEntry entry : keys) {
77-
String key = entry.key();
78-
byte[] bytes = key == null ? EMPTY_KEY : key.getBytes(StandardCharsets.UTF_8);
79-
streamOutput.writeVInt(bytes.length);
80-
streamOutput.writeBytes(bytes, 0, bytes.length);
77+
String key = entry.key() == null ? "" : entry.key();
78+
// byte[] bytes = key == null ? EMPTY_KEY : key.getBytes(StandardCharsets.UTF_8);
79+
// streamOutput.writeVInt(bytes.length);
80+
// streamOutput.writeBytes(bytes, 0, bytes.length);
81+
streamOutput.writeUTF8String(key);
8182
streamOutput.writeByte(entry.type());
8283
streamOutput.writeInt(entry.offsetOrCount());
8384
}

0 commit comments

Comments
 (0)