github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/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/tiflow/cdc/processor/tablepb"
    22  )
    23  
    24  // OpType for the kv, delete or put
    25  type OpType int
    26  
    27  // OpType for kv
    28  const (
    29  	OpTypeUnknown OpType = iota
    30  	OpTypePut
    31  	OpTypeDelete
    32  	OpTypeResolved
    33  )
    34  
    35  // RegionFeedEvent from the kv layer.
    36  // Only one of the event will be set.
    37  //
    38  //msgp:ignore RegionFeedEvent
    39  type RegionFeedEvent struct {
    40  	Val      *RawKVEntry
    41  	Resolved *ResolvedSpans
    42  
    43  	// Additional debug info, not used
    44  	RegionID uint64
    45  }
    46  
    47  // GetValue returns the underlying value
    48  func (e *RegionFeedEvent) GetValue() interface{} {
    49  	if e.Val != nil {
    50  		return e.Val
    51  	} else if e.Resolved != nil {
    52  		return e.Resolved
    53  	} else {
    54  		return nil
    55  	}
    56  }
    57  
    58  // ResolvedSpans guarantees all the KV value event
    59  // with commit ts less than ResolvedTs has been emitted.
    60  //
    61  //msgp:ignore ResolvedSpans
    62  type ResolvedSpans struct {
    63  	Spans      []RegionComparableSpan
    64  	ResolvedTs uint64
    65  }
    66  
    67  // String implements fmt.Stringer interface.
    68  func (rs *ResolvedSpans) String() string {
    69  	return fmt.Sprintf("span: %v, resolved-ts: %d", rs.Spans, rs.ResolvedTs)
    70  }
    71  
    72  // RegionComparableSpan contains a comparable span and a region id of that span
    73  //
    74  //msgp:ignore RegionComparableSpan
    75  type RegionComparableSpan struct {
    76  	Span   tablepb.Span
    77  	Region uint64
    78  }
    79  
    80  // RawKVEntry notify the KV operator
    81  type RawKVEntry struct {
    82  	OpType OpType `msg:"op_type"`
    83  	Key    []byte `msg:"key"`
    84  	// nil for delete type
    85  	Value []byte `msg:"value"`
    86  	// nil for insert type
    87  	OldValue []byte `msg:"old_value"`
    88  	StartTs  uint64 `msg:"start_ts"`
    89  	// Commit or resolved TS
    90  	CRTs uint64 `msg:"crts"`
    91  
    92  	// Additional debug info
    93  	RegionID uint64 `msg:"region_id"`
    94  }
    95  
    96  // IsUpdate checks if the event is an update event.
    97  func (v *RawKVEntry) IsUpdate() bool {
    98  	return v.OpType == OpTypePut && v.OldValue != nil && v.Value != nil
    99  }
   100  
   101  func (v *RawKVEntry) String() string {
   102  	// TODO: redact values.
   103  	return fmt.Sprintf(
   104  		"OpType: %v, Key: %s, Value: %s, OldValue: %s, StartTs: %d, CRTs: %d, RegionID: %d",
   105  		v.OpType, string(v.Key), string(v.Value), string(v.OldValue), v.StartTs, v.CRTs, v.RegionID)
   106  }
   107  
   108  // ApproximateDataSize calculate the approximate size of protobuf binary
   109  // representation of this event.
   110  func (v *RawKVEntry) ApproximateDataSize() int64 {
   111  	return int64(len(v.Key) + len(v.Value) + len(v.OldValue))
   112  }