github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/tests/br_key_locked/codec.go (about)

     1  // Copyright 2019 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  // This file is copied from pingcap/tidb/store/tikv/pd_codec.go https://git.io/Je1Ww
    15  
    16  package main
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/pingcap/errors"
    22  	"github.com/pingcap/kvproto/pkg/metapb"
    23  	"github.com/pingcap/tidb/util/codec"
    24  	pd "github.com/tikv/pd/client"
    25  )
    26  
    27  type codecPDClient struct {
    28  	pd.Client
    29  }
    30  
    31  // GetRegion encodes the key before send requests to pd-server and decodes the
    32  // returned StartKey && EndKey from pd-server.
    33  func (c *codecPDClient) GetRegion(ctx context.Context, key []byte) (*pd.Region, error) {
    34  	encodedKey := codec.EncodeBytes(nil, key)
    35  	region, err := c.Client.GetRegion(ctx, encodedKey)
    36  	return processRegionResult(region, err)
    37  }
    38  
    39  func (c *codecPDClient) GetPrevRegion(ctx context.Context, key []byte) (*pd.Region, error) {
    40  	encodedKey := codec.EncodeBytes(nil, key)
    41  	region, err := c.Client.GetPrevRegion(ctx, encodedKey)
    42  	return processRegionResult(region, err)
    43  }
    44  
    45  // GetRegionByID encodes the key before send requests to pd-server and decodes the
    46  // returned StartKey && EndKey from pd-server.
    47  func (c *codecPDClient) GetRegionByID(ctx context.Context, regionID uint64) (*pd.Region, error) {
    48  	region, err := c.Client.GetRegionByID(ctx, regionID)
    49  	return processRegionResult(region, err)
    50  }
    51  
    52  func (c *codecPDClient) ScanRegions(
    53  	ctx context.Context,
    54  	startKey []byte,
    55  	endKey []byte,
    56  	limit int,
    57  ) ([]*pd.Region, error) {
    58  	startKey = codec.EncodeBytes(nil, startKey)
    59  	if len(endKey) > 0 {
    60  		endKey = codec.EncodeBytes(nil, endKey)
    61  	}
    62  
    63  	regions, err := c.Client.ScanRegions(ctx, startKey, endKey, limit)
    64  	if err != nil {
    65  		return nil, errors.Trace(err)
    66  	}
    67  	for _, region := range regions {
    68  		if region != nil {
    69  			err = decodeRegionMetaKey(region.Meta)
    70  			if err != nil {
    71  				return nil, errors.Trace(err)
    72  			}
    73  		}
    74  	}
    75  	return regions, nil
    76  }
    77  
    78  func processRegionResult(region *pd.Region, err error) (*pd.Region, error) {
    79  	if err != nil {
    80  		return nil, errors.Trace(err)
    81  	}
    82  	if region == nil {
    83  		return nil, nil
    84  	}
    85  	err = decodeRegionMetaKey(region.Meta)
    86  	if err != nil {
    87  		return nil, errors.Trace(err)
    88  	}
    89  	return region, nil
    90  }
    91  
    92  func decodeRegionMetaKey(r *metapb.Region) error {
    93  	if len(r.StartKey) != 0 {
    94  		_, decoded, err := codec.DecodeBytes(r.StartKey, nil)
    95  		if err != nil {
    96  			return errors.Trace(err)
    97  		}
    98  		r.StartKey = decoded
    99  	}
   100  	if len(r.EndKey) != 0 {
   101  		_, decoded, err := codec.DecodeBytes(r.EndKey, nil)
   102  		if err != nil {
   103  			return errors.Trace(err)
   104  		}
   105  		r.EndKey = decoded
   106  	}
   107  	return nil
   108  }