-
Notifications
You must be signed in to change notification settings - Fork 1
/
creation_tree.go
179 lines (141 loc) · 4.38 KB
/
creation_tree.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
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
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package codec
import (
"fmt"
"github.com/pinax-network/firehose-antelope/codec/antelope"
"strings"
)
type nodes []*node
type node struct {
kind string
actionIndex int
children []*node
}
func computeCreationTree(ops []*creationOp) (nodes, error) {
if len(ops) <= 0 {
return nil, nil
}
actionIndex := -1
opsMap := creationOpsToMap(ops)
var roots []*node
opKinds, ok := opsMap[actionIndex+1]
for ok {
if opKinds[0] != "ROOT" {
return nil, fmt.Errorf("first exec op kind of execution start should be ROOT, got %s", opKinds[0])
}
root := &node{"ROOT", -1, nil}
roots = append(roots, root)
executeAction(&actionIndex, root, opsMap)
opKinds, ok = opsMap[actionIndex+1]
// TODO: We should check for gaps in action indices here. Assume an exec ops
// list of `[{ROOT, 0}, {NOTIFY, 1}, {ROOT, 2}]`. In this list, we would
// create a ROOT #0, skip NOTIFY then try to execute ROOT #2. This is incorrect
// and there is a gap, i.e. there is an action index lower than next 2 that is
// not part of previous tree. How exactly we would do it is unsure, but that would
// add a validation step that everything is kosher.
}
return roots, nil
}
func executeAction(
actionIndex *int,
root *node,
opsMap map[int][]string,
) {
*actionIndex++
root.actionIndex = *actionIndex
notifies, cfas, inlines := recordChildCreationOp(root, opsMap[root.actionIndex])
for i := 0; i < len(notifies); i++ {
nestedNotifies, nestedCfas, nestedInlines := executeNotify(actionIndex, notifies[i], opsMap)
notifies = append(notifies, nestedNotifies...)
cfas = append(cfas, nestedCfas...)
inlines = append(inlines, nestedInlines...)
}
for _, cfa := range cfas {
executeAction(actionIndex, cfa, opsMap)
}
for _, inline := range inlines {
executeAction(actionIndex, inline, opsMap)
}
}
func executeNotify(
actionIndex *int,
root *node,
opsMap map[int][]string,
) (notifies []*node, cfas []*node, inlines []*node) {
*actionIndex++
root.actionIndex = *actionIndex
return recordChildCreationOp(root, opsMap[root.actionIndex])
}
func recordChildCreationOp(root *node, opKinds []string) (notifies []*node, cfas []*node, inlines []*node) {
for _, opKind := range opKinds {
if opKind == "ROOT" {
continue
}
child := &node{opKind, -1, nil}
switch opKind {
case "NOTIFY":
notifies = append(notifies, child)
case "CFA_INLINE":
cfas = append(cfas, child)
case "INLINE":
inlines = append(inlines, child)
}
root.children = append(root.children, child)
}
return
}
func creationOpsToMap(ops []*creationOp) map[int][]string {
mapping := map[int][]string{}
for _, op := range ops {
mapping[op.actionIndex] = append(mapping[op.actionIndex], op.kind)
}
return mapping
}
func toFlatTree(roots ...*node) antelope.CreationFlatTree {
var tree antelope.CreationFlatTree
walkIndex := -1
for _, root := range roots {
walkIndex++
tree = append(tree, _toFlatTree(root, -1, &walkIndex)...)
}
return tree
}
func _toFlatTree(root *node, parentIndex int, walkIndex *int) (tree antelope.CreationFlatTree) {
tree = append(tree, [3]int{*walkIndex, parentIndex, root.actionIndex})
childRootIndex := *walkIndex
for _, child := range root.children {
*walkIndex++
tree = append(tree, _toFlatTree(child, childRootIndex, walkIndex)...)
}
return
}
func (nodes nodes) Stringer() string {
builder := &strings.Builder{}
for _, node := range nodes {
node.toString(builder, "")
}
return builder.String()
}
func (node *node) Stringer() string {
builder := &strings.Builder{}
node.toString(builder, "")
return builder.String()
}
func (node *node) toString(builder *strings.Builder, spacing string) {
fmt.Fprintf(builder, "%s (%d, %s)\n", spacing, node.actionIndex, node.kind)
for _, child := range node.children {
child.toString(builder, spacing+" ")
}
}