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

fix for deleting the last element #4

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading