aboutsummaryrefslogtreecommitdiff
path: root/core/operator/map.go
diff options
context:
space:
mode:
authorSangTran-127 <tranquangsang12.7@gmail.com>2026-08-09 18:32:44 +0700
committerSangTran-127 <tranquangsang12.7@gmail.com>2026-08-09 18:32:44 +0700
commit4ca27aec303f54dc9ee670c67c2fefa80dd004a1 (patch)
tree5628714f47e374bfb98c1cea99970b19892e8b3e /core/operator/map.go
parenta892cb5b1d121177881fa56a79abbafef5880a80 (diff)
downloadgoflink-4ca27aec303f54dc9ee670c67c2fefa80dd004a1.tar.gz
goflink-4ca27aec303f54dc9ee670c67c2fefa80dd004a1.zip
feat: add KeyByOperator and wordCounter for stateful processing
Diffstat (limited to 'core/operator/map.go')
-rw-r--r--core/operator/map.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/core/operator/map.go b/core/operator/map.go
new file mode 100644
index 0000000..ef6912e
--- /dev/null
+++ b/core/operator/map.go
@@ -0,0 +1,53 @@
+package operator
+
+import (
+ "context"
+ "fmt"
+ "goflink/core"
+)
+
+// MapFunc is business logic for user implement
+type MapFunc[IN, OUT any] func(in IN) (OUT, error)
+
+type MapOperator[IN, OUT any] struct {
+ userFunc MapFunc[IN, OUT]
+}
+
+func NewMapOperator[IN, OUT any](fn MapFunc[IN, OUT]) *MapOperator[IN, OUT] {
+ return &MapOperator[IN, OUT]{
+ userFunc: fn,
+ }
+}
+
+func (fo *MapOperator[IN, OUT]) Open(ctx context.Context) error {
+ return nil
+}
+
+func (fo *MapOperator[IN, OUT]) Process(ctx context.Context, record core.StreamRecord[IN], out Emitter[OUT]) error {
+ if record.Type != core.RecordData {
+ // watermark and barrier
+ return out.Emit(ctx, core.StreamRecord[OUT]{
+ Type: record.Type,
+ Key: record.Key,
+ Timestamp: record.Timestamp,
+ BarrierID: record.BarrierID,
+ })
+ }
+
+ outValue, err := fo.userFunc(record.Value)
+
+ if err != nil {
+ return fmt.Errorf("cannot process value %v: %w", record.Value, err)
+ }
+
+ return out.Emit(ctx, core.StreamRecord[OUT]{
+ Type: core.RecordData,
+ Key: record.Key,
+ Timestamp: record.Timestamp,
+ Value: outValue,
+ })
+}
+
+func (fo *MapOperator[IN, OUT]) Close(ctx context.Context) error {
+ return nil
+}