aboutsummaryrefslogtreecommitdiff
path: root/core/operator/keyby.go
blob: 1296df0d5eccafda41c0db98c62d16730ab03b95 (plain)
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
package operator

import (
	"context"
	"goflink/core"
)

type KeySelector[IN any] func(in IN) string
type KeyByOperator[IN any] struct {
	selector KeySelector[IN]
}

func NewKeyByOperator[IN any](selector KeySelector[IN]) *KeyByOperator[IN] {
	return &KeyByOperator[IN]{selector: selector}
}

func (k *KeyByOperator[IN]) Open(ctx context.Context) error {
	return nil
}

func (k *KeyByOperator[IN]) Process(ctx context.Context, record core.StreamRecord[IN], out Emitter[IN]) error {
	if record.Type != core.RecordData {
		return out.Emit(ctx, record)
	}

	record.Key = k.selector(record.Value)
	return out.Emit(ctx, record)
}

func (k *KeyByOperator[IN]) Close(ctx context.Context) error {
	return nil
}