From 17b2f248a5c153772e589a3d3346998f4146d65d Mon Sep 17 00:00:00 2001 From: Michael Schaefers Date: Fri, 9 Mar 2018 15:26:45 +0100 Subject: [PATCH 1/3] Performance: Escape fields and keys more efficiently than using String.replace twice per value. --- src/main/java/org/influxdb/dto/Point.java | 55 ++++++++++++++++------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/influxdb/dto/Point.java b/src/main/java/org/influxdb/dto/Point.java index 8c0f91b79..ad33c4b72 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(() -> { @@ -182,7 +176,7 @@ public Builder fields(final Map fieldsToAdd) { /** * Add a time to this point. * - * @param timeToSet the time for this point + * @param timeToSet the time for this point * @param precisionToSet the TimeUnit * @return the Builder instance. */ @@ -205,8 +199,8 @@ public Point build() { point.setFields(this.fields); point.setMeasurement(this.measurement); if (this.time != null) { - point.setTime(this.time); - point.setPrecision(this.precision); + point.setTime(this.time); + point.setPrecision(this.precision); } point.setTags(this.tags); return point; @@ -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(StringBuilder sb, 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(StringBuilder sb, 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(); } From e2169b3f03cda481975cb7fb872988c4354e912e Mon Sep 17 00:00:00 2001 From: Michael Schaefers Date: Fri, 9 Mar 2018 15:36:21 +0100 Subject: [PATCH 2/3] fix travis errors --- src/main/java/org/influxdb/dto/Point.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/influxdb/dto/Point.java b/src/main/java/org/influxdb/dto/Point.java index ad33c4b72..35f7f3a1c 100644 --- a/src/main/java/org/influxdb/dto/Point.java +++ b/src/main/java/org/influxdb/dto/Point.java @@ -364,7 +364,7 @@ private void concatenatedFields(final StringBuilder sb) { } } - static void escapeKey(StringBuilder sb, String key) { + static void escapeKey(final StringBuilder sb, final String key) { for (int i = 0; i < key.length(); i++) { switch (key.charAt(i)) { case ' ': @@ -377,7 +377,7 @@ static void escapeKey(StringBuilder sb, String key) { } } - static void escapeField(StringBuilder sb, String field) { + static void escapeField(final StringBuilder sb, final String field) { for (int i = 0; i < field.length(); i++) { switch (field.charAt(i)) { case '\\': From 458d4ea6156172298896cb076923da9204999160 Mon Sep 17 00:00:00 2001 From: Michael Schaefers Date: Sat, 10 Mar 2018 15:00:24 +0100 Subject: [PATCH 3/3] revert wrong formatting as requested --- src/main/java/org/influxdb/dto/Point.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/influxdb/dto/Point.java b/src/main/java/org/influxdb/dto/Point.java index 35f7f3a1c..acac4a82d 100644 --- a/src/main/java/org/influxdb/dto/Point.java +++ b/src/main/java/org/influxdb/dto/Point.java @@ -176,7 +176,7 @@ public Builder fields(final Map fieldsToAdd) { /** * Add a time to this point. * - * @param timeToSet the time for this point + * @param timeToSet the time for this point * @param precisionToSet the TimeUnit * @return the Builder instance. */ @@ -199,8 +199,8 @@ public Point build() { point.setFields(this.fields); point.setMeasurement(this.measurement); if (this.time != null) { - point.setTime(this.time); - point.setPrecision(this.precision); + point.setTime(this.time); + point.setPrecision(this.precision); } point.setTags(this.tags); return point;