Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit c2b5530

Browse files
committed
The time precision was move to separate pull request influxdata#501
1 parent 4a1f929 commit c2b5530

File tree

2 files changed

+11
-149
lines changed

2 files changed

+11
-149
lines changed

src/main/java/org/influxdb/impl/InfluxDBResultMapper.java

+10-74
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import java.time.temporal.ChronoField;
2828
import java.util.LinkedList;
2929
import java.util.List;
30-
import java.util.Map.Entry;
3130
import java.util.Objects;
31+
import java.util.Map.Entry;
3232
import java.util.concurrent.ConcurrentHashMap;
3333
import java.util.concurrent.ConcurrentMap;
3434
import java.util.concurrent.TimeUnit;
@@ -84,33 +84,9 @@ public class InfluxDBResultMapper {
8484
* possible to define the values of your POJO (e.g. due to an unsupported field type).
8585
*/
8686
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz) throws InfluxDBMapperException {
87-
return toPOJO(queryResult, clazz, TimeUnit.MILLISECONDS);
88-
}
89-
90-
/**
91-
* <p>
92-
* Process a {@link QueryResult} object returned by the InfluxDB client inspecting the internal
93-
* data structure and creating the respective object instances based on the Class passed as
94-
* parameter.
95-
* </p>
96-
*
97-
* @param queryResult the InfluxDB result object
98-
* @param clazz the Class that will be used to hold your measurement data
99-
* @param precision the time precision of results
100-
* @param <T> the target type
101-
*
102-
* @return a {@link List} of objects from the same Class passed as parameter and sorted on the
103-
* same order as received from InfluxDB.
104-
*
105-
* @throws InfluxDBMapperException If {@link QueryResult} parameter contain errors,
106-
* <tt>clazz</tt> parameter is not annotated with &#64;Measurement or it was not
107-
* possible to define the values of your POJO (e.g. due to an unsupported field type).
108-
*/
109-
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz,
110-
final TimeUnit precision) throws InfluxDBMapperException {
11187
throwExceptionIfMissingAnnotation(clazz);
11288
String measurementName = getMeasurementName(clazz);
113-
return this.toPOJO(queryResult, clazz, measurementName, precision);
89+
return this.toPOJO(queryResult, clazz, measurementName);
11490
}
11591

11692
/**
@@ -134,32 +110,6 @@ public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz,
134110
*/
135111
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, final String measurementName)
136112
throws InfluxDBMapperException {
137-
return toPOJO(queryResult, clazz, measurementName, TimeUnit.MILLISECONDS);
138-
}
139-
140-
/**
141-
* <p>
142-
* Process a {@link QueryResult} object returned by the InfluxDB client inspecting the internal
143-
* data structure and creating the respective object instances based on the Class passed as
144-
* parameter.
145-
* </p>
146-
*
147-
* @param queryResult the InfluxDB result object
148-
* @param clazz the Class that will be used to hold your measurement data
149-
* @param <T> the target type
150-
* @param measurementName name of the Measurement
151-
* @param precision the time precision of results
152-
*
153-
* @return a {@link List} of objects from the same Class passed as parameter and sorted on the
154-
* same order as received from InfluxDB.
155-
*
156-
* @throws InfluxDBMapperException If {@link QueryResult} parameter contain errors,
157-
* <tt>clazz</tt> parameter is not annotated with &#64;Measurement or it was not
158-
* possible to define the values of your POJO (e.g. due to an unsupported field type).
159-
*/
160-
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, final String measurementName,
161-
final TimeUnit precision)
162-
throws InfluxDBMapperException {
163113

164114
Objects.requireNonNull(measurementName, "measurementName");
165115
Objects.requireNonNull(queryResult, "queryResult");
@@ -176,7 +126,7 @@ public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, f
176126
internalResult.getSeries().stream()
177127
.filter(series -> series.getName().equals(measurementName))
178128
.forEachOrdered(series -> {
179-
parseSeriesAs(series, clazz, result, precision);
129+
parseSeriesAs(series, clazz, result);
180130
});
181131
});
182132

@@ -227,11 +177,6 @@ String getMeasurementName(final Class<?> clazz) {
227177
}
228178

229179
<T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz, final List<T> result) {
230-
return parseSeriesAs(series, clazz, result, TimeUnit.MILLISECONDS);
231-
}
232-
233-
<T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz, final List<T> result,
234-
final TimeUnit precision) {
235180
int columnSize = series.getColumns().size();
236181
ConcurrentMap<String, Field> colNameAndFieldMap = CLASS_FIELD_CACHE.get(clazz.getName());
237182
try {
@@ -243,7 +188,7 @@ <T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz,
243188
if (object == null) {
244189
object = clazz.newInstance();
245190
}
246-
setFieldValue(object, correspondingField, row.get(i), precision);
191+
setFieldValue(object, correspondingField, row.get(i));
247192
}
248193
}
249194
// When the "GROUP BY" clause is used, "tags" are returned as Map<String,String> and
@@ -255,7 +200,7 @@ <T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz,
255200
Field correspondingField = colNameAndFieldMap.get(entry.getKey()/*InfluxDB columnName*/);
256201
if (correspondingField != null) {
257202
// I don't think it is possible to reach here without a valid "object"
258-
setFieldValue(object, correspondingField, entry.getValue(), precision);
203+
setFieldValue(object, correspondingField, entry.getValue());
259204
}
260205
}
261206
}
@@ -278,11 +223,10 @@ <T> List<T> parseSeriesAs(final QueryResult.Series series, final Class<T> clazz,
278223
* @param object
279224
* @param field
280225
* @param value
281-
* @param precision
282226
* @throws IllegalArgumentException
283227
* @throws IllegalAccessException
284228
*/
285-
<T> void setFieldValue(final T object, final Field field, final Object value, final TimeUnit precision)
229+
<T> void setFieldValue(final T object, final Field field, final Object value)
286230
throws IllegalArgumentException, IllegalAccessException {
287231
if (value == null) {
288232
return;
@@ -292,7 +236,7 @@ <T> void setFieldValue(final T object, final Field field, final Object value, fi
292236
if (!field.isAccessible()) {
293237
field.setAccessible(true);
294238
}
295-
if (fieldValueModified(fieldType, field, object, value, precision)
239+
if (fieldValueModified(fieldType, field, object, value)
296240
|| fieldValueForPrimitivesModified(fieldType, field, object, value)
297241
|| fieldValueForPrimitiveWrappersModified(fieldType, field, object, value)) {
298242
return;
@@ -308,8 +252,7 @@ <T> void setFieldValue(final T object, final Field field, final Object value, fi
308252
}
309253
}
310254

311-
<T> boolean fieldValueModified(final Class<?> fieldType, final Field field, final T object, final Object value,
312-
final TimeUnit precision)
255+
<T> boolean fieldValueModified(final Class<?> fieldType, final Field field, final T object, final Object value)
313256
throws IllegalArgumentException, IllegalAccessException {
314257
if (String.class.isAssignableFrom(fieldType)) {
315258
field.set(object, String.valueOf(value));
@@ -320,11 +263,9 @@ <T> boolean fieldValueModified(final Class<?> fieldType, final Field field, fina
320263
if (value instanceof String) {
321264
instant = Instant.from(ISO8601_FORMATTER.parse(String.valueOf(value)));
322265
} else if (value instanceof Long) {
323-
instant = Instant.ofEpochMilli(toMillis((long) value, precision));
266+
instant = Instant.ofEpochMilli((Long) value);
324267
} else if (value instanceof Double) {
325-
instant = Instant.ofEpochMilli(toMillis(((Double) value).longValue(), precision));
326-
} else if (value instanceof Integer) {
327-
instant = Instant.ofEpochMilli(toMillis(((Integer) value).longValue(), precision));
268+
instant = Instant.ofEpochMilli(((Double) value).longValue());
328269
} else {
329270
throw new InfluxDBMapperException("Unsupported type " + field.getClass() + " for field " + field.getName());
330271
}
@@ -375,9 +316,4 @@ <T> boolean fieldValueForPrimitiveWrappersModified(final Class<?> fieldType, fin
375316
}
376317
return false;
377318
}
378-
379-
private Long toMillis(final long value, final TimeUnit precision) {
380-
381-
return TimeUnit.MILLISECONDS.convert(value, precision);
382-
}
383319
}

src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java

+1-75
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
import java.util.Map;
3030
import java.util.Random;
3131
import java.util.UUID;
32-
import java.util.concurrent.TimeUnit;
3332

3433
import org.influxdb.InfluxDBMapperException;
3534
import org.influxdb.annotation.Column;
3635
import org.influxdb.annotation.Measurement;
3736
import org.influxdb.dto.QueryResult;
37+
import org.influxdb.impl.InfluxDBResultMapper;
3838
import org.junit.jupiter.api.Assertions;
3939
import org.junit.jupiter.api.Test;
4040
import org.junit.platform.runner.JUnitPlatform;
@@ -220,26 +220,6 @@ public void testFieldValueModified_DateAsISO8601() {
220220
Assertions.assertTrue(result.size() == 1);
221221
}
222222

223-
@Test
224-
public void testFieldValueModified_DateAsInteger() {
225-
// Given...
226-
mapper.cacheMeasurementClass(MyCustomMeasurement.class);
227-
228-
List<String> columnList = Arrays.asList("time");
229-
List<Object> firstSeriesResult = Arrays.asList(1_000);
230-
231-
QueryResult.Series series = new QueryResult.Series();
232-
series.setColumns(columnList);
233-
series.setValues(Arrays.asList(firstSeriesResult));
234-
235-
//When...
236-
List<MyCustomMeasurement> result = new LinkedList<>();
237-
mapper.parseSeriesAs(series, MyCustomMeasurement.class, result);
238-
239-
//Then...
240-
Assertions.assertTrue(result.size() == 1);
241-
}
242-
243223
@Test
244224
public void testUnsupportedField() {
245225
// Given...
@@ -355,60 +335,6 @@ public void testToPOJO_ticket363() {
355335
Assertions.assertEquals(1, result.get(0).time.getNano(), "incorrect value for the nanoseconds field");
356336
}
357337

358-
@Test
359-
void testToPOJO_Precision() {
360-
// Given...
361-
mapper.cacheMeasurementClass(MyCustomMeasurement.class);
362-
363-
List<String> columnList = Arrays.asList("time");
364-
List<Object> firstSeriesResult = Arrays.asList(1_500_000L);
365-
366-
QueryResult.Series series = new QueryResult.Series();
367-
series.setName("CustomMeasurement");
368-
series.setColumns(columnList);
369-
series.setValues(Arrays.asList(firstSeriesResult));
370-
371-
QueryResult.Result internalResult = new QueryResult.Result();
372-
internalResult.setSeries(Arrays.asList(series));
373-
374-
QueryResult queryResult = new QueryResult();
375-
queryResult.setResults(Arrays.asList(internalResult));
376-
377-
// When...
378-
List<MyCustomMeasurement> result = mapper.toPOJO(queryResult, MyCustomMeasurement.class, TimeUnit.SECONDS);
379-
380-
// Then...
381-
Assertions.assertEquals(1, result.size(), "incorrect number of elements");
382-
Assertions.assertEquals(1_500_000_000L, result.get(0).time.toEpochMilli(), "incorrect value for the millis field");
383-
}
384-
385-
@Test
386-
void testToPOJO_SetMeasureName() {
387-
// Given...
388-
mapper.cacheMeasurementClass(MyCustomMeasurement.class);
389-
390-
List<String> columnList = Arrays.asList("uuid");
391-
List<Object> firstSeriesResult = Arrays.asList(UUID.randomUUID().toString());
392-
393-
QueryResult.Series series = new QueryResult.Series();
394-
series.setName("MySeriesName");
395-
series.setColumns(columnList);
396-
series.setValues(Arrays.asList(firstSeriesResult));
397-
398-
QueryResult.Result internalResult = new QueryResult.Result();
399-
internalResult.setSeries(Arrays.asList(series));
400-
401-
QueryResult queryResult = new QueryResult();
402-
queryResult.setResults(Arrays.asList(internalResult));
403-
404-
//When...
405-
List<MyCustomMeasurement> result =
406-
mapper.toPOJO(queryResult, MyCustomMeasurement.class, "MySeriesName");
407-
408-
//Then...
409-
Assertions.assertTrue(result.size() == 1);
410-
}
411-
412338
@Measurement(name = "CustomMeasurement")
413339
static class MyCustomMeasurement {
414340

0 commit comments

Comments
 (0)