// GetRegion encodes the key before send requests to pd-server and decodes the // returned StartKey && EndKey from pd-server. func (c *codecPDClient) GetRegion(key []byte) (*metapb.Region, *metapb.Peer, error) { encodedKey := codec.EncodeBytes([]byte(nil), key) region, peer, err := c.Client.GetRegion(encodedKey) if err != nil { return nil, nil, errors.Trace(err) } if region == nil { return nil, nil, nil } if len(region.StartKey) != 0 { _, decoded, err := codec.DecodeBytes(region.StartKey) if err != nil { return nil, nil, errors.Trace(err) } region.StartKey = decoded } if len(region.EndKey) != 0 { _, decoded, err := codec.DecodeBytes(region.EndKey) if err != nil { return nil, nil, errors.Trace(err) } region.EndKey = decoded } return region, peer, nil }
func (t *TxStructure) decodeHashDataKey(ek kv.Key) ([]byte, []byte, error) { var ( key []byte field []byte err error tp uint64 ) if !bytes.HasPrefix(ek, t.prefix) { return nil, nil, errors.New("invalid encoded hash data key prefix") } ek = ek[len(t.prefix):] ek, key, err = codec.DecodeBytes(ek) if err != nil { return nil, nil, errors.Trace(err) } ek, tp, err = codec.DecodeUint(ek) if err != nil { return nil, nil, errors.Trace(err) } else if TypeFlag(tp) != HashData { return nil, nil, errors.Errorf("invalid encoded hash data key flag %c", byte(tp)) } _, field, err = codec.DecodeBytes(ek) return key, field, errors.Trace(err) }
func decodeRegionMetaKey(r *metapb.Region) error { if len(r.StartKey) != 0 { _, decoded, err := codec.DecodeBytes(r.StartKey) if err != nil { return errors.Trace(err) } r.StartKey = decoded } if len(r.EndKey) != 0 { _, decoded, err := codec.DecodeBytes(r.EndKey) if err != nil { return errors.Trace(err) } r.EndKey = decoded } return nil }
// Raw decodes a mvccKey to original key. func (key mvccKey) Raw() []byte { if len(key) == 0 { return nil } _, k, err := codec.DecodeBytes(key) if err != nil { panic(err) } return k }
// MvccDecode parses the origin key and version of an encoded key, if the encoded key is a meta key, // just returns the origin key func MvccDecode(encodedKey kv.EncodedKey) (kv.Key, kv.Version, error) { // Skip DataPrefix remainBytes, key, err := codec.DecodeBytes([]byte(encodedKey)) if err != nil { // should never happen return nil, kv.Version{}, errors.Trace(err) } // if it's meta key if len(remainBytes) == 0 { return key, kv.Version{}, nil } var ver uint64 remainBytes, ver, err = codec.DecodeUintDesc(remainBytes) if err != nil { // should never happen return nil, kv.Version{}, errors.Trace(err) } if len(remainBytes) != 0 { return nil, kv.Version{}, ErrInvalidEncodedKey } return key, kv.Version{ver}, nil }