18
18
package com .uber .cadence .internal .common ;
19
19
20
20
import com .google .common .base .Defaults ;
21
+ import com .google .common .collect .Lists ;
21
22
import com .uber .cadence .DataBlob ;
22
23
import com .uber .cadence .History ;
23
24
import com .uber .cadence .HistoryEvent ;
31
32
import com .uber .cadence .workflow .WorkflowMethod ;
32
33
import java .lang .reflect .Method ;
33
34
import java .nio .ByteBuffer ;
34
- import java .util .ArrayList ;
35
35
import java .util .Arrays ;
36
36
import java .util .HashMap ;
37
37
import java .util .List ;
@@ -153,23 +153,41 @@ public static SearchAttributes convertMapToSearchAttributes(
153
153
return new SearchAttributes ().setIndexedFields (mapOfByteBuffer );
154
154
}
155
155
156
- // This method deserialize the DataBlob data to the HistoriyEvent data
157
- public static History DeserializeFromBlobToHistoryEvents (
156
+ // This method serializes history to blob data
157
+ public static DataBlob SerializeFromHistoryToBlobData (History history ) {
158
+
159
+ // TODO: move to global dependency after https://issues.apache.org/jira/browse/THRIFT-2218
160
+ TSerializer serializer = new TSerializer ();
161
+ DataBlob blob = new DataBlob ();
162
+ try {
163
+ blob .setData (serializer .serialize (history ));
164
+ } catch (org .apache .thrift .TException err ) {
165
+ throw new RuntimeException ("Serialize history to blob data failed" , err );
166
+ }
167
+
168
+ return blob ;
169
+ }
170
+
171
+ // This method deserialize the DataBlob data to the History data
172
+ public static History DeserializeFromBlobDataToHistory (
158
173
List <DataBlob > blobData , HistoryEventFilterType historyEventFilterType ) throws TException {
159
174
160
- List <HistoryEvent > events = new ArrayList <HistoryEvent >();
175
+ // TODO: move to global dependency after https://issues.apache.org/jira/browse/THRIFT-2218
176
+ TDeserializer deSerializer = new TDeserializer ();
177
+ List <HistoryEvent > events = Lists .newArrayList ();
161
178
for (DataBlob data : blobData ) {
162
179
History history = new History ();
163
180
try {
164
181
byte [] dataByte = data .getData ();
165
- dataByte = Arrays .copyOfRange (dataByte , 1 , dataByte .length );
182
+ // TODO: verify the beginning index
183
+ dataByte = Arrays .copyOfRange (dataByte , 0 , dataByte .length );
166
184
deSerializer .deserialize (history , dataByte );
167
185
168
186
if (history == null || history .getEvents () == null || history .getEvents ().size () == 0 ) {
169
187
return null ;
170
188
}
171
189
} catch (org .apache .thrift .TException err ) {
172
- throw new TException ("Deserialize blob data to history event failed with unknown error" );
190
+ throw new TException ("Deserialize blob data to history failed with unknown error" );
173
191
}
174
192
175
193
events .addAll (history .getEvents ());
@@ -184,22 +202,43 @@ public static History DeserializeFromBlobToHistoryEvents(
184
202
185
203
// This method serializes history event to blob data
186
204
public static List <DataBlob > SerializeFromHistoryEventToBlobData (List <HistoryEvent > events ) {
187
- List <DataBlob > blobs = new ArrayList <>(events .size ());
205
+
206
+ // TODO: move to global dependency after https://issues.apache.org/jira/browse/THRIFT-2218
207
+ TSerializer serializer = new TSerializer ();
208
+ List <DataBlob > blobs = Lists .newArrayListWithCapacity (events .size ());
188
209
for (HistoryEvent event : events ) {
189
210
DataBlob blob = new DataBlob ();
190
211
try {
191
212
blob .setData (serializer .serialize (event ));
192
213
} catch (org .apache .thrift .TException err ) {
193
- throw new RuntimeException ("Serialize to blob data failed" , err );
214
+ throw new RuntimeException ("Serialize history event to blob data failed" , err );
194
215
}
195
216
blobs .add (blob );
196
217
}
197
-
198
218
return blobs ;
199
219
}
200
220
201
- private static final TDeserializer deSerializer = new TDeserializer ();
202
- private static final TSerializer serializer = new TSerializer ();
221
+ // This method serializes blob data to history event
222
+ public static List <HistoryEvent > DeserializeFromBlobDataToHistoryEvents (List <DataBlob > blobData )
223
+ throws TException {
224
+
225
+ // TODO: move to global dependency after https://issues.apache.org/jira/browse/THRIFT-2218
226
+ TDeserializer deSerializer = new TDeserializer ();
227
+ List <HistoryEvent > events = Lists .newArrayList ();
228
+ for (DataBlob data : blobData ) {
229
+ try {
230
+ HistoryEvent event = new HistoryEvent ();
231
+ byte [] dataByte = data .getData ();
232
+ // TODO: verify the beginning index
233
+ dataByte = Arrays .copyOfRange (dataByte , 0 , dataByte .length );
234
+ deSerializer .deserialize (event , dataByte );
235
+ events .add (event );
236
+ } catch (org .apache .thrift .TException err ) {
237
+ throw new TException ("Deserialize blob data to history event failed with unknown error" );
238
+ }
239
+ }
240
+ return events ;
241
+ }
203
242
204
243
/** Prohibit instantiation */
205
244
private InternalUtils () {}
0 commit comments