Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: Escape fields and keys more efficiently #424

Merged
merged 3 commits into from
Mar 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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