-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
node.go
53 lines (44 loc) · 1.09 KB
/
node.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package block
import (
"github.com/oklog/ulid"
"github.com/thanos-io/thanos/pkg/block/metadata"
)
// Node type represents a node of a tree.
type Node struct {
metadata.Meta
Children []*Node
}
// NewNode creates a new node with children as empty slice.
func NewNode(meta *metadata.Meta) *Node {
return &Node{
Meta: *meta,
Children: []*Node{},
}
}
// getNonRootIDs returns list of ids which are not on root level.
func getNonRootIDs(root *Node) []ulid.ULID {
var ulids []ulid.ULID
for _, node := range root.Children {
ulids = append(ulids, childrenToULIDs(node)...)
ulids = remove(ulids, node.ULID)
}
return ulids
}
func childrenToULIDs(a *Node) []ulid.ULID {
var ulids = []ulid.ULID{a.ULID}
for _, childNode := range a.Children {
ulids = append(ulids, childrenToULIDs(childNode)...)
}
return ulids
}
func remove(items []ulid.ULID, item ulid.ULID) []ulid.ULID {
newitems := []ulid.ULID{}
for _, i := range items {
if i.Compare(item) != 0 {
newitems = append(newitems, i)
}
}
return newitems
}