forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_parts.go
139 lines (115 loc) · 3.91 KB
/
select_parts.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
package processor
import (
"time"
"github.com/dafanshu/benthos/v3/internal/docs"
"github.com/dafanshu/benthos/v3/lib/log"
"github.com/dafanshu/benthos/v3/lib/message"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/response"
"github.com/dafanshu/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeSelectParts] = TypeSpec{
constructor: NewSelectParts,
Categories: []Category{
CategoryUtility,
},
Summary: `
Cherry pick a set of messages from a batch by their index. Indexes larger than
the number of messages are simply ignored.`,
Description: `
The selected parts are added to the new message batch in the same order as the
selection array. E.g. with 'parts' set to [ 2, 0, 1 ] and the message parts
[ '0', '1', '2', '3' ], the output will be [ '2', '0', '1' ].
If none of the selected parts exist in the input batch (resulting in an empty
output message) the batch is dropped entirely.
Message indexes can be negative, and if so the part will be selected from the
end counting backwards starting from -1. E.g. if index = -1 then the selected
part will be the last part of the message, if index = -2 then the part before
the last element with be selected, and so on.`,
UsesBatches: true,
FieldSpecs: docs.FieldSpecs{
PartsFieldSpec,
},
}
}
//------------------------------------------------------------------------------
// SelectPartsConfig contains configuration fields for the SelectParts
// processor.
type SelectPartsConfig struct {
Parts []int `json:"parts" yaml:"parts"`
}
// NewSelectPartsConfig returns a SelectPartsConfig with default values.
func NewSelectPartsConfig() SelectPartsConfig {
return SelectPartsConfig{
Parts: []int{0},
}
}
//------------------------------------------------------------------------------
// SelectParts is a processor that selects parts from a message to append to a
// new message.
type SelectParts struct {
conf Config
log log.Modular
stats metrics.Type
mCount metrics.StatCounter
mSkipped metrics.StatCounter
mSelected metrics.StatCounter
mDropped metrics.StatCounter
mSent metrics.StatCounter
mBatchSent metrics.StatCounter
}
// NewSelectParts returns a SelectParts processor.
func NewSelectParts(
conf Config, mgr types.Manager, log log.Modular, stats metrics.Type,
) (Type, error) {
return &SelectParts{
conf: conf,
log: log,
stats: stats,
mCount: stats.GetCounter("count"),
mSkipped: stats.GetCounter("skipped"),
mSelected: stats.GetCounter("selected"),
mDropped: stats.GetCounter("dropped"),
mSent: stats.GetCounter("sent"),
mBatchSent: stats.GetCounter("batch.sent"),
}, nil
}
//------------------------------------------------------------------------------
// ProcessMessage applies the processor to a message, either creating >0
// resulting messages or a response to be sent back to the message source.
func (m *SelectParts) ProcessMessage(msg types.Message) ([]types.Message, types.Response) {
m.mCount.Incr(1)
newMsg := message.New(nil)
lParts := msg.Len()
for _, index := range m.conf.SelectParts.Parts {
if index < 0 {
// Negative indexes count backwards from the end.
index = lParts + index
}
// Check boundary of part index.
if index < 0 || index >= lParts {
m.mSkipped.Incr(1)
} else {
m.mSelected.Incr(1)
newMsg.Append(msg.Get(index).Copy())
}
}
if newMsg.Len() == 0 {
m.mDropped.Incr(1)
return nil, response.NewAck()
}
m.mBatchSent.Incr(1)
m.mSent.Incr(int64(newMsg.Len()))
msgs := [1]types.Message{newMsg}
return msgs[:], nil
}
// CloseAsync shuts down the processor and stops processing requests.
func (m *SelectParts) CloseAsync() {
}
// WaitForClose blocks until the processor has closed down.
func (m *SelectParts) WaitForClose(timeout time.Duration) error {
return nil
}
//------------------------------------------------------------------------------