-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
filepos_gtid.go
161 lines (136 loc) · 3.66 KB
/
filepos_gtid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mysql
import (
"fmt"
"strconv"
"strings"
)
// FilePosFlavorID is the string identifier for the filePos flavor.
const FilePosFlavorID = "FilePos"
// parsefilePosGTID is registered as a GTID parser.
func parseFilePosGTID(s string) (GTID, error) {
// Split into parts.
parts := strings.Split(s, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid FilePos GTID (%v): expecting file:pos", s)
}
pos, err := strconv.ParseUint(parts[1], 0, 32)
if err != nil {
return nil, fmt.Errorf("invalid FilePos GTID (%v): expecting pos to be an integer", s)
}
return filePosGTID{
file: parts[0],
pos: uint32(pos),
}, nil
}
// ParseFilePosGTIDSet is registered as a GTIDSet parser.
func ParseFilePosGTIDSet(s string) (GTIDSet, error) {
gtid, err := parseFilePosGTID(s)
if err != nil {
return nil, err
}
return gtid.(filePosGTID), err
}
// filePosGTID implements GTID.
type filePosGTID struct {
file string
pos uint32
}
// String implements GTID.String().
func (gtid filePosGTID) String() string {
return fmt.Sprintf("%s:%d", gtid.file, gtid.pos)
}
// Flavor implements GTID.Flavor().
func (gtid filePosGTID) Flavor() string {
return FilePosFlavorID
}
// SequenceDomain implements GTID.SequenceDomain().
func (gtid filePosGTID) SequenceDomain() any {
return nil
}
// SourceServer implements GTID.SourceServer().
func (gtid filePosGTID) SourceServer() any {
return nil
}
// SequenceNumber implements GTID.SequenceNumber().
func (gtid filePosGTID) SequenceNumber() any {
return nil
}
// GTIDSet implements GTID.GTIDSet().
func (gtid filePosGTID) GTIDSet() GTIDSet {
return gtid
}
// ContainsGTID implements GTIDSet.ContainsGTID().
func (gtid filePosGTID) ContainsGTID(other GTID) bool {
if other == nil {
return true
}
filePosOther, ok := other.(filePosGTID)
if !ok {
return false
}
if filePosOther.file < gtid.file {
return true
}
if filePosOther.file > gtid.file {
return false
}
return filePosOther.pos <= gtid.pos
}
// Contains implements GTIDSet.Contains().
func (gtid filePosGTID) Contains(other GTIDSet) bool {
if other == nil {
return false
}
filePosOther, ok := other.(filePosGTID)
if !ok {
return false
}
return gtid.ContainsGTID(filePosOther)
}
// Equal implements GTIDSet.Equal().
func (gtid filePosGTID) Equal(other GTIDSet) bool {
filePosOther, ok := other.(filePosGTID)
if !ok {
return false
}
return gtid == filePosOther
}
// AddGTID implements GTIDSet.AddGTID().
func (gtid filePosGTID) AddGTID(other GTID) GTIDSet {
filePosOther, ok := other.(filePosGTID)
if !ok {
return gtid
}
return filePosOther
}
// Union implements GTIDSet.Union().
func (gtid filePosGTID) Union(other GTIDSet) GTIDSet {
filePosOther, ok := other.(filePosGTID)
if !ok || gtid.Contains(other) {
return gtid
}
return filePosOther
}
// Last returns last filePosition
// For filePos based GTID we have only one position
// here we will just return the current filePos
func (gtid filePosGTID) Last() string {
return gtid.String()
}
func init() {
gtidParsers[FilePosFlavorID] = parseFilePosGTID
gtidSetParsers[FilePosFlavorID] = ParseFilePosGTIDSet
flavors[FilePosFlavorID] = newFilePosFlavor
}