github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/dmlsink/mq/dispatcher/topic/dispatcher.go (about) 1 // Copyright 2022 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package topic 15 16 import ( 17 "fmt" 18 ) 19 20 // Dispatcher is an abstraction for dispatching rows and ddls into different topics. 21 type Dispatcher interface { 22 fmt.Stringer 23 Substitute(schema, table string) string 24 } 25 26 // StaticTopicDispatcher is a topic dispatcher which dispatches rows and DDL to the specific topic. 27 type StaticTopicDispatcher struct { 28 topic string 29 } 30 31 // NewStaticTopicDispatcher returns a StaticTopicDispatcher. 32 func NewStaticTopicDispatcher(defaultTopic string) *StaticTopicDispatcher { 33 return &StaticTopicDispatcher{ 34 topic: defaultTopic, 35 } 36 } 37 38 // Substitute converts schema/table name in a topic expression to kafka topic name. 39 func (s *StaticTopicDispatcher) Substitute(schema, table string) string { 40 return s.topic 41 } 42 43 func (s *StaticTopicDispatcher) String() string { 44 return s.topic 45 } 46 47 // DynamicTopicDispatcher is a topic dispatcher which dispatches rows and DDLs 48 // dynamically to the target topics. 49 type DynamicTopicDispatcher struct { 50 expression Expression 51 } 52 53 // NewDynamicTopicDispatcher creates a DynamicTopicDispatcher. 54 func NewDynamicTopicDispatcher(topicExpr Expression) *DynamicTopicDispatcher { 55 return &DynamicTopicDispatcher{ 56 expression: topicExpr, 57 } 58 } 59 60 // Substitute converts schema/table name in a topic expression to kafka topic name. 61 func (d *DynamicTopicDispatcher) Substitute(schema, table string) string { 62 return d.expression.Substitute(schema, table) 63 } 64 65 func (d *DynamicTopicDispatcher) String() string { 66 return string(d.expression) 67 }