1111
1212import org .apache .lucene .util .BytesRef ;
1313import org .elasticsearch .common .bytes .BytesReference ;
14- import org .elasticsearch .common .io .stream .RecyclerBytesStreamOutput ;
14+ import org .elasticsearch .common .io .stream .BytesStreamOutput ;
1515import org .elasticsearch .common .recycler .Recycler ;
1616import org .elasticsearch .transport .BytesRefRecycler ;
1717import org .elasticsearch .xcontent .ToXContent ;
@@ -36,15 +36,16 @@ public class ESONSource {
3636
3737 public static class Builder {
3838
39- private final RecyclerBytesStreamOutput bytes ;
39+ private final BytesStreamOutput bytes ;
4040 private final boolean ordered ;
4141
4242 public Builder (boolean ordered ) {
4343 this (BytesRefRecycler .NON_RECYCLING_INSTANCE , ordered );
4444 }
4545
4646 public Builder (Recycler <BytesRef > refRecycler , boolean ordered ) {
47- this .bytes = new RecyclerBytesStreamOutput (refRecycler );
47+ // this.bytes = new RecyclerBytesStreamOutput(refRecycler);
48+ this .bytes = new BytesStreamOutput (128 );
4849 this .ordered = ordered ;
4950 }
5051
@@ -67,7 +68,7 @@ public ESONObject parse(XContentParser parser) throws IOException {
6768 return rootObject ;
6869 }
6970
70- private static ESONObject parseObject (XContentParser parser , RecyclerBytesStreamOutput bytes , Supplier <Values > valuesSupplier )
71+ private static ESONObject parseObject (XContentParser parser , BytesStreamOutput bytes , Supplier <Values > valuesSupplier )
7172 throws IOException {
7273 Map <String , Type > map = new HashMap <>();
7374 String currentFieldName ;
@@ -79,7 +80,7 @@ private static ESONObject parseObject(XContentParser parser, RecyclerBytesStream
7980 return new ESONObject (map , valuesSupplier );
8081 }
8182
82- private static ESONArray parseArray (XContentParser parser , RecyclerBytesStreamOutput bytes , Supplier <Values > valuesSupplier )
83+ private static ESONArray parseArray (XContentParser parser , BytesStreamOutput bytes , Supplier <Values > valuesSupplier )
8384 throws IOException {
8485 List <Type > elements = new ArrayList <>();
8586 XContentParser .Token token ;
@@ -92,7 +93,7 @@ private static ESONArray parseArray(XContentParser parser, RecyclerBytesStreamOu
9293
9394 private static Type parseValue (
9495 XContentParser parser ,
95- RecyclerBytesStreamOutput bytes ,
96+ BytesStreamOutput bytes ,
9697 Supplier <Values > valuesSupplier ,
9798 XContentParser .Token token
9899 ) throws IOException {
@@ -131,7 +132,7 @@ private static Type parseValue(
131132 }
132133 }
133134
134- private static ValueType handleNumber (XContentParser parser , RecyclerBytesStreamOutput bytes ) throws IOException {
135+ private static ValueType handleNumber (XContentParser parser , BytesStreamOutput bytes ) throws IOException {
135136 switch (parser .numberType ()) {
136137 case INT -> {
137138 int value = parser .intValue ();
@@ -181,11 +182,11 @@ private static ValueType handleNumber(XContentParser parser, RecyclerBytesStream
181182 }
182183 }
183184
184- private static void writeByteArray (RecyclerBytesStreamOutput bytes , byte [] value , int offset , int length ) {
185+ private static void writeByteArray (BytesStreamOutput bytes , byte [] value , int offset , int length ) {
185186 bytes .writeBytes (value , offset , length );
186187 }
187188
188- private static void writeString (RecyclerBytesStreamOutput bytes , String value ) throws IOException {
189+ private static void writeString (BytesStreamOutput bytes , String value ) throws IOException {
189190 byte [] utf8Bytes = value .getBytes (java .nio .charset .StandardCharsets .UTF_8 );
190191 writeByteArray (bytes , utf8Bytes , 0 , utf8Bytes .length );
191192 }
@@ -391,13 +392,15 @@ public boolean remove(Object o) {
391392 @ Override
392393 public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
393394 builder .startObject ();
395+ // TODO: Maybe need explicit null type to ensure not lost
394396 for (Entry <String , Type > entry : map .entrySet ()) {
395397 builder .field (entry .getKey ());
396398 switch (entry .getValue ()) {
397399 case ESONObject o -> o .toXContent (builder , params );
398400 case ESONArray a -> a .toXContent (builder , params );
399401 case FixedValue v -> v .writeToXContent (builder , objectValues .get ());
400402 case VariableValue v -> v .writeToXContent (builder , objectValues .get ());
403+ case Mutation m -> builder .value (m .object ());
401404 default -> throw new IllegalArgumentException ("Unknown type: " + entry .getValue ());
402405 }
403406 }
@@ -556,7 +559,14 @@ public Object getValue(Values source) {
556559 }
557560
558561 public void writeToXContent (XContentBuilder builder , Values values ) throws IOException {
559- builder .value (getValue (values ));
562+ switch (valueType ) {
563+ case INT -> builder .value (values .readInt (position ));
564+ case LONG -> builder .value (values .readLong (position ));
565+ case FLOAT -> builder .value (values .readFloat (position ));
566+ case DOUBLE -> builder .value (values .readDouble (position ));
567+ case BOOLEAN -> builder .value (values .readBoolean (position ));
568+ default -> throw new IllegalArgumentException ("Invalid value type: " + valueType );
569+ }
560570 }
561571 }
562572
0 commit comments