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

Add has method #3

Merged
merged 2 commits into from
Nov 1, 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
1 change: 1 addition & 0 deletions hook_debug.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build hashring_debug
// +build hashring_debug

package hashring
Expand Down
7 changes: 7 additions & 0 deletions hook_gtrace.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions hook_gtrace_stub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hook_nodebug.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !hashring_debug
// +build !hashring_debug

package hashring
Expand Down
10 changes: 10 additions & 0 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ func (r *Ring) Get(v Item) Item {
return item.(*point).bucket.item
}

func (r *Ring) Has(x Item) bool {
d := r.digest(x)

r.ringMu.RLock()
defer r.ringMu.RUnlock()

_, has := r.buckets[d]
return has
}

func (r *Ring) update(x Item, w float64) error {
id := r.digest(x)

Expand Down
24 changes: 24 additions & 0 deletions ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,30 @@ func TestRingCollisions(t *testing.T) {
}
}

func TestRingHas(t *testing.T) {
var ring Ring

if ring.Has(StringItem("server01")) {
t.Error("has server on empty ring")
}

ring.Insert(StringItem("server01"), 1.0)
if !ring.Has(StringItem("server01")) {
t.Error("failed to find server")
}
if ring.Has(StringItem("key")) {
t.Error("ring has not inserted key")
}

ring.Insert(StringItem("key"), 1.0)
if !ring.Has(StringItem("server01")) {
t.Error("failed to find server")
}
if !ring.Has(StringItem("key")) {
t.Error("failed to find key")
}
}

func applyActions(t testing.TB, r *Ring, actions ...ringAction) {
for _, a := range actions {
if err := a.apply(r); err != nil {
Expand Down
Loading