11/**
22 The MIT License (MIT)
33
4- Copyright (c) 2010-2020 head systems, ltd
4+ Copyright (c) 2010-2021 head systems, ltd
55
66 Permission is hereby granted, free of charge, to any person obtaining a copy of
77 this software and associated documentation files (the "Software"), to deal in
@@ -70,7 +70,9 @@ public class DataChunk implements Chunk {
7070 private UndoChunk uc ;
7171 private FrameData uframe ;
7272 private boolean terminate ;
73- private final CustomSerializer sr = new CustomSerializer ();
73+ private boolean external ;
74+ private static final CustomSerializer sr = new CustomSerializer ();
75+ private static final CustomSerializer sr_ = new CustomSerializer (true );
7476
7577 //returns datacolumn set
7678 public ValueSet getDcs () {
@@ -154,7 +156,7 @@ private ValueSet getDcsFromBytes() {
154156 if (Modifier .isPrivate (m )) {
155157 cs [i ].setAccessible (true );
156158 }
157- dcs .getValueSet ()[i ] = sr .deserialize (data , cs [i ]);
159+ dcs .getValueSet ()[i ] = this . external ? sr_ . deserialize ( data , cs [ i ]) : sr .deserialize (data , cs [i ]);
158160 if (a != null ) {
159161 id = (Comparable ) dcs .getValueSet ()[i ];
160162 }
@@ -170,6 +172,16 @@ private ValueSet getDcsFromBytes() {
170172 }
171173 }
172174
175+ public GenericObject getGenericObject () {
176+ final ValueSet vs = getDcs ();
177+ final Field [] cs = t .getFields ();
178+ final Map <String , Object > vmap = new HashMap ();
179+ for (int i =0 ; i <cs .length ; i ++) {
180+ vmap .put (cs [i ].getName (), vs .getValueSet ()[i ]);
181+ }
182+ return new GenericObject (vmap );
183+ }
184+
173185 public UndoChunk getUndoChunk () {
174186 if (header ==null ) {
175187 return null ;
@@ -238,11 +250,11 @@ public byte[] getSerializedId (Session s) throws InvocationTargetException, NoSu
238250 if (t .isNoTran ()) {
239251 final Method z = t .getIdmethod ();
240252 id = (Comparable ) z .invoke (entity , null );
241- serializedId = sr .serialize (idf .getType ().getName (), id );
253+ serializedId = this . external ? sr_ . serialize ( idf . getType (). getName (), id ) : sr .serialize (idf .getType ().getName (), id );
242254 } else {
243255 final Method z = t .getIdmethod_ ();
244256 id = (Comparable ) z .invoke (entity , new Object []{s });
245- serializedId = sr .serialize (idf .getType ().getName (), id );
257+ serializedId = this . external ? sr_ . serialize ( idf . getType (). getName (), id ) : sr .serialize (idf .getType ().getName (), id );
246258 }
247259 }
248260
@@ -278,7 +290,7 @@ public DataChunk (ValueSet vs, Session s, RowId r, Table t) throws ClassNotFound
278290 final ByteString res = new ByteString ();
279291 final Field [] f = t .getFields ();
280292 for (int i =0 ; i <f .length ; i ++) {
281- byte [] b = sr .serialize (f [i ].getType ().getName (), vs .getValueSet ()[i ]);
293+ byte [] b = this . external ? sr_ . serialize ( f [ i ]. getType (). getName (), vs . getValueSet ()[ i ]) : sr .serialize (f [i ].getType ().getName (), vs .getValueSet ()[i ]);
282294 if (b ==null ) { b = new byte []{}; } //stub for reflection convert null fields to byte[] object in DataRecord
283295 if (Types .isVarType (f [i ])) {
284296 res .addBytesFromInt (b .length );
@@ -331,6 +343,15 @@ public DataChunk (Object o, Session s, Table t) {
331343 this (o , s , null , t );
332344 }
333345
346+ //serializer INSERT ONLY!!! (with generate Id value)
347+ public DataChunk (Object o , Session s , Table t , boolean external ) {
348+ this .entity = o ;
349+ this .t = t ;
350+ this .state = NORMAL_STATE ;
351+ this .external = external ;
352+ this .header = new RowHeader (null , null , getChunk ().length , false );
353+ }
354+
334355 //serializer INSERT ONLY!!! (with generate Id value) - rowid for index chunk
335356 public DataChunk (Object o , Session s , RowId r , Table t ) {
336357 this .entity = o ;
@@ -357,6 +378,15 @@ public DataChunk (byte[] b, Table t, RowHeader h, DataChunk source) {
357378 this .t = t ;
358379 }
359380
381+ //constructor ONLY for external client deserializer
382+ public DataChunk (byte [] b , Table t , boolean external ) {
383+ this .chunk = b ;
384+ this .t = t ;
385+ this .state = INIT_STATE ;
386+ this .external = external ;
387+ this .header = new RowHeader (null , null , getChunk ().length , false );
388+ }
389+
360390 public byte [] getChunk () {
361391 if (state == INIT_STATE ) {
362392 return chunk ;
@@ -543,6 +573,27 @@ public Object getStandaloneEntity() {
543573 return null ;
544574 }
545575
576+ public Object getClientStandaloneEntity () {
577+ try {
578+ final ValueSet dcs = getDcs ();
579+ final Object o = t .getInstance (); //returns empty instance
580+ final Field [] cs = t .getFields ();
581+ for (int i =0 ; i <cs .length ; i ++) {
582+ final int m = cs [i ].getModifiers ();
583+ if (Modifier .isPrivate (m )) {
584+ cs [i ].setAccessible (true );
585+ }
586+ if (dcs .getValueSet ()[i ]!=null ) {
587+ cs [i ].set (o , dcs .getValueSet ()[i ]);
588+ }
589+ }
590+ return o ;
591+ } catch (Exception e ) {
592+ e .printStackTrace ();
593+ }
594+ return null ;
595+ }
596+
546597 //for bootstrap system / not use table objects
547598 public Object getEntity (Class c , Object [] params ) {
548599 SystemEntity ca = (SystemEntity )c .getAnnotation (SystemEntity .class );
@@ -665,7 +716,7 @@ public DataChunk restore(UndoChunk uc) throws Exception {
665716 private byte [] getBytes (Field f , Object o ) throws IllegalAccessException , UnsupportedEncodingException , ClassNotFoundException , InternalException , InstantiationException {
666717 final String t = f .getType ().getName ();
667718 final Object fo = f .get (o );
668- return sr .serialize (t , fo );
719+ return this . external ? sr_ . serialize ( t , fo ) : sr .serialize (t , fo );
669720 }
670721
671722 private int getLen (Field f , Object o ) throws IllegalAccessException , UnsupportedEncodingException , ClassNotFoundException , InternalException , InstantiationException {
0 commit comments