diff --git a/src/main/java/org/influxdb/dto/Point.java b/src/main/java/org/influxdb/dto/Point.java index 8c0f91b79..acac4a82d 100644 --- a/src/main/java/org/influxdb/dto/Point.java +++ b/src/main/java/org/influxdb/dto/Point.java @@ -10,7 +10,6 @@ import java.util.Objects; import java.util.TreeMap; import java.util.concurrent.TimeUnit; -import java.util.function.Function; import org.influxdb.impl.Preconditions; @@ -26,11 +25,6 @@ public class Point { private Long time; private TimeUnit precision = TimeUnit.NANOSECONDS; private Map fields; - - private static final Function FIELD_ESCAPER = s -> - s.replace("\\", "\\\\").replace("\"", "\\\""); - private static final Function KEY_ESCAPER = s -> - s.replace(" ", "\\ ").replace(",", "\\,").replace("=", "\\="); private static final int MAX_FRACTION_DIGITS = 340; private static final ThreadLocal NUMBER_FORMATTER = ThreadLocal.withInitial(() -> { @@ -329,10 +323,10 @@ public String lineProtocol() { private void concatenatedTags(final StringBuilder sb) { for (Entry tag : this.tags.entrySet()) { - sb.append(',') - .append(KEY_ESCAPER.apply(tag.getKey())) - .append('=') - .append(KEY_ESCAPER.apply(tag.getValue())); + sb.append(','); + escapeKey(sb, tag.getKey()); + sb.append('='); + escapeKey(sb, tag.getValue()); } sb.append(' '); } @@ -343,8 +337,8 @@ private void concatenatedFields(final StringBuilder sb) { if (value == null) { continue; } - - sb.append(KEY_ESCAPER.apply(field.getKey())).append('='); + escapeKey(sb, field.getKey()); + sb.append('='); if (value instanceof Number) { if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) { sb.append(NUMBER_FORMATTER.get().format(value)); @@ -353,7 +347,9 @@ private void concatenatedFields(final StringBuilder sb) { } } else if (value instanceof String) { String stringValue = (String) value; - sb.append('"').append(FIELD_ESCAPER.apply(stringValue)).append('"'); + sb.append('"'); + escapeField(sb, stringValue); + sb.append('"'); } else { sb.append(value); } @@ -368,6 +364,31 @@ private void concatenatedFields(final StringBuilder sb) { } } + static void escapeKey(final StringBuilder sb, final String key) { + for (int i = 0; i < key.length(); i++) { + switch (key.charAt(i)) { + case ' ': + case ',': + case '=': + sb.append('\\'); + default: + sb.append(key.charAt(i)); + } + } + } + + static void escapeField(final StringBuilder sb, final String field) { + for (int i = 0; i < field.length(); i++) { + switch (field.charAt(i)) { + case '\\': + case '\"': + sb.append('\\'); + default: + sb.append(field.charAt(i)); + } + } + } + private void formatedTime(final StringBuilder sb) { if (this.time == null || this.precision == null) { return; @@ -380,7 +401,7 @@ private static class MeasurementStringBuilder { private final int length; MeasurementStringBuilder(final String measurement) { - this.sb.append(KEY_ESCAPER.apply(measurement)); + escapeKey(this.sb, measurement); this.length = sb.length(); }