influxdata / influxdb-java

Java client for InfluxDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`BatchPoints.Builder` not reusable

jpalus opened this issue · comments

There's nothing in BatchPoints.Builder javadoc that would warn against calling build() multiple times:

/**
* The Builder to create a new BatchPoints instance.
*/
public static final class Builder {

/**
* Create a new BatchPoints instance.
*
* @return the created BatchPoints.
*/
public BatchPoints build() {

and nothing in build() method itself that would cause ie exception:

public BatchPoints build() {
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);
if (null == this.precision) {
this.precision = TimeUnit.NANOSECONDS;
}
batchPoints.setPrecision(this.precision);
return batchPoints;
}

but creating multiple BatchPoints instances from BatchPoints.Builder is not actually safe since BatchPoints.Builder does not make defensive copy of this.points:

batchPoints.setPoints(this.points);

so ie this code will fail:

BatchPoints bp1 = builder.build();
int size = bp1.getPoints().size();
bp1.point(point);
BatchPoints bp2 = builder.build();
assertEquals(size, bp2.getPoints().size());

since bp1.point(point) modified collection builder refers to.