Skip to content

Commit

Permalink
add hex
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Jun 6, 2024
1 parent 494c0e9 commit b173483
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/core/region_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (t *regionTree) find(item *regionItem) *regionItem {
// until f return false
func (t *regionTree) scanRange(startKey []byte, f func(*RegionInfo) bool) {
region := &RegionInfo{meta: &metapb.Region{StartKey: startKey}}
// find if there is a region with key range [s, d), s < startKey < d
// find if there is a region with key range [s, d), s <= startKey < d
fn := func(item *regionItem) bool {
r := item
return f(r.RegionInfo)
Expand Down
19 changes: 19 additions & 0 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ func (h *regionsHandler) ScanRegions(w http.ResponseWriter, r *http.Request) {
rc := getCluster(r)
startKey := r.URL.Query().Get("key")
endKey := r.URL.Query().Get("end_key")

// decode hex if query has params with hex format
formatStr := r.URL.Query().Get("format")
if formatStr == "hex" {
keyBytes, err := hex.DecodeString(startKey)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return

Check warning on line 186 in server/api/region.go

View check run for this annotation

Codecov / codecov/patch

server/api/region.go#L185-L186

Added lines #L185 - L186 were not covered by tests
}
startKey = string(keyBytes)

keyBytes, err = hex.DecodeString(endKey)
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return

Check warning on line 193 in server/api/region.go

View check run for this annotation

Codecov / codecov/patch

server/api/region.go#L192-L193

Added lines #L192 - L193 were not covered by tests
}
endKey = string(keyBytes)
}

limit, err := h.AdjustLimit(r.URL.Query().Get("limit"))
if err != nil {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
Expand Down
29 changes: 29 additions & 0 deletions server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,35 @@ func (suite *getRegionTestSuite) TestScanRegionByKeys() {
for i, v := range regionIDs {
re.Equal(regions.Regions[i].ID, v)
}
url = fmt.Sprintf("%s/regions/key?key=%s&format=hex", suite.urlPrefix, hex.EncodeToString([]byte("b")))
regionIDs = []uint64{3, 4, 5, 99}
regions = &response.RegionsInfo{}
err = tu.ReadGetJSON(re, testDialClient, url, regions)
re.NoError(err)
re.Len(regionIDs, regions.Count)
for i, v := range regionIDs {
re.Equal(regions.Regions[i].ID, v)
}
url = fmt.Sprintf("%s/regions/key?key=%s&end_key=%s&format=hex",
suite.urlPrefix, hex.EncodeToString([]byte("b")), hex.EncodeToString([]byte("g")))
regionIDs = []uint64{3, 4}
regions = &response.RegionsInfo{}
err = tu.ReadGetJSON(re, testDialClient, url, regions)
re.NoError(err)
re.Len(regionIDs, regions.Count)
for i, v := range regionIDs {
re.Equal(regions.Regions[i].ID, v)
}
url = fmt.Sprintf("%s/regions/key?key=%s&end_key=%s&format=hex",
suite.urlPrefix, hex.EncodeToString([]byte("b")), hex.EncodeToString([]byte{0xFF, 0xFF, 0xCC}))
regionIDs = []uint64{3, 4, 5, 99}
regions = &response.RegionsInfo{}
err = tu.ReadGetJSON(re, testDialClient, url, regions)
re.NoError(err)
re.Len(regionIDs, regions.Count)
for i, v := range regionIDs {
re.Equal(regions.Regions[i].ID, v)
}
}

// Start a new test suite to prevent from being interfered by other tests.
Expand Down

0 comments on commit b173483

Please sign in to comment.