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 8 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
20 changes: 20 additions & 0 deletions src/main/java/org/influxdb/InfluxDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ 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.
*
* {@linkplain "https://github.com/influxdb/influxdb/pull/2696"}
*
* @param records
*/
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 @@ -207,6 +217,16 @@ 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.
*
* {@linkplain "https://github.com/influxdb/influxdb/pull/2696"}
*
* @param records
*/
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 @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.influxdb.InfluxDB.ConsistencyLevel;

Expand All @@ -27,6 +28,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 @@ -52,6 +54,7 @@ public static final class Builder {
private final Map<String, String> tags = Maps.newTreeMap(Ordering.natural());
private final List<Point> points = Lists.newArrayList();
private ConsistencyLevel consistency;
private TimeUnit precision;

/**
* @param database
Expand Down Expand Up @@ -118,6 +121,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 @@ -138,6 +151,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 @@ -187,6 +204,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 @@ -242,12 +273,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 @@ -264,6 +296,8 @@ public String toString() {
builder.append(this.consistency);
builder.append(", tags=");
builder.append(this.tags);
builder.append(", precision=");
builder.append(this.precision);
builder.append(", points=");
builder.append(this.points);
builder.append("]");
Expand All @@ -278,8 +312,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
20 changes: 20 additions & 0 deletions src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,20 @@ 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 = new StringBuilder();
sb.append(KEY_ESCAPER.escape(this.measurement));
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to synchronize this with #322
@simon04 can you keep track of this change in you PR, depending on what we merge first.

sb.append(concatenatedTags());
sb.append(concatenateFields());
sb.append(formatedTime(precision));
return sb.toString();
}

private StringBuilder concatenatedTags() {
final StringBuilder sb = new StringBuilder();
for (Entry<String, String> tag : this.tags.entrySet()) {
Expand Down Expand Up @@ -385,4 +399,10 @@ private StringBuilder formatedTime() {
return sb;
}

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

}
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 @@ -269,31 +269,46 @@ 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, TimeUnit.NANOSECONDS, records);
}


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


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