Skip to content

Commit

Permalink
added method to check if key is present in ring
Browse files Browse the repository at this point in the history
  • Loading branch information
serejkus committed Nov 1, 2023
1 parent de247c2 commit b73fc2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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

0 comments on commit b73fc2d

Please sign in to comment.