Skip to content

Commit

Permalink
pkg: improve region path (#8180)
Browse files Browse the repository at this point in the history
ref #7897

Signed-off-by: Ryan Leung <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
rleungx and ti-chi-bot[bot] authored May 16, 2024
1 parent 7e85817 commit 59e29cc
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions pkg/storage/endpoint/key_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,23 @@ func storeRegionWeightPath(storeID uint64) string {
// RegionPath returns the region meta info key path with the given region ID.
func RegionPath(regionID uint64) string {
var buf strings.Builder
buf.Grow(len(regionPathPrefix) + 1 + keyLen) // Preallocate memory

buf.WriteString(regionPathPrefix)
buf.WriteString("/")
s := strconv.FormatUint(regionID, 10)
if len(s) > keyLen {
s = s[len(s)-keyLen:]
} else {
b := make([]byte, keyLen)
b := make([]byte, keyLen)
copy(b, s)
if len(s) < keyLen {
diff := keyLen - len(s)
for i := 0; i < keyLen; i++ {
if i < diff {
b[i] = 48
} else {
b[i] = s[i-diff]
}
copy(b[diff:], s)
for i := 0; i < diff; i++ {
b[i] = '0'
}
s = string(b)
} else if len(s) > keyLen {
copy(b, s[len(s)-keyLen:])
}
buf.WriteString(s)
buf.Write(b)

return buf.String()
}
Expand Down

0 comments on commit 59e29cc

Please sign in to comment.