Skip to content

Commit

Permalink
Merge pull request #4 from hurd54/main
Browse files Browse the repository at this point in the history
fix for deleting the last element
  • Loading branch information
serejkus authored Dec 15, 2023
2 parents 7825400 + d87c843 commit 3806641
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
20 changes: 14 additions & 6 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,19 +329,27 @@ func (r *Ring) deletePoint(tree avl.Tree, p *point) (_ avl.Tree, removed bool) {
return tree, true
}

func (r *Ring) magicFactor() int {
func (r *Ring) magicFactor() float64 {
if m := r.MagicFactor; m > 0 {
return m
return float64(m)
}
return DefaultMagicFactor
}

// r.mu must be held.
func (r *Ring) rebuild() {
numPoints := line(
r.maxWeight, float64(r.magicFactor()),
r.minWeight, math.Ceil(float64(r.magicFactor())*(r.minWeight/r.maxWeight)),
func (r *Ring) numPoints() func(float64) int {
if r.maxWeight == 0 {
return func(float64) int { return 0 }
}
return line(
r.maxWeight, r.magicFactor(),
r.minWeight, math.Ceil(r.magicFactor())*(r.minWeight/r.maxWeight),
)
}

// r.mu must be held.
func (r *Ring) rebuild() {
numPoints := r.numPoints()

r.ringMu.RLock()
root := r.ring
Expand Down
23 changes: 23 additions & 0 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,29 @@ func TestRingDeleteNotExisting(t *testing.T) {
}
}

func TestRingInsertDeleteSequence(t *testing.T) {
var r Ring
x := StringItem("foo")
if err := r.Insert(x, 1); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := r.Delete(x); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if item := r.Get(x); item != nil {
t.Fatalf("unexpected item from empty ring")
}
if err := r.Insert(x, 1); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if item := r.Get(x); item == nil {
t.Fatalf("want item, but return empty")
}
if err := r.Delete(x); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}

func TestRingUpdateNotExisting(t *testing.T) {
var r Ring
x := StringItem("foo")
Expand Down

0 comments on commit 3806641

Please sign in to comment.