Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation and design level code refactoring #918

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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)) {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/influxdb/impl/InfluxDBMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ public InfluxDBMapper(final InfluxDB influxDB) {
this.influxDB = influxDB;
}

String getDatabaseName(final Class<?> clazz) {
return ((Measurement) clazz.getAnnotation(Measurement.class)).database();
}

/**
* <p>
* 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.
* </p>
*
* @param queryResult the InfluxDB result object
* @param clazz the Class that will be used to hold your measurement data
* @param <T> 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,
* <code>clazz</code> parameter is not annotated with &#64;Measurement or it was not
* possible to define the values of your POJO (e.g. due to an unsupported field type).
*/
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, final String measurementName)
throws InfluxDBMapperException {
return toPOJO(queryResult, clazz, measurementName, TimeUnit.MILLISECONDS);
}

public <T> List<T> query(final Query query, final Class<T> clazz, final String measurementName) {
QueryResult queryResult = influxDB.query(query);
return toPOJO(queryResult, clazz, measurementName);
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/org/influxdb/impl/InfluxDBResultMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,6 @@ public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz,
return this.toPOJO(queryResult, clazz, measurementName, precision);
}

/**
* <p>
* 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.
* </p>
*
* @param queryResult the InfluxDB result object
* @param clazz the Class that will be used to hold your measurement data
* @param <T> 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,
* <code>clazz</code> parameter is not annotated with &#64;Measurement or it was not
* possible to define the values of your POJO (e.g. due to an unsupported field type).
*/
public <T> List<T> toPOJO(final QueryResult queryResult, final Class<T> clazz, final String measurementName)
throws InfluxDBMapperException {
return toPOJO(queryResult, clazz, measurementName, TimeUnit.MILLISECONDS);
}

/**
* <p>
* Process a {@link QueryResult} object returned by the InfluxDB client inspecting the internal
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/influxdb/impl/Preconditions.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.influxdb.impl;

import java.util.regex.Pattern;

/**
* Functions for parameter validation.
*
* @author Simon Legner
*/
public final class Preconditions {

private static final String DURATION_REGULAR_EXPRESSION = "(\\d+[wdmhs])+|inf";

private Preconditions() {
}

Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/influxdb/impl/RetryCapableBatchWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ private WriteResult(final InfluxDBException e) {
@Override
public synchronized void write(final Collection<BatchPoints> 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<BatchPoints> collection){
ListIterator<BatchPoints> batchQueueIterator = batchQueue.listIterator();
while (batchQueueIterator.hasNext()) {
BatchPoints entry = batchQueueIterator.next();
Expand All @@ -88,7 +94,9 @@ public synchronized void write(final Collection<BatchPoints> collection) {
return;
}
}
// write the last given batch last so that duplicate data points get overwritten correctly
}

private void writeLastBatch(final Collection<BatchPoints> collection){
Iterator<BatchPoints> collectionIterator = collection.iterator();
while (collectionIterator.hasNext()) {
BatchPoints batchPoints = collectionIterator.next();
Expand Down
21 changes: 1 addition & 20 deletions src/main/java/org/influxdb/querybuilder/BuiltQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/influxdb/querybuilder/Utility.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 3 additions & 2 deletions src/test/java/org/influxdb/impl/InfluxDBResultMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
public class InfluxDBResultMapperTest {

InfluxDBResultMapper mapper = new InfluxDBResultMapper();
private InfluxDBMapper influxDBMapper;

@Test
public void testToPOJO_HappyPath() {
Expand Down Expand Up @@ -404,7 +405,7 @@ void testToPOJO_SetMeasureName() {

//When...
List<MyCustomMeasurement> result =
mapper.toPOJO(queryResult, MyCustomMeasurement.class, "MySeriesName");
influxDBMapper.toPOJO(queryResult, MyCustomMeasurement.class, "MySeriesName");

//Then...
Assertions.assertTrue(result.size() == 1);
Expand Down Expand Up @@ -434,7 +435,7 @@ void testToPOJOInheritance() {

//When...
List<MySubMeasurement> result =
mapper.toPOJO(queryResult, MySubMeasurement.class, "MySeriesName");
influxDBMapper.toPOJO(queryResult, MySubMeasurement.class, "MySeriesName");

//Then...
Assertions.assertTrue(result.size() == 1);
Expand Down