Skip to content

Commit b74333d

Browse files
fubhytim-smart
andauthored
Replace existing HashRing nodes when primary keys match (#6911)
Co-authored-by: Tim Smart <[email protected]>
1 parent 34ced69 commit b74333d

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎.changeset/brave-rings-update.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Update existing `HashRing` nodes when adding a value with the same primary key.

‎packages/effect/src/HashRing.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export const addMany: {
145145
const key = PrimaryKey.value(node)
146146
const entry = self.nodes.get(key)
147147
if (entry) {
148+
entry[0] = node
148149
if (entry[1] === weight) continue
149150
toRemove ??= new Set()
150151
toRemove.add(key)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { assert, describe, it } from "@effect/vitest"
2+
import * as HashRing from "effect/HashRing"
3+
import * as PrimaryKey from "effect/PrimaryKey"
4+
5+
describe("HashRing", () => {
6+
it("updates the stored node when adding the same primary key", () => {
7+
const first = {
8+
name: "first",
9+
[PrimaryKey.symbol]() {
10+
return "node"
11+
}
12+
}
13+
const updated = {
14+
name: "updated",
15+
[PrimaryKey.symbol]() {
16+
return "node"
17+
}
18+
}
19+
const ring = HashRing.make<typeof first>()
20+
21+
HashRing.add(ring, first)
22+
HashRing.add(ring, updated)
23+
24+
assert.strictEqual(HashRing.get(ring, "request"), updated)
25+
})
26+
27+
it("updates the stored node when changing its weight", () => {
28+
const first = {
29+
name: "first",
30+
[PrimaryKey.symbol]() {
31+
return "node"
32+
}
33+
}
34+
const updated = {
35+
name: "updated",
36+
[PrimaryKey.symbol]() {
37+
return "node"
38+
}
39+
}
40+
const ring = HashRing.make<typeof first>()
41+
42+
HashRing.add(ring, first)
43+
HashRing.add(ring, updated, { weight: 2 })
44+
45+
assert.strictEqual(HashRing.get(ring, "request"), updated)
46+
})
47+
})

0 commit comments

Comments
 (0)