-
Notifications
You must be signed in to change notification settings - Fork 0
/
ballots.go
66 lines (51 loc) · 1.14 KB
/
ballots.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 scp
import (
"bytes"
"github.com/google/btree"
)
type ballot struct {
slotIndex uint64
counter uint32
value Value
prepare *ratification
commit *ratification
}
func (b *ballot) Less(than btree.Item) bool {
return b.less(than.(*ballot))
}
func (b *ballot) less(ballot *ballot) bool {
if b.slotIndex < ballot.slotIndex {
return true
}
if b.slotIndex > ballot.slotIndex {
return false
}
if b.counter < ballot.counter {
return true
}
if b.counter > ballot.counter {
return false
}
return bytes.Compare(b.value, ballot.value) == -1
}
func (b *ballot) compatible(ballot *ballot) bool {
return bytes.Compare(b.value, ballot.value) == 0
}
type ballots struct {
btree *btree.BTree
}
func newBallots() ballots {
return ballots{
btree: btree.New(100),
}
}
func (b ballots) findOrCreate(slotIndex uint64, counter uint32, value Value, slices quorumSlices) *ballot {
bal := &ballot{slotIndex: slotIndex, counter: counter, value: value}
if item := b.btree.Get(bal); item != nil {
return item.(*ballot)
}
bal.prepare = newRatification()
bal.commit = newRatification()
b.btree.ReplaceOrInsert(bal)
return bal
}