Created
September 11, 2014 16:18
-
-
Save vladimir-smirnov-sociomantic/bfdc663198f9f4dcc9d8 to your computer and use it in GitHub Desktop.
Test patch for json marshaling in influxdb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/api/http/api.go b/api/http/api.go | |
index 0c83192..4baeff8 100644 | |
--- a/api/http/api.go | |
+++ b/api/http/api.go | |
@@ -491,7 +491,7 @@ func serializeSingleSeries(series *protocol.Series, precision TimePrecision, pre | |
if pretty { | |
return json.MarshalIndent(SerializeSeries(arg, precision)[0], "", JSON_PRETTY_PRINT_INDENT) | |
} else { | |
- return json.Marshal(SerializeSeries(arg, precision)[0]) | |
+ return SerializeSeriesJson(arg, precision, 1), nil | |
} | |
} | |
@@ -499,7 +499,7 @@ func serializeMultipleSeries(series map[string]*protocol.Series, precision TimeP | |
if pretty { | |
return json.MarshalIndent(SerializeSeries(series, precision), "", JSON_PRETTY_PRINT_INDENT) | |
} else { | |
- return json.Marshal(SerializeSeries(series, precision)) | |
+ return SerializeSeriesJson(series, precision, 0), nil | |
} | |
} | |
diff --git a/common/serialize_series.go b/common/serialize_series.go | |
index 315ffcd..67491ac 100644 | |
--- a/common/serialize_series.go | |
+++ b/common/serialize_series.go | |
@@ -3,6 +3,8 @@ package common | |
import ( | |
"encoding/json" | |
"fmt" | |
+ "bytes" | |
+ "strconv" | |
log "code.google.com/p/log4go" | |
"github.com/influxdb/influxdb/protocol" | |
@@ -224,3 +226,116 @@ func SerializeSeries(memSeries map[string]*protocol.Series, precision TimePrecis | |
SortSerializedSeries(serializedSeries) | |
return serializedSeries | |
} | |
+ | |
+func SerializeSeriesJson(memSeries map[string]*protocol.Series, precision TimePrecision, numberOfSeries uint64) []byte { | |
+ var ( | |
+ buf bytes.Buffer | |
+ memSeriesFirstRun bool | |
+ pointsFirstRun bool | |
+ cnt uint64 | |
+ ) | |
+ cnt = 0 | |
+ buf.Grow(128*1024) | |
+ if (numberOfSeries != 1) { | |
+ buf.WriteString("[") | |
+ } | |
+ | |
+ memSeriesFirstRun = true | |
+ for _, series := range memSeries { | |
+ cnt++ | |
+ if (!memSeriesFirstRun) { | |
+ buf.WriteString(",") | |
+ } else { | |
+ memSeriesFirstRun = false | |
+ } | |
+ buf.WriteString("{\"name\":\"") | |
+ buf.WriteString(*series.Name) | |
+ buf.WriteString("\",\"columns\":[") | |
+ pointsFirstRun = true | |
+ | |
+ includeSequenceNumber := true | |
+ if len(series.Points) > 0 && series.Points[0].SequenceNumber == nil { | |
+ includeSequenceNumber = false | |
+ } | |
+ | |
+ buf.WriteString("\"time\"") | |
+ if includeSequenceNumber { | |
+ buf.WriteString(",\"sequence_number\"") | |
+ } | |
+ for _, field := range series.Fields { | |
+ buf.WriteString(",\"") | |
+ buf.WriteString(field) | |
+ buf.WriteString("\"") | |
+ } | |
+ buf.WriteString("],\"points\":[") | |
+ | |
+ for _, row := range series.Points { | |
+ if (!pointsFirstRun) { | |
+ buf.WriteString(",") | |
+ } else { | |
+ pointsFirstRun = false | |
+ } | |
+ buf.WriteString("[") | |
+ timestamp := int64(0) | |
+ if t := row.Timestamp; t != nil { | |
+ timestamp = *row.GetTimestampInMicroseconds() | |
+ switch precision { | |
+ case SecondPrecision: | |
+ timestamp /= 1000 | |
+ fallthrough | |
+ case MillisecondPrecision: | |
+ timestamp /= 1000 | |
+ } | |
+ } | |
+ | |
+ buf.WriteString(strconv.FormatInt(timestamp, 10)) | |
+ s := uint64(0) | |
+ if includeSequenceNumber { | |
+ if row.SequenceNumber != nil { | |
+ s = row.GetSequenceNumber() | |
+ } | |
+ buf.WriteString(",") | |
+ buf.WriteString(strconv.FormatUint(s, 10)) | |
+ } | |
+ | |
+ for _, value := range row.Values { | |
+ buf.WriteString(",") | |
+ | |
+ if value == nil { | |
+ buf.WriteString("null") | |
+ continue | |
+ } | |
+ _, ok := value.GetValue() | |
+ if !ok { | |
+ buf.WriteString("null") | |
+ log.Warn("Infinite or NaN value encountered") | |
+ continue | |
+ } | |
+ | |
+ if value.StringValue != nil { | |
+ buf.WriteString("\"") | |
+ buf.WriteString(*value.StringValue) | |
+ buf.WriteString("\"") | |
+ } else if value.DoubleValue != nil { | |
+ buf.WriteString(strconv.FormatFloat(*value.DoubleValue, 'f', 6, 64)) | |
+ } else if value.Int64Value != nil { | |
+ buf.WriteString(strconv.FormatInt(*value.Int64Value, 10)) | |
+ } else { | |
+ buf.WriteString("null") | |
+ } | |
+ } | |
+ buf.WriteString("]") | |
+ } | |
+ | |
+ | |
+ buf.WriteString("]}") | |
+ if (cnt > numberOfSeries) { | |
+ break | |
+ } | |
+ } | |
+ if (numberOfSeries != 1) { | |
+ buf.WriteString("]") | |
+ } | |
+ | |
+ return buf.Bytes() | |
+} | |
diff --git a/metastore/store.go b/metastore/store.go | |
index a97ad85..74ae110 100644 | |
--- a/metastore/store.go | |
+++ b/metastore/store.go | |
@@ -6,7 +6,6 @@ import ( | |
"encoding/json" | |
"fmt" | |
"regexp" | |
- "sort" | |
"sync" | |
"github.com/influxdb/influxdb/protocol" | |
@@ -135,7 +134,6 @@ func (self *Store) GetSeriesForDatabaseAndRegex(database string, regex *regexp.R | |
matchingSeries = append(matchingSeries, series) | |
} | |
} | |
- sort.Strings(matchingSeries) | |
return matchingSeries | |
} | |
@@ -150,7 +148,6 @@ func (self *Store) GetSeriesForDatabase(database string) []string { | |
for s := range databaseSeries { | |
series = append(series, s) | |
} | |
- sort.Strings(series) | |
return series | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment