Skip to content

Commit

Permalink
improve region path
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <[email protected]>
  • Loading branch information
rleungx committed Oct 26, 2022
1 parent a4d9f29 commit 6024b05
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion server/storage/endpoint/key_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package endpoint

import (
"bytes"
"fmt"
"path"
"strconv"
)

const (
Expand All @@ -38,6 +40,8 @@ const (
keyspaceMetaInfix = "meta"
keyspaceIDInfix = "id"
keyspaceAllocID = "alloc_id"
regionPathPrefix = "raft/r"
prefix = "00000000000000000000"
)

// AppendToRootPath appends the given key to the rootPath.
Expand Down Expand Up @@ -74,7 +78,13 @@ func storeRegionWeightPath(storeID uint64) string {

// RegionPath returns the region meta info key path with the given region ID.
func RegionPath(regionID uint64) string {
return path.Join(clusterPath, "r", fmt.Sprintf("%020d", regionID))
buf := new(bytes.Buffer)
buf.WriteString(regionPathPrefix)
buf.WriteString("/")
b := strconv.AppendUint([]byte(prefix), regionID, 10)
buf.WriteString(string(b[len(b)-20:]))

return buf.String()
}

func ruleKeyPath(ruleKey string) string {
Expand Down
29 changes: 29 additions & 0 deletions server/storage/endpoint/key_path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package endpoint

import (
"fmt"
"math/rand"
"path"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestRegionPath(t *testing.T) {
re := require.New(t)
f := func(id uint64) string {
return path.Join(regionPathPrefix, fmt.Sprintf("%020d", id))
}
rand.Seed(time.Now().Unix())
for i := 0; i < 1000; i++ {
id := rand.Uint64()
re.Equal(f(id), RegionPath(id))
}
}

func BenchmarkRegionPath(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = RegionPath(uint64(i))
}
}

0 comments on commit 6024b05

Please sign in to comment.