-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_map.go
67 lines (56 loc) · 1.09 KB
/
index_map.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
package raft
import (
"fmt"
"sync"
)
// raftIdIndexMap used for leader's matchIndex and nextIndex
type raftIdIndexMap struct {
mux sync.Mutex
m map[RaftId]uint64
}
func (m *raftIdIndexMap) Load(id RaftId) (index uint64, ok bool) {
m.mux.Lock()
defer m.mux.Unlock()
if m.m == nil {
m.m = map[RaftId]uint64{}
}
index, ok = m.m[id]
return index, ok
}
func (m *raftIdIndexMap) Store(id RaftId, index uint64) {
m.mux.Lock()
defer m.mux.Unlock()
if m.m == nil {
m.m = map[RaftId]uint64{}
}
m.m[id] = index
}
func (m *raftIdIndexMap) Range(fn func(id RaftId, index uint64) bool) {
m.mux.Lock()
defer m.mux.Unlock()
if m.m == nil {
m.m = map[RaftId]uint64{}
}
for id, index := range m.m {
ok := fn(id, index)
if !ok {
return
}
}
}
// neaten remove unused index
func (m *raftIdIndexMap) neaten(usedPeers []RaftPeer) {
m.mux.Lock()
defer m.mux.Unlock()
for id := range m.m {
if !includePeer(usedPeers, RaftPeer{Id: id}) {
delete(m.m, id)
}
}
}
// String
func (m *raftIdIndexMap) String() string {
m.mux.Lock()
defer m.mux.Unlock()
return fmt.Sprintf("%+v", m.m)
}