Skip to content

Commit

Permalink
address the comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Oct 11, 2019
1 parent bda04dc commit 46a47f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 51 deletions.
33 changes: 1 addition & 32 deletions server/core/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"reflect"
"strings"

Expand Down Expand Up @@ -520,37 +519,7 @@ func (rst *regionSubTree) RandomRegion(startKey, endKey []byte) *RegionInfo {
}

t := rst.regionTree
var startIndex, endIndex, index int
var startRegion, endRegion *RegionInfo

if len(startKey) != 0 {
startRegion, startIndex = t.getWithIndex(&regionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: startKey}}})
} else {
startRegion, startIndex = t.getWithIndex(t.tree.Min())
}

if len(endKey) != 0 {
endRegion, endIndex = t.getWithIndex(&regionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: endKey}}})
} else {
_, endIndex = t.getWithIndex(t.tree.Max())
endRegion = nil
endIndex++
}

if endIndex == startIndex {
if endRegion == nil {
return t.tree.GetAt(startIndex - 1).(*regionItem).region
}
return t.tree.GetAt(startIndex).(*regionItem).region
}

if endRegion == nil && startRegion == nil {
index = rand.Intn(endIndex-startIndex+1) + startIndex
return t.tree.GetAt(index - 1).(*regionItem).region
}

index = rand.Intn(endIndex-startIndex) + startIndex
return t.tree.GetAt(index).(*regionItem).region
return t.RandomRegion(startKey, endKey)
}

// RegionsInfo for export
Expand Down
30 changes: 11 additions & 19 deletions server/core/region_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func (t *regionTree) getAdjacentRegions(region *RegionInfo) (*regionItem, *regio
return prev, next
}

// RandomRegion is used to get a random region from a given range [startKey, endKey].
func (t *regionTree) RandomRegion(startKey, endKey []byte) *RegionInfo {
if t.length() == 0 {
return nil
Expand All @@ -204,36 +205,27 @@ func (t *regionTree) RandomRegion(startKey, endKey []byte) *RegionInfo {
return t.tree.Min().(*regionItem).region
}

var startIndex, endIndex, index int
var startRegion, endRegion *RegionInfo

var (
startRegion *RegionInfo
startIndex, endIndex int
)
if len(startKey) != 0 {
startRegion, startIndex = t.getWithIndex(&regionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: startKey}}})
} else {
startRegion, startIndex = t.getWithIndex(t.tree.Min())
startRegion, startIndex = t.getWithIndex(t.tree.Min()) // for test purpose
}

if len(endKey) != 0 {
endRegion, endIndex = t.getWithIndex(&regionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: endKey}}})
_, endIndex = t.getWithIndex(&regionItem{region: &RegionInfo{meta: &metapb.Region{StartKey: endKey}}})
} else {
_, endIndex = t.getWithIndex(t.tree.Max())
endRegion = nil
endIndex++
}

if endIndex == startIndex {
if endRegion == nil {
return t.tree.GetAt(startIndex - 1).(*regionItem).region
}
return t.tree.GetAt(startIndex).(*regionItem).region
endIndex = t.tree.Len()
}

if endRegion == nil && startRegion == nil {
index = rand.Intn(endIndex-startIndex+1) + startIndex
return t.tree.GetAt(index - 1).(*regionItem).region
if startRegion == nil {
startIndex--
}

index = rand.Intn(endIndex-startIndex) + startIndex
index := rand.Intn(endIndex-startIndex) + startIndex
return t.tree.GetAt(index).(*regionItem).region
}

Expand Down
2 changes: 2 additions & 0 deletions server/core/region_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ func (s *testRegionSuite) TestRegionTreeSplitAndMerge(c *C) {

func (s *testRegionSuite) TestRandomRegion(c *C) {
tree := newRegionTree()
r := tree.RandomRegion([]byte(""), []byte(""))
c.Assert(r, IsNil)

regionA := NewTestRegionInfo([]byte(""), []byte("g"))
tree.update(regionA)
Expand Down

0 comments on commit 46a47f6

Please sign in to comment.