vitess.io/vitess@v0.16.2/go/vt/binlog/tables_filter.go (about) 1 /* 2 Copyright 2019 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 binlog 18 19 import ( 20 "strings" 21 22 "vitess.io/vitess/go/vt/log" 23 24 binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" 25 querypb "vitess.io/vitess/go/vt/proto/query" 26 ) 27 28 const ( 29 streamComment = "/* _stream " 30 space = " " 31 ) 32 33 // tablesFilterFunc returns a function that calls callback only if statements 34 // in the transaction match the specified tables. The resulting function can be 35 // passed into the Streamer: bls.Stream(file, pos, sendTransaction) -> 36 // bls.Stream(file, pos, tablesFilterFunc(sendTransaction)) 37 func tablesFilterFunc(tables []string, callback func(*binlogdatapb.BinlogTransaction) error) sendTransactionFunc { 38 return func(eventToken *querypb.EventToken, statements []FullBinlogStatement) error { 39 matched := false 40 filtered := make([]*binlogdatapb.BinlogTransaction_Statement, 0, len(statements)) 41 for _, statement := range statements { 42 switch statement.Statement.Category { 43 case binlogdatapb.BinlogTransaction_Statement_BL_SET: 44 filtered = append(filtered, statement.Statement) 45 case binlogdatapb.BinlogTransaction_Statement_BL_DDL: 46 log.Warningf("Not forwarding DDL: %s", statement.Statement.Sql) 47 continue 48 case binlogdatapb.BinlogTransaction_Statement_BL_INSERT, 49 binlogdatapb.BinlogTransaction_Statement_BL_UPDATE, 50 binlogdatapb.BinlogTransaction_Statement_BL_DELETE: 51 tableName := statement.Table 52 if tableName == "" { 53 // The statement doesn't 54 // contain the table name (SBR 55 // event), figure it out. 56 sql := string(statement.Statement.Sql) 57 tableIndex := strings.LastIndex(sql, streamComment) 58 if tableIndex == -1 { 59 updateStreamErrors.Add("TablesStream", 1) 60 log.Errorf("Error parsing table name: %s", sql) 61 continue 62 } 63 tableStart := tableIndex + len(streamComment) 64 tableEnd := strings.Index(sql[tableStart:], space) 65 if tableEnd == -1 { 66 updateStreamErrors.Add("TablesStream", 1) 67 log.Errorf("Error parsing table name: %s", sql) 68 continue 69 } 70 tableName = sql[tableStart : tableStart+tableEnd] 71 } 72 for _, t := range tables { 73 if t == tableName { 74 filtered = append(filtered, statement.Statement) 75 matched = true 76 break 77 } 78 } 79 case binlogdatapb.BinlogTransaction_Statement_BL_UNRECOGNIZED: 80 updateStreamErrors.Add("TablesStream", 1) 81 log.Errorf("Error parsing table name: %s", string(statement.Statement.Sql)) 82 continue 83 } 84 } 85 86 trans := &binlogdatapb.BinlogTransaction{ 87 EventToken: eventToken, 88 } 89 if matched { 90 trans.Statements = filtered 91 } 92 return callback(trans) 93 } 94 }