-
Notifications
You must be signed in to change notification settings - Fork 16
/
doublejump.go
190 lines (161 loc) · 3.58 KB
/
doublejump.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Package doublejump provides a revamped Google's jump consistent hash.
package doublejump
import (
"math/rand"
"github.com/dgryski/go-jump"
)
type looseHolder struct {
a []interface{}
m map[interface{}]int
f []int
}
func (holder *looseHolder) add(obj interface{}) {
if _, ok := holder.m[obj]; ok {
return
}
if nf := len(holder.f); nf == 0 {
holder.a = append(holder.a, obj)
holder.m[obj] = len(holder.a) - 1
} else {
idx := holder.f[nf-1]
holder.f = holder.f[:nf-1]
holder.a[idx] = obj
holder.m[obj] = idx
}
}
func (holder *looseHolder) remove(obj interface{}) {
if idx, ok := holder.m[obj]; ok {
holder.f = append(holder.f, idx)
holder.a[idx] = nil
delete(holder.m, obj)
}
}
func (holder *looseHolder) get(key uint64) interface{} {
na := len(holder.a)
if na == 0 {
return nil
}
h := jump.Hash(key, na)
return holder.a[h]
}
func (holder *looseHolder) shrink() {
if len(holder.f) == 0 {
return
}
var a []interface{}
for _, obj := range holder.a {
if obj != nil {
a = append(a, obj)
holder.m[obj] = len(a) - 1
}
}
holder.a = a
holder.f = nil
}
type compactHolder struct {
a []interface{}
m map[interface{}]int
}
func (holder *compactHolder) add(obj interface{}) {
if _, ok := holder.m[obj]; ok {
return
}
holder.a = append(holder.a, obj)
holder.m[obj] = len(holder.a) - 1
}
func (holder *compactHolder) shrink(a []interface{}) {
for i, obj := range a {
holder.a[i] = obj
holder.m[obj] = i
}
}
func (holder *compactHolder) remove(obj interface{}) {
if idx, ok := holder.m[obj]; ok {
n := len(holder.a)
holder.a[idx] = holder.a[n-1]
holder.m[holder.a[idx]] = idx
holder.a[n-1] = nil
holder.a = holder.a[:n-1]
delete(holder.m, obj)
}
}
func (holder *compactHolder) get(key uint64) interface{} {
na := len(holder.a)
if na == 0 {
return nil
}
h := jump.Hash(key*0xc6a4a7935bd1e995, na)
return holder.a[h]
}
// Hash is a revamped Google's jump consistent hash. It overcomes the shortcoming of
// the original implementation - being unable to remove nodes.
//
// Hash is NOT thread-safe.
type Hash struct {
loose looseHolder
compact compactHolder
}
// NewHash creates a new doublejump hash instance.
func NewHash() *Hash {
hash := &Hash{}
hash.loose.m = make(map[interface{}]int)
hash.compact.m = make(map[interface{}]int)
return hash
}
// Add adds an object to the hash.
func (h *Hash) Add(obj interface{}) {
if obj == nil {
return
}
h.loose.add(obj)
h.compact.add(obj)
}
// Remove removes an object from the hash.
func (h *Hash) Remove(obj interface{}) {
if obj == nil {
return
}
h.loose.remove(obj)
h.compact.remove(obj)
}
// Len returns the number of objects in the hash.
func (h *Hash) Len() int {
return len(h.compact.a)
}
// LooseLen returns the size of the inner loose object holder.
func (h *Hash) LooseLen() int {
return len(h.loose.a)
}
// Shrink removes all empty slots from the hash.
func (h *Hash) Shrink() {
h.loose.shrink()
h.compact.shrink(h.loose.a)
}
// Get returns the existing object for the key, or nil if there is no object in the hash.
func (h *Hash) Get(key uint64) interface{} {
obj := h.loose.get(key)
switch obj {
case nil:
return h.compact.get(key)
default:
return obj
}
}
// All returns all the objects in this Hash.
func (h *Hash) All() []interface{} {
n := len(h.compact.a)
if n == 0 {
return nil
}
all := make([]interface{}, n)
copy(all, h.compact.a)
return all
}
// Random returns a random object, or nil if there is no object in the hash.
func (h *Hash) Random() interface{} {
if n := len(h.compact.a); n > 0 {
idx := rand.Intn(n)
return h.compact.a[idx]
}
return nil
}