This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
types.go
81 lines (64 loc) · 1.79 KB
/
types.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
package balancer
import (
"sort"
"github.com/featurebasedb/featurebase/v3/dax"
)
// jobSetDiffs is used internally to capture the diffs as they're happening. We
// call output() to generate the final result.
type jobSetDiffs struct {
added dax.Set[dax.Job]
removed dax.Set[dax.Job]
}
func newJobSetDiffs() jobSetDiffs {
return jobSetDiffs{
added: dax.NewSet[dax.Job](),
removed: dax.NewSet[dax.Job](),
}
}
type InternalDiffs map[dax.Address]jobSetDiffs
func NewInternalDiffs() InternalDiffs {
return make(InternalDiffs)
}
func (d InternalDiffs) Added(address dax.Address, job dax.Job) {
if _, ok := d[address]; !ok {
d[address] = newJobSetDiffs()
}
// Before adding the job, make sure we haven't indicated that it has been
// removed prior to this. If it has, we need to invalidate that "remove"
// instruction.
d[address].removed.Remove(job)
d[address].added.Add(job)
}
func (d InternalDiffs) Removed(address dax.Address, job dax.Job) {
if _, ok := d[address]; !ok {
d[address] = newJobSetDiffs()
}
// Before removing the job, make sure we haven't indicated that it has been
// added prior to this. If it has, we need to invalidate that "add"
// instruction.
d[address].added.Remove(job)
d[address].removed.Add(job)
}
func (d InternalDiffs) Merge(d2 InternalDiffs) {
for k, v := range d2 {
if _, ok := d[k]; !ok {
d[k] = newJobSetDiffs()
}
d[k].added.Merge(v.added)
d[k].removed.Merge(v.removed)
}
}
// Output converts internalDiff to []controller.WorkerDiff for external
// consumption.
func (d InternalDiffs) Output() []dax.WorkerDiff {
out := make([]dax.WorkerDiff, len(d))
i := 0
for k, v := range d {
out[i].Address = k
out[i].AddedJobs = v.added.Sorted()
out[i].RemovedJobs = v.removed.Sorted()
i++
}
sort.Sort(dax.WorkerDiffs(out))
return out
}