diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index be32817c..811fc677 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.18 - name: Test run: go test ./... @@ -28,7 +28,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: 1.18 - name: Test with race run: go test -race ./... diff --git a/internal/locate/sorted_btree.go b/internal/locate/sorted_btree.go index 933332e0..c72bed67 100644 --- a/internal/locate/sorted_btree.go +++ b/internal/locate/sorted_btree.go @@ -32,9 +32,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build !go1.18 -// +build !go1.18 - package locate import ( @@ -43,31 +40,31 @@ import ( "github.com/google/btree" ) -// SortedRegionsRegions is a sorted btree. +// SortedRegions is a sorted btree. type SortedRegions struct { - b *btree.BTree + b *btree.BTreeG[*btreeItem] } // NewSortedRegions returns a new SortedRegions. func NewSortedRegions(btreeDegree int) *SortedRegions { return &SortedRegions{ - b: btree.New(btreeDegree), + b: btree.NewG(btreeDegree, func(a, b *btreeItem) bool { return a.Less(b) }), } } // ReplaceOrInsert inserts a new item into the btree. func (s *SortedRegions) ReplaceOrInsert(cachedRegion *Region) *Region { - old := s.b.ReplaceOrInsert(newBtreeItem(cachedRegion)) + old, _ := s.b.ReplaceOrInsert(newBtreeItem(cachedRegion)) if old != nil { - return old.(*btreeItem).cachedRegion + return old.cachedRegion } return nil } // DescendLessOrEqual returns all items that are less than or equal to the key. func (s *SortedRegions) DescendLessOrEqual(key []byte, isEndKey bool, ts int64) (r *Region) { - s.b.DescendLessOrEqual(newBtreeSearchItem(key), func(item btree.Item) bool { - r = item.(*btreeItem).cachedRegion + s.b.DescendLessOrEqual(newBtreeSearchItem(key), func(item *btreeItem) bool { + r = item.cachedRegion if isEndKey && bytes.Equal(r.StartKey(), key) { r = nil // clear result return true // iterate next item @@ -83,8 +80,8 @@ func (s *SortedRegions) DescendLessOrEqual(key []byte, isEndKey bool, ts int64) // AscendGreaterOrEqual returns all items that are greater than or equal to the key. func (s *SortedRegions) AscendGreaterOrEqual(startKey, endKey []byte, limit int) (regions []*Region) { - s.b.AscendGreaterOrEqual(newBtreeSearchItem(startKey), func(item btree.Item) bool { - region := item.(*btreeItem).cachedRegion + s.b.AscendGreaterOrEqual(newBtreeSearchItem(startKey), func(item *btreeItem) bool { + region := item.cachedRegion if len(endKey) > 0 && bytes.Compare(region.StartKey(), endKey) >= 0 { return false } @@ -101,8 +98,8 @@ func (s *SortedRegions) Clear() { // ValidRegionsInBtree returns the number of valid regions in the btree. func (s *SortedRegions) ValidRegionsInBtree(ts int64) (len int) { - s.b.Descend(func(item btree.Item) bool { - r := item.(*btreeItem).cachedRegion + s.b.Descend(func(item *btreeItem) bool { + r := item.cachedRegion if !r.checkRegionCacheTTL(ts) { return true } diff --git a/internal/locate/sorted_btree_generic.go b/internal/locate/sorted_btree_generic.go deleted file mode 100644 index 02ca4cd5..00000000 --- a/internal/locate/sorted_btree_generic.go +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2022 TiKV Authors -// -// 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, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// NOTE: The code in this file is based on code from the -// TiDB project, licensed under the Apache License v 2.0 -// -// https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/locate/region_request.go -// - -// 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, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//go:build go1.18 -// +build go1.18 - -package locate - -import ( - "bytes" - - "github.com/google/btree" -) - -// SortedRegions is a sorted btree. -type SortedRegions struct { - b *btree.BTreeG[*btreeItem] -} - -// NewSortedRegions returns a new SortedRegions. -func NewSortedRegions(btreeDegree int) *SortedRegions { - return &SortedRegions{ - b: btree.NewG(btreeDegree, func(a, b *btreeItem) bool { return a.Less(b) }), - } -} - -// ReplaceOrInsert inserts a new item into the btree. -func (s *SortedRegions) ReplaceOrInsert(cachedRegion *Region) *Region { - old, _ := s.b.ReplaceOrInsert(newBtreeItem(cachedRegion)) - if old != nil { - return old.cachedRegion - } - return nil -} - -// DescendLessOrEqual returns all items that are less than or equal to the key. -func (s *SortedRegions) DescendLessOrEqual(key []byte, isEndKey bool, ts int64) (r *Region) { - s.b.DescendLessOrEqual(newBtreeSearchItem(key), func(item *btreeItem) bool { - r = item.cachedRegion - if isEndKey && bytes.Equal(r.StartKey(), key) { - r = nil // clear result - return true // iterate next item - } - if !r.checkRegionCacheTTL(ts) { - r = nil - return true - } - return false - }) - return r -} - -// AscendGreaterOrEqual returns all items that are greater than or equal to the key. -func (s *SortedRegions) AscendGreaterOrEqual(startKey, endKey []byte, limit int) (regions []*Region) { - s.b.AscendGreaterOrEqual(newBtreeSearchItem(startKey), func(item *btreeItem) bool { - region := item.cachedRegion - if len(endKey) > 0 && bytes.Compare(region.StartKey(), endKey) >= 0 { - return false - } - regions = append(regions, region) - return len(regions) < limit - }) - return regions -} - -// Clear removes all items from the btree. -func (s *SortedRegions) Clear() { - s.b.Clear(false) -} - -// ValidRegionsInBtree returns the number of valid regions in the btree. -func (s *SortedRegions) ValidRegionsInBtree(ts int64) (len int) { - s.b.Descend(func(item *btreeItem) bool { - r := item.cachedRegion - if !r.checkRegionCacheTTL(ts) { - return true - } - len++ - return true - }) - return -}