Skip to content

Commit

Permalink
Add msgpack handler for smarter JSON encoding
Browse files Browse the repository at this point in the history
The msgpack handler correctly retains information about whether the
number is a float or an integer, unlike JSON. While the format is not
human-readable, it makes a good interchange format for applications that
don't necessarily care about a human readable output.

This uses `github.com/tinylib/msgp` to precompile the marshaling for
`models.Row`. Since we only use this library to marshal the one type,
this is a much more efficient method of encoding than using reflection.
  • Loading branch information
jsternberg committed Sep 3, 2016
1 parent c6763fc commit 77154e8
Show file tree
Hide file tree
Showing 8 changed files with 563 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [#7099](https://github.com/influxdata/influxdb/pull/7099): Implement text/csv content encoding for the response writer.
- [#6992](https://github.com/influxdata/influxdb/issues/6992): Support tools for running async queries.
- [#7136](https://github.com/influxdata/influxdb/pull/7136): Update jwt-go dependency to version 3.
- [#7154](https://github.com/influxdata/influxdb/pull/7154): Add msgpack handler for smart JSON encoding.

### Bugfixes

Expand Down
2 changes: 2 additions & 0 deletions Godeps
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/jwilder/encoding ac74639f65b2180a2e5eb5ff197f0c122441aed0
github.com/kimor79/gollectd 61d0deeb4ffcc167b2a1baa8efd72365692811bc
github.com/paulbellamy/ratecounter 5a11f585a31379765c190c033b6ad39956584447
github.com/peterh/liner 8975875355a81d612fafb9f5a6037bdcc2d9b073
github.com/philhofer/fwd 98c11a7a6ec829d672b03833c3d69a7fae1ca972
github.com/rakyll/statik 274df120e9065bdd08eb1120e0375e3dc1ae8465
github.com/retailnext/hllpp 38a7bb71b483e855d35010808143beaf05b67f9d
github.com/tinylib/msgp ad0ff2e232ad2e37faf67087fb24bf8d04a8ce20
golang.org/x/crypto c197bcf24cde29d3f73c7b4ac6fd41f4384e8af6
2 changes: 2 additions & 0 deletions LICENSE_OF_DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
- github.com/kimor79/gollectd [BSD LICENSE](https://github.com/kimor79/gollectd/blob/master/LICENSE)
- github.com/paulbellamy/ratecounter [MIT LICENSE](https://github.com/paulbellamy/ratecounter/blob/master/LICENSE)
- github.com/peterh/liner [MIT LICENSE](https://github.com/peterh/liner/blob/master/COPYING)
- github.com/philhofer/fwd [MIT LICENSE](https://github.com/philhofer/fwd/blob/master/LICENSE.md)
- github.com/rakyll/statik [APACHE LICENSE](https://github.com/rakyll/statik/blob/master/LICENSE)
- github.com/retailnext/hllpp [BSD LICENSE](https://github.com/retailnext/hllpp/blob/master/LICENSE)
- github.com/tinylib/msgp [BSD LICENSE](https://github.com/tinylib/msgp/blob/master/LICENSE)
- glyphicons [LICENSE](http://glyphicons.com/license/)
- golang.org/x/crypto [BSD LICENSE](https://github.com/golang/crypto/blob/master/LICENSE)
- jquery 2.1.4 [MIT LICENSE](https://github.com/jquery/jquery/blob/master/LICENSE.txt)
Expand Down
271 changes: 271 additions & 0 deletions models/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
package models

// NOTE: THIS FILE WAS PRODUCED BY THE
// MSGP CODE GENERATION TOOL (github.com/tinylib/msgp)
// DO NOT EDIT

import "github.com/tinylib/msgp/msgp"

// DecodeMsg implements msgp.Decodable
func (z *Row) DecodeMsg(dc *msgp.Reader) (err error) {
var field []byte
_ = field
var zwht uint32
zwht, err = dc.ReadMapHeader()
if err != nil {
return
}
for zwht > 0 {
zwht--
field, err = dc.ReadMapKeyPtr()
if err != nil {
return
}
switch msgp.UnsafeString(field) {
case "name":
z.Name, err = dc.ReadString()
if err != nil {
return
}
case "tags":
var zhct uint32
zhct, err = dc.ReadMapHeader()
if err != nil {
return
}
if z.Tags == nil && zhct > 0 {
z.Tags = make(map[string]string, zhct)
} else if len(z.Tags) > 0 {
for key, _ := range z.Tags {
delete(z.Tags, key)
}
}
for zhct > 0 {
zhct--
var zxvk string
var zbzg string
zxvk, err = dc.ReadString()
if err != nil {
return
}
zbzg, err = dc.ReadString()
if err != nil {
return
}
z.Tags[zxvk] = zbzg
}
case "columns":
var zcua uint32
zcua, err = dc.ReadArrayHeader()
if err != nil {
return
}
if cap(z.Columns) >= int(zcua) {
z.Columns = (z.Columns)[:zcua]
} else {
z.Columns = make([]string, zcua)
}
for zbai := range z.Columns {
z.Columns[zbai], err = dc.ReadString()
if err != nil {
return
}
}
case "values":
var zxhx uint32
zxhx, err = dc.ReadArrayHeader()
if err != nil {
return
}
if cap(z.Values) >= int(zxhx) {
z.Values = (z.Values)[:zxhx]
} else {
z.Values = make([][]interface{}, zxhx)
}
for zcmr := range z.Values {
var zlqf uint32
zlqf, err = dc.ReadArrayHeader()
if err != nil {
return
}
if cap(z.Values[zcmr]) >= int(zlqf) {
z.Values[zcmr] = (z.Values[zcmr])[:zlqf]
} else {
z.Values[zcmr] = make([]interface{}, zlqf)
}
for zajw := range z.Values[zcmr] {
z.Values[zcmr][zajw], err = dc.ReadIntf()
if err != nil {
return
}
}
}
default:
err = dc.Skip()
if err != nil {
return
}
}
}
return
}

// EncodeMsg implements msgp.Encodable
func (z *Row) EncodeMsg(en *msgp.Writer) (err error) {
// map header, size 4
// write "name"
err = en.Append(0x84, 0xa4, 0x6e, 0x61, 0x6d, 0x65)
if err != nil {
return err
}
err = en.WriteString(z.Name)
if err != nil {
return
}
// write "tags"
err = en.Append(0xa4, 0x74, 0x61, 0x67, 0x73)
if err != nil {
return err
}
err = en.WriteMapHeader(uint32(len(z.Tags)))
if err != nil {
return
}
for zxvk, zbzg := range z.Tags {
err = en.WriteString(zxvk)
if err != nil {
return
}
err = en.WriteString(zbzg)
if err != nil {
return
}
}
// write "columns"
err = en.Append(0xa7, 0x63, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x73)
if err != nil {
return err
}
err = en.WriteArrayHeader(uint32(len(z.Columns)))
if err != nil {
return
}
for zbai := range z.Columns {
err = en.WriteString(z.Columns[zbai])
if err != nil {
return
}
}
// write "values"
err = en.Append(0xa6, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73)
if err != nil {
return err
}
err = en.WriteArrayHeader(uint32(len(z.Values)))
if err != nil {
return
}
for zcmr := range z.Values {
err = en.WriteArrayHeader(uint32(len(z.Values[zcmr])))
if err != nil {
return
}
for zajw := range z.Values[zcmr] {
err = en.WriteIntf(z.Values[zcmr][zajw])
if err != nil {
return
}
}
}
return
}

// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (z *Row) Msgsize() (s int) {
s = 1 + 5 + msgp.StringPrefixSize + len(z.Name) + 5 + msgp.MapHeaderSize
if z.Tags != nil {
for zxvk, zbzg := range z.Tags {
_ = zbzg
s += msgp.StringPrefixSize + len(zxvk) + msgp.StringPrefixSize + len(zbzg)
}
}
s += 8 + msgp.ArrayHeaderSize
for zbai := range z.Columns {
s += msgp.StringPrefixSize + len(z.Columns[zbai])
}
s += 7 + msgp.ArrayHeaderSize
for zcmr := range z.Values {
s += msgp.ArrayHeaderSize
for zajw := range z.Values[zcmr] {
s += msgp.GuessSize(z.Values[zcmr][zajw])
}
}
return
}

// DecodeMsg implements msgp.Decodable
func (z *Rows) DecodeMsg(dc *msgp.Reader) (err error) {
var zjfb uint32
zjfb, err = dc.ReadArrayHeader()
if err != nil {
return
}
if cap((*z)) >= int(zjfb) {
(*z) = (*z)[:zjfb]
} else {
(*z) = make(Rows, zjfb)
}
for zpks := range *z {
if dc.IsNil() {
err = dc.ReadNil()
if err != nil {
return
}
(*z)[zpks] = nil
} else {
if (*z)[zpks] == nil {
(*z)[zpks] = new(Row)
}
err = (*z)[zpks].DecodeMsg(dc)
if err != nil {
return
}
}
}
return
}

// EncodeMsg implements msgp.Encodable
func (z Rows) EncodeMsg(en *msgp.Writer) (err error) {
err = en.WriteArrayHeader(uint32(len(z)))
if err != nil {
return
}
for zcxo := range z {
if z[zcxo] == nil {
err = en.WriteNil()
if err != nil {
return
}
} else {
err = z[zcxo].EncodeMsg(en)
if err != nil {
return
}
}
}
return
}

// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (z Rows) Msgsize() (s int) {
s = msgp.ArrayHeaderSize
for zcxo := range z {
if z[zcxo] == nil {
s += msgp.NilSize
} else {
s += z[zcxo].Msgsize()
}
}
return
}
Loading

0 comments on commit 77154e8

Please sign in to comment.