diff --git a/src/main/java/org/influxdb/dto/Point.java b/src/main/java/org/influxdb/dto/Point.java index 96069026c..da9ff7386 100755 --- a/src/main/java/org/influxdb/dto/Point.java +++ b/src/main/java/org/influxdb/dto/Point.java @@ -526,7 +526,7 @@ private int concatenatedFields(final StringBuilder sb) { escapeKey(sb, field.getKey()); sb.append('='); if (value instanceof Number) { - if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) { + if (isValidInstanceType(value)) { sb.append(NUMBER_FORMATTER.get().format(value)); } else { sb.append(value).append('i'); @@ -554,6 +554,10 @@ private int concatenatedFields(final StringBuilder sb) { return fieldCount; } + private boolean isValidInstanceType(Object value){ + return value instanceof Double || value instanceof Float || value instanceof BigDecimal; + } + static void escapeKey(final StringBuilder sb, final String key) { for (int i = 0; i < key.length(); i++) { switch (key.charAt(i)) { diff --git a/src/main/java/org/influxdb/impl/InfluxDBMapper.java b/src/main/java/org/influxdb/impl/InfluxDBMapper.java index 700a960cf..58a6e4c51 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBMapper.java +++ b/src/main/java/org/influxdb/impl/InfluxDBMapper.java @@ -21,6 +21,34 @@ public InfluxDBMapper(final InfluxDB influxDB) { this.influxDB = influxDB; } + String getDatabaseName(final Class clazz) { + return ((Measurement) clazz.getAnnotation(Measurement.class)).database(); + } + + /** + *

+ * Process a {@link QueryResult} object returned by the InfluxDB client inspecting the internal + * data structure and creating the respective object instances based on the Class passed as + * parameter. + *

+ * + * @param queryResult the InfluxDB result object + * @param clazz the Class that will be used to hold your measurement data + * @param the target type + * @param measurementName name of the Measurement + * + * @return a {@link List} of objects from the same Class passed as parameter and sorted on the + * same order as received from InfluxDB. + * + * @throws InfluxDBMapperException If {@link QueryResult} parameter contain errors, + * clazz parameter is not annotated with @Measurement or it was not + * possible to define the values of your POJO (e.g. due to an unsupported field type). + */ + public List toPOJO(final QueryResult queryResult, final Class clazz, final String measurementName) + throws InfluxDBMapperException { + return toPOJO(queryResult, clazz, measurementName, TimeUnit.MILLISECONDS); + } + public List query(final Query query, final Class clazz, final String measurementName) { QueryResult queryResult = influxDB.query(query); return toPOJO(queryResult, clazz, measurementName); diff --git a/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java b/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java index d6edef17c..17fc22289 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java +++ b/src/main/java/org/influxdb/impl/InfluxDBResultMapper.java @@ -113,30 +113,6 @@ public List toPOJO(final QueryResult queryResult, final Class clazz, return this.toPOJO(queryResult, clazz, measurementName, precision); } - /** - *

- * Process a {@link QueryResult} object returned by the InfluxDB client inspecting the internal - * data structure and creating the respective object instances based on the Class passed as - * parameter. - *

- * - * @param queryResult the InfluxDB result object - * @param clazz the Class that will be used to hold your measurement data - * @param the target type - * @param measurementName name of the Measurement - * - * @return a {@link List} of objects from the same Class passed as parameter and sorted on the - * same order as received from InfluxDB. - * - * @throws InfluxDBMapperException If {@link QueryResult} parameter contain errors, - * clazz parameter is not annotated with @Measurement or it was not - * possible to define the values of your POJO (e.g. due to an unsupported field type). - */ - public List toPOJO(final QueryResult queryResult, final Class clazz, final String measurementName) - throws InfluxDBMapperException { - return toPOJO(queryResult, clazz, measurementName, TimeUnit.MILLISECONDS); - } - /** *

* Process a {@link QueryResult} object returned by the InfluxDB client inspecting the internal @@ -231,10 +207,6 @@ String getMeasurementName(final Class clazz) { return ((Measurement) clazz.getAnnotation(Measurement.class)).name(); } - String getDatabaseName(final Class clazz) { - return ((Measurement) clazz.getAnnotation(Measurement.class)).database(); - } - String getRetentionPolicy(final Class clazz) { return ((Measurement) clazz.getAnnotation(Measurement.class)).retentionPolicy(); } diff --git a/src/main/java/org/influxdb/impl/Preconditions.java b/src/main/java/org/influxdb/impl/Preconditions.java index 1e34204a3..b996b6a06 100644 --- a/src/main/java/org/influxdb/impl/Preconditions.java +++ b/src/main/java/org/influxdb/impl/Preconditions.java @@ -1,5 +1,7 @@ package org.influxdb.impl; +import java.util.regex.Pattern; + /** * Functions for parameter validation. * @@ -7,6 +9,8 @@ */ public final class Preconditions { + private static final String DURATION_REGULAR_EXPRESSION = "(\\d+[wdmhs])+|inf"; + private Preconditions() { } @@ -54,7 +58,7 @@ public static void checkNotNegativeNumber(final Number number, final String name * @throws IllegalArgumentException if the given duration is not valid. */ public static void checkDuration(final String duration, final String name) throws IllegalArgumentException { - if (!duration.matches("(\\d+[wdmhs])+|inf")) { + if (!duration.matches(DURATION_REGULAR_EXPRESSION)) { throw new IllegalArgumentException("Invalid InfluxDB duration: " + duration + " for " + name); } diff --git a/src/main/java/org/influxdb/impl/RetryCapableBatchWriter.java b/src/main/java/org/influxdb/impl/RetryCapableBatchWriter.java index e2b08190b..806302de7 100644 --- a/src/main/java/org/influxdb/impl/RetryCapableBatchWriter.java +++ b/src/main/java/org/influxdb/impl/RetryCapableBatchWriter.java @@ -67,6 +67,12 @@ private WriteResult(final InfluxDBException e) { @Override public synchronized void write(final Collection collection) { // empty the cached data first + emptyCachedData(collection); + // write the last given batch last so that duplicate data points get overwritten correctly + writeLastBatch(collection); + } + + private void emptyCachedData(final Collection collection){ ListIterator batchQueueIterator = batchQueue.listIterator(); while (batchQueueIterator.hasNext()) { BatchPoints entry = batchQueueIterator.next(); @@ -88,7 +94,9 @@ public synchronized void write(final Collection collection) { return; } } - // write the last given batch last so that duplicate data points get overwritten correctly + } + + private void writeLastBatch(final Collection collection){ Iterator collectionIterator = collection.iterator(); while (collectionIterator.hasNext()) { BatchPoints batchPoints = collectionIterator.next(); diff --git a/src/main/java/org/influxdb/querybuilder/BuiltQuery.java b/src/main/java/org/influxdb/querybuilder/BuiltQuery.java index d9a3c5920..8b88e7a01 100644 --- a/src/main/java/org/influxdb/querybuilder/BuiltQuery.java +++ b/src/main/java/org/influxdb/querybuilder/BuiltQuery.java @@ -29,29 +29,10 @@ public BuiltQuery(final String database, final boolean requiresPost) { super(null, database, requiresPost); } - static StringBuilder addSemicolonIfMissing(final StringBuilder stringBuilder) { - int length = trimLast(stringBuilder); - if (length == 0 || stringBuilder.charAt(length - 1) != ';') { - stringBuilder.append(';'); - } - return stringBuilder; - } - - static int trimLast(final StringBuilder stringBuilder) { - int length = stringBuilder.length(); - while (length > 0 && stringBuilder.charAt(length - 1) <= ' ') { - length -= 1; - } - if (length != stringBuilder.length()) { - stringBuilder.setLength(length); - } - return length; - } - @Override public String getCommand() { StringBuilder sb = buildQueryString(new StringBuilder()); - addSemicolonIfMissing(sb); + Utility.addSemicolonIfMissing(sb); return sb.toString(); } diff --git a/src/main/java/org/influxdb/querybuilder/SelectCoreImpl.java b/src/main/java/org/influxdb/querybuilder/SelectCoreImpl.java index 37ccf47be..d334003ad 100644 --- a/src/main/java/org/influxdb/querybuilder/SelectCoreImpl.java +++ b/src/main/java/org/influxdb/querybuilder/SelectCoreImpl.java @@ -3,7 +3,7 @@ import static org.influxdb.querybuilder.Appender.appendValue; import static org.influxdb.querybuilder.Appender.joinAndAppend; import static org.influxdb.querybuilder.Appender.joinAndAppendNames; -import static org.influxdb.querybuilder.BuiltQuery.trimLast; +import static org.influxdb.querybuilder.Utility.trimLast; import static org.influxdb.querybuilder.FunctionFactory.function; import java.util.Arrays; diff --git a/src/main/java/org/influxdb/querybuilder/Utility.java b/src/main/java/org/influxdb/querybuilder/Utility.java new file mode 100644 index 000000000..1d1b3c1c2 --- /dev/null +++ b/src/main/java/org/influxdb/querybuilder/Utility.java @@ -0,0 +1,23 @@ +package org.influxdb.querybuilder; + +public class Utility { + + static StringBuilder addSemicolonIfMissing(final StringBuilder stringBuilder) { + int length = trimLast(stringBuilder); + if (length == 0 || stringBuilder.charAt(length - 1) != ';') { + stringBuilder.append(';'); + } + return stringBuilder; + } + + static int trimLast(final StringBuilder stringBuilder) { + int length = stringBuilder.length(); + while (length > 0 && stringBuilder.charAt(length - 1) <= ' ') { + length -= 1; + } + if (length != stringBuilder.length()) { + stringBuilder.setLength(length); + } + return length; + } +} diff --git a/src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java b/src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java index f4eee0ba2..e4a6cfc51 100644 --- a/src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java +++ b/src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java @@ -48,6 +48,7 @@ public class InfluxDBResultMapperTest { InfluxDBResultMapper mapper = new InfluxDBResultMapper(); + private InfluxDBMapper influxDBMapper; @Test public void testToPOJO_HappyPath() { @@ -404,7 +405,7 @@ void testToPOJO_SetMeasureName() { //When... List result = - mapper.toPOJO(queryResult, MyCustomMeasurement.class, "MySeriesName"); + influxDBMapper.toPOJO(queryResult, MyCustomMeasurement.class, "MySeriesName"); //Then... Assertions.assertTrue(result.size() == 1); @@ -434,7 +435,7 @@ void testToPOJOInheritance() { //When... List result = - mapper.toPOJO(queryResult, MySubMeasurement.class, "MySeriesName"); + influxDBMapper.toPOJO(queryResult, MySubMeasurement.class, "MySeriesName"); //Then... Assertions.assertTrue(result.size() == 1);