github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/codec/internal/message_key.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 internal
    15  
    16  import (
    17  	"encoding/json"
    18  
    19  	"github.com/pingcap/tiflow/cdc/model"
    20  	cerror "github.com/pingcap/tiflow/pkg/errors"
    21  )
    22  
    23  // MessageKey defines the key for a message.
    24  type MessageKey struct {
    25  	Ts        uint64            `json:"ts"`
    26  	Schema    string            `json:"scm,omitempty"`
    27  	Table     string            `json:"tbl,omitempty"`
    28  	RowID     int64             `json:"rid,omitempty"`
    29  	Partition *int64            `json:"ptn,omitempty"`
    30  	Type      model.MessageType `json:"t"`
    31  	// Only Handle Key Columns encoded in the message's value part.
    32  	OnlyHandleKey bool `json:"ohk,omitempty"`
    33  
    34  	// Claim check location for the message
    35  	ClaimCheckLocation string `json:"ccl,omitempty"`
    36  }
    37  
    38  // Encode encodes the message key to a byte slice.
    39  func (m *MessageKey) Encode() ([]byte, error) {
    40  	data, err := json.Marshal(m)
    41  	return data, cerror.WrapError(cerror.ErrMarshalFailed, err)
    42  }
    43  
    44  // Decode codes a message key from a byte slice.
    45  func (m *MessageKey) Decode(data []byte) error {
    46  	return cerror.WrapError(cerror.ErrUnmarshalFailed, json.Unmarshal(data, m))
    47  }