1
- package flux
1
+ package operation
2
2
3
3
import (
4
4
"time"
5
5
6
+ "github.com/influxdata/flux"
6
7
"github.com/influxdata/flux/codes"
7
8
"github.com/influxdata/flux/internal/errors"
9
+ "github.com/influxdata/flux/interpreter"
8
10
)
9
11
12
+ // Node denotes a single operation in a query.
13
+ type Node struct {
14
+ ID NodeID `json:"id"`
15
+ Spec flux.OperationSpec `json:"spec"`
16
+ Source NodeSource `json:"source"`
17
+ }
18
+
19
+ // NodeSource specifies the source location that created
20
+ // an operation.
21
+ type NodeSource struct {
22
+ Stack []interpreter.StackEntry `json:"stack"`
23
+ }
24
+
25
+ // NodeID is a unique ID within a query for the operation.
26
+ type NodeID string
27
+
10
28
// Spec specifies a query.
11
29
type Spec struct {
12
- Operations []* Operation `json:"operations"`
13
- Edges []Edge `json:"edges"`
14
- Resources ResourceManagement `json:"resources"`
15
- Now time.Time `json:"now"`
16
-
17
- sorted []* Operation
18
- children map [OperationID ][]* Operation
19
- parents map [OperationID ][]* Operation
30
+ Operations []* Node `json:"operations"`
31
+ Edges []Edge `json:"edges"`
32
+ Resources flux. ResourceManagement `json:"resources"`
33
+ Now time.Time `json:"now"`
34
+
35
+ sorted []* Node
36
+ children map [NodeID ][]* Node
37
+ parents map [NodeID ][]* Node
20
38
}
21
39
22
40
// Edge is a data flow relationship between a parent and a child
23
41
type Edge struct {
24
- Parent OperationID `json:"parent"`
25
- Child OperationID `json:"child"`
42
+ Parent NodeID `json:"parent"`
43
+ Child NodeID `json:"child"`
26
44
}
27
45
28
46
// Walk calls f on each operation exactly once.
29
47
// The function f will be called on an operation only after
30
48
// all of its parents have already been passed to f.
31
- func (q * Spec ) Walk (f func (o * Operation ) error ) error {
49
+ func (q * Spec ) Walk (f func (o * Node ) error ) error {
32
50
if len (q .sorted ) == 0 {
33
51
if err := q .prepare (); err != nil {
34
52
return err
@@ -53,7 +71,7 @@ func (q *Spec) Validate() error {
53
71
54
72
// Children returns a list of children for a given operation.
55
73
// If the query is invalid no children will be returned.
56
- func (q * Spec ) Children (id OperationID ) []* Operation {
74
+ func (q * Spec ) Children (id NodeID ) []* Node {
57
75
if q .children == nil {
58
76
err := q .prepare ()
59
77
if err != nil {
@@ -65,7 +83,7 @@ func (q *Spec) Children(id OperationID) []*Operation {
65
83
66
84
// Parents returns a list of parents for a given operation.
67
85
// If the query is invalid no parents will be returned.
68
- func (q * Spec ) Parents (id OperationID ) []* Operation {
86
+ func (q * Spec ) Parents (id NodeID ) []* Node {
69
87
if q .parents == nil {
70
88
err := q .prepare ()
71
89
if err != nil {
@@ -91,23 +109,23 @@ func (q *Spec) prepare() error {
91
109
q .parents = parents
92
110
q .children = children
93
111
94
- tMarks := make (map [OperationID ]bool )
95
- pMarks := make (map [OperationID ]bool )
112
+ tMarks := make (map [NodeID ]bool )
113
+ pMarks := make (map [NodeID ]bool )
96
114
97
115
for _ , r := range roots {
98
116
if err := q .visit (tMarks , pMarks , r ); err != nil {
99
117
return err
100
118
}
101
119
}
102
- //reverse q.sorted
120
+ // reverse q.sorted
103
121
for i , j := 0 , len (q .sorted )- 1 ; i < j ; i , j = i + 1 , j - 1 {
104
122
q .sorted [i ], q .sorted [j ] = q .sorted [j ], q .sorted [i ]
105
123
}
106
124
return nil
107
125
}
108
126
109
- func (q * Spec ) computeLookup () (map [OperationID ] * Operation , error ) {
110
- lookup := make (map [OperationID ] * Operation , len (q .Operations ))
127
+ func (q * Spec ) computeLookup () (map [NodeID ] * Node , error ) {
128
+ lookup := make (map [NodeID ] * Node , len (q .Operations ))
111
129
for _ , o := range q .Operations {
112
130
if _ , ok := lookup [o .ID ]; ok {
113
131
return nil , errors .Newf (codes .Internal , "found duplicate operation ID %q" , o .ID )
@@ -117,13 +135,13 @@ func (q *Spec) computeLookup() (map[OperationID]*Operation, error) {
117
135
return lookup , nil
118
136
}
119
137
120
- func (q * Spec ) determineParentsChildrenAndRoots () (parents , children map [OperationID ][]* Operation , roots []* Operation , _ error ) {
138
+ func (q * Spec ) determineParentsChildrenAndRoots () (parents , children map [NodeID ][]* Node , roots []* Node , _ error ) {
121
139
lookup , err := q .computeLookup ()
122
140
if err != nil {
123
141
return nil , nil , nil , err
124
142
}
125
- children = make (map [OperationID ][]* Operation , len (q .Operations ))
126
- parents = make (map [OperationID ][]* Operation , len (q .Operations ))
143
+ children = make (map [NodeID ][]* Node , len (q .Operations ))
144
+ parents = make (map [NodeID ][]* Node , len (q .Operations ))
127
145
for _ , e := range q .Edges {
128
146
// Build children map
129
147
c , ok := lookup [e .Child ]
@@ -150,7 +168,7 @@ func (q *Spec) determineParentsChildrenAndRoots() (parents, children map[Operati
150
168
151
169
// Depth first search topological sorting of a DAG.
152
170
// https://en.wikipedia.org/wiki/Topological_sorting#Algorithms
153
- func (q * Spec ) visit (tMarks , pMarks map [OperationID ]bool , o * Operation ) error {
171
+ func (q * Spec ) visit (tMarks , pMarks map [NodeID ]bool , o * Node ) error {
154
172
id := o .ID
155
173
if tMarks [id ] {
156
174
return errors .New (codes .Invalid , "found cycle in query" )
@@ -173,7 +191,7 @@ func (q *Spec) visit(tMarks, pMarks map[OperationID]bool, o *Operation) error {
173
191
// Functions return the names of all functions used in the plan
174
192
func (q * Spec ) Functions () ([]string , error ) {
175
193
funcs := []string {}
176
- err := q .Walk (func (o * Operation ) error {
194
+ err := q .Walk (func (o * Node ) error {
177
195
funcs = append (funcs , string (o .Spec .Kind ()))
178
196
return nil
179
197
})
0 commit comments