-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathBatchPoints.java
More file actions
368 lines (332 loc) · 9.1 KB
/
BatchPoints.java
File metadata and controls
368 lines (332 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package org.influxdb.dto;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import org.influxdb.InfluxDB.ConsistencyLevel;
import org.influxdb.dto.utils.CheckTags;
/**
* {Purpose of This Type}.
*
* {Other Notes Relating to This Type (Optional)}
*
* @author stefan
*
*/
public class BatchPoints {
private String database;
private String retentionPolicy;
private Map<String, String> tags;
private List<Point> points;
private ConsistencyLevel consistency;
private TimeUnit precision;
BatchPoints() {
// Only visible in the Builder
}
/**
* Create a new BatchPoints build to create a new BatchPoints in a fluent manner.
*
* @return the Builder to be able to add further Builder calls.
*/
public static Builder builder() {
return new Builder(null);
}
/**
* 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<String, String> tags = new TreeMap<>();
private final List<Point> points = new ArrayList<>();
private ConsistencyLevel consistency;
private TimeUnit precision;
/**
* @param database
*/
Builder(final String database) {
this.database = database;
}
/**
* The retentionPolicy to use.
*
* @param policy the retentionPolicy to use
* @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) {
Objects.requireNonNull(tagName, "tagName");
Objects.requireNonNull(value, "value");
if (CheckTags.isLegalFullCheck(tagName, value)) {
this.tags.put(tagName, value);
}
return this;
}
/**
* Add a Point to this set of points.
*
* @param pointToAdd the Point to add
* @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 the Points to add
* @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 the Points to add
* @return the Builder instance
*/
public Builder points(final Collection<Point> pointsToAdd) {
this.points.addAll(pointsToAdd);
return this;
}
/**
* Set the ConsistencyLevel to use. If not given it defaults to {@link ConsistencyLevel#ONE}
*
* @param consistencyLevel the ConsistencyLevel
* @return the Builder instance
*/
public Builder consistency(final ConsistencyLevel consistencyLevel) {
this.consistency = consistencyLevel;
return this;
}
/**
* Set the time precision to use for the whole batch. If unspecified, will default to {@link TimeUnit#NANOSECONDS}
* @param precision the precision of the points
* @return the Builder instance
*/
public Builder precision(final TimeUnit precision) {
this.precision = precision;
return this;
}
/**
* Create a new BatchPoints instance.
*
* @return the created BatchPoints.
*/
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;
}
}
/**
* @return the database
*/
public String getDatabase() {
return this.database;
}
/**
* @param database
* the database to set
*/
void setDatabase(final String database) {
this.database = database;
}
/**
* @return the retentionPolicy
*/
public String getRetentionPolicy() {
return this.retentionPolicy;
}
/**
* @param retentionPolicy
* the retentionPolicy to set
*/
void setRetentionPolicy(final String retentionPolicy) {
this.retentionPolicy = retentionPolicy;
}
/**
* @return the points
*/
public List<Point> getPoints() {
return this.points;
}
/**
* @param points
* the points to set
*/
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
*/
void setPrecision(final TimeUnit precision) {
this.precision = precision;
}
/**
* Add a single Point to these batches.
*
* @param point the Point to add
* @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<String, String> getTags() {
return this.tags;
}
/**
* @param tags
* the tags to set
*/
void setTags(final Map<String, String> tags) {
this.tags = tags;
}
/**
* @return the consistency
*/
public ConsistencyLevel getConsistency() {
return this.consistency;
}
/**
* @param consistency
* the consistency to set
*/
void setConsistency(final ConsistencyLevel consistency) {
this.consistency = consistency;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BatchPoints that = (BatchPoints) o;
return Objects.equals(database, that.database)
&& Objects.equals(retentionPolicy, that.retentionPolicy)
&& Objects.equals(tags, that.tags)
&& Objects.equals(points, that.points)
&& consistency == that.consistency
&& precision == that.precision;
}
@Override
public int hashCode() {
return Objects.hash(database, retentionPolicy, tags, points, consistency, precision);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("BatchPoints [database=")
.append(this.database)
.append(", retentionPolicy=")
.append(this.retentionPolicy)
.append(", consistency=")
.append(this.consistency)
.append(", tags=")
.append(this.tags)
.append(", precision=")
.append(this.precision)
.append(", points=")
.append(this.points)
.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(this.precision)).append("\n");
}
return sb.toString();
}
/**
* Test whether is possible to merge two BatchPoints objects.
*
* @param that batch point to merge in
* @return true if the batch points can be sent in a single HTTP request write
*/
public boolean isMergeAbleWith(final BatchPoints that) {
return Objects.equals(database, that.database)
&& Objects.equals(retentionPolicy, that.retentionPolicy)
&& Objects.equals(tags, that.tags)
&& consistency == that.consistency;
}
/**
* Merge two BatchPoints objects.
*
* @param that batch point to merge in
* @return true if the batch points have been merged into this BatchPoints instance. Return false otherwise.
*/
public boolean mergeIn(final BatchPoints that) {
boolean mergeAble = isMergeAbleWith(that);
if (mergeAble) {
this.points.addAll(that.points);
}
return mergeAble;
}
}