Created
September 11, 2014 16:50
-
-
Save vladimir-smirnov-sociomantic/4c8bad1185258bf86aa7 to your computer and use it in GitHub Desktop.
Hack way to implement simplier marshaling and remove sorting
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..8ed939a 100644 | |
--- a/api/http/api.go | |
+++ b/api/http/api.go | |
@@ -491,7 +491,8 @@ 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 json.Marshal(SerializeSeries(arg, precision)[0]) | |
+ return SerializeSeriesJson(arg, precision, 1), nil | |
} | |
} | |
@@ -499,7 +500,8 @@ 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 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..c28a92b 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" | |
@@ -221,6 +223,120 @@ func SerializeSeries(memSeries map[string]*protocol.Series, precision TimePrecis | |
Points: points, | |
}) | |
} | |
- SortSerializedSeries(serializedSeries) | |
+ | |
+// 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 (numberOfSeries > 0 && cnt > numberOfSeries) { | |
+ break | |
+ } | |
+ } | |
+ if (numberOfSeries != 1) { | |
+ buf.WriteString("]") | |
+ } | |
+ | |
+ return buf.Bytes() | |
+} | |
diff --git a/metastore/store.go b/metastore/store.go | |
index a97ad85..b0a99d0 100644 | |
--- a/metastore/store.go | |
+++ b/metastore/store.go | |
@@ -6,8 +6,8 @@ import ( | |
"encoding/json" | |
"fmt" | |
"regexp" | |
- "sort" | |
"sync" | |
+// "sort" | |
"github.com/influxdb/influxdb/protocol" | |
) | |
@@ -135,7 +135,8 @@ func (self *Store) GetSeriesForDatabaseAndRegex(database string, regex *regexp.R | |
matchingSeries = append(matchingSeries, series) | |
} | |
} | |
- sort.Strings(matchingSeries) | |
+ | |
+// sort.Strings(matchingSeries) | |
return matchingSeries | |
} | |
@@ -150,7 +151,8 @@ func (self *Store) GetSeriesForDatabase(database string) []string { | |
for s := range databaseSeries { | |
series = append(series, s) | |
} | |
- sort.Strings(series) | |
+ | |
+// sort.Strings(series) | |
return series | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment