Skip to content

BatchPoints.Builder not reusable #952

@jpalus

Description

@jpalus

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions