github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/tikv/pd_codec.go (about) 1 // Copyright 2016 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package tikv 15 16 import ( 17 "github.com/insionng/yougam/libraries/juju/errors" 18 "github.com/insionng/yougam/libraries/pingcap/kvproto/pkg/metapb" 19 "github.com/insionng/yougam/libraries/pingcap/pd/pd-client" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/util/codec" 21 ) 22 23 type codecPDClient struct { 24 pd.Client 25 } 26 27 // GetRegion encodes the key before send requests to pd-server and decodes the 28 // returned StartKey && EndKey from pd-server. 29 func (c *codecPDClient) GetRegion(key []byte) (*metapb.Region, error) { 30 encodedKey := codec.EncodeBytes([]byte(nil), key) 31 region, err := c.Client.GetRegion(encodedKey) 32 if err != nil { 33 return nil, errors.Trace(err) 34 } 35 if len(region.StartKey) != 0 { 36 _, decoded, err := codec.DecodeBytes(region.StartKey) 37 if err != nil { 38 return nil, errors.Trace(err) 39 } 40 region.StartKey = decoded 41 } 42 if len(region.EndKey) != 0 { 43 _, decoded, err := codec.DecodeBytes(region.EndKey) 44 if err != nil { 45 return nil, errors.Trace(err) 46 } 47 region.EndKey = decoded 48 } 49 return region, nil 50 }