github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/codec/encoder.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 codec 15 16 import ( 17 "bytes" 18 "context" 19 20 "github.com/pingcap/tiflow/cdc/model" 21 "github.com/pingcap/tiflow/pkg/sink/codec/common" 22 ) 23 24 const ( 25 // BatchVersion1 represents the version of batch format 26 BatchVersion1 uint64 = 1 27 28 // MemBufShrinkThreshold represents the threshold of shrinking the buffer. 29 MemBufShrinkThreshold = 1024 * 1024 30 ) 31 32 // DDLEventBatchEncoder is an abstraction for DDL event encoder. 33 type DDLEventBatchEncoder interface { 34 // EncodeCheckpointEvent appends a checkpoint event into the batch. 35 // This event will be broadcast to all partitions to signal a global checkpoint. 36 EncodeCheckpointEvent(ts uint64) (*common.Message, error) 37 // EncodeDDLEvent appends a DDL event into the batch 38 EncodeDDLEvent(e *model.DDLEvent) (*common.Message, error) 39 } 40 41 // MessageBuilder is an abstraction to build message. 42 type MessageBuilder interface { 43 // Build builds the batch and returns the bytes of key and value. 44 // Should be called after `AppendRowChangedEvent` 45 Build() []*common.Message 46 } 47 48 // RowEventEncoder is an abstraction for events encoder 49 type RowEventEncoder interface { 50 DDLEventBatchEncoder 51 // AppendRowChangedEvent appends a row changed event into the batch or buffer. 52 AppendRowChangedEvent(context.Context, string, *model.RowChangedEvent, func()) error 53 MessageBuilder 54 } 55 56 // RowEventEncoderBuilder builds row encoder with context. 57 type RowEventEncoderBuilder interface { 58 Build() RowEventEncoder 59 CleanMetrics() 60 } 61 62 // TxnEventEncoder is an abstraction for txn events encoder. 63 type TxnEventEncoder interface { 64 // AppendTxnEvent append a txn event into the buffer. 65 AppendTxnEvent(*model.SingleTableTxn, func()) error 66 MessageBuilder 67 } 68 69 // TxnEventEncoderBuilder builds txn encoder with context. 70 type TxnEventEncoderBuilder interface { 71 Build() TxnEventEncoder 72 } 73 74 // IsColumnValueEqual checks whether the preValue and updatedValue are equal. 75 func IsColumnValueEqual(preValue, updatedValue interface{}) bool { 76 if preValue == nil || updatedValue == nil { 77 return preValue == updatedValue 78 } 79 80 preValueBytes, ok1 := preValue.([]byte) 81 updatedValueBytes, ok2 := updatedValue.([]byte) 82 if ok1 && ok2 { 83 return bytes.Equal(preValueBytes, updatedValueBytes) 84 } 85 // mounter use the same table info to parse the value, 86 // the value type should be the same 87 return preValue == updatedValue 88 } 89 90 // MockRowEventEncoderBuilder is a mock implementation of RowEventEncoderBuilder 91 type MockRowEventEncoderBuilder struct{} 92 93 // Build implement the RowEventEncoderBuilder interface 94 func (m *MockRowEventEncoderBuilder) Build() RowEventEncoder { 95 return &MockRowEventEncoder{} 96 } 97 98 // CleanMetrics implement the RowEventEncoderBuilder interface 99 func (m *MockRowEventEncoderBuilder) CleanMetrics() { 100 // Clean up metrics if needed 101 } 102 103 // MockRowEventEncoder is a mock implementation of RowEventEncoder 104 type MockRowEventEncoder struct{} 105 106 // EncodeCheckpointEvent implement the DDLEventBatchEncoder interface 107 func (m *MockRowEventEncoder) EncodeCheckpointEvent(ts uint64) (*common.Message, error) { 108 // Implement the encoding logic for checkpoint event 109 return nil, nil 110 } 111 112 // EncodeDDLEvent implement the DDLEventBatchEncoder interface 113 func (m *MockRowEventEncoder) EncodeDDLEvent(e *model.DDLEvent) (*common.Message, error) { 114 // Implement the encoding logic for DDL event 115 return nil, nil 116 } 117 118 // AppendRowChangedEvent implement the RowEventEncoder interface 119 func (m *MockRowEventEncoder) AppendRowChangedEvent( 120 ctx context.Context, tableID string, event *model.RowChangedEvent, callback func(), 121 ) error { 122 // Implement the logic for appending row changed event 123 return nil 124 } 125 126 // Build implement the RowEventEncoder interface 127 func (m *MockRowEventEncoder) Build() []*common.Message { 128 // Implement the logic for building the batch and returning the bytes of key and value 129 return nil 130 }