2121 */
2222package com .influxdb .v3 .client .internal ;
2323
24+ import java .math .BigInteger ;
2425import java .time .LocalDateTime ;
26+ import java .util .ArrayList ;
2527import java .util .List ;
2628import java .util .Objects ;
29+ import java .util .logging .Logger ;
2730import javax .annotation .Nonnull ;
2831import javax .annotation .concurrent .ThreadSafe ;
2932
4245 * This class is thread-safe.
4346 */
4447@ ThreadSafe
45- final class VectorSchemaRootConverter {
48+ public final class VectorSchemaRootConverter {
4649
47- static final VectorSchemaRootConverter INSTANCE = new VectorSchemaRootConverter ();
50+ private static final Logger LOG = Logger .getLogger (VectorSchemaRootConverter .class .getName ());
51+
52+ public static final VectorSchemaRootConverter INSTANCE = new VectorSchemaRootConverter ();
4853
4954 /**
5055 * Converts a given row of data from a VectorSchemaRoot object to PointValues.
@@ -103,8 +108,16 @@ PointValues toPointValues(final int rowNumber,
103108 return p ;
104109 }
105110
106- private void setFieldWithMetaType (final PointValues p ,
107- final String name ,
111+ /**
112+ * Set field value for PointValues base on iox::column::type
113+ *
114+ * @param p The target PointValues
115+ * @param fieldName Field name in PointValues
116+ * @param value The value to be set
117+ * @param metaType The iox::column::type column meta type, eg: iox::column_type::field::integer, iox::column_type::field::float
118+ */
119+ public void setFieldWithMetaType (final PointValues p ,
120+ final String fieldName ,
108121 final Object value ,
109122 final String metaType ) {
110123 if (value == null ) {
@@ -114,20 +127,110 @@ private void setFieldWithMetaType(final PointValues p,
114127 switch (metaType ) {
115128 case "iox::column_type::field::integer" :
116129 case "iox::column_type::field::uinteger" :
117- p .setIntegerField (name , (Long ) value );
130+ if (value instanceof Long ) {
131+ p .setIntegerField (fieldName , TypeCasting .toLongValue (value ));
132+ } else {
133+ p .setNullField (fieldName );
134+ LOG .warning (String .format ("Value of %s is not an Integer" , fieldName ));
135+ }
118136 break ;
119137 case "iox::column_type::field::float" :
120- p .setFloatField (name , (Double ) value );
138+ if (value instanceof Double ) {
139+ p .setFloatField (fieldName , TypeCasting .toDoubleValue (value ));
140+ } else {
141+ p .setNullField (fieldName );
142+ LOG .warning (String .format ("Value of %s is not a Double" , fieldName ));
143+ }
121144 break ;
122145 case "iox::column_type::field::string" :
123- p .setStringField (name , (String ) value );
146+ if (value instanceof String || value instanceof Text ) {
147+ p .setStringField (fieldName , TypeCasting .toStringValue (value ));
148+ } else {
149+ p .setNullField (fieldName );
150+ LOG .warning (String .format ("Value of %s is not a String" , fieldName ));
151+ }
124152 break ;
125153 case "iox::column_type::field::boolean" :
126- p .setBooleanField (name , (Boolean ) value );
154+ if (value instanceof Boolean ) {
155+ p .setBooleanField (fieldName , (Boolean ) value );
156+ } else {
157+ p .setNullField (fieldName );
158+ LOG .warning (String .format ("Value of %s is not a Boolean" , fieldName ));
159+ }
127160 break ;
128161 default :
129- p .setField (name , value );
162+ p .setField (fieldName , value );
130163 break ;
131164 }
132165 }
166+
167+ /**
168+ * Get array of values from VectorSchemaRoot
169+ *
170+ * @param vector The data return from InfluxDB
171+ * @param rowNumber The row number of data
172+ * @return An array of Objects represent for a row of data
173+ */
174+ public Object [] getArrayObjectFromVectorSchemaRoot (VectorSchemaRoot vector , int rowNumber ) {
175+ List <Object > row = new ArrayList <>();
176+ for (FieldVector fieldVector : vector .getFieldVectors ()) {
177+ var field = fieldVector .getField ();
178+ var metaType = field .getMetadata ().get ("iox::column::type" );
179+ String valueType = metaType != null ? metaType .split ("::" )[2 ] : null ;
180+ String fieldName = field .getName ();
181+
182+ Object value = fieldVector .getObject (rowNumber );
183+ if (value == null ) {
184+ row .add (null );
185+ continue ;
186+ }
187+
188+ if ("field" .equals (valueType )) {
189+ switch (metaType ) {
190+ case "iox::column_type::field::integer" :
191+ case "iox::column_type::field::uinteger" :
192+ if (value instanceof Long ) {
193+ row .add (TypeCasting .toLongValue (value ));
194+ } else {
195+ row .add (null );
196+ LOG .warning (String .format ("Value of %s is not an Integer" , fieldName ));
197+ }
198+ break ;
199+ case "iox::column_type::field::float" :
200+ if (value instanceof Double ) {
201+ row .add (TypeCasting .toDoubleValue (value ));
202+ } else {
203+ row .add (null );
204+ LOG .warning (String .format ("Value of %s is not a Double" , fieldName ));
205+ }
206+ break ;
207+ case "iox::column_type::field::string" :
208+ if (value instanceof Text || value instanceof String ) {
209+ row .add (TypeCasting .toStringValue (value ));
210+ } else {
211+ row .add (null );
212+ LOG .warning (String .format ("Value of %s is not a String" , fieldName ));
213+ }
214+ break ;
215+ case "iox::column_type::field::boolean" :
216+ if (value instanceof Boolean ) {
217+ row .add ((Boolean ) value );
218+ } else {
219+ row .add (null );
220+ LOG .warning (String .format ("Value of %s is not a Boolean" , fieldName ));
221+ }
222+ break ;
223+ default :
224+ }
225+ } else if ("timestamp" .equals (valueType )
226+ || Objects .equals (fieldName , "time" )) {
227+ BigInteger time = NanosecondConverter .getTimestampNano (value , field );
228+ row .add (time );
229+ } else {
230+ row .add (value );
231+ }
232+ }
233+
234+ return row .toArray ();
235+ }
133236}
0 commit comments