-
Notifications
You must be signed in to change notification settings - Fork 35
/
cell.go
150 lines (131 loc) · 3.36 KB
/
cell.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
package beehive
import "fmt"
// CellKey represents a key in a dictionary.
type CellKey struct {
Dict string
Key string
}
// AppCellKey represents a key in a dictionary of a specific app.
type AppCellKey struct {
App string
Dict string
Key string
}
// Cell returns the CellKey of this AppCellKey.
func (ack AppCellKey) Cell() CellKey {
return CellKey{
Dict: ack.Dict,
Key: ack.Key,
}
}
// IsNil returns whether the AppCellKey represents no cells.
func (ack AppCellKey) IsNil() bool {
return ack.App == ""
}
// MappedCells is the list of dictionary keys returned by the map functions.
type MappedCells []CellKey
func (mc MappedCells) String() string {
return fmt.Sprintf("Cells{%v}", []CellKey(mc))
}
func (mc MappedCells) Len() int { return len(mc) }
func (mc MappedCells) Swap(i, j int) { mc[i], mc[j] = mc[j], mc[i] }
func (mc MappedCells) Less(i, j int) bool {
return mc[i].Dict < mc[j].Dict ||
(mc[i].Dict == mc[j].Dict && mc[i].Key < mc[j].Key)
}
// LocalBroadcast returns whether the mapped cells indicate a local broadcast.
// An empty set means a local broadcast of message. Note that nil means drop.
func (mc MappedCells) LocalBroadcast() bool {
return len(mc) == 0
}
type cellStore struct {
// colonyid -> term
Colonies map[uint64]uint64
// appname -> dict -> key -> colony
CellBees map[string]map[string]map[string]Colony
// beeid -> dict -> key
BeeCells map[uint64]map[string]map[string]struct{}
}
func newCellStore() cellStore {
return cellStore{
Colonies: make(map[uint64]uint64),
CellBees: make(map[string]map[string]map[string]Colony),
BeeCells: make(map[uint64]map[string]map[string]struct{}),
}
}
func (s *cellStore) assign(app string, k CellKey, c Colony) {
s.assignCellBees(app, k, c)
s.assignBeeCells(app, k, c)
}
func (s *cellStore) assignCellBees(app string, k CellKey, c Colony) {
dicts, ok := s.CellBees[app]
if !ok {
dicts = make(map[string]map[string]Colony)
s.CellBees[app] = dicts
}
keys, ok := dicts[k.Dict]
if !ok {
keys = make(map[string]Colony)
dicts[k.Dict] = keys
}
keys[k.Key] = c
}
func (s *cellStore) assignBeeCells(app string, k CellKey, c Colony) {
dicts, ok := s.BeeCells[c.Leader]
if !ok {
dicts = make(map[string]map[string]struct{})
s.BeeCells[c.Leader] = dicts
}
keys, ok := dicts[k.Dict]
if !ok {
keys = make(map[string]struct{})
dicts[k.Dict] = keys
}
keys[k.Key] = struct{}{}
}
func (s *cellStore) colony(app string, cell CellKey) (c Colony, ok bool) {
dicts, ok := s.CellBees[app]
if !ok {
return Colony{}, false
}
keys, ok := dicts[cell.Dict]
if !ok {
return Colony{}, false
}
c, ok = keys[cell.Key]
return c, ok
}
func (s *cellStore) cells(bee uint64) MappedCells {
dicts, ok := s.BeeCells[bee]
if !ok {
return nil
}
c := make(MappedCells, 0, len(dicts))
for d, dict := range dicts {
for k := range dict {
c = append(c, CellKey{Dict: d, Key: k})
}
}
return c
}
func (s *cellStore) updateColony(app string, oldc Colony, newc Colony,
term uint64) error {
if t, ok := s.Colonies[newc.ID]; ok {
if term < t {
return fmt.Errorf("cellstore: colony is older than %v", t)
}
}
s.Colonies[newc.ID] = term
bcells := s.BeeCells[oldc.Leader]
if oldc.Leader != newc.Leader {
s.BeeCells[newc.Leader] = bcells
delete(s.BeeCells, oldc.Leader)
}
acells := s.CellBees[app]
for d, dict := range bcells {
for k := range dict {
acells[d][k] = newc
}
}
return nil
}