github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/milevadb-server/einsteindb/fidel_codec.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 einsteindb
    15  
    16  import (
    17  	"context"
    18  
    19  	fidel "github.com/einsteindb/fidel/client"
    20  	"github.com/whtcorpsinc/ekvproto/pkg/spacetimepb"
    21  	"github.com/whtcorpsinc/errors"
    22  	"github.com/whtcorpsinc/milevadb/soliton/codec"
    23  )
    24  
    25  type codecFIDelClient struct {
    26  	fidel.Client
    27  }
    28  
    29  // GetRegion encodes the key before send requests to fidel-server and decodes the
    30  // returned StartKey && EndKey from fidel-server.
    31  func (c *codecFIDelClient) GetRegion(ctx context.Context, key []byte) (*fidel.Region, error) {
    32  	encodedKey := codec.EncodeBytes([]byte(nil), key)
    33  	region, err := c.Client.GetRegion(ctx, encodedKey)
    34  	return processRegionResult(region, err)
    35  }
    36  
    37  func (c *codecFIDelClient) GetPrevRegion(ctx context.Context, key []byte) (*fidel.Region, error) {
    38  	encodedKey := codec.EncodeBytes([]byte(nil), key)
    39  	region, err := c.Client.GetPrevRegion(ctx, encodedKey)
    40  	return processRegionResult(region, err)
    41  }
    42  
    43  // GetRegionByID encodes the key before send requests to fidel-server and decodes the
    44  // returned StartKey && EndKey from fidel-server.
    45  func (c *codecFIDelClient) GetRegionByID(ctx context.Context, regionID uint64) (*fidel.Region, error) {
    46  	region, err := c.Client.GetRegionByID(ctx, regionID)
    47  	return processRegionResult(region, err)
    48  }
    49  
    50  func (c *codecFIDelClient) ScanRegions(ctx context.Context, startKey []byte, endKey []byte, limit int) ([]*fidel.Region, error) {
    51  	startKey = codec.EncodeBytes([]byte(nil), startKey)
    52  	if len(endKey) > 0 {
    53  		endKey = codec.EncodeBytes([]byte(nil), endKey)
    54  	}
    55  
    56  	regions, err := c.Client.ScanRegions(ctx, startKey, endKey, limit)
    57  	if err != nil {
    58  		return nil, errors.Trace(err)
    59  	}
    60  	for _, region := range regions {
    61  		if region != nil {
    62  			err = decodeRegionMetaKeyInPlace(region.Meta)
    63  			if err != nil {
    64  				return nil, errors.Trace(err)
    65  			}
    66  		}
    67  	}
    68  	return regions, nil
    69  }
    70  
    71  func processRegionResult(region *fidel.Region, err error) (*fidel.Region, error) {
    72  	if err != nil {
    73  		return nil, errors.Trace(err)
    74  	}
    75  	if region == nil || region.Meta == nil {
    76  		return nil, nil
    77  	}
    78  	err = decodeRegionMetaKeyInPlace(region.Meta)
    79  	if err != nil {
    80  		return nil, errors.Trace(err)
    81  	}
    82  	return region, nil
    83  }
    84  
    85  func decodeRegionMetaKeyInPlace(r *spacetimepb.Region) error {
    86  	if len(r.StartKey) != 0 {
    87  		_, decoded, err := codec.DecodeBytes(r.StartKey, nil)
    88  		if err != nil {
    89  			return errors.Trace(err)
    90  		}
    91  		r.StartKey = decoded
    92  	}
    93  	if len(r.EndKey) != 0 {
    94  		_, decoded, err := codec.DecodeBytes(r.EndKey, nil)
    95  		if err != nil {
    96  			return errors.Trace(err)
    97  		}
    98  		r.EndKey = decoded
    99  	}
   100  	return nil
   101  }
   102  
   103  func decodeRegionMetaKeyWithShallowCopy(r *spacetimepb.Region) (*spacetimepb.Region, error) {
   104  	nr := *r
   105  	if len(r.StartKey) != 0 {
   106  		_, decoded, err := codec.DecodeBytes(r.StartKey, nil)
   107  		if err != nil {
   108  			return nil, errors.Trace(err)
   109  		}
   110  		nr.StartKey = decoded
   111  	}
   112  	if len(r.EndKey) != 0 {
   113  		_, decoded, err := codec.DecodeBytes(r.EndKey, nil)
   114  		if err != nil {
   115  			return nil, errors.Trace(err)
   116  		}
   117  		nr.EndKey = decoded
   118  	}
   119  	return &nr, nil
   120  }