github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/model/kv.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  //go:generate msgp
    15  
    16  package model
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"github.com/pingcap/ticdc/pkg/regionspan"
    22  )
    23  
    24  // OpType for the kv, delete or put
    25  type OpType int
    26  
    27  // OpType for kv
    28  const (
    29  	OpTypeUnknow OpType = iota
    30  	OpTypePut
    31  	OpTypeDelete
    32  	OpTypeResolved
    33  )
    34  
    35  // RegionFeedEvent from the kv layer.
    36  // Only one of the event will be setted.
    37  //msgp:ignore RegionFeedEvent
    38  type RegionFeedEvent struct {
    39  	Val      *RawKVEntry
    40  	Resolved *ResolvedSpan
    41  
    42  	// Additonal debug info
    43  	RegionID uint64
    44  }
    45  
    46  // GetValue returns the underlying value
    47  func (e *RegionFeedEvent) GetValue() interface{} {
    48  	if e.Val != nil {
    49  		return e.Val
    50  	} else if e.Resolved != nil {
    51  		return e.Resolved
    52  	} else {
    53  		return nil
    54  	}
    55  }
    56  
    57  // ResolvedSpan guarantees all the KV value event
    58  // with commit ts less than ResolvedTs has been emitted.
    59  //msgp:ignore ResolvedSpan
    60  type ResolvedSpan struct {
    61  	Span       regionspan.ComparableSpan
    62  	ResolvedTs uint64
    63  }
    64  
    65  // String implements fmt.Stringer interface.
    66  func (rs *ResolvedSpan) String() string {
    67  	return fmt.Sprintf("span: %s, resolved-ts: %d", rs.Span, rs.ResolvedTs)
    68  }
    69  
    70  // RawKVEntry notify the KV operator
    71  type RawKVEntry struct {
    72  	OpType OpType `msg:"op_type"`
    73  	Key    []byte `msg:"key"`
    74  	// nil for delete type
    75  	Value []byte `msg:"value"`
    76  	// nil for insert type
    77  	OldValue []byte `msg:"old_value"`
    78  	StartTs  uint64 `msg:"start_ts"`
    79  	// Commit or resolved TS
    80  	CRTs uint64 `msg:"crts"`
    81  
    82  	// Additonal debug info
    83  	RegionID uint64 `msg:"region_id"`
    84  }
    85  
    86  func (v *RawKVEntry) String() string {
    87  	// TODO: redact values.
    88  	return fmt.Sprintf(
    89  		"OpType: %v, Key: %s, Value: %s, OldValue: %s, StartTs: %d, CRTs: %d, RegionID: %d",
    90  		v.OpType, string(v.Key), string(v.Value), string(v.OldValue), v.StartTs, v.CRTs, v.RegionID)
    91  }
    92  
    93  // ApproximateSize calculate the approximate size of this event
    94  func (v *RawKVEntry) ApproximateSize() int64 {
    95  	return int64(len(v.Key) + len(v.Value) + len(v.OldValue))
    96  }