vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/vreplication/insert_generator.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 vreplication
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"time"
    23  
    24  	binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
    25  	"vitess.io/vitess/go/vt/throttler"
    26  )
    27  
    28  // InsertGenerator generates a vreplication insert statement.
    29  type InsertGenerator struct {
    30  	buf    *strings.Builder
    31  	prefix string
    32  
    33  	state  string
    34  	dbname string
    35  	now    int64
    36  }
    37  
    38  // NewInsertGenerator creates a new InsertGenerator.
    39  func NewInsertGenerator(state, dbname string) *InsertGenerator {
    40  	buf := &strings.Builder{}
    41  	buf.WriteString("insert into _vt.vreplication(workflow, source, pos, max_tps, max_replication_lag, cell, tablet_types, time_updated, transaction_timestamp, state, db_name, workflow_type, workflow_sub_type, defer_secondary_keys) values ")
    42  	return &InsertGenerator{
    43  		buf:    buf,
    44  		state:  state,
    45  		dbname: dbname,
    46  		now:    time.Now().Unix(),
    47  	}
    48  }
    49  
    50  // AddRow adds a row to the insert statement.
    51  func (ig *InsertGenerator) AddRow(workflow string, bls *binlogdatapb.BinlogSource, pos, cell, tabletTypes string,
    52  	workflowType int64, workflowSubType int64, deferSecondaryKeys bool) {
    53  	fmt.Fprintf(ig.buf, "%s(%v, %v, %v, %v, %v, %v, %v, %v, 0, '%v', %v, %v, %v, %v)",
    54  		ig.prefix,
    55  		encodeString(workflow),
    56  		encodeString(bls.String()),
    57  		encodeString(pos),
    58  		throttler.MaxRateModuleDisabled,
    59  		throttler.ReplicationLagModuleDisabled,
    60  		encodeString(cell),
    61  		encodeString(tabletTypes),
    62  		ig.now,
    63  		ig.state,
    64  		encodeString(ig.dbname),
    65  		workflowType,
    66  		workflowSubType,
    67  		deferSecondaryKeys,
    68  	)
    69  	ig.prefix = ", "
    70  }
    71  
    72  // String returns the generated statement.
    73  func (ig *InsertGenerator) String() string {
    74  	return ig.buf.String()
    75  }