github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/processor/sourcemanager/sorter/pebble/encoding/value.go (about)

     1  // Copyright 2020 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 encoding
    15  
    16  import (
    17  	"github.com/pingcap/errors"
    18  	"github.com/pingcap/tiflow/cdc/model"
    19  )
    20  
    21  // SerializerDeserializer is the interface encodes and decodes model.PolymorphicEvent.
    22  type SerializerDeserializer interface {
    23  	Marshal(event *model.PolymorphicEvent, bytes []byte) ([]byte, error)
    24  	Unmarshal(event *model.PolymorphicEvent, bytes []byte) ([]byte, error)
    25  }
    26  
    27  // MsgPackGenSerde encodes model.PolymorphicEvent into bytes and decodes
    28  // model.PolymorphicEvent from bytes.
    29  type MsgPackGenSerde struct{}
    30  
    31  // Marshal encodes model.PolymorphicEvent into bytes.
    32  func (m *MsgPackGenSerde) Marshal(event *model.PolymorphicEvent, bytes []byte) ([]byte, error) {
    33  	bytes = bytes[:0]
    34  	return event.RawKV.MarshalMsg(bytes)
    35  }
    36  
    37  // Unmarshal decodes model.PolymorphicEvent from bytes.
    38  func (m *MsgPackGenSerde) Unmarshal(event *model.PolymorphicEvent, bytes []byte) ([]byte, error) {
    39  	if event.RawKV == nil {
    40  		event.RawKV = new(model.RawKVEntry)
    41  	}
    42  
    43  	bytes, err := event.RawKV.UnmarshalMsg(bytes)
    44  	if err != nil {
    45  		return nil, errors.Trace(err)
    46  	}
    47  
    48  	event.StartTs = event.RawKV.StartTs
    49  	event.CRTs = event.RawKV.CRTs
    50  
    51  	return bytes, nil
    52  }