Skip to content

Commit

Permalink
Merge pull request #424 from mschaefers/master
Browse files Browse the repository at this point in the history
Performance: Escape fields and keys more efficiently
  • Loading branch information
majst01 authored Mar 10, 2018
2 parents cc2ffe7 + 458d4ea commit 90f02ea
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -26,11 +25,6 @@ public class Point {
private Long time;
private TimeUnit precision = TimeUnit.NANOSECONDS;
private Map<String, Object> fields;

private static final Function<String, String> FIELD_ESCAPER = s ->
s.replace("\\", "\\\\").replace("\"", "\\\"");
private static final Function<String, String> KEY_ESCAPER = s ->
s.replace(" ", "\\ ").replace(",", "\\,").replace("=", "\\=");
private static final int MAX_FRACTION_DIGITS = 340;
private static final ThreadLocal<NumberFormat> NUMBER_FORMATTER =
ThreadLocal.withInitial(() -> {
Expand Down Expand Up @@ -329,10 +323,10 @@ public String lineProtocol() {

private void concatenatedTags(final StringBuilder sb) {
for (Entry<String, String> 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(' ');
}
Expand All @@ -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));
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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();
}

Expand Down

0 comments on commit 90f02ea

Please sign in to comment.