-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuildergroup.go
74 lines (65 loc) · 2.26 KB
/
buildergroup.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
package ring
// BuilderGroup is a group within a builder; a group is a collection of nodes,
// and perhaps other groups.
type BuilderGroup struct {
builder *Builder
index int
info string
}
// Info returns the associated info string for the group; this is user-defined
// and not used directly by the builder.
func (g *BuilderGroup) Info() string {
return g.info
}
// SetInfo sets the associated info string for the group; this is user-defined
// and not used directly by the builder.
func (g *BuilderGroup) SetInfo(v string) {
g.info = v
}
// Parent returns the parent group, or nil if there is no parent, of the group.
func (g *BuilderGroup) Parent() *BuilderGroup {
parent := g.builder.ring.GroupToGroup[g.index]
if parent == 0 {
return nil
}
return g.builder.groups[parent]
}
// SetParent sets the parent group of this group; it may be nil to indicate
// this group is a top-level group.
func (g *BuilderGroup) SetParent(v *BuilderGroup) {
g.builder.ring.GroupToGroup[g.index] = v.index
}
// Nodes returns the slice of nodes within this group and all its child groups.
func (g *BuilderGroup) Nodes() []*BuilderNode {
nodes := []*BuilderNode{}
for node, group := range g.builder.ring.NodeToGroup {
if group == g.index {
nodes = append(nodes, g.builder.nodes[node])
}
}
return nodes
}
// AddNode will create a new node in the associated builder with this group set
// as the node's parent. Info is a user-defined string and is not used directly
// by the builder. Capacity specifies, relative to other nodes, how many
// assignments the node should have.
func (g *BuilderGroup) AddNode(info string, capacity int) *BuilderNode {
return g.builder.AddNode(info, capacity, g)
}
// Groups returns the slice of groups that are the direct children of this
// group.
func (g *BuilderGroup) Groups() []*BuilderGroup {
groups := []*BuilderGroup{}
for child, parent := range g.builder.ring.GroupToGroup {
if parent == g.index {
groups = append(groups, g.builder.groups[child])
}
}
return groups
}
// AddGroup adds a new group to the associated builder with this group set as
// the new group's parent. Info is a user-defined string and is not used
// directly by the builder.
func (g *BuilderGroup) AddGroup(info string) *BuilderGroup {
return g.builder.AddGroup(info, g)
}