Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch committed Mar 29, 2017
1 parent 12c8c1c commit 293a720
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
20 changes: 10 additions & 10 deletions pdctl/command/region_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

var (
regionsPrefix = "pd/api/v1/regions"
regionIDPrefix = "pd/api/v1/region/id/%s"
regionKeyPrefix = "pd/api/v1/region/key/%s"
regionIDPrefix = "pd/api/v1/region/id"
regionKeyPrefix = "pd/api/v1/region/key"
)

type regionInfo struct {
Expand All @@ -54,7 +54,7 @@ func showRegionCommandFunc(cmd *cobra.Command, args []string) {
fmt.Println("region_id should be a number")
return
}
prefix = fmt.Sprintf(regionIDPrefix, args[0])
prefix = regionIDPrefix + "/" + args[0]
}
r, err := doRequest(cmd, prefix, http.MethodGet)
if err != nil {
Expand Down Expand Up @@ -82,14 +82,14 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) {
}

var (
key []byte
key string
err error
)

format := cmd.Flags().Lookup("format").Value.String()
switch format {
case "raw":
key = []byte(args[0])
key = args[0]
case "pb", "proto", "protobuf":
key, err = decodeProtobufText(args[0])
if err != nil {
Expand All @@ -100,7 +100,7 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) {
fmt.Println("Error: unknown format")
return
}
prefix := fmt.Sprintf(regionKeyPrefix, key)
prefix := regionKeyPrefix + "/" + key
r, err := doRequest(cmd, prefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get region: %s", err)
Expand All @@ -110,24 +110,24 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) {

}

func decodeProtobufText(text string) ([]byte, error) {
func decodeProtobufText(text string) (string, error) {
var buf []byte
r := bytes.NewBuffer([]byte(text))
for {
c, err := r.ReadByte()
if err != nil {
if err != io.EOF {
return nil, err
return "", err
}
break
}
if c == '\\' {
_, err := fmt.Sscanf(string(r.Next(3)), "%03o", &c)
if err != nil {
return nil, err
return "", err
}
}
buf = append(buf, c)
}
return buf, nil
return string(buf), nil
}
12 changes: 2 additions & 10 deletions server/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,7 @@ func (c *RaftCluster) GetRegionByKey(regionKey []byte) (*metapb.Region, *metapb.

// GetRegionInfoByKey gets regionInfo by region key from cluster.
func (c *RaftCluster) GetRegionInfoByKey(regionKey []byte) *RegionInfo {
region := c.cachedCluster.searchRegion(regionKey)
if region == nil {
return nil
}
return region
return c.cachedCluster.searchRegion(regionKey)
}

// GetRegionByID gets region and leader peer by regionID from cluster.
Expand All @@ -320,11 +316,7 @@ func (c *RaftCluster) GetRegionByID(regionID uint64) (*metapb.Region, *metapb.Pe

// GetRegionInfoByID gets regionInfo by regionID from cluster.
func (c *RaftCluster) GetRegionInfoByID(regionID uint64) *RegionInfo {
region := c.cachedCluster.getRegion(regionID)
if region == nil {
return nil
}
return region
return c.cachedCluster.getRegion(regionID)
}

// GetRegions gets regions from cluster.
Expand Down

0 comments on commit 293a720

Please sign in to comment.