Skip to content

Commit

Permalink
Merge pull request #3194 from influxdb/shard_field_func
Browse files Browse the repository at this point in the history
Minor changes to field codec functions
  • Loading branch information
otoolep committed Jun 30, 2015
2 parents d5f67c8 + babc63d commit b063c15
Showing 1 changed file with 11 additions and 57 deletions.
68 changes: 11 additions & 57 deletions tsdb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,65 +889,9 @@ func (f *FieldCodec) EncodeFields(values map[string]interface{}) ([]byte, error)
func (f *FieldCodec) FieldIDByName(s string) (uint8, error) {
fi := f.fieldsByName[s]
if fi == nil {
return 0, fmt.Errorf("field doesn't exist")
}
return fi.ID, nil
}

// DecodeByID scans a byte slice for a field with the given ID, converts it to its
// expected type, and return that value.
func (f *FieldCodec) decodeByID(targetID uint8, b []byte) (interface{}, error) {
if len(b) == 0 {
return 0, ErrFieldNotFound
}

for {
if len(b) < 1 {
// No more bytes.
break
}
field, ok := f.fieldsByID[b[0]]
if !ok {
// This can happen, though is very unlikely. If this node receives encoded data, to be written
// to disk, and is queried for that data before its metastore is updated, there will be no field
// mapping for the data during decode. All this can happen because data is encoded by the node
// that first received the write request, not the node that actually writes the data to disk.
// So if this happens, the read must be aborted.
return 0, ErrFieldUnmappedID
}

var value interface{}
switch field.Type {
case influxql.Float:
// Move bytes forward.
value = math.Float64frombits(binary.BigEndian.Uint64(b[1:9]))
b = b[9:]
case influxql.Integer:
value = int64(binary.BigEndian.Uint64(b[1:9]))
b = b[9:]
case influxql.Boolean:
if b[1] == 1 {
value = true
} else {
value = false
}
// Move bytes forward.
b = b[2:]
case influxql.String:
size := binary.BigEndian.Uint16(b[1:3])
value = string(b[3 : 3+size])
// Move bytes forward.
b = b[size+3:]
default:
panic(fmt.Sprintf("unsupported value type during decode by id: %T", field.Type))
}

if field.ID == targetID {
return value, nil
}
}

return 0, ErrFieldNotFound
return fi.ID, nil
}

// DecodeFields decodes a byte slice into a set of field ids and values.
Expand Down Expand Up @@ -1081,6 +1025,16 @@ func (f *FieldCodec) DecodeByID(targetID uint8, b []byte) (interface{}, error) {
return 0, ErrFieldNotFound
}

// DecodeByName scans a byte slice for a field with the given name, converts it to its
// expected type, and return that value.
func (f *FieldCodec) DecodeByName(name string, b []byte) (interface{}, error) {
if fi := f.fieldByName(name); fi == nil {
return 0, ErrFieldNotFound
} else {
return f.DecodeByID(fi.ID, b)
}
}

// FieldByName returns the field by its name. It will return a nil if not found
func (f *FieldCodec) fieldByName(name string) *field {
return f.fieldsByName[name]
Expand Down

0 comments on commit b063c15

Please sign in to comment.