-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPoint.java
More file actions
771 lines (671 loc) · 19.2 KB
/
Point.java
File metadata and controls
771 lines (671 loc) · 19.2 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.v3.client;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.NumberFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
import com.influxdb.v3.client.internal.Arguments;
import com.influxdb.v3.client.write.WriteOptions;
import com.influxdb.v3.client.write.WritePrecision;
/**
* Point defines the values that will be written to the database.
* <a href="https://bit.ly/influxdb3-java-point">See Go Implementation</a>.
*
* @author Jakub Bednar (bednar@github) (11/10/2018 11:40)
*/
@NotThreadSafe
public final class Point {
private static final int MAX_FRACTION_DIGITS = 340;
private static final ThreadLocal<NumberFormat> NUMBER_FORMATTER =
ThreadLocal.withInitial(() -> {
NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
numberFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
numberFormat.setGroupingUsed(false);
numberFormat.setMinimumFractionDigits(1);
return numberFormat;
});
private final PointValues values;
private Point(final PointValues values) {
this.values = values;
}
/**
* Create a new Point.
*
* @param measurementName the measurement name
*/
public Point(@Nonnull final String measurementName) {
Arguments.checkNotNull(measurementName, "measurement");
values = new PointValues();
values.setMeasurement(measurementName);
}
/**
* Create a new Point withe specified a measurement name.
*
* @param measurementName the measurement name
* @return new instance of {@link Point}
*/
@Nonnull
public static Point measurement(@Nonnull final String measurementName) {
Arguments.checkNotNull(measurementName, "measurement");
return new Point(new PointValues()).setMeasurement(measurementName);
}
/**
* Create a new Point with given values.
*
* @param values the point values
* @return the new Point
* @throws Exception if measurement is missing
*/
public static Point fromValues(final PointValues values) throws Exception {
if (values.getMeasurement() == null) {
throw new Exception("Missing measurement!");
}
return new Point(values);
}
/**
* Get measurement name.
* It will return null when querying with SQL Query.
*
* @return Measurement name
*/
@Nonnull
public String getMeasurement() {
assert values.getMeasurement() != null;
return values.getMeasurement();
}
/**
* Updates the measurement for the point.
*
* @param measurement the measurement
* @return this
*/
@Nonnull
public Point setMeasurement(@Nonnull final String measurement) {
Arguments.checkNotNull(measurement, "precision");
values.setMeasurement(measurement);
return this;
}
/**
* Get timestamp in nanoseconds. If the timestamp is not set, returns null.
*
* @return timestamp or null
*/
@Nullable
public Number getTimestamp() {
return values.getTimestamp();
}
/**
* Updates the timestamp for the point.
*
* @param time the timestamp
* @return this
*/
@Nonnull
public Point setTimestamp(@Nullable final Instant time) {
values.setTimestamp(time);
return this;
}
/**
* Updates the timestamp for the point.
*
* @param time the timestamp
* @param precision the timestamp precision
* @return this
*/
@Nonnull
public Point setTimestamp(@Nullable final Number time, @Nonnull final WritePrecision precision) {
Arguments.checkNotNull(precision, "precision");
values.setTimestamp(time, precision);
return this;
}
/**
* Updates the timestamp for the point.
*
* @param time the timestamp
* @param precision the timestamp precision
* @return this
*/
@Nonnull
public Point setTimestamp(@Nullable final Long time, @Nonnull final WritePrecision precision) {
return setTimestamp((Number) time, precision);
}
/**
* Gets value of tag with given name. Returns null if tag not found.
*
* @param name the tag name
* @return tag value or null
*/
@Nullable
public String getTag(@Nonnull final String name) {
Arguments.checkNotNull(name, "tagName");
return values.getTag(name);
}
/**
* Adds or replaces a tag value for this point.
*
* @param key the tag name
* @param value the tag value
* @return this
*/
@Nonnull
public Point setTag(@Nonnull final String key, @Nullable final String value) {
Arguments.checkNotNull(key, "tagName");
values.setTag(key, value);
return this;
}
/**
* Adds or replaces tags for this point.
*
* @param tagsToAdd the Map of tags to add
* @return this
*/
@Nonnull
public Point setTags(@Nonnull final Map<String, String> tagsToAdd) {
Arguments.checkNotNull(tagsToAdd, "tagsToAdd");
values.setTags(tagsToAdd);
return this;
}
/**
* Removes a tag with the specified name if it exists; otherwise, it does nothing.
*
* @param name the tag name
* @return this
*/
@Nonnull
public Point removeTag(@Nonnull final String name) {
Arguments.checkNotNull(name, "tagName");
values.removeTag(name);
return this;
}
/**
* Gets an array of tag names.
*
* @return An array of tag names
*/
@Nonnull
public String[] getTagNames() {
return values.getTagNames();
}
/**
* Gets the float field value associated with the specified name.
* If the field is not present, returns null.
*
* @param name the field name
* @return The float field value or null
*/
@Nullable
public Double getFloatField(@Nonnull final String name) throws ClassCastException {
return getField(name, Double.class);
}
/**
* Adds or replaces a float field.
*
* @param name the field name
* @param value the field value
* @return this
*/
@Nonnull
public Point setFloatField(@Nonnull final String name, final double value) {
return putField(name, value);
}
/**
* Gets the integer field value associated with the specified name.
* If the field is not present, returns null.
*
* @param name the field name
* @return The integer field value or null
*/
@Nullable
public Long getIntegerField(@Nonnull final String name) throws ClassCastException {
return getField(name, Long.class);
}
/**
* Adds or replaces a integer field.
*
* @param name the field name
* @param value the field value
* @return this
*/
public Point setIntegerField(@Nonnull final String name, final long value) {
return putField(name, value);
}
/**
* Gets the string field value associated with the specified name.
* If the field is not present, returns null.
*
* @param name the field name
* @return The string field value or null
*/
@Nullable
public String getStringField(@Nonnull final String name) throws ClassCastException {
return getField(name, String.class);
}
/**
* Adds or replaces a string field.
*
* @param name the field name
* @param value the field value
* @return this
*/
public Point setStringField(@Nonnull final String name, final String value) {
return putField(name, value);
}
/**
* Gets the boolean field value associated with the specified name.
* If the field is not present, returns null.
*
* @param name the field name
* @return The boolean field value or null
*/
@Nullable
public Boolean getBooleanField(@Nonnull final String name) throws ClassCastException {
return getField(name, Boolean.class);
}
/**
* Adds or replaces a boolean field.
*
* @param name the field name
* @param value the field value
* @return this
*/
public Point setBooleanField(@Nonnull final String name, final boolean value) {
return putField(name, value);
}
/**
* Get field of given name. Can be null if field doesn't exist.
*
* @param name the field name
* @return Field as object
*/
@Nullable
public Object getField(@Nonnull final String name) {
return values.getField(name);
}
/**
* Get field of given name as type. Can be null if field doesn't exist.
*
* @param name the field name
* @param type the field type Class
* @param <T> the field type
* @return Field as given type
*/
@Nullable
public <T> T getField(final String name, final Class<T> type) throws ClassCastException {
Object field = getField(name);
if (field == null) {
return null;
}
return type.cast(field);
}
/**
* Gets the type of field with given name, if it exists.
* If the field is not present, returns null.
*
* @param name the field name
* @return The field type or null.
*/
@Nullable
public Class<?> getFieldType(@Nonnull final String name) {
Object field = getField(name);
if (field == null) {
return null;
}
return field.getClass();
}
/**
* Add {@link Double} field.
*
* @param field the field name
* @param value the field value
* @return this
*/
@Nonnull
public Point setField(@Nonnull final String field, final double value) {
return putField(field, value);
}
/**
* Add {@link Long} field.
*
* @param field the field name
* @param value the field value
* @return this
*/
public Point setField(@Nonnull final String field, final long value) {
return putField(field, value);
}
/**
* Add {@link Number} field.
*
* @param field the field name
* @param value the field value
* @return this
*/
@Nonnull
public Point setField(@Nonnull final String field, @Nullable final Number value) {
return putField(field, value);
}
/**
* Add {@link String} field.
*
* @param field the field name
* @param value the field value
* @return this
*/
@Nonnull
public Point setField(@Nonnull final String field, @Nullable final String value) {
return putField(field, value);
}
/**
* Add {@link Boolean} field.
*
* @param field the field name
* @param value the field value
* @return this
*/
@Nonnull
public Point setField(@Nonnull final String field, final boolean value) {
return putField(field, value);
}
/**
* Add {@link Object} field.
*
* @param field the field name
* @param value the field value
* @return this
*/
@Nonnull
public Point setField(@Nonnull final String field, @Nullable final Object value) {
return putField(field, value);
}
/**
* Adds or replaces fields for this point.
*
* @param fieldsToAdd the Map of fields to add
* @return this
*/
@Nonnull
public Point setFields(@Nonnull final Map<String, Object> fieldsToAdd) {
Arguments.checkNotNull(fieldsToAdd, "fieldsToAdd");
fieldsToAdd.forEach(this::putField);
return this;
}
/**
* Removes a field with the specified name if it exists; otherwise, it does nothing.
*
* @param name the field name
* @return this
*/
@Nonnull
public Point removeField(@Nonnull final String name) {
values.removeField(name);
return this;
}
/**
* Gets an array of field names.
*
* @return An array of field names
*/
@Nonnull
public String[] getFieldNames() {
return values.getFieldNames();
}
/**
* Has point any fields?
*
* @return true, if the point contains any fields, false otherwise.
*/
public boolean hasFields() {
return values.hasFields();
}
/**
* Creates a copy of this object.
*
* @return A new instance with same values.
*/
@Nonnull
public Point copy() {
return new Point(values.copy());
}
/**
* Transform to Line Protocol with nanosecond precision.
*
* @return Line Protocol
*/
@Nonnull
public String toLineProtocol() {
return toLineProtocol(null, null, null);
}
/**
* Transform to Line Protocol.
*
* @param precision required precision
* @return Line Protocol
*/
@Nonnull
public String toLineProtocol(@Nullable final WritePrecision precision) {
return toLineProtocol(precision, null, null);
}
/**
* Transform to Line Protocol.
*
* @param precision required precision
* @param defaultTags default tags to include in point serialization
* @param tagOrder preferred order of tags in point serialization
* @return Line Protocol
*/
@Nonnull
public String toLineProtocol(@Nullable final WritePrecision precision,
@Nullable final Map<String, String> defaultTags,
@Nullable final List<String> tagOrder) {
StringBuilder sb = new StringBuilder();
escapeKey(sb, getMeasurement(), false);
appendTags(sb, defaultTags, tagOrder);
boolean appendedFields = appendFields(sb);
if (!appendedFields) {
return "";
}
appendTime(sb, precision);
return sb.toString();
}
@Nonnull
private Point putField(@Nonnull final String field, @Nullable final Object value) {
Arguments.checkNonEmpty(field, "fieldName");
values.setField(field, value);
return this;
}
private void appendTags(@Nonnull final StringBuilder sb,
@Nullable final Map<String, String> defaultTags,
@Nullable final List<String> tagOrder) {
Set<String> remaining = new TreeSet<>();
for (String pointTag : values.getTagNames()) {
if (!pointTag.isEmpty()) {
remaining.add(pointTag);
}
}
if (defaultTags != null) {
for (String defaultTag : defaultTags.keySet()) {
if (defaultTag != null && !defaultTag.isEmpty()) {
remaining.add(defaultTag);
}
}
}
List<String> orderedTagNames = new ArrayList<>();
if (tagOrder != null && !tagOrder.isEmpty()) {
Set<String> seen = new HashSet<>();
for (String preferredTag : tagOrder) {
if (preferredTag == null || preferredTag.isEmpty() || !seen.add(preferredTag)) {
continue;
}
if (remaining.remove(preferredTag)) {
orderedTagNames.add(preferredTag);
}
}
}
orderedTagNames.addAll(remaining);
for (String name : orderedTagNames) {
String value = values.getTag(name);
if (defaultTags != null && defaultTags.containsKey(name)) {
value = defaultTags.get(name);
}
if (value == null || value.isEmpty()) {
continue;
}
sb.append(',');
escapeKey(sb, name);
sb.append('=');
escapeKey(sb, value);
}
sb.append(' ');
}
private boolean appendFields(@Nonnull final StringBuilder sb) {
boolean appended = false;
for (String field : values.getFieldNames()) {
Object value = values.getField(field);
if (isNotDefined(value)) {
continue;
}
escapeKey(sb, field);
sb.append('=');
if (value instanceof Number) {
if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) {
sb.append(NUMBER_FORMATTER.get().format(value));
} else {
sb.append(value).append('i');
}
} else if (value instanceof String) {
String stringValue = (String) value;
sb.append('"');
escapeValue(sb, stringValue);
sb.append('"');
} else {
sb.append(value);
}
sb.append(',');
appended = true;
}
// efficiently chop off the trailing comma
int lengthMinusOne = sb.length() - 1;
if (sb.charAt(lengthMinusOne) == ',') {
sb.setLength(lengthMinusOne);
}
return appended;
}
private void appendTime(@Nonnull final StringBuilder sb, @Nullable final WritePrecision precision) {
var time = getTimestamp();
if (time == null) {
return;
}
sb.append(" ");
WritePrecision precisionNotNull = precision != null ? precision : WriteOptions.DEFAULT_WRITE_PRECISION;
if (WritePrecision.NS.equals(precisionNotNull)) {
if (time instanceof BigDecimal) {
sb.append(((BigDecimal) time).toBigInteger());
} else if (time instanceof BigInteger) {
sb.append(time);
} else {
sb.append(time.longValue());
}
} else {
long timeLong;
if (time instanceof BigDecimal) {
timeLong = ((BigDecimal) time).longValueExact();
} else if (time instanceof BigInteger) {
timeLong = ((BigInteger) time).longValueExact();
} else {
timeLong = time.longValue();
}
sb.append(toTimeUnit(precisionNotNull).convert(timeLong, toTimeUnit(WritePrecision.NS)));
}
}
private void escapeKey(@Nonnull final StringBuilder sb, @Nonnull final String key) {
escapeKey(sb, key, true);
}
private void escapeKey(@Nonnull final StringBuilder sb, @Nonnull final String key, final boolean escapeEqual) {
for (int i = 0; i < key.length(); i++) {
switch (key.charAt(i)) {
case '\n':
sb.append("\\n");
continue;
case '\r':
sb.append("\\r");
continue;
case '\t':
sb.append("\\t");
continue;
case ' ':
case ',':
sb.append('\\');
break;
case '=':
if (escapeEqual) {
sb.append('\\');
}
break;
default:
}
sb.append(key.charAt(i));
}
}
private void escapeValue(@Nonnull final StringBuilder sb, @Nonnull final String value) {
for (int i = 0; i < value.length(); i++) {
switch (value.charAt(i)) {
case '\\':
case '\"':
sb.append('\\');
default:
sb.append(value.charAt(i));
}
}
}
private boolean isNotDefined(final Object value) {
return value == null
|| (value instanceof Double && !Double.isFinite((Double) value))
|| (value instanceof Float && !Float.isFinite((Float) value));
}
@Nonnull
private TimeUnit toTimeUnit(@Nonnull final WritePrecision precision) {
switch (precision) {
case MS:
return TimeUnit.MILLISECONDS;
case S:
return TimeUnit.SECONDS;
case US:
return TimeUnit.MICROSECONDS;
case NS:
return TimeUnit.NANOSECONDS;
default:
throw new IllegalStateException("Unexpected value: " + precision);
}
}
}