github.com/matrixorigin/matrixone@v1.2.0/pkg/pb/metadata/metadata.go (about) 1 // Copyright 2022 Matrix Origin 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package metadata 16 17 import ( 18 "bytes" 19 "fmt" 20 "strings" 21 ) 22 23 // IsEmpty return true if is a empty DNShard 24 func (m TNShard) IsEmpty() bool { 25 return m.ShardID == 0 26 } 27 28 // Equal returns true if DNShard is same 29 func (m TNShard) Equal(tn TNShard) bool { 30 return m.ShardID == tn.ShardID && m.ReplicaID == tn.ReplicaID 31 } 32 33 // DebugString returns debug string 34 func (m TNShard) DebugString() string { 35 return fmt.Sprintf("%d-%d-%d-%s", m.ShardID, m.ReplicaID, m.LogShardID, m.Address) 36 } 37 38 // DebugString returns debug string 39 func (m TNStore) DebugString() string { 40 n := len(m.Shards) 41 var buf bytes.Buffer 42 buf.WriteString(m.UUID) 43 buf.WriteString("/") 44 buf.WriteString(fmt.Sprintf("%d", len(m.Shards))) 45 buf.WriteString(" DNShards[") 46 for idx, shard := range m.Shards { 47 buf.WriteString(shard.DebugString()) 48 if idx < n-1 { 49 buf.WriteString(", ") 50 } 51 } 52 buf.WriteString("]") 53 return buf.String() 54 } 55 56 // DebugString returns debug string 57 func (m CNStore) DebugString() string { 58 return fmt.Sprintf("%s/%s", m.UUID, m.Role.String()) 59 } 60 61 // MustParseCNRole parse CN Role from role string 62 func MustParseCNRole(role string) CNRole { 63 if v, ok := CNRole_value[strings.ToUpper(role)]; ok { 64 return CNRole(v) 65 } 66 panic(fmt.Sprintf("invalid CN Role %s", role)) 67 } 68 69 func (m CNService) DebugString() string { 70 var buf bytes.Buffer 71 buf.WriteString(m.ServiceID) 72 buf.WriteString("/sql(") 73 buf.WriteString(m.SQLAddress) 74 buf.WriteString(")/pipeline(") 75 buf.WriteString(m.PipelineServiceAddress) 76 buf.WriteString(")/lock(") 77 buf.WriteString(m.LockServiceAddress) 78 buf.WriteString(")/[") 79 for k, v := range m.Labels { 80 buf.WriteString(k) 81 buf.WriteString("=") 82 buf.WriteString(v.String()) 83 buf.WriteString(" ") 84 } 85 buf.WriteString("]") 86 return buf.String() 87 } 88 89 func (m TNService) DebugString() string { 90 var buf bytes.Buffer 91 buf.WriteString(m.ServiceID) 92 buf.WriteString("/txn(") 93 buf.WriteString(m.TxnServiceAddress) 94 buf.WriteString(")/[") 95 n := len(m.Shards) 96 for idx, shard := range m.Shards { 97 buf.WriteString(shard.DebugString()) 98 if idx < n-1 { 99 buf.WriteString(", ") 100 } 101 } 102 buf.WriteString("]/") 103 for k, v := range m.Labels { 104 buf.WriteString(k) 105 buf.WriteString("=") 106 buf.WriteString(v.String()) 107 buf.WriteString(" ") 108 } 109 buf.WriteString("]") 110 return buf.String() 111 } 112 113 // ToTitle converts the state string to title-like string. 114 func ToTitle(s string) string { 115 if s == "" { 116 return "" 117 } 118 return strings.ToUpper(s[:1]) + strings.ToLower(s[1:]) 119 } 120 121 // ValidStateString returns true if the state string is valid. 122 func ValidStateString(s string) bool { 123 title := ToTitle(s) 124 for v := range WorkState_value { 125 if title == v { 126 return true 127 } 128 } 129 return false 130 }