forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
97 lines (88 loc) · 2.22 KB
/
delete.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
package kapacitor
import (
"log"
"github.com/influxdata/kapacitor/expvar"
"github.com/influxdata/kapacitor/models"
"github.com/influxdata/kapacitor/pipeline"
)
const (
statsFieldsDeleted = "fields_deleted"
statsTagsDeleted = "tags_deleted"
)
type DeleteNode struct {
node
d *pipeline.DeleteNode
fieldsDeleted *expvar.Int
tagsDeleted *expvar.Int
}
// Create a new DeleteNode which applies a transformation func to each point in a stream and returns a single point.
func newDeleteNode(et *ExecutingTask, n *pipeline.DeleteNode, l *log.Logger) (*DeleteNode, error) {
dn := &DeleteNode{
node: node{Node: n, et: et, logger: l},
d: n,
}
dn.node.runF = dn.runDelete
return dn, nil
}
func (e *DeleteNode) runDelete(snapshot []byte) error {
e.fieldsDeleted = &expvar.Int{}
e.tagsDeleted = &expvar.Int{}
e.statMap.Set(statsFieldsDeleted, e.fieldsDeleted)
e.statMap.Set(statsTagsDeleted, e.tagsDeleted)
switch e.Provides() {
case pipeline.StreamEdge:
for p, ok := e.ins[0].NextPoint(); ok; p, ok = e.ins[0].NextPoint() {
e.timer.Start()
p.Fields, p.Tags = e.doDeletes(p.Fields, p.Tags)
e.timer.Stop()
for _, child := range e.outs {
err := child.CollectPoint(p)
if err != nil {
return err
}
}
}
case pipeline.BatchEdge:
for b, ok := e.ins[0].NextBatch(); ok; b, ok = e.ins[0].NextBatch() {
e.timer.Start()
for i := range b.Points {
b.Points[i].Fields, b.Points[i].Tags = e.doDeletes(b.Points[i].Fields, b.Points[i].Tags)
}
e.timer.Stop()
for _, child := range e.outs {
err := child.CollectBatch(b)
if err != nil {
return err
}
}
}
}
return nil
}
func (d *DeleteNode) doDeletes(fields models.Fields, tags models.Tags) (models.Fields, models.Tags) {
newFields := fields
fieldsCopied := false
for _, field := range d.d.Fields {
if _, ok := fields[field]; ok {
if !fieldsCopied {
newFields = newFields.Copy()
fieldsCopied = true
}
d.fieldsDeleted.Add(1)
delete(newFields, field)
}
}
newTags := tags
tagsCopied := false
for _, tag := range d.d.Tags {
if _, ok := tags[tag]; ok {
if !tagsCopied {
newTags = newTags.Copy()
tagsCopied = true
}
d.tagsDeleted.Add(1)
delete(newTags, tag)
}
}
return newFields, newTags
}