-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
114 lines (94 loc) · 2.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
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
package ssp
import (
"github.com/affo/ssp/values"
)
type Node interface {
Do(collector Collector, v values.Value) error
Out() *Arch
Clone() Node
// Options.
SetParallelism(par int) Node
GetParallelism() int
SetName(name string) Node
GetName() string
}
type baseNode struct {
par int
name string
}
func newBaseNode() baseNode {
return baseNode{par: 1}
}
func (n baseNode) GetParallelism() int {
return n.par
}
func (n baseNode) GetName() string {
return n.name
}
func (n baseNode) String() string {
return n.name
}
func (n baseNode) Clone() baseNode {
return baseNode{
par: n.par,
name: n.name,
}
}
type NodeFunc func(state values.Value, collector Collector, v values.Value) (values.Value, error)
type AnonymousNode struct {
baseNode
state0 values.Value
state values.Value
do NodeFunc
}
func NewNode(do func(collector Collector, v values.Value) error) *AnonymousNode {
return NewStatefulNode(
values.NewNull(values.Int64),
func(state values.Value, collector Collector, v values.Value) (value values.Value, e error) {
return state, do(collector, v)
},
)
}
func NewStatefulNode(state0 values.Value, do NodeFunc) *AnonymousNode {
return &AnonymousNode{
baseNode: newBaseNode(),
state0: state0,
state: state0,
do: do,
}
}
func (n *AnonymousNode) Do(collector Collector, v values.Value) error {
s, err := n.do(n.state, collector, v)
if err != nil {
return err
}
n.state = s
return nil
}
func (n *AnonymousNode) Out() *Arch {
return NewLink(n)
}
func (n *AnonymousNode) SetParallelism(par int) Node {
n.baseNode.par = par
return n
}
func (n *AnonymousNode) SetName(name string) Node {
n.baseNode.name = name
return n
}
func (n *AnonymousNode) Clone() Node {
return &AnonymousNode{
baseNode: n.baseNode.Clone(),
state0: n.state0,
state: n.state0,
do: n.do,
}
}
func NewLogSink(t values.Type) (Node, *values.List) {
s := values.NewList(t)
return NewStatefulNode(s,
func(state values.Value, collector Collector, v values.Value) (values.Value, error) {
err := state.(*values.List).AddValue(v)
return state, err
}), s
}