forked from openGemini/opengemini-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: line protocol bug (openGemini#41)
Signed-off-by: weiping-code <[email protected]>
- Loading branch information
1 parent
5f984be
commit da1b121
Showing
4 changed files
with
243 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 15 additions & 11 deletions
26
opengemini-client-api/src/main/java/io/opengemini/client/api/Precision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
package io.opengemini.client.api; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Getter | ||
public enum Precision { | ||
PRECISIONNANOSECOND("PrecisionNanoSecond"), | ||
PRECISIONNANOSECOND("PrecisionNanoSecond", TimeUnit.NANOSECONDS), | ||
|
||
PRECISIONMICROSECOND("PrecisionMicrosecond"), | ||
PRECISIONMICROSECOND("PrecisionMicrosecond", TimeUnit.MICROSECONDS), | ||
|
||
PRECISIONMILLISECOND("PrecisionMillisecond"), | ||
PRECISIONMILLISECOND("PrecisionMillisecond", TimeUnit.MILLISECONDS), | ||
|
||
PRECISIONSECOND("PrecisionSecond"), | ||
PRECISIONSECOND("PrecisionSecond", TimeUnit.SECONDS), | ||
|
||
PRECISIONMINUTE("PrecisionMinute"), | ||
PRECISIONMINUTE("PrecisionMinute", TimeUnit.MINUTES), | ||
|
||
PRECISIONHOUR("PrecisionHour"); | ||
PRECISIONHOUR("PrecisionHour", TimeUnit.HOURS); | ||
|
||
private final String description; | ||
|
||
Precision(String description) { | ||
this.description = description; | ||
} | ||
private final TimeUnit timeUnit; | ||
|
||
public String getDescription() { | ||
return description; | ||
Precision(String description, TimeUnit timeUnit) { | ||
this.description = description; | ||
this.timeUnit = timeUnit; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
opengemini-client-api/src/test/java/io/opengemini/client/api/PointTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package io.opengemini.client.api; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
|
||
class PointTest { | ||
|
||
@Test | ||
void lineProtocol_without_escaped_chars() { | ||
Assertions.assertEquals("test,T0=0 a=1i 1", testPoint("test", "T0", "0", "a", 1).lineProtocol()); | ||
} | ||
|
||
@Test | ||
void lineProtocol_measurement_with_escaped_chars() { | ||
Assertions.assertEquals("test\\,,T0=0 a=1i 1", testPoint("test,", "T0", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test\\ ,T0=0 a=1i 1", testPoint("test ", "T0", "0", "a", 1).lineProtocol()); | ||
} | ||
|
||
@Test | ||
void lineProtocol_tag_key_with_escaped_chars() { | ||
Assertions.assertEquals("test,T0\\,=0 a=1i 1", testPoint("test", "T0,", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0\\==0 a=1i 1", testPoint("test", "T0=", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0\\ =0 a=1i 1", testPoint("test", "T0 ", "0", "a", 1).lineProtocol()); | ||
} | ||
|
||
@Test | ||
void lineProtocol_tag_value_with_escaped_chars() { | ||
Assertions.assertEquals("test,T0=0\\, a=1i 1", testPoint("test", "T0", "0,", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0\\= a=1i 1", testPoint("test", "T0", "0=", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0\\ a=1i 1", testPoint("test", "T0", "0 ", "a", 1).lineProtocol()); | ||
} | ||
|
||
@Test | ||
void lineProtocol_field_key_with_escaped_chars() { | ||
Assertions.assertEquals("test,T0=0 a\\,=1i 1", testPoint("test", "T0", "0", "a,", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a\\==1i 1", testPoint("test", "T0", "0", "a=", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a\\ =1i 1", testPoint("test", "T0", "0", "a ", 1).lineProtocol()); | ||
} | ||
|
||
@Test | ||
void lineProtocol_field_value_with_escaped_chars() { | ||
Assertions.assertEquals("test,T0=0 a=\"1\\\"\" 1", testPoint("test", "T0", "0", "a", "1\"").lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=\"1\\\\\" 1", testPoint("test", "T0", "0", "a", "1\\").lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=\"1\\\\\\\\\" 1", | ||
testPoint("test", "T0", "0", "a", "1\\\\").lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=\"1\\\\\\\\\\\\\" 1", | ||
testPoint("test", "T0", "0", "a", "1\\\\\\").lineProtocol()); | ||
} | ||
|
||
@Test | ||
void lineProtocol_field_value_with_different_types() { | ||
Assertions.assertEquals("test,T0=0 a=1.0 1", testPoint("test", "T0", "0", "a", 1D).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1.1 1", testPoint("test", "T0", "0", "a", 1.1D).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1.0 1", testPoint("test", "T0", "0", "a", 1F).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1.1 1", testPoint("test", "T0", "0", "a", 1.1F).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1.0 1", | ||
testPoint("test", "T0", "0", "a", BigDecimal.valueOf(1D)).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1.1 1", | ||
testPoint("test", "T0", "0", "a", BigDecimal.valueOf(1.1D)).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 1", testPoint("test", "T0", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 1", testPoint("test", "T0", "0", "a", 1L).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 1", testPoint("test", "T0", "0", "a", (short) 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 1", testPoint("test", "T0", "0", "a", (byte) 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=true 1", testPoint("test", "T0", "0", "a", true).lineProtocol()); | ||
} | ||
|
||
@Test | ||
void lineProtocol_timestamp_convert_precision() { | ||
Assertions.assertEquals("test,T0=0 a=1i 1", | ||
testPoint("test", Precision.PRECISIONNANOSECOND, "T0", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 1000", | ||
testPoint("test", Precision.PRECISIONMICROSECOND, "T0", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 1000000", | ||
testPoint("test", Precision.PRECISIONMILLISECOND, "T0", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 1000000000", | ||
testPoint("test", Precision.PRECISIONSECOND, "T0", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 60000000000", | ||
testPoint("test", Precision.PRECISIONMINUTE, "T0", "0", "a", 1).lineProtocol()); | ||
Assertions.assertEquals("test,T0=0 a=1i 3600000000000", | ||
testPoint("test", Precision.PRECISIONHOUR, "T0", "0", "a", 1).lineProtocol()); | ||
} | ||
|
||
@Test | ||
void lineProtocol_with_multi_tags_and_fields() { | ||
Point point = new Point(); | ||
point.setMeasurement("test"); | ||
point.setPrecision(Precision.PRECISIONNANOSECOND); | ||
point.setTime(1); | ||
|
||
Map<String, String> tags = new LinkedHashMap<>(); | ||
tags.put("T0", "0"); | ||
tags.put("T1", "1"); | ||
point.setTags(tags); | ||
|
||
Map<String, Object> fields = new LinkedHashMap<>(); | ||
fields.put("a", 1); | ||
fields.put("b", 2); | ||
point.setFields(fields); | ||
|
||
Assertions.assertEquals("test,T0=0,T1=1 a=1i,b=2i 1", point.lineProtocol()); | ||
} | ||
|
||
private static Point testPoint(String measurement, String tagKey, String tagValue, String fieldKey, | ||
Object fieldValue) { | ||
return testPoint(measurement, Precision.PRECISIONNANOSECOND, tagKey, tagValue, fieldKey, fieldValue); | ||
} | ||
|
||
private static Point testPoint(String measurement, Precision precision, String tagKey, String tagValue, | ||
String fieldKey, Object fieldValue) { | ||
Point point = new Point(); | ||
point.setMeasurement(measurement); | ||
point.setPrecision(precision); | ||
point.setTime(1); | ||
point.setTags(Collections.singletonMap(tagKey, tagValue)); | ||
point.setFields(Collections.singletonMap(fieldKey, fieldValue)); | ||
return point; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters