vitess.io/vitess@v0.16.2/go/vt/vtctl/workflow/vreplication_stream.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package workflow 18 19 import ( 20 "fmt" 21 "sort" 22 "strings" 23 24 "google.golang.org/protobuf/proto" 25 26 "vitess.io/vitess/go/mysql" 27 28 binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" 29 ) 30 31 // VReplicationStream represents a single stream of a vreplication workflow. 32 type VReplicationStream struct { 33 ID uint32 34 Workflow string 35 BinlogSource *binlogdatapb.BinlogSource 36 Position mysql.Position 37 WorkflowType binlogdatapb.VReplicationWorkflowType 38 WorkflowSubType binlogdatapb.VReplicationWorkflowSubType 39 DeferSecondaryKeys bool 40 } 41 42 // VReplicationStreams wraps a slice of VReplicationStream objects to provide 43 // some aggregate functionality. 44 type VReplicationStreams []*VReplicationStream 45 46 // Values returns a string representing the IDs of the VReplicationStreams for 47 // use in an IN clause. 48 // 49 // (TODO|@ajm188) This currently returns the literal ")" if len(streams) == 0. 50 // We should probably update this function to return the full "IN" clause, and 51 // then if len(streams) == 0, return "1 != 1" so that we can still execute a 52 // valid SQL query. 53 func (streams VReplicationStreams) Values() string { 54 buf := &strings.Builder{} 55 prefix := "(" 56 57 for _, vrs := range streams { 58 fmt.Fprintf(buf, "%s%d", prefix, vrs.ID) 59 prefix = ", " 60 } 61 62 buf.WriteString(")") 63 return buf.String() 64 } 65 66 // Workflows returns a list of unique workflow names in the list of vreplication 67 // streams. 68 func (streams VReplicationStreams) Workflows() []string { 69 set := make(map[string]bool, len(streams)) 70 for _, vrs := range streams { 71 set[vrs.Workflow] = true 72 } 73 74 list := make([]string, 0, len(set)) 75 for k := range set { 76 list = append(list, k) 77 } 78 79 sort.Strings(list) 80 return list 81 } 82 83 // Copy returns a copy of the list of streams. All fields except .Position are 84 // copied. 85 func (streams VReplicationStreams) Copy() VReplicationStreams { 86 out := make([]*VReplicationStream, len(streams)) 87 88 for i, vrs := range streams { 89 out[i] = &VReplicationStream{ 90 ID: vrs.ID, 91 Workflow: vrs.Workflow, 92 BinlogSource: proto.Clone(vrs.BinlogSource).(*binlogdatapb.BinlogSource), 93 Position: vrs.Position, 94 } 95 } 96 97 return VReplicationStreams(out) 98 } 99 100 // ToSlice unwraps a VReplicationStreams object into the underlying slice of 101 // VReplicationStream objects. 102 func (streams VReplicationStreams) ToSlice() []*VReplicationStream { 103 return []*VReplicationStream(streams) 104 }