@@ -62,11 +62,36 @@ public static ReadOnlyMemory<byte> GetBytes<TEvent>(ArraySegment<TEvent> page,
6262 var field = Encoding . UTF8 . GetBytes ( hashUpdate . UpdateInformation . Field ) ;
6363 bufferWriter . Write ( field ) ;
6464 writer . Reset ( ) ;
65- bufferWriter . Write ( ScriptedHashUpsertMiddle ) ;
65+ bufferWriter . Write ( ScriptedHashUpsertAfterIfCheck ) ;
6666 writer . Reset ( ) ;
67+
68+ bufferWriter . Write ( ScriptedHashUpdateScript ) ;
69+ writer . Reset ( ) ;
70+ if ( hashUpdate . UpdateInformation . UpdateScript is not null )
71+ {
72+ bufferWriter . Write ( Encoding . UTF8 . GetBytes ( hashUpdate . UpdateInformation . UpdateScript ) ) ;
73+ writer . Reset ( ) ;
74+ }
75+ else
76+ {
77+ bufferWriter . Write ( ScriptedHashAfterIfCheckOp ) ;
78+ writer . Reset ( ) ;
79+ }
6780 var hash = hashUpdate . UpdateInformation . Hash ;
6881 JsonSerializer . Serialize ( writer , hash , options . SerializerOptions ) ;
69- bufferWriter . Write ( ScriptedHashUpsertDocPreamble ) ;
82+
83+ if ( hashUpdate . UpdateInformation . Parameters is not null )
84+ foreach ( var ( key , value ) in hashUpdate . UpdateInformation . Parameters )
85+ {
86+ bufferWriter . Write ( ScriptedHashParamComma ) ;
87+ writer . Reset ( ) ;
88+ JsonSerializer . Serialize ( writer , key , options . SerializerOptions ) ;
89+ bufferWriter . Write ( ScriptedHashKeySeparator ) ;
90+ writer . Reset ( ) ;
91+ JsonSerializer . Serialize ( writer , value , options . SerializerOptions ) ;
92+ }
93+
94+ bufferWriter . Write ( ScriptHashDocAsParameter ) ;
7095 writer . Reset ( ) ;
7196 }
7297
@@ -137,10 +162,31 @@ await JsonSerializer.SerializeAsync(stream, indexHeader, indexHeader.GetType(),
137162 await stream . WriteAsync ( ScriptedHashUpsertStart , 0 , ScriptedHashUpsertStart . Length , ctx ) . ConfigureAwait ( false ) ;
138163 var field = Encoding . UTF8 . GetBytes ( hashUpdate . UpdateInformation . Field ) ;
139164 await stream . WriteAsync ( field , 0 , field . Length , ctx ) . ConfigureAwait ( false ) ;
140- await stream . WriteAsync ( ScriptedHashUpsertMiddle , 0 , ScriptedHashUpsertMiddle . Length , ctx ) . ConfigureAwait ( false ) ;
165+ await stream . WriteAsync ( ScriptedHashUpsertAfterIfCheck , 0 , ScriptedHashUpsertAfterIfCheck . Length , ctx ) . ConfigureAwait ( false ) ;
166+
167+ if ( hashUpdate . UpdateInformation . UpdateScript is { } script && ! string . IsNullOrWhiteSpace ( script ) )
168+ {
169+ var bytes = Encoding . UTF8 . GetBytes ( script ) ;
170+ await stream . WriteAsync ( bytes , 0 , bytes . Length , ctx ) . ConfigureAwait ( false ) ;
171+ }
172+ else
173+ await stream . WriteAsync ( ScriptedHashUpdateScript , 0 , ScriptedHashUpdateScript . Length , ctx ) . ConfigureAwait ( false ) ;
174+
175+ await stream . WriteAsync ( ScriptedHashAfterIfCheckOp , 0 , ScriptedHashAfterIfCheckOp . Length , ctx ) . ConfigureAwait ( false ) ;
176+
141177 var hash = hashUpdate . UpdateInformation . Hash ;
142178 await JsonSerializer . SerializeAsync ( stream , hash , options . SerializerOptions , ctx ) . ConfigureAwait ( false ) ;
143- await stream . WriteAsync ( ScriptedHashUpsertDocPreamble , 0 , ScriptedHashUpsertDocPreamble . Length , ctx ) . ConfigureAwait ( false ) ;
179+
180+ if ( hashUpdate . UpdateInformation . Parameters is { } parameters )
181+ foreach ( var kv in parameters )
182+ {
183+ await stream . WriteAsync ( ScriptedHashParamComma , 0 , ScriptedHashParamComma . Length , ctx ) . ConfigureAwait ( false ) ;
184+ await JsonSerializer . SerializeAsync ( stream , kv . Key , options . SerializerOptions , ctx ) . ConfigureAwait ( false ) ;
185+ await stream . WriteAsync ( ScriptedHashKeySeparator , 0 , ScriptedHashKeySeparator . Length , ctx ) . ConfigureAwait ( false ) ;
186+ await JsonSerializer . SerializeAsync ( stream , kv . Value , options . SerializerOptions , ctx ) . ConfigureAwait ( false ) ;
187+ }
188+
189+ await stream . WriteAsync ( ScriptHashDocAsParameter , 0 , ScriptHashDocAsParameter . Length , ctx ) . ConfigureAwait ( false ) ;
144190 }
145191
146192 if ( options . EventWriter ? . WriteToStreamAsync != null )
0 commit comments