vitess.io/vitess@v0.16.2/go/mysql/filepos_gtid.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 mysql
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  // FilePosFlavorID is the string identifier for the filePos flavor.
    26  const FilePosFlavorID = "FilePos"
    27  
    28  // parsefilePosGTID is registered as a GTID parser.
    29  func parseFilePosGTID(s string) (GTID, error) {
    30  	// Split into parts.
    31  	parts := strings.Split(s, ":")
    32  	if len(parts) != 2 {
    33  		return nil, fmt.Errorf("invalid FilePos GTID (%v): expecting file:pos", s)
    34  	}
    35  
    36  	pos, err := strconv.Atoi(parts[1])
    37  	if err != nil {
    38  		return nil, fmt.Errorf("invalid FilePos GTID (%v): expecting pos to be an integer", s)
    39  	}
    40  
    41  	return filePosGTID{
    42  		file: parts[0],
    43  		pos:  pos,
    44  	}, nil
    45  }
    46  
    47  // ParseFilePosGTIDSet is registered as a GTIDSet parser.
    48  func ParseFilePosGTIDSet(s string) (GTIDSet, error) {
    49  	gtid, err := parseFilePosGTID(s)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return gtid.(filePosGTID), err
    54  }
    55  
    56  // filePosGTID implements GTID.
    57  type filePosGTID struct {
    58  	file string
    59  	pos  int
    60  }
    61  
    62  // String implements GTID.String().
    63  func (gtid filePosGTID) String() string {
    64  	return fmt.Sprintf("%s:%d", gtid.file, gtid.pos)
    65  }
    66  
    67  // Flavor implements GTID.Flavor().
    68  func (gtid filePosGTID) Flavor() string {
    69  	return FilePosFlavorID
    70  }
    71  
    72  // SequenceDomain implements GTID.SequenceDomain().
    73  func (gtid filePosGTID) SequenceDomain() any {
    74  	return nil
    75  }
    76  
    77  // SourceServer implements GTID.SourceServer().
    78  func (gtid filePosGTID) SourceServer() any {
    79  	return nil
    80  }
    81  
    82  // SequenceNumber implements GTID.SequenceNumber().
    83  func (gtid filePosGTID) SequenceNumber() any {
    84  	return nil
    85  }
    86  
    87  // GTIDSet implements GTID.GTIDSet().
    88  func (gtid filePosGTID) GTIDSet() GTIDSet {
    89  	return gtid
    90  }
    91  
    92  // ContainsGTID implements GTIDSet.ContainsGTID().
    93  func (gtid filePosGTID) ContainsGTID(other GTID) bool {
    94  	if other == nil {
    95  		return true
    96  	}
    97  	filePosOther, ok := other.(filePosGTID)
    98  	if !ok {
    99  		return false
   100  	}
   101  	if filePosOther.file < gtid.file {
   102  		return true
   103  	}
   104  	if filePosOther.file > gtid.file {
   105  		return false
   106  	}
   107  	return filePosOther.pos <= gtid.pos
   108  }
   109  
   110  // Contains implements GTIDSet.Contains().
   111  func (gtid filePosGTID) Contains(other GTIDSet) bool {
   112  	if other == nil {
   113  		return false
   114  	}
   115  	filePosOther, ok := other.(filePosGTID)
   116  	if !ok {
   117  		return false
   118  	}
   119  	return gtid.ContainsGTID(filePosOther)
   120  }
   121  
   122  // Equal implements GTIDSet.Equal().
   123  func (gtid filePosGTID) Equal(other GTIDSet) bool {
   124  	filePosOther, ok := other.(filePosGTID)
   125  	if !ok {
   126  		return false
   127  	}
   128  	return gtid == filePosOther
   129  }
   130  
   131  // AddGTID implements GTIDSet.AddGTID().
   132  func (gtid filePosGTID) AddGTID(other GTID) GTIDSet {
   133  	filePosOther, ok := other.(filePosGTID)
   134  	if !ok {
   135  		return gtid
   136  	}
   137  	return filePosOther
   138  }
   139  
   140  // Union implements GTIDSet.Union().
   141  func (gtid filePosGTID) Union(other GTIDSet) GTIDSet {
   142  	filePosOther, ok := other.(filePosGTID)
   143  	if !ok || gtid.Contains(other) {
   144  		return gtid
   145  	}
   146  
   147  	return filePosOther
   148  }
   149  
   150  // Last returns last filePosition
   151  // For filePos based GTID we have only one position
   152  // here we will just return the current filePos
   153  func (gtid filePosGTID) Last() string {
   154  	return gtid.String()
   155  }
   156  
   157  func init() {
   158  	gtidParsers[FilePosFlavorID] = parseFilePosGTID
   159  	gtidSetParsers[FilePosFlavorID] = ParseFilePosGTIDSet
   160  	flavors[FilePosFlavorID] = newFilePosFlavor
   161  }