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

influxdb-java-182 Allow write precision of TimeUnit other than Nanoseconds #321

Merged
merged 18 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
38 changes: 38 additions & 0 deletions src/main/java/org/influxdb/InfluxDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,25 @@ public InfluxDB enableBatch(final int actions, final int flushDuration, final Ti
public void write(final String database, final String retentionPolicy,
final ConsistencyLevel consistency, final String records);

/**
* Write a set of Points to the influxdb database with the string records.
*
* @see <a href="https://github.com/influxdb/influxdb/pull/2696">2696</a>
*
* @param database
* the name of the database to write
* @param retentionPolicy
* the retentionPolicy to use
* @param consistency
* the ConsistencyLevel to use
* @param precision
* the time precision to use
* @param records
* the points in the correct lineprotocol.
*/
public void write(final String database, final String retentionPolicy,
final ConsistencyLevel consistency, final TimeUnit precision, final String records);

/**
* Write a set of Points to the influxdb database with the list of string records.
*
Expand All @@ -313,6 +332,25 @@ public void write(final String database, final String retentionPolicy,
public void write(final String database, final String retentionPolicy,
final ConsistencyLevel consistency, final List<String> records);

/**
* Write a set of Points to the influxdb database with the list of string records.
*
* @see <a href="https://github.com/influxdb/influxdb/pull/2696">2696</a>
*
* @param database
* the name of the database to write
* @param retentionPolicy
* the retentionPolicy to use
* @param consistency
* the ConsistencyLevel to use
* @param precision
* the time precision to use
* @param records
* the List of points in the correct lineprotocol.
*/
public void write(final String database, final String retentionPolicy,
final ConsistencyLevel consistency, final TimeUnit precision, final List<String> records);

/**
* Write a set of Points to the influxdb database with the string records through UDP.
*
Expand Down
41 changes: 38 additions & 3 deletions src/main/java/org/influxdb/dto/BatchPoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.TreeMap;

import org.influxdb.InfluxDB.ConsistencyLevel;
Expand All @@ -25,6 +26,7 @@ public class BatchPoints {
private Map<String, String> tags;
private List<Point> points;
private ConsistencyLevel consistency;
private TimeUnit precision;

BatchPoints() {
// Only visible in the Builder
Expand All @@ -50,6 +52,7 @@ public static final class Builder {
private final Map<String, String> tags = new TreeMap<>();
private final List<Point> points = new ArrayList<>();
private ConsistencyLevel consistency;
private TimeUnit precision;

/**
* @param database
Expand Down Expand Up @@ -116,6 +119,16 @@ public Builder consistency(final ConsistencyLevel consistencyLevel) {
return this;
}

/**
* Set the time precision to use for the whole batch. If unspecified, will default to {@link TimeUnit#NANOSECONDS}
* @param precision
* @return the Builder instance
*/
public Builder precision(final TimeUnit precision) {
this.precision = precision;
return this;
}

/**
* Create a new BatchPoints instance.
*
Expand All @@ -135,6 +148,10 @@ public BatchPoints build() {
this.consistency = ConsistencyLevel.ONE;
}
batchPoints.setConsistency(this.consistency);
if (null == this.precision) {
this.precision = TimeUnit.NANOSECONDS;
}
batchPoints.setPrecision(this.precision);
return batchPoints;
}
}
Expand Down Expand Up @@ -184,6 +201,20 @@ void setPoints(final List<Point> points) {
this.points = points;
}

/**
* @return the time precision unit
*/
public TimeUnit getPrecision() {
return precision;
}

/**
* @param precision the time precision to set for the batch points
*/
public void setPrecision(final TimeUnit precision) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this setter public, we have a builder which is responsible to do the object creation ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, seems this long-standing PR has not attracted its author anymore. Let me see if I can help something

this.precision = precision;
}

/**
* Add a single Point to these batches.
*
Expand Down Expand Up @@ -239,12 +270,13 @@ public boolean equals(final Object o) {
&& Objects.equals(retentionPolicy, that.retentionPolicy)
&& Objects.equals(tags, that.tags)
&& Objects.equals(points, that.points)
&& consistency == that.consistency;
&& consistency == that.consistency
&& precision == that.precision;
}

@Override
public int hashCode() {
return Objects.hash(database, retentionPolicy, tags, points, consistency);
return Objects.hash(database, retentionPolicy, tags, points, consistency, precision);
}

/**
Expand All @@ -261,6 +293,8 @@ public String toString() {
.append(this.consistency)
.append(", tags=")
.append(this.tags)
.append(", precision=")
.append(this.precision)
.append(", points=")
.append(this.points)
.append("]");
Expand All @@ -275,8 +309,9 @@ public String toString() {
*/
public String lineProtocol() {
StringBuilder sb = new StringBuilder();

for (Point point : this.points) {
sb.append(point.lineProtocol()).append("\n");
sb.append(point.lineProtocol(this.precision)).append("\n");
}
return sb.toString();
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,23 @@ public String lineProtocol() {
return sb.toString();
}

/**
* Calculate the lineprotocol entry for a single point, using a specific {@link TimeUnit} for the timestamp.
* @param precision the time precision unit for this point
* @return the String without newLine
*/
public String lineProtocol(final TimeUnit precision) {
final StringBuilder sb = CACHED_STRINGBUILDERS
.get()
.computeIfAbsent(this.measurement, MeasurementStringBuilder::new)
.resetForUse();

concatenatedTags(sb);
concatenatedFields(sb);
formatedTime(sb, precision);
return sb.toString();
}

private void concatenatedTags(final StringBuilder sb) {
for (Entry<String, String> tag : this.tags.entrySet()) {
sb.append(',');
Expand Down Expand Up @@ -405,6 +422,14 @@ private void formatedTime(final StringBuilder sb) {
sb.append(' ').append(TimeUnit.NANOSECONDS.convert(this.time, this.precision));
}

private StringBuilder formatedTime(final StringBuilder sb, final TimeUnit precision) {
if (this.time == null || this.precision == null) {
return sb;
}
sb.append(" ").append(precision.convert(this.time, this.precision));
return sb;
}

private static class MeasurementStringBuilder {
private final StringBuilder sb = new StringBuilder(128);
private final int length;
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,30 +358,45 @@ public void write(final BatchPoints batchPoints) {
this.password,
batchPoints.getDatabase(),
batchPoints.getRetentionPolicy(),
TimeUtil.toTimePrecision(TimeUnit.NANOSECONDS),
TimeUtil.toTimePrecision(batchPoints.getPrecision()),
batchPoints.getConsistency().value(),
lineProtocol));
}


@Override
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency,
final String records) {
final TimeUnit precision, final String records) {
execute(this.influxDBService.writePoints(
this.username,
this.password,
database,
retentionPolicy,
TimeUtil.toTimePrecision(TimeUnit.NANOSECONDS),
TimeUtil.toTimePrecision(precision),
consistency.value(),
RequestBody.create(MEDIA_TYPE_STRING, records)));
}

@Override
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency,
final String records) {
write(database, retentionPolicy, consistency, TimeUnit.NANOSECONDS, records);
}

@Override
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency,
final List<String> records) {
write(database, retentionPolicy, consistency, String.join("\n", records));
write(database, retentionPolicy, consistency, TimeUnit.NANOSECONDS, records);
}


@Override
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency,
final TimeUnit precision, final List<String> records) {
write(database, retentionPolicy, consistency, precision, String.join("\n", records));
}


/**
* {@inheritDoc}
*/
Expand Down
Loading