Skip to content

Commit bd571fd

Browse files
committed
updates from feedback
1 parent d0a3365 commit bd571fd

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldSyntheticWriterHelper.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@
5858
*/
5959
public class FlattenedFieldSyntheticWriterHelper {
6060

61-
private record Prefix(List<String> prefix) {
61+
private static final String PATH_SEPARATOR = ".";
62+
private static final String PATH_SEPARATOR_PATTERN = "\\.";
63+
64+
private record Prefix(List<String> parts) {
6265

6366
Prefix() {
6467
this(new ArrayList<>());
@@ -79,7 +82,7 @@ private static int numObjectsToEnd(final List<String> curr, final List<String> n
7982
}
8083

8184
private Prefix diff(final Prefix other) {
82-
return diff(this.prefix, other.prefix);
85+
return diff(this.parts, other.parts);
8386
}
8487

8588
private static Prefix diff(final List<String> a, final List<String> b) {
@@ -99,7 +102,7 @@ private static Prefix diff(final List<String> a, final List<String> b) {
99102

100103
@Override
101104
public int hashCode() {
102-
return Objects.hash(this.prefix);
105+
return Objects.hash(this.parts);
103106
}
104107

105108
@Override
@@ -111,7 +114,7 @@ public boolean equals(Object obj) {
111114
return false;
112115
}
113116
Prefix other = (Prefix) obj;
114-
return Objects.equals(this.prefix, other.prefix);
117+
return Objects.equals(this.parts, other.parts);
115118
}
116119
}
117120

@@ -130,7 +133,7 @@ private KeyValue(final String value, final Prefix prefix, final String leaf) {
130133

131134
KeyValue(final BytesRef keyValue) {
132135
this(
133-
FlattenedFieldParser.extractKey(keyValue).utf8ToString().split("\\."),
136+
FlattenedFieldParser.extractKey(keyValue).utf8ToString().split(PATH_SEPARATOR_PATTERN),
134137
FlattenedFieldParser.extractValue(keyValue).utf8ToString()
135138
);
136139
}
@@ -184,8 +187,8 @@ public FlattenedFieldSyntheticWriterHelper(final SortedKeyedValues sortedKeyedVa
184187

185188
private String concatPath(Prefix prefix, String leaf) {
186189
StringBuilder builder = new StringBuilder();
187-
for (String part : prefix.prefix) {
188-
builder.append(part).append(".");
190+
for (String part : prefix.parts) {
191+
builder.append(part).append(PATH_SEPARATOR);
189192
}
190193
builder.append(leaf);
191194
return builder.toString();
@@ -198,13 +201,13 @@ public void write(final XContentBuilder b) throws IOException {
198201
String lastScalarSingleLeaf = null;
199202
KeyValue next;
200203

201-
do {
204+
while (curr.equals(KeyValue.EMPTY) == false) {
202205
values.add(curr.value());
203206
BytesRef nextValue = sortedKeyedValues.next();
204207
next = nextValue == null ? KeyValue.EMPTY : new KeyValue(nextValue);
205208

206209
var startPrefix = curr.prefix.diff(openObjects);
207-
if (startPrefix.prefix.isEmpty() == false && startPrefix.prefix.getFirst().equals(lastScalarSingleLeaf)) {
210+
if (startPrefix.parts.isEmpty() == false && startPrefix.parts.getFirst().equals(lastScalarSingleLeaf)) {
208211
// In the open object, there is a leaf with a scalar value, which is also the first
209212
// part of the current path. Instead of traversing down into the path and building objects,
210213
// combine the path into a single leaf and add it as a field.
@@ -214,18 +217,18 @@ public void write(final XContentBuilder b) throws IOException {
214217
}
215218
} else {
216219
// Traverse down into path, writing object keys to output, and adding to the openObject context.
217-
startObject(b, startPrefix.prefix, openObjects.prefix);
220+
startObject(b, startPrefix.parts, openObjects.parts);
218221
if (curr.pathEquals(next) == false) {
219222
lastScalarSingleLeaf = curr.leaf();
220223
writeField(b, values, curr.leaf());
221224
}
222225
}
223226

224-
int numObjectsToEnd = Prefix.numObjectsToEnd(openObjects.prefix, next.prefix.prefix);
225-
endObject(b, numObjectsToEnd, openObjects.prefix);
227+
int numObjectsToEnd = Prefix.numObjectsToEnd(openObjects.parts, next.prefix.parts);
228+
endObject(b, numObjectsToEnd, openObjects.parts);
226229

227230
curr = next;
228-
} while (next.equals(KeyValue.EMPTY) == false);
231+
}
229232
}
230233

231234
private static void endObject(final XContentBuilder b, int numObjectsToClose, List<String> openObjects) throws IOException {

server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldSyntheticWriterHelperTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public void testScalarObjectMismatchInNestedObject() throws IOException {
225225
final FlattenedFieldSyntheticWriterHelper writer = new FlattenedFieldSyntheticWriterHelper(new SortedSetSortedKeyedValues(dv));
226226
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
227227
final XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), baos);
228-
final List<byte[]> bytes = List.of("a.b" + '\0' + "10", "a.b.c" + '\0' + "20")
228+
final List<byte[]> bytes = List.of("a.b.c" + '\0' + "10", "a.b.c.d" + '\0' + "20")
229229
.stream()
230230
.map(x -> x.getBytes(StandardCharsets.UTF_8))
231231
.collect(Collectors.toList());
@@ -243,7 +243,7 @@ public void testScalarObjectMismatchInNestedObject() throws IOException {
243243
builder.flush();
244244

245245
// THEN
246-
assertEquals("{\"a\":{\"b\":\"10\",\"b.c\":\"20\"}}", baos.toString(StandardCharsets.UTF_8));
246+
assertEquals("{\"a\":{\"b\":{\"c\":\"10\",\"c.d\":\"20\"}}}", baos.toString(StandardCharsets.UTF_8));
247247
}
248248

249249
private class SortedSetSortedKeyedValues implements FlattenedFieldSyntheticWriterHelper.SortedKeyedValues {

0 commit comments

Comments
 (0)