-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnominates.go
50 lines (39 loc) · 881 Bytes
/
nominates.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
package scp
import (
"bytes"
"github.com/google/btree"
)
type nominate struct {
value Value
*ratification
}
func (n *nominate) Less(than btree.Item) bool {
return bytes.Compare(n.value, than.(*nominate).value) == -1
}
type nominates struct {
btree *btree.BTree
}
func newNominates() nominates {
return nominates{btree.New(100)}
}
func (n nominates) findOrCreate(value Value, slices quorumSlices) *nominate {
nom := n.find(value)
if nom == nil {
nom = n.create(value, slices)
}
return nom
}
func (n nominates) find(value Value) *nominate {
if item := n.btree.Get(&nominate{value: value}); item != nil {
return item.(*nominate)
}
return nil
}
func (n nominates) create(value Value, slices quorumSlices) *nominate {
nominate := &nominate{
value: value,
ratification: newRatification(),
}
n.btree.ReplaceOrInsert(nominate)
return nominate
}