Skip to content

Commit

Permalink
Optimize Point.lineProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
bentatham committed Jun 12, 2017
1 parent df18f64 commit 8dbdc19
Showing 1 changed file with 16 additions and 24 deletions.
40 changes: 16 additions & 24 deletions src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public class Point {
.build();
private static final int MAX_FRACTION_DIGITS = 340;

private static final ThreadLocal<NumberFormat> FIELD_VALUE_FORMAT = ThreadLocal.withInitial(() -> {
NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
numberFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
numberFormat.setGroupingUsed(false);
numberFormat.setMinimumFractionDigits(1);
return numberFormat;
});

Point() {
}

Expand Down Expand Up @@ -322,34 +330,23 @@ public String toString() {
public String lineProtocol() {
final StringBuilder sb = new StringBuilder();
sb.append(KEY_ESCAPER.escape(this.measurement));
sb.append(concatenatedTags());
sb.append(concatenateFields());
sb.append(formatedTime());
appendTags(sb);
appendFields(sb);
appendFormatedTime(sb);
return sb.toString();
}

private StringBuilder concatenatedTags() {
final StringBuilder sb = new StringBuilder();
private void appendTags(StringBuilder sb) {
for (Entry<String, String> tag : this.tags.entrySet()) {
sb.append(",")
.append(KEY_ESCAPER.escape(tag.getKey()))
.append("=")
.append(KEY_ESCAPER.escape(tag.getValue()));
sb.append(",").append(KEY_ESCAPER.escape(tag.getKey())).append("=").append(KEY_ESCAPER.escape(tag.getValue()));
}
sb.append(" ");
return sb;
}

private StringBuilder concatenateFields() {
final StringBuilder sb = new StringBuilder();
private void appendFields(StringBuilder sb) {
final int fieldCount = this.fields.size();
int loops = 0;

NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
numberFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
numberFormat.setGroupingUsed(false);
numberFormat.setMinimumFractionDigits(1);

for (Entry<String, Object> field : this.fields.entrySet()) {
loops++;
Object value = field.getValue();
Expand All @@ -363,7 +360,7 @@ private StringBuilder concatenateFields() {
sb.append("\"").append(FIELD_ESCAPER.escape(stringValue)).append("\"");
} else if (value instanceof Number) {
if (value instanceof Double || value instanceof Float || value instanceof BigDecimal) {
sb.append(numberFormat.format(value));
sb.append(FIELD_VALUE_FORMAT.get().format(value));
} else {
sb.append(value).append("i");
}
Expand All @@ -375,14 +372,9 @@ private StringBuilder concatenateFields() {
sb.append(",");
}
}

return sb;
}

private StringBuilder formatedTime() {
final StringBuilder sb = new StringBuilder();
private void appendFormatedTime(StringBuilder sb) {
sb.append(" ").append(TimeUnit.NANOSECONDS.convert(this.time, this.precision));
return sb;
}

}

0 comments on commit 8dbdc19

Please sign in to comment.