From 149015a93c3f380f0a539d1839f73b694805a01c Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Mon, 23 Feb 2015 18:58:15 -0800 Subject: [PATCH] Remove field value count when writing to disk Fixes issue #1636 --- database.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/database.go b/database.go index 97cfa9a83af..c24e9b4a0d6 100644 --- a/database.go +++ b/database.go @@ -714,9 +714,8 @@ func NewFieldCodec(m *Measurement) *FieldCodec { // If a field exists in the codec, but its type is different, an error is returned. If // a field is not present in the codec, the system panics. func (f *FieldCodec) EncodeFields(values map[string]interface{}) ([]byte, error) { - // Allocate byte slice and write field count. - b := make([]byte, 1, 10) - b[0] = byte(len(values)) + // Allocate byte slice + b := make([]byte, 0, 10) for k, v := range values { field := f.fieldsByName[k] @@ -782,11 +781,11 @@ func (f *FieldCodec) DecodeByID(targetID uint8, b []byte) (interface{}, error) { return 0, ErrFieldNotFound } - // Read the field count from the field byte. - n := int(b[0]) - // Start from the second byte and iterate over until we're done decoding. - b = b[1:] - for i := 0; i < n; i++ { + for { + if len(b) < 1 { + // No more bytes. + break + } field, ok := f.fieldsByID[b[0]] if !ok { panic(fmt.Sprintf("field ID %d has no mapping", b[0])) @@ -829,15 +828,15 @@ func (f *FieldCodec) DecodeFields(b []byte) map[uint8]interface{} { return nil } - // Read the field count from the field byte. - n := int(b[0]) - // Create a map to hold the decoded data. - values := make(map[uint8]interface{}, n) + values := make(map[uint8]interface{}, 0) + + for { + if len(b) < 1 { + // No more bytes. + break + } - // Start from the second byte and iterate over until we're done decoding. - b = b[1:] - for i := 0; i < n; i++ { // First byte is the field identifier. fieldID := b[0] field := f.fieldsByID[fieldID]