diff --git a/pom.xml b/pom.xml index 163166343..c220aa479 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.influxdb influxdb-java jar - 2.0-SNAPSHOT + 2.1-SNAPSHOT influxdb java bindings Java API to access the InfluxDB REST API http://www.influxdb.org @@ -92,7 +92,7 @@ com.google.guava guava - 18.0 + 20.0 com.squareup.retrofit diff --git a/src/main/java/org/influxdb/InfluxDBFactory.java b/src/main/java/org/influxdb/InfluxDBFactory.java index 404e0d327..85cb32b21 100644 --- a/src/main/java/org/influxdb/InfluxDBFactory.java +++ b/src/main/java/org/influxdb/InfluxDBFactory.java @@ -1,8 +1,9 @@ package org.influxdb; import org.influxdb.impl.InfluxDBImpl; +import org.influxdb.impl.StringUtil; + import com.google.common.base.Preconditions; -import com.google.common.base.Strings; /** * A Factory to create a instance of a InfluxDB Database adapter. @@ -11,23 +12,23 @@ * */ public enum InfluxDBFactory { - ; - /** - * Create a connection to a InfluxDB. - * - * @param url - * the url to connect to. - * @param username - * the username which is used to authorize against the influxDB instance. - * @param password - * the password for the username which is used to authorize against the influxDB - * instance. - * @return a InfluxDB adapter suitable to access a InfluxDB. - */ - public static InfluxDB connect(final String url, final String username, final String password) { - Preconditions.checkArgument(!Strings.isNullOrEmpty(url), "The URL may not be null or empty."); - Preconditions.checkArgument(!Strings.isNullOrEmpty(username), "The username may not be null or empty."); - return new InfluxDBImpl(url, username, password); - } + ; + /** + * Create a connection to a InfluxDB. + * + * @param url + * the url to connect to. + * @param username + * the username which is used to authorize against the influxDB instance. + * @param password + * the password for the username which is used to authorize against the influxDB + * instance. + * @return a InfluxDB adapter suitable to access a InfluxDB. + */ + public static InfluxDB connect(final String url, final String username, final String password) { + Preconditions.checkArgument(!StringUtil.isNullOrEmpty(url), "The URL may not be null or empty."); + Preconditions.checkArgument(!StringUtil.isNullOrEmpty(username), "The username may not be null or empty."); + return new InfluxDBImpl(url, username, password); + } } diff --git a/src/main/java/org/influxdb/dto/BatchPoints.java b/src/main/java/org/influxdb/dto/BatchPoints.java index 0c31e810f..41d652957 100644 --- a/src/main/java/org/influxdb/dto/BatchPoints.java +++ b/src/main/java/org/influxdb/dto/BatchPoints.java @@ -3,10 +3,11 @@ import java.util.Arrays; import java.util.List; import java.util.Map; + import org.influxdb.InfluxDB.ConsistencyLevel; +import org.influxdb.impl.StringUtil; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Ordering; @@ -20,242 +21,242 @@ * */ public class BatchPoints { - private String database; - private String retentionPolicy; - private Map tags; - private List points; - private ConsistencyLevel consistency; + private String database; + private String retentionPolicy; + private Map tags; + private List points; + private ConsistencyLevel consistency; - BatchPoints() { - // Only visible in the Builder - } + BatchPoints() { + // Only visible in the Builder + } - /** - * Create a new BatchPoints build to create a new BatchPoints in a fluent manner- - * - * @param database - * the name of the Database - * @return the Builder to be able to add further Builder calls. - */ - public static Builder database(final String database) { - return new Builder(database); - } + /** + * Create a new BatchPoints build to create a new BatchPoints in a fluent manner- + * + * @param database + * the name of the Database + * @return the Builder to be able to add further Builder calls. + */ + public static Builder database(final String database) { + return new Builder(database); + } - /** - * The Builder to create a new BatchPoints instance. - */ - public static final class Builder { - private final String database; - private String retentionPolicy; - private final Map tags = Maps.newTreeMap(Ordering.natural()); - private final List points = Lists.newArrayList(); - private ConsistencyLevel consistency; + /** + * The Builder to create a new BatchPoints instance. + */ + public static final class Builder { + private final String database; + private String retentionPolicy; + private final Map tags = Maps.newTreeMap(Ordering.natural()); + private final List points = Lists.newArrayList(); + private ConsistencyLevel consistency; - /** - * @param database - */ - Builder(final String database) { - super(); - this.database = database; - } + /** + * @param database + */ + Builder(final String database) { + super(); + this.database = database; + } - /** - * The retentionPolicy to use. - * - * @param policy - * @return the Builder instance - */ - public Builder retentionPolicy(final String policy) { - this.retentionPolicy = policy; - return this; - } + /** + * The retentionPolicy to use. + * + * @param policy + * @return the Builder instance + */ + public Builder retentionPolicy(final String policy) { + this.retentionPolicy = policy; + return this; + } - /** - * Add a tag to this set of points. - * - * @param tagName - * the tag name - * @param value - * the tag value - * @return the Builder instance. - */ - public Builder tag(final String tagName, final String value) { - this.tags.put(tagName, value); - return this; - } + /** + * Add a tag to this set of points. + * + * @param tagName + * the tag name + * @param value + * the tag value + * @return the Builder instance. + */ + public Builder tag(final String tagName, final String value) { + this.tags.put(tagName, value); + return this; + } - /** - * Add a Point to this set of points. - * - * @param pointToAdd - * @return the Builder instance - */ - public Builder point(final Point pointToAdd) { - this.points.add(pointToAdd); - return this; - } + /** + * Add a Point to this set of points. + * + * @param pointToAdd + * @return the Builder instance + */ + public Builder point(final Point pointToAdd) { + this.points.add(pointToAdd); + return this; + } - /** - * Add a set of Points to this set of points. - * - * @param pointsToAdd - * @return the Builder instance - */ - public Builder points(final Point... pointsToAdd) { - this.points.addAll(Arrays.asList(pointsToAdd)); - return this; - } + /** + * Add a set of Points to this set of points. + * + * @param pointsToAdd + * @return the Builder instance + */ + public Builder points(final Point... pointsToAdd) { + this.points.addAll(Arrays.asList(pointsToAdd)); + return this; + } - /** - * Set the ConsistencyLevel to use. If not given it defaults to {@link ConsistencyLevel#ONE} - * - * @param consistencyLevel - * @return the Builder instance - */ - public Builder consistency(final ConsistencyLevel consistencyLevel) { - this.consistency = consistencyLevel; - return this; - } + /** + * Set the ConsistencyLevel to use. If not given it defaults to {@link ConsistencyLevel#ONE} + * + * @param consistencyLevel + * @return the Builder instance + */ + public Builder consistency(final ConsistencyLevel consistencyLevel) { + this.consistency = consistencyLevel; + return this; + } - /** - * Create a new BatchPoints instance. - * - * @return the created BatchPoints. - */ - public BatchPoints build() { - Preconditions.checkArgument(!Strings.isNullOrEmpty(this.database), "Database must not be null or empty."); - BatchPoints batchPoints = new BatchPoints(); - batchPoints.setDatabase(this.database); - for (Point point : this.points) { - point.getTags().putAll(this.tags); - } - batchPoints.setPoints(this.points); - batchPoints.setRetentionPolicy(this.retentionPolicy); - batchPoints.setTags(this.tags); - if (null == this.consistency) { - this.consistency = ConsistencyLevel.ONE; - } - batchPoints.setConsistency(this.consistency); - return batchPoints; - } - } + /** + * Create a new BatchPoints instance. + * + * @return the created BatchPoints. + */ + public BatchPoints build() { + Preconditions.checkArgument(!StringUtil.isNullOrEmpty(this.database), "Database must not be null or empty."); + BatchPoints batchPoints = new BatchPoints(); + batchPoints.setDatabase(this.database); + for (Point point : this.points) { + point.getTags().putAll(this.tags); + } + batchPoints.setPoints(this.points); + batchPoints.setRetentionPolicy(this.retentionPolicy); + batchPoints.setTags(this.tags); + if (null == this.consistency) { + this.consistency = ConsistencyLevel.ONE; + } + batchPoints.setConsistency(this.consistency); + return batchPoints; + } + } - /** - * @return the database - */ - public String getDatabase() { - return this.database; - } + /** + * @return the database + */ + public String getDatabase() { + return this.database; + } - /** - * @param database - * the database to set - */ - void setDatabase(final String database) { - this.database = database; - } + /** + * @param database + * the database to set + */ + void setDatabase(final String database) { + this.database = database; + } - /** - * @return the retentionPolicy - */ - public String getRetentionPolicy() { - return this.retentionPolicy; - } + /** + * @return the retentionPolicy + */ + public String getRetentionPolicy() { + return this.retentionPolicy; + } - /** - * @param retentionPolicy - * the retentionPolicy to set - */ - void setRetentionPolicy(final String retentionPolicy) { - this.retentionPolicy = retentionPolicy; - } + /** + * @param retentionPolicy + * the retentionPolicy to set + */ + void setRetentionPolicy(final String retentionPolicy) { + this.retentionPolicy = retentionPolicy; + } - /** - * @return the points - */ - public List getPoints() { - return this.points; - } + /** + * @return the points + */ + public List getPoints() { + return this.points; + } - /** - * @param points - * the points to set - */ - void setPoints(final List points) { - this.points = points; - } + /** + * @param points + * the points to set + */ + void setPoints(final List points) { + this.points = points; + } - /** - * Add a single Point to these batches. - * - * @param point - * @return this Instance to be able to daisy chain calls. - */ - public BatchPoints point(final Point point) { - point.getTags().putAll(this.tags); - this.points.add(point); - return this; - } + /** + * Add a single Point to these batches. + * + * @param point + * @return this Instance to be able to daisy chain calls. + */ + public BatchPoints point(final Point point) { + point.getTags().putAll(this.tags); + this.points.add(point); + return this; + } - /** - * @return the tags - */ - public Map getTags() { - return this.tags; - } + /** + * @return the tags + */ + public Map getTags() { + return this.tags; + } - /** - * @param tags - * the tags to set - */ - void setTags(final Map tags) { - this.tags = tags; - } + /** + * @param tags + * the tags to set + */ + void setTags(final Map tags) { + this.tags = tags; + } - /** - * @return the consistency - */ - public ConsistencyLevel getConsistency() { - return this.consistency; - } + /** + * @return the consistency + */ + public ConsistencyLevel getConsistency() { + return this.consistency; + } - /** - * @param consistency - * the consistency to set - */ - void setConsistency(final ConsistencyLevel consistency) { - this.consistency = consistency; - } + /** + * @param consistency + * the consistency to set + */ + void setConsistency(final ConsistencyLevel consistency) { + this.consistency = consistency; + } - /** - * {@inheritDoc} - */ - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("BatchPoints [database="); - builder.append(this.database); - builder.append(", retentionPolicy="); - builder.append(this.retentionPolicy); - builder.append(", tags="); - builder.append(this.tags); - builder.append(", points="); - builder.append(this.points); - builder.append("]"); - return builder.toString(); - } + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("BatchPoints [database="); + builder.append(this.database); + builder.append(", retentionPolicy="); + builder.append(this.retentionPolicy); + builder.append(", tags="); + builder.append(this.tags); + builder.append(", points="); + builder.append(this.points); + builder.append("]"); + return builder.toString(); + } - // measurement[,tag=value,tag2=value2...] field=value[,field2=value2...] [unixnano] - /** - * calculate the lineprotocol for all Points. - * - * @return the String with newLines. - */ - public String lineProtocol() { - StringBuilder sb = new StringBuilder(); - for (Point point : this.points) { - sb.append(point.lineProtocol()).append("\n"); - } - return sb.toString(); - } + // measurement[,tag=value,tag2=value2...] field=value[,field2=value2...] [unixnano] + /** + * calculate the lineprotocol for all Points. + * + * @return the String with newLines. + */ + public String lineProtocol() { + StringBuilder sb = new StringBuilder(); + for (Point point : this.points) { + sb.append(point.lineProtocol()).append("\n"); + } + return sb.toString(); + } } diff --git a/src/main/java/org/influxdb/dto/Point.java b/src/main/java/org/influxdb/dto/Point.java index 62c950763..7bcf7f5a4 100644 --- a/src/main/java/org/influxdb/dto/Point.java +++ b/src/main/java/org/influxdb/dto/Point.java @@ -4,8 +4,9 @@ import java.util.Map.Entry; import java.util.concurrent.TimeUnit; +import org.influxdb.impl.StringUtil; + import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import com.google.common.collect.Maps; import com.google.common.collect.Ordering; import com.google.common.escape.Escaper; @@ -18,237 +19,261 @@ * */ public class Point { - private String measurement; - private Map tags; - private Long time; - private TimeUnit precision = TimeUnit.NANOSECONDS; - private Map fields; - - private static final Escaper FIELD_ESCAPER = Escapers.builder().addEscape('"', "\\\"").build(); - private static final Escaper KEY_ESCAPER = Escapers.builder().addEscape(' ', "\\ ").addEscape(',', "\\,").build(); - - Point() { - } - - /** - * Create a new Point Build build to create a new Point in a fluent manner- - * - * @param measurement - * the name of the measurement. - * @return the Builder to be able to add further Builder calls. - */ - - public static Builder measurement(final String measurement) { - return new Builder(measurement); - } - - /** - * Builder for a new Point. - * - * @author stefan.majer [at] gmail.com - * - */ - public static final class Builder { - private final String measurement; - private final Map tags = Maps.newTreeMap(Ordering.natural()); - private Long time; - private TimeUnit precision = TimeUnit.NANOSECONDS; - private final Map fields = Maps.newTreeMap(Ordering.natural()); - - /** - * @param measurement - */ - Builder(final String measurement) { - this.measurement = measurement; - } - - /** - * Add a tag to this point. - * - * @param tagName - * the tag name - * @param value - * the tag value - * @return the Builder instance. - */ - public Builder tag(final String tagName, final String value) { - this.tags.put(tagName, value); - return this; - } - - /** - * Add a field to this point. - * - * @param field - * the field name - * @param value - * the value of this field - * @return the Builder instance. - */ - public Builder field(final String field, final Object value) { - this.fields.put(field, value); - return this; - } - - /** - * Add a time to this point - * - * @param precisionToSet - * @param timeToSet - * @return the Builder instance. - */ - public Builder time(final long timeToSet, final TimeUnit precisionToSet) { - this.time = timeToSet; - if (null != precisionToSet) { - this.precision = precisionToSet; - } - return this; - } - - /** - * Create a new Point. - * - * @return the newly created Point. - */ - public Point build() { - Preconditions - .checkArgument(!Strings.isNullOrEmpty(this.measurement), "Point name must not be null or empty."); - Preconditions.checkArgument(this.fields.size() > 0, "Point must have at least one field specified."); - Point point = new Point(); - point.setFields(this.fields); - point.setMeasurement(this.measurement); - point.setPrecision(this.precision); - point.setTags(this.tags); - point.setTime(this.time); - return point; - } - } - - /** - * @param measurement - * the measurement to set - */ - void setMeasurement(final String measurement) { - this.measurement = measurement; - } - - /** - * @param time - * the time to set - */ - void setTime(final Long time) { - this.time = time; - } - - /** - * @param tags - * the tags to set - */ - void setTags(final Map tags) { - this.tags = tags; - } - - /** - * @return the tags - */ - Map getTags() { - return this.tags; - } - - /** - * @param precision - * the precision to set - */ - void setPrecision(final TimeUnit precision) { - this.precision = precision; - } - - /** - * @param fields - * the fields to set - */ - void setFields(final Map fields) { - this.fields = fields; - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Point [name="); - builder.append(this.measurement); - builder.append(", time="); - builder.append(this.time); - builder.append(", tags="); - builder.append(this.tags); - builder.append(", precision="); - builder.append(this.precision); - builder.append(", fields="); - builder.append(this.fields); - builder.append("]"); - return builder.toString(); - } - - /** - * calculate the lineprotocol entry for a single Point. - * - * Documentation is WIP : https://github.com/influxdb/influxdb/pull/2997 - * - * https://github.com/influxdb/influxdb/blob/master/tsdb/README.md - * - * @return the String without newLine. - */ - public String lineProtocol() { - final StringBuilder sb = new StringBuilder(); - sb.append(KEY_ESCAPER.escape(this.measurement)); - sb.append(concatenatedTags()); - sb.append(concatenateFields()); - sb.append(formatedTime()); - return sb.toString(); - } - - private StringBuilder concatenatedTags() { - final StringBuilder sb = new StringBuilder(); - for (Entry tag : this.tags.entrySet()) { - sb.append(","); - sb.append(KEY_ESCAPER.escape(tag.getKey())).append("=").append(KEY_ESCAPER.escape(tag.getValue())); - } - sb.append(" "); - return sb; - } - - private StringBuilder concatenateFields() { - final StringBuilder sb = new StringBuilder(); - final int fieldCount = this.fields.size(); - int loops = 0; - - for (Entry field : this.fields.entrySet()) { - sb.append(KEY_ESCAPER.escape(field.getKey())).append("="); - loops++; - Object value = field.getValue(); - if (value instanceof String) { - String stringValue = (String) value; - sb.append("\"").append(FIELD_ESCAPER.escape(stringValue)).append("\""); - } else { - sb.append(value); - } - - if (loops < fieldCount) { - sb.append(","); - } - } - return sb; - } - - private StringBuilder formatedTime() { - final StringBuilder sb = new StringBuilder(); - if (null == this.time) { - this.time = System.nanoTime(); - } - sb.append(" ").append(TimeUnit.NANOSECONDS.convert(this.time, this.precision)); - return sb; - } + private String measurement; + private Map tags; + private Long time; + private TimeUnit precision = TimeUnit.NANOSECONDS; + private Map fields; + + private static final Escaper FIELD_ESCAPER = Escapers.builder().addEscape('"', "\\\"").build(); + private static final Escaper KEY_ESCAPER = Escapers.builder().addEscape(' ', "\\ ").addEscape(',', "\\,").build(); + + Point() {} + + /** + * Create a new Point Build build to create a new Point in a fluent manner- + * + * @param measurement + * the name of the measurement. + * @return the Builder to be able to add further Builder calls. + */ + + public static Builder measurement(final String measurement) { + return new Builder(measurement); + } + + /** + * Builder for a new Point. + * + * @author stefan.majer [at] gmail.com + * + */ + public static final class Builder { + private final String measurement; + private final Map tags = Maps.newTreeMap(Ordering.natural()); + private Long time; + private TimeUnit precision = TimeUnit.NANOSECONDS; + private final Map fields = Maps.newTreeMap(Ordering.natural()); + + /** + * @param measurement + */ + Builder(final String measurement) { + this.measurement = measurement; + } + + /** + * Add a tag to this point. + * + * @param tagName + * the tag name + * @param value + * the tag value + * @return the Builder instance. + */ + public Builder tag(final String tagName, final String value) { + this.tags.put(tagName, value); + return this; + } + + /** + * Add a Map of tags to this point. + * + * @param tags + * the map of fields + * @return the Builder instance. + */ + public Builder tags(final Map tags) { + this.tags.putAll(tags); + return this; + } + + /** + * Add a field to this point. + * + * @param field + * the field name + * @param value + * the value of this field + * @return the Builder instance. + */ + public Builder field(final String field, final Object value) { + this.fields.put(field, value); + return this; + } + + /** + * Add a Map of fields to this point. + * + * @param fields + * the map of fields + * @return the Builder instance. + */ + public Builder fields(final Map fields) { + this.fields.putAll(fields); + return this; + } + + /** + * Add a time to this point + * + * @param precisionToSet + * @param timeToSet + * @return the Builder instance. + */ + public Builder time(final long timeToSet, final TimeUnit precisionToSet) { + this.time = timeToSet; + if (null != precisionToSet) { + this.precision = precisionToSet; + } + return this; + } + + /** + * Create a new Point. + * + * @return the newly created Point. + */ + public Point build() { + Preconditions + .checkArgument(!StringUtil.isNullOrEmpty(this.measurement), "Point name must not be null or empty."); + Preconditions.checkArgument(this.fields.size() > 0, "Point must have at least one field specified."); + Point point = new Point(); + point.setFields(this.fields); + point.setMeasurement(this.measurement); + point.setPrecision(this.precision); + point.setTags(this.tags); + point.setTime(this.time); + return point; + } + } + + /** + * @param measurement + * the measurement to set + */ + void setMeasurement(final String measurement) { + this.measurement = measurement; + } + + /** + * @param time + * the time to set + */ + void setTime(final Long time) { + this.time = time; + } + + /** + * @param tags + * the tags to set + */ + void setTags(final Map tags) { + this.tags = tags; + } + + /** + * @return the tags + */ + Map getTags() { + return this.tags; + } + + /** + * @param precision + * the precision to set + */ + void setPrecision(final TimeUnit precision) { + this.precision = precision; + } + + /** + * @param fields + * the fields to set + */ + void setFields(final Map fields) { + this.fields = fields; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Point [name="); + builder.append(this.measurement); + builder.append(", time="); + builder.append(this.time); + builder.append(", tags="); + builder.append(this.tags); + builder.append(", precision="); + builder.append(this.precision); + builder.append(", fields="); + builder.append(this.fields); + builder.append("]"); + return builder.toString(); + } + + /** + * calculate the lineprotocol entry for a single Point. + * + * Documentation is WIP : https://github.com/influxdb/influxdb/pull/2997 + * + * https://github.com/influxdb/influxdb/blob/master/tsdb/README.md + * + * @return the String without newLine. + */ + public String lineProtocol() { + final StringBuilder sb = new StringBuilder(); + sb.append(KEY_ESCAPER.escape(this.measurement)); + sb.append(concatenatedTags()); + sb.append(concatenateFields()); + sb.append(formatedTime()); + return sb.toString(); + } + + private StringBuilder concatenatedTags() { + final StringBuilder sb = new StringBuilder(); + for (Entry tag : this.tags.entrySet()) { + sb.append(","); + sb.append(KEY_ESCAPER.escape(tag.getKey())).append("=").append(KEY_ESCAPER.escape(tag.getValue())); + } + sb.append(" "); + return sb; + } + + private StringBuilder concatenateFields() { + final StringBuilder sb = new StringBuilder(); + final int fieldCount = this.fields.size(); + int loops = 0; + + for (Entry field : this.fields.entrySet()) { + sb.append(KEY_ESCAPER.escape(field.getKey())).append("="); + loops++; + Object value = field.getValue(); + if (value instanceof String) { + String stringValue = (String)value; + sb.append("\"").append(FIELD_ESCAPER.escape(stringValue)).append("\""); + } + else { + sb.append(value); + } + + if (loops < fieldCount) { + sb.append(","); + } + } + return sb; + } + + private StringBuilder formatedTime() { + final StringBuilder sb = new StringBuilder(); + if (null == this.time) { + this.time = System.nanoTime(); + } + sb.append(" ").append(TimeUnit.NANOSECONDS.convert(this.time, this.precision)); + return sb; + } } \ No newline at end of file diff --git a/src/main/java/org/influxdb/dto/QueryResult.java b/src/main/java/org/influxdb/dto/QueryResult.java index 7de765387..177fa6e8b 100644 --- a/src/main/java/org/influxdb/dto/QueryResult.java +++ b/src/main/java/org/influxdb/dto/QueryResult.java @@ -1,6 +1,10 @@ package org.influxdb.dto; import java.util.List; +import java.util.Map; + +import com.google.common.collect.Maps; +import com.google.common.collect.Ordering; /** * {Purpose of This Type} @@ -24,169 +28,184 @@ // {"results":[{"series":[{"name":"databases","columns":["name"],"values":[["mydb"]]}]}]} public class QueryResult { - private List results; - private String error; - - /** - * @return the results - */ - public List getResults() { - return this.results; - } - - /** - * @param results - * the results to set - */ - public void setResults(final List results) { - this.results = results; - } - - /** - * @return the error - */ - public String getError() { - return this.error; - } - - /** - * @param error - * the error to set - */ - public void setError(final String error) { - this.error = error; - } - - public static class Result { - private List series; - private String error; - - /** - * @return the series - */ - public List getSeries() { - return this.series; - } - - /** - * @param series - * the series to set - */ - public void setSeries(final List series) { - this.series = series; - } - - /** - * @return the error - */ - public String getError() { - return this.error; - } - - /** - * @param error - * the error to set - */ - public void setError(final String error) { - this.error = error; - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Result [series="); - builder.append(this.series); - builder.append(", error="); - builder.append(this.error); - builder.append("]"); - return builder.toString(); - } - - } - - public static class Series { - private String name; - private List columns; - private List> values; - - /** - * @return the name - */ - public String getName() { - return this.name; - } - - /** - * @param name - * the name to set - */ - public void setName(final String name) { - this.name = name; - } - - /** - * @return the columns - */ - public List getColumns() { - return this.columns; - } - - /** - * @param columns - * the columns to set - */ - public void setColumns(final List columns) { - this.columns = columns; - } - - /** - * @return the values - */ - public List> getValues() { - return this.values; - } - - /** - * @param values - * the values to set - */ - public void setValues(final List> values) { - this.values = values; - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Series [name="); - builder.append(this.name); - builder.append(", columns="); - builder.append(this.columns); - builder.append(", values="); - builder.append(this.values); - builder.append("]"); - return builder.toString(); - } - - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("QueryResult [results="); - builder.append(this.results); - builder.append(", error="); - builder.append(this.error); - builder.append("]"); - return builder.toString(); - } + private List results; + private String error; + + /** + * @return the results + */ + public List getResults() { + return this.results; + } + + /** + * @param results + * the results to set + */ + public void setResults(final List results) { + this.results = results; + } + + /** + * @return the error + */ + public String getError() { + return this.error; + } + + /** + * @param error + * the error to set + */ + public void setError(final String error) { + this.error = error; + } + + public static class Result { + private List series; + private String error; + + /** + * @return the series + */ + public List getSeries() { + return this.series; + } + + /** + * @param series + * the series to set + */ + public void setSeries(final List series) { + this.series = series; + } + + /** + * @return the error + */ + public String getError() { + return this.error; + } + + /** + * @param error + * the error to set + */ + public void setError(final String error) { + this.error = error; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Result [series="); + builder.append(this.series); + builder.append(", error="); + builder.append(this.error); + builder.append("]"); + return builder.toString(); + } + + } + + public static class Series { + private String name; + private List columns; + private List> values; + private Map tags = Maps.newTreeMap(Ordering.natural()); + + /** + * @return the name + */ + public String getName() { + return this.name; + } + + /** + * @param name + * the name to set + */ + public void setName(final String name) { + this.name = name; + } + + /** + * @return the columns + */ + public List getColumns() { + return this.columns; + } + + /** + * @param columns + * the columns to set + */ + public void setColumns(final List columns) { + this.columns = columns; + } + + /** + * @return the values + */ + public List> getValues() { + return this.values; + } + + /** + * @param values + * the values to set + */ + public void setValues(final List> values) { + this.values = values; + } + + /** + * @param values + * the values to set + */ + public void setTags(final Map tags) { + this.tags = tags; + } + + public Map getTags() { + return tags; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Series [name="); + builder.append(this.name); + builder.append(", tags="); + builder.append(this.tags); + builder.append(", columns="); + builder.append(this.columns); + builder.append(", values="); + builder.append(this.values); + builder.append("]"); + return builder.toString(); + } + + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("QueryResult [results="); + builder.append(this.results); + builder.append(", error="); + builder.append(this.error); + builder.append("]"); + return builder.toString(); + } } diff --git a/src/main/java/org/influxdb/impl/InfluxDBImpl.java b/src/main/java/org/influxdb/impl/InfluxDBImpl.java index f2908ecd4..b70d877b2 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBImpl.java +++ b/src/main/java/org/influxdb/impl/InfluxDBImpl.java @@ -20,7 +20,6 @@ import retrofit.mime.TypedString; import com.google.common.base.Preconditions; -import com.google.common.base.Stopwatch; import com.google.common.collect.Lists; import com.squareup.okhttp.OkHttpClient; @@ -31,179 +30,171 @@ * */ public class InfluxDBImpl implements InfluxDB { - private final String username; - private final String password; - private final RestAdapter restAdapter; - private final InfluxDBService influxDBService; - private BatchProcessor batchProcessor; - private final AtomicBoolean batchEnabled = new AtomicBoolean(false); - private final AtomicLong writeCount = new AtomicLong(); - private final AtomicLong unBatchedCount = new AtomicLong(); - private final AtomicLong batchedCount = new AtomicLong(); - private LogLevel logLevel = LogLevel.NONE; - - /** - * Constructor which should only be used from the InfluxDBFactory. - * - * @param url - * the url where the influxdb is accessible. - * @param username - * the user to connect. - * @param password - * the password for this user. - */ - public InfluxDBImpl(final String url, final String username, final String password) { - super(); - this.username = username; - this.password = password; - OkHttpClient okHttpClient = new OkHttpClient(); - this.restAdapter = new RestAdapter.Builder() - .setEndpoint(url) - .setErrorHandler(new InfluxDBErrorHandler()) - .setClient(new OkClient(okHttpClient)) - .build(); - this.influxDBService = this.restAdapter.create(InfluxDBService.class); - } - - @Override - public InfluxDB setLogLevel(final LogLevel logLevel) { - switch (logLevel) { - case NONE: - this.restAdapter.setLogLevel(retrofit.RestAdapter.LogLevel.NONE); - break; - case BASIC: - this.restAdapter.setLogLevel(retrofit.RestAdapter.LogLevel.BASIC); - break; - case HEADERS: - this.restAdapter.setLogLevel(retrofit.RestAdapter.LogLevel.HEADERS); - break; - case FULL: - this.restAdapter.setLogLevel(retrofit.RestAdapter.LogLevel.FULL); - break; - default: - break; - } - this.logLevel = logLevel; - return this; - } - - @Override - public InfluxDB enableBatch(final int actions, final int flushDuration, final TimeUnit flushDurationTimeUnit) { - if (this.batchEnabled.get()) { - throw new IllegalArgumentException("BatchProcessing is already enabled."); - } - this.batchProcessor = BatchProcessor - .builder(this) - .actions(actions) - .interval(flushDuration, flushDurationTimeUnit) - .build(); - this.batchEnabled.set(true); - return this; - } - - @Override - public void disableBatch() { - this.batchEnabled.set(false); - this.batchProcessor.flush(); - if (this.logLevel != LogLevel.NONE) { - System.out.println( - "total writes:" + this.writeCount.get() + " unbatched:" + this.unBatchedCount.get() + "batchPoints:" - + this.batchedCount); - } - } - - @Override - public Pong ping() { - Stopwatch watch = Stopwatch.createStarted(); - Response response = this.influxDBService.ping(); - List
headers = response.getHeaders(); - String version = "unknown"; - for (Header header : headers) { - if (null != header.getName() && header.getName().equalsIgnoreCase("X-Influxdb-Version")) { - version = header.getValue(); - } - } - Pong pong = new Pong(); - pong.setVersion(version); - pong.setResponseTime(watch.elapsed(TimeUnit.MILLISECONDS)); - return pong; - } - - @Override - public String version() { - return ping().getVersion(); - } - - @Override - public void write(final String database, final String retentionPolicy, final Point point) { - if (this.batchEnabled.get()) { - BatchEntry batchEntry = new BatchEntry(point, database, retentionPolicy); - this.batchProcessor.put(batchEntry); - } else { - BatchPoints batchPoints = BatchPoints.database(database).retentionPolicy(retentionPolicy).build(); - batchPoints.point(point); - this.write(batchPoints); - this.unBatchedCount.incrementAndGet(); - } - this.writeCount.incrementAndGet(); - } - - @Override - public void write(final BatchPoints batchPoints) { - this.batchedCount.addAndGet(batchPoints.getPoints().size()); - TypedString lineProtocol = new TypedString(batchPoints.lineProtocol()); - this.influxDBService.writePoints( - this.username, - this.password, - batchPoints.getDatabase(), - batchPoints.getRetentionPolicy(), - TimeUtil.toTimePrecision(TimeUnit.NANOSECONDS), - batchPoints.getConsistency().value(), - lineProtocol); - - } - - /** - * {@inheritDoc} - */ - @Override - public QueryResult query(final Query query) { - QueryResult response = this.influxDBService - .query(this.username, this.password, query.getDatabase(), query.getCommand()); - return response; - } - - /** - * {@inheritDoc} - */ - @Override - public void createDatabase(final String name) { - Preconditions.checkArgument(!name.contains("-"), "Databasename cant contain -"); - this.influxDBService.query(this.username, this.password, "CREATE DATABASE " + name); - } - - /** - * {@inheritDoc} - */ - @Override - public void deleteDatabase(final String name) { - this.influxDBService.query(this.username, this.password, "DROP DATABASE " + name); - } - - /** - * {@inheritDoc} - */ - @Override - public List describeDatabases() { - QueryResult result = this.influxDBService.query(this.username, this.password, "SHOW DATABASES"); - // {"results":[{"series":[{"name":"databases","columns":["name"],"values":[["mydb"]]}]}]} - // Series [name=databases, columns=[name], values=[[mydb], [unittest_1433605300968]]] - List> databaseNames = result.getResults().get(0).getSeries().get(0).getValues(); - List databases = Lists.newArrayList(); - for (List database : databaseNames) { - databases.add(database.get(0).toString()); - } - return databases; - } + private final String username; + private final String password; + private final RestAdapter restAdapter; + private final InfluxDBService influxDBService; + private BatchProcessor batchProcessor; + private final AtomicBoolean batchEnabled = new AtomicBoolean(false); + private final AtomicLong writeCount = new AtomicLong(); + private final AtomicLong unBatchedCount = new AtomicLong(); + private final AtomicLong batchedCount = new AtomicLong(); + private LogLevel logLevel = LogLevel.NONE; + + /** + * Constructor which should only be used from the InfluxDBFactory. + * + * @param url + * the url where the influxdb is accessible. + * @param username + * the user to connect. + * @param password + * the password for this user. + */ + public InfluxDBImpl(final String url, final String username, final String password) { + super(); + this.username = username; + this.password = password; + OkHttpClient okHttpClient = new OkHttpClient(); + this.restAdapter = new RestAdapter.Builder().setEndpoint(url).setErrorHandler(new InfluxDBErrorHandler()) + .setClient(new OkClient(okHttpClient)).build(); + this.influxDBService = this.restAdapter.create(InfluxDBService.class); + } + + @Override + public InfluxDB setLogLevel(final LogLevel logLevel) { + switch (logLevel) { + case NONE: + this.restAdapter.setLogLevel(retrofit.RestAdapter.LogLevel.NONE); + break; + case BASIC: + this.restAdapter.setLogLevel(retrofit.RestAdapter.LogLevel.BASIC); + break; + case HEADERS: + this.restAdapter.setLogLevel(retrofit.RestAdapter.LogLevel.HEADERS); + break; + case FULL: + this.restAdapter.setLogLevel(retrofit.RestAdapter.LogLevel.FULL); + break; + default: + break; + } + this.logLevel = logLevel; + return this; + } + + @Override + public InfluxDB enableBatch(final int actions, final int flushDuration, final TimeUnit flushDurationTimeUnit) { + if (this.batchEnabled.get()) { + throw new IllegalArgumentException("BatchProcessing is already enabled."); + } + this.batchProcessor = BatchProcessor.builder(this).actions(actions) + .interval(flushDuration, flushDurationTimeUnit).build(); + this.batchEnabled.set(true); + return this; + } + + @Override + public void disableBatch() { + this.batchEnabled.set(false); + this.batchProcessor.flush(); + if (this.logLevel != LogLevel.NONE) { + System.out.println("total writes:" + this.writeCount.get() + " unbatched:" + this.unBatchedCount.get() + + "batchPoints:" + this.batchedCount); + } + } + + @Override + public Pong ping() { + long startTime = System.currentTimeMillis(); + Response response = this.influxDBService.ping(); + List
headers = response.getHeaders(); + String version = "unknown"; + for (Header header : headers) { + if (null != header.getName() && header.getName().equalsIgnoreCase("X-Influxdb-Version")) { + version = header.getValue(); + } + } + Pong pong = new Pong(); + pong.setVersion(version); + pong.setResponseTime(System.currentTimeMillis() - startTime); + return pong; + } + + @Override + public String version() { + return ping().getVersion(); + } + + @Override + public void write(final String database, final String retentionPolicy, final Point point) { + if (this.batchEnabled.get()) { + BatchEntry batchEntry = new BatchEntry(point, database, retentionPolicy); + this.batchProcessor.put(batchEntry); + } + else { + BatchPoints batchPoints = BatchPoints.database(database).retentionPolicy(retentionPolicy).build(); + batchPoints.point(point); + this.write(batchPoints); + this.unBatchedCount.incrementAndGet(); + } + this.writeCount.incrementAndGet(); + } + + @Override + public void write(final BatchPoints batchPoints) { + this.batchedCount.addAndGet(batchPoints.getPoints().size()); + TypedString lineProtocol = new TypedString(batchPoints.lineProtocol()); + this.influxDBService.writePoints(this.username, this.password, batchPoints.getDatabase(), batchPoints + .getRetentionPolicy(), TimeUtil.toTimePrecision(TimeUnit.NANOSECONDS), batchPoints.getConsistency() + .value(), lineProtocol); + + } + + /** + * {@inheritDoc} + */ + @Override + public QueryResult query(final Query query) { + QueryResult response = this.influxDBService.query(this.username, this.password, query.getDatabase(), + query.getCommand()); + return response; + } + + /** + * {@inheritDoc} + */ + @Override + public void createDatabase(final String name) { + Preconditions.checkArgument(!name.contains("-"), "Databasename cant contain -"); + this.influxDBService.query(this.username, this.password, "CREATE DATABASE " + name); + } + + /** + * {@inheritDoc} + */ + @Override + public void deleteDatabase(final String name) { + this.influxDBService.query(this.username, this.password, "DROP DATABASE " + name); + } + + /** + * {@inheritDoc} + */ + @Override + public List describeDatabases() { + QueryResult result = this.influxDBService.query(this.username, this.password, "SHOW DATABASES"); + // {"results":[{"series":[{"name":"databases","columns":["name"],"values":[["mydb"]]}]}]} + // Series [name=databases, columns=[name], values=[[mydb], + // [unittest_1433605300968]]] + List> databaseNames = result.getResults().get(0).getSeries().get(0).getValues(); + List databases = Lists.newArrayList(); + if (!databases.isEmpty()) { + for (List database : databaseNames) { + databases.add(database.get(0).toString()); + } + } + return databases; + } } diff --git a/src/main/java/org/influxdb/impl/StringUtil.java b/src/main/java/org/influxdb/impl/StringUtil.java new file mode 100644 index 000000000..0dfa5a102 --- /dev/null +++ b/src/main/java/org/influxdb/impl/StringUtil.java @@ -0,0 +1,12 @@ +package org.influxdb.impl; + +public enum StringUtil { + INSTANCE; + + public static boolean isNullOrEmpty(final String str) { + if (str != null && !str.isEmpty()) { + return false; + } + return true; + } +}