github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/processor/tablepb/table.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 tablepb 15 16 import ( 17 "bytes" 18 "encoding" 19 "encoding/hex" 20 "encoding/json" 21 "fmt" 22 "strconv" 23 "strings" 24 "sync/atomic" 25 ) 26 27 // Load TableState with THREAD-SAFE 28 func (s *TableState) Load() TableState { 29 return TableState(atomic.LoadInt32((*int32)(s))) 30 } 31 32 // Store TableState with THREAD-SAFE 33 func (s *TableState) Store(new TableState) { 34 atomic.StoreInt32((*int32)(s), int32(new)) 35 } 36 37 // CompareAndSwap is just like sync/atomic.Atomic*.CompareAndSwap. 38 func (s *TableState) CompareAndSwap(old, new TableState) bool { 39 oldx := int32(old) 40 newx := int32(new) 41 return atomic.CompareAndSwapInt32((*int32)(s), oldx, newx) 42 } 43 44 // TableID is the ID of the table 45 type TableID = int64 46 47 // Ts is the timestamp with a logical count 48 type Ts = uint64 49 50 // Key is a custom type for bytes encoded in memcomparable format. 51 // Key is read-only, must not be mutated. 52 type Key []byte 53 54 var ( 55 _ fmt.Stringer = Key{} 56 _ fmt.Stringer = (*Key)(nil) 57 ) 58 59 func (k Key) String() string { 60 return hex.EncodeToString(k) 61 } 62 63 var ( 64 _ json.Marshaler = Key{} 65 _ json.Marshaler = (*Key)(nil) 66 ) 67 68 // MarshalJSON implements json.Marshaler. 69 // It is helpful to format span in log. 70 func (k Key) MarshalJSON() ([]byte, error) { 71 return json.Marshal(k.String()) 72 } 73 74 var ( 75 _ encoding.TextMarshaler = Span{} 76 _ encoding.TextMarshaler = (*Span)(nil) 77 ) 78 79 // MarshalText implements encoding.TextMarshaler (used in proto.CompactTextString). 80 // It is helpful to format span in log. 81 func (s Span) MarshalText() ([]byte, error) { 82 return []byte(s.String()), nil 83 } 84 85 func (s *Span) String() string { 86 length := len("{table_id:,start_key:,end_key:}") 87 length += 8 // for TableID 88 length += len(s.StartKey) + len(s.EndKey) 89 b := strings.Builder{} 90 b.Grow(length) 91 b.Write([]byte("{table_id:")) 92 b.Write([]byte(strconv.Itoa(int(s.TableID)))) 93 if len(s.StartKey) > 0 { 94 b.Write([]byte(",start_key:")) 95 b.Write([]byte(s.StartKey.String())) 96 } 97 if len(s.EndKey) > 0 { 98 b.Write([]byte(",end_key:")) 99 b.Write([]byte(s.EndKey.String())) 100 } 101 b.Write([]byte("}")) 102 return b.String() 103 } 104 105 // Less compares two Spans, defines the order between spans. 106 func (s *Span) Less(b *Span) bool { 107 if s.TableID < b.TableID { 108 return true 109 } 110 if bytes.Compare(s.StartKey, b.StartKey) < 0 { 111 return true 112 } 113 return false 114 } 115 116 // Eq compares two Spans, defines the equality between spans. 117 func (s *Span) Eq(b *Span) bool { 118 return s.TableID == b.TableID && 119 bytes.Equal(s.StartKey, b.StartKey) && 120 bytes.Equal(s.EndKey, b.EndKey) 121 }