-
Notifications
You must be signed in to change notification settings - Fork 5
/
point.go
66 lines (53 loc) · 1.26 KB
/
point.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package hashring
import "github.com/gobwas/avl"
// point represents a point on the ring.
// To handle collisions properly it may change its value to another one,
// increasing its generation by one.
type point struct {
// bucket is a bucket where point belongs to.
bucket *bucket
// index is a constant index of the point within bucket.
index int
// val is a current value of the point.
// It might be changed if point collides with another one.
val uint64
// stack holds a history of point values.
// It's non-nil only if point collides with another one.
stack []uint64
}
func newPoint(b *bucket, i int, v uint64) *point {
return &point{
bucket: b,
index: i,
val: v,
}
}
func (p *point) generation() int {
return len(p.stack)
}
func (p *point) proceed(v uint64) {
p.stack = append(p.stack, p.val)
p.val = v
}
func (p *point) rewind() {
n := len(p.stack)
p.val = p.stack[n-1]
p.stack = p.stack[:n-1]
}
func (p *point) value() uint64 {
return p.val
}
func (p *point) Compare(x avl.Item) int {
return compare(p.val, x.(*point).val)
}
type collision struct {
*point
}
func (c collision) Compare(x avl.Item) int {
p0 := c.point
p1 := x.(collision).point
if x := compare(p0.bucket.id, p1.bucket.id); x != 0 {
return x
}
return p0.index - p1.index
}