forked from streamingfast/bstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork_tree.go
More file actions
180 lines (149 loc) · 2.99 KB
/
fork_tree.go
File metadata and controls
180 lines (149 loc) · 2.99 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package forkable
import (
"sort"
)
type ForkableError string
func (e ForkableError) Error() string { return string(e) }
const RootNotFound = ForkableError("root not found")
const NoLinkErr = ForkableError("no link")
func (db *ForkDB) BuildTree() (*Node, error) {
db.linksLock.Lock()
defer db.linksLock.Unlock()
root, err := db.Root()
if err != nil {
return nil, err
}
tree := db.buildTreeWithID(root)
return tree, nil
}
func (db *ForkDB) Root() (string, error) {
if !db.HasLIB() {
return "", RootNotFound
}
next := db.libRef.ID()
root := ""
for {
parent, found := db.links[next]
if found {
root = next
next = parent
continue
}
break
}
if root == "" {
return "", RootNotFound
}
return root, nil
}
func (n *Node) Chains() *ChainList {
chains := &ChainList{
Chains: [][]string{},
}
n.chains(nil, chains)
return chains
}
func (db *ForkDB) BuildTreeWithID(root string) *Node {
db.linksLock.Lock()
defer db.linksLock.Unlock()
return db.buildTreeWithID(root)
}
func (db *ForkDB) buildTreeWithID(root string) *Node {
rootNode := newNode(root)
rootNode.growBranches(db)
return rootNode
}
func (db *ForkDB) findChildren(parentID string) []string {
var children []string
for id, prevID := range db.links {
if prevID == parentID {
children = append(children, id)
}
}
sort.Strings(children)
return children
}
type Node struct {
ID string
Children []*Node
}
func newNode(id string) *Node {
return &Node{
ID: id,
}
}
func (n *Node) growBranches(db *ForkDB) {
children := db.findChildren(n.ID)
for _, childID := range children {
node := newNode(childID)
node.growBranches(db)
n.Children = append(n.Children, node)
}
}
func (n *Node) chains(current []string, out *ChainList) {
current = append(current, n.ID)
if len(n.Children) == 0 { //reach the leaf
out.Chains = append(out.Chains, current)
return
}
for _, child := range n.Children {
c := make([]string, len(current))
copy(c, current)
child.chains(c, out)
}
}
func (n *Node) Size() int {
if len(n.Children) == 0 {
return 0
}
return n.size(0)
}
func (n *Node) size(count int) int {
for _, child := range n.Children {
count = child.size(count)
}
count++
return count
}
type ChainList struct {
Chains [][]string
}
func (l *ChainList) LongestChain() []string {
count := 0
longestID := -1
longestLen := 0
for i, chain := range l.Chains {
if len(chain) == longestLen {
count++
}
if len(chain) > longestLen {
count = 1
longestLen = len(chain)
longestID = i
}
}
if count > 1 { // found multiple chain with same length
return nil
}
if len(l.Chains) > 0 {
return l.Chains[longestID]
}
return nil
}
func (db *ForkDB) Roots() ([]string, error) {
if len(db.links) == 0 {
return nil, NoLinkErr
}
roots := db.roots()
return roots, nil
}
func (db *ForkDB) roots() []string {
var roots []string
for blockID, prevID := range db.links {
if _, found := db.links[prevID]; !found {
roots = append(roots, blockID)
}
}
sort.Strings(roots)
return roots
}