Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*: Merge adjacent handles when building key range #1696

Merged
merged 3 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
*: Merge adjacent handles when building key range
  • Loading branch information
shenli committed Sep 6, 2016
commit 1ff3323a141868a8bda21941fc76eeb268108889
62 changes: 62 additions & 0 deletions executor/executor_pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package executor

import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/tablecodec"
)

var _ = Suite(&testExecSuite{})

type testExecSuite struct {
}

type handleRange struct {
start int64
end int64
}

func getExpectedRanges(tid int64, hrs []*handleRange) []kv.KeyRange {
krs := make([]kv.KeyRange, 0, len(hrs))
for _, hr := range hrs {
startKey := tablecodec.EncodeRowKeyWithHandle(tid, hr.start)
endKey := tablecodec.EncodeRowKeyWithHandle(tid, hr.end)
krs = append(krs, kv.KeyRange{StartKey: startKey, EndKey: endKey})
}
return krs
}

func (s *testExecSuite) TestMergeHandles(c *C) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this empty line.

handles := []int64{0, 2, 3, 4, 5, 10, 11, 100}

hrs := make([]*handleRange, 0, len(handles))
hrs = append(hrs, &handleRange{start: 0, end: 1})
hrs = append(hrs, &handleRange{start: 2, end: 6})
hrs = append(hrs, &handleRange{start: 10, end: 12})
hrs = append(hrs, &handleRange{start: 100, end: 101})

expectedKrs := getExpectedRanges(1, hrs)

krs := tableHandlesToKVRanges(1, handles)

c.Assert(len(krs), Equals, len(expectedKrs))
for i, kr := range krs {
ekr := expectedKrs[i]
c.Assert(kr.StartKey, DeepEquals, ekr.StartKey)
c.Assert(kr.EndKey, DeepEquals, ekr.EndKey)
}
}
21 changes: 19 additions & 2 deletions executor/executor_xapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,33 @@ func tableRangesToKVRanges(tid int64, tableRanges []plan.TableRange) []kv.KeyRan
return krs
}

/*
* Convert sorted handle to kv ranges.
* For continuous handles, we should merge them to a single key range.
*/
func tableHandlesToKVRanges(tid int64, handles []int64) []kv.KeyRange {
krs := make([]kv.KeyRange, 0, len(handles))
for _, h := range handles {
i := 0
for i < len(handles) {
h := handles[i]
if h == math.MaxInt64 {
// We can't convert MaxInt64 into an left closed, right open range.
i++
continue
}
j := i + 1
endHandle := h + 1
for ; j < len(handles); j++ {
if handles[j] == endHandle {
endHandle = handles[j] + 1
continue
}
break
}
startKey := tablecodec.EncodeRowKeyWithHandle(tid, h)
endKey := tablecodec.EncodeRowKeyWithHandle(tid, h+1)
endKey := tablecodec.EncodeRowKeyWithHandle(tid, endHandle)
krs = append(krs, kv.KeyRange{StartKey: startKey, EndKey: endKey})
i = j
}
return krs
}
Expand Down