From 138c3f9e29ec8da06f763ed2f9927be863e42f74 Mon Sep 17 00:00:00 2001 From: bufferflies <1045931706@qq.com> Date: Thu, 12 May 2022 09:57:50 +0800 Subject: [PATCH] rename ring item Signed-off-by: bufferflies <1045931706@qq.com> --- .../{bucket => buckets}/bucket_tree.go | 20 +++++++++---------- .../{bucket => buckets}/bucket_tree_test.go | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) rename server/statistics/{bucket => buckets}/bucket_tree.go (88%) rename server/statistics/{bucket => buckets}/bucket_tree_test.go (95%) diff --git a/server/statistics/bucket/bucket_tree.go b/server/statistics/buckets/bucket_tree.go similarity index 88% rename from server/statistics/bucket/bucket_tree.go rename to server/statistics/buckets/bucket_tree.go index b09c863c052c..765f10b025b4 100644 --- a/server/statistics/bucket/bucket_tree.go +++ b/server/statistics/buckets/bucket_tree.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package bucket +package buckets import ( "bytes" @@ -32,13 +32,13 @@ func NewRing(degree int) *Ring { } } -// RingItem is a ring item. -type RingItem interface { +// BucketItem is a ring item. +type BucketItem interface { Less(than btree.Item) bool StartKey() []byte EndKey() []byte // Debris returns the debris after replacing the key range. - Debris(startKey, endKey []byte) []RingItem + Debris(startKey, endKey []byte) []BucketItem String() string } @@ -51,12 +51,12 @@ func (r *Ring) Len() int { // cache key range: |001-----100|100-----200| // request key range: |005-----120| // return items: |001-----100|100-----200| -func (r *Ring) GetRange(item RingItem) []RingItem { - var res []RingItem +func (r *Ring) GetRange(item BucketItem) []BucketItem { + var res []BucketItem - var first RingItem + var first BucketItem r.tree.DescendLessOrEqual(item, func(i btree.Item) bool { - first = i.(RingItem) + first = i.(BucketItem) return false }) @@ -69,7 +69,7 @@ func (r *Ring) GetRange(item RingItem) []RingItem { // find the next item util the item greater than end key. r.tree.AscendGreaterOrEqual(first, func(i btree.Item) bool { - ringItem := i.(RingItem) + ringItem := i.(BucketItem) if len(item.EndKey()) > 0 && bytes.Compare(ringItem.StartKey(), item.EndKey()) >= 0 { return false } @@ -80,7 +80,7 @@ func (r *Ring) GetRange(item RingItem) []RingItem { } // Put puts a new item into the ring. -func (r *Ring) Put(item RingItem) { +func (r *Ring) Put(item BucketItem) { overlaps := r.GetRange(item) for _, overlap := range overlaps { r.tree.Delete(overlap) diff --git a/server/statistics/bucket/bucket_tree_test.go b/server/statistics/buckets/bucket_tree_test.go similarity index 95% rename from server/statistics/bucket/bucket_tree_test.go rename to server/statistics/buckets/bucket_tree_test.go index 202ea6a385a5..93147d008bc2 100644 --- a/server/statistics/bucket/bucket_tree_test.go +++ b/server/statistics/buckets/bucket_tree_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package bucket +package buckets import ( "bytes" @@ -51,13 +51,13 @@ func (s *simpleBucketItem) String() string { // Less returns true if the start key of the item is less than the start key of the argument. func (s *simpleBucketItem) Less(than btree.Item) bool { - return bytes.Compare(s.StartKey(), than.(RingItem).StartKey()) < 0 + return bytes.Compare(s.StartKey(), than.(BucketItem).StartKey()) < 0 } // Debris returns the debris of the item. // details: https://leetcode.cn/problems/interval-list-intersections/ -func (s simpleBucketItem) Debris(startKey, endKey []byte) []RingItem { - var res []RingItem +func (s simpleBucketItem) Debris(startKey, endKey []byte) []BucketItem { + var res []BucketItem left := maxKey(startKey, s.startKey) right := minKey(endKey, s.endKey) @@ -135,7 +135,7 @@ func (bs *testBucketSuite) TestRingPutItem(c *C) { func (bs *testBucketSuite) TestDebris(c *C) { ringItem := newSimpleBucketItem([]byte("010"), []byte("090")) - var overlaps []RingItem + var overlaps []BucketItem overlaps = ringItem.Debris([]byte("000"), []byte("100")) c.Assert(overlaps, HasLen, 0) overlaps = ringItem.Debris([]byte("000"), []byte("080"))