codec: use reference instead of value copy of "[]types.Datum" (#4408)

This commit is contained in:
Jian Zhang
2017-09-04 11:30:47 +08:00
committed by GitHub
parent ae30c4dcbc
commit f3134bede9

View File

@ -42,29 +42,29 @@ const (
// encode will encode a datum and append it to a byte slice. If comparable is true, the encoded bytes can be sorted as it's original order.
// If hash is true, the encoded bytes can be checked equal as it's original value.
func encode(b []byte, vals []types.Datum, comparable bool, hash bool) ([]byte, error) {
for _, val := range vals {
switch val.Kind() {
for i, length := 0, len(vals); i < length; i++ {
switch vals[i].Kind() {
case types.KindInt64:
b = encodeSignedInt(b, val.GetInt64(), comparable)
b = encodeSignedInt(b, vals[i].GetInt64(), comparable)
case types.KindUint64:
if hash {
int := val.GetInt64()
int := vals[i].GetInt64()
if int < 0 {
b = encodeUnsignedInt(b, uint64(int), comparable)
} else {
b = encodeSignedInt(b, int, comparable)
}
} else {
b = encodeUnsignedInt(b, val.GetUint64(), comparable)
b = encodeUnsignedInt(b, vals[i].GetUint64(), comparable)
}
case types.KindFloat32, types.KindFloat64:
b = append(b, floatFlag)
b = EncodeFloat(b, val.GetFloat64())
b = EncodeFloat(b, vals[i].GetFloat64())
case types.KindString, types.KindBytes:
b = encodeBytes(b, val.GetBytes(), comparable)
b = encodeBytes(b, vals[i].GetBytes(), comparable)
case types.KindMysqlTime:
b = append(b, uintFlag)
t := val.GetMysqlTime()
t := vals[i].GetMysqlTime()
// Encoding timestamp need to consider timezone.
// If it's not in UTC, transform to UTC first.
if t.Type == mysql.TypeTimestamp && t.TimeZone != time.UTC {
@ -78,12 +78,12 @@ func encode(b []byte, vals []types.Datum, comparable bool, hash bool) ([]byte, e
case types.KindMysqlDuration:
// duration may have negative value, so we cannot use String to encode directly.
b = append(b, durationFlag)
b = EncodeInt(b, int64(val.GetMysqlDuration().Duration))
b = EncodeInt(b, int64(vals[i].GetMysqlDuration().Duration))
case types.KindMysqlDecimal:
b = append(b, decimalFlag)
if hash {
// If hash is true, we only consider the original value of this decimal and ignore it's precision.
dec := val.GetMysqlDecimal()
dec := vals[i].GetMysqlDecimal()
precision, frac := dec.PrecisionAndFrac()
bin, err := dec.ToBin(precision, frac)
if err != nil {
@ -91,19 +91,19 @@ func encode(b []byte, vals []types.Datum, comparable bool, hash bool) ([]byte, e
}
b = append(b, bin...)
} else {
b = EncodeDecimal(b, val)
b = EncodeDecimal(b, vals[i])
}
case types.KindMysqlHex:
b = encodeSignedInt(b, int64(val.GetMysqlHex().ToNumber()), comparable)
b = encodeSignedInt(b, int64(vals[i].GetMysqlHex().ToNumber()), comparable)
case types.KindMysqlBit:
b = encodeUnsignedInt(b, uint64(val.GetMysqlBit().ToNumber()), comparable)
b = encodeUnsignedInt(b, uint64(vals[i].GetMysqlBit().ToNumber()), comparable)
case types.KindMysqlEnum:
b = encodeUnsignedInt(b, uint64(val.GetMysqlEnum().ToNumber()), comparable)
b = encodeUnsignedInt(b, uint64(vals[i].GetMysqlEnum().ToNumber()), comparable)
case types.KindMysqlSet:
b = encodeUnsignedInt(b, uint64(val.GetMysqlSet().ToNumber()), comparable)
b = encodeUnsignedInt(b, uint64(vals[i].GetMysqlSet().ToNumber()), comparable)
case types.KindMysqlJSON:
b = append(b, jsonFlag)
b = append(b, json.Serialize(val.GetMysqlJSON())...)
b = append(b, json.Serialize(vals[i].GetMysqlJSON())...)
case types.KindNull:
b = append(b, NilFlag)
case types.KindMinNotNull:
@ -111,7 +111,7 @@ func encode(b []byte, vals []types.Datum, comparable bool, hash bool) ([]byte, e
case types.KindMaxValue:
b = append(b, maxFlag)
default:
return nil, errors.Errorf("unsupport encode type %d", val.Kind())
return nil, errors.Errorf("unsupport encode type %d", vals[i].Kind())
}
}