5858 */
5959public 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 {
0 commit comments