From 59e29cca6a0c1acb191fa50b10ca080971b2a47a Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Thu, 16 May 2024 18:50:43 +0800 Subject: [PATCH] pkg: improve region path (#8180) ref tikv/pd#7897 Signed-off-by: Ryan Leung Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com> --- pkg/storage/endpoint/key_path.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pkg/storage/endpoint/key_path.go b/pkg/storage/endpoint/key_path.go index 69b8d0f2f8e..dbcd9690419 100644 --- a/pkg/storage/endpoint/key_path.go +++ b/pkg/storage/endpoint/key_path.go @@ -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() }