@@ -24,8 +24,15 @@ import (
2424)
2525
2626var (
27+ // Regex Pattern to match a Singe Value Interval e.g. 1 in uuid:1
2728 singleValueInterval = regexp .MustCompile ("^([0-9]+)$" )
28- multiValueInterval = regexp .MustCompile ("^([0-9]+)[-]([0-9]+)$" )
29+
30+ // Regex Pattern to match a Multi Value Interval e.g. 1-5 in uuid:1-5
31+ multiValueInterval = regexp .MustCompile ("^([0-9]+)[-]([0-9]+)$" )
32+
33+ // Regex Pattern to match GTID tags
34+ // Tag must start with a letter e.g. tag1 in uuid:tag1:1-5
35+ tagRegex = regexp .MustCompile ("^[a-z_][a-z0-9_]{0,31}" )
2936)
3037
3138type TagInterval struct {
@@ -57,97 +64,74 @@ func ParseOracleGtidSetEntry(gtidRangeString string) (*OracleGtidSetEntry, error
5764 return nil , fmt .Errorf ("Cannot parse OracleGtidSetEntry from %s" , gtidRangeString )
5865 }
5966
60- first_part := gtid_str [0 ]
61- second_part := gtid_str [1 ]
62-
63- if first_part == "" {
64- return nil , fmt .Errorf ("Unexpected UUID: %s" , first_part )
67+ if gtid_str [0 ] == "" {
68+ return nil , fmt .Errorf ("Unexpected UUID: %s" , gtid_str [0 ])
6569 }
6670
67- if second_part == "" {
68- return nil , fmt .Errorf ("Unexpected GTID range: %s" , second_part )
71+ if gtid_str [ 1 ] == "" {
72+ return nil , fmt .Errorf ("Unexpected GTID range: %s" , gtid_str [ 1 ] )
6973 }
7074
7175 // UUID is the first part
72- uuid := first_part
76+ uuid := gtid_str [ 0 ]
7377
7478 // Split the non-UUID parts into multiple blocks
75- s := strings .SplitN (second_part , ":" , - 1 )
79+ s := strings .SplitN (gtid_str [ 1 ] , ":" , - 1 )
7680
77- var auto_iv string // default interval
78- var tag string // any tag
79- var iv []string // any interval
81+ // Initialize the tag and interval
82+ var default_iv string // Default interval
8083 var tag_ivs []TagInterval // Full tagged interval
81-
82- // Regex Patterns to match tag and interval s
83- re_tag := regexp .MustCompile ("^[a-z_][a-z0-9_]" ) // tag must start with a letter
84+ var ti TagInterval // Current tag interval
8485
8586 for i := range s {
8687
8788 // If it is a GTID tag
88- if re_tag .MatchString (s [i ]) {
89-
90- // Finalize previous tag before processing new tag
91- if len (tag ) != 0 {
92-
93- if len (iv ) == 0 {
94- return nil , fmt .Errorf ("Invalid format: Found a tag without any intervals" )
95- }
96-
97- var ti TagInterval
98- ti .Tag = tag
99- ti .Interval = iv
89+ if tagRegex .MatchString (s [i ]) {
10090
91+ if (ti .Tag != "" ) && (len (ti .Interval ) == 0 ) {
92+ // If the tag is already set and we got another tag
93+ return nil , fmt .Errorf ("Invalid format" )
94+ } else if (ti .Tag == "" ) && (len (ti .Interval ) != 0 ) {
95+ // If the tag is not set and we already have the interval set
96+ return nil , fmt .Errorf ("Invalid format" )
97+ } else {
98+ // Now process the new tag
99+ ti .Tag = s [i ]
100+ // Reset iv for the current tag
101+ ti .Interval = nil
102+ // Append the new tag to tag_ivs
101103 tag_ivs = append (tag_ivs , ti )
102104 }
103-
104- // Reset iv for next tag
105- iv = nil
106-
107- // Now process the new tag
108- tag = s [i ]
109-
110105 } else {
111-
112106 // If it is an GTID interval
113107 if singleValueInterval .MatchString (s [i ]) || multiValueInterval .MatchString (s [i ]) {
114-
115108 // If it is an empty tag, add it to default interval
116- if len (tag ) == 0 {
117- auto_iv += ":" + s [i ]
109+ if len (ti . Tag ) == 0 {
110+ default_iv += ":" + s [i ]
118111 } else {
119- // If tag is mentioned
120- iv = append (iv , s [i ])
112+ // If tag is already set, add it to the tag interval
113+ ti .Interval = append (ti .Interval , s [i ])
114+ tag_ivs [len (tag_ivs )- 1 ].Interval = append (tag_ivs [len (tag_ivs )- 1 ].Interval , s [i ])
121115 }
122-
123116 } else {
117+ // Regex failed, invalid format
124118 return nil , fmt .Errorf ("Invalid format" )
125119 }
126120 }
127121 }
128122
129- // Finalize the last tag
130- if len (tag ) != 0 {
131-
132- // If there are no intervals for the last tag
133- if len (iv ) == 0 {
134- return nil , fmt .Errorf ("Invalid format: Found a tag without any intervals" )
135- }
136-
137- // Create a new tag interval and append it to the list
138- var ti TagInterval
139- ti .Tag = tag
140- ti .Interval = iv
141- tag_ivs = append (tag_ivs , ti )
123+ // If the interval of the last tag is empty, then it is an invalid format
124+ // eg: "UUID:1-5139::tag1:"
125+ if (ti .Tag != "" ) && (len (tag_ivs [len (tag_ivs )- 1 ].Interval ) == 0 ) {
126+ return nil , fmt .Errorf ("Invalid format" )
142127 }
143128
144129 // Don't append ':' for the first interval in the default set
145- if len (auto_iv ) != 0 {
146- after , _ := strings .CutPrefix (auto_iv , ":" )
147- auto_iv = after
130+ if len (default_iv ) != 0 {
131+ default_iv , _ = strings .CutPrefix (default_iv , ":" )
148132 }
149133
150- entry := OracleGtidSetEntry {UUID : uuid , DefaultIv : auto_iv , TaggedIv : tag_ivs }
134+ entry := OracleGtidSetEntry {UUID : uuid , DefaultIv : default_iv , TaggedIv : tag_ivs }
151135
152136 return & entry , nil
153137}
@@ -164,7 +148,7 @@ func NewOracleGtidSetEntry(gtidRangeString string) (*OracleGtidSetEntry, error)
164148 return gtidRange , nil
165149}
166150
167- // String returns a user-friendly string representation of this entry
151+ // String() returns a user-friendly string representation of this entry
168152func (this * OracleGtidSetEntry ) String () string {
169153
170154 var res string
@@ -177,18 +161,18 @@ func (this *OracleGtidSetEntry) String() string {
177161 res += ":" + this .DefaultIv
178162 }
179163
180- // Tagged ranges are added in the end of the Gtid_set
164+ // Tagged ranges are added in the end
181165 for _ , v := range this .TaggedIv {
182166 res += ":" + v .Tag
183- for _ , iv := range v .Interval {
184- res += ":" + iv
167+ if len ( v .Interval ) != 0 {
168+ res += ":" + strings . Join ( v . Interval , ":" )
185169 }
186170 }
187171 return res
188172}
189173
190174/*
191- String returns a user-friendly individual string representation of the gtid set
175+ Explode() returns a list of individual gtids that are represented by this entry.
192176
193177Example:
194178Explode of the GTID set "48ebed33-0d12-11ef-a3ec-ac198e4551c8:1-3:7:tag1:1-2:10-12:tag2:74-75:78:81"
@@ -229,35 +213,30 @@ func (this *OracleGtidSetEntry) Explode() (result [](*OracleGtidSetEntry)) {
229213 }
230214
231215 // Appends tagged intervals to the result
232- var AppendTaggedInterval = func (tag * string , interval string ) {
233-
216+ var AppendTaggedInterval = func (tag string , interval string ) {
234217 intervals := strings .Split (interval , ":" )
235218 for _ , interval := range intervals {
236-
237219 // Multi-value interval
238220 if submatch := multiValueInterval .FindStringSubmatch (interval ); submatch != nil {
239-
240221 intervalStart , _ := strconv .Atoi (submatch [1 ])
241222 intervalEnd , _ := strconv .Atoi (submatch [2 ])
242223 for i := intervalStart ; i <= intervalEnd ; i ++ {
243- var ti TagInterval
244- ti .Tag = * tag
245- ti .Interval = append (ti .Interval , fmt .Sprintf ("%d" , i ))
246224
247- var taggedIv []TagInterval
248- taggedIv = append (taggedIv , ti )
225+ ti := TagInterval {
226+ Tag : tag ,
227+ Interval : []string {fmt .Sprintf ("%d" , i )}}
228+ taggedIv := []TagInterval {ti }
249229
250230 entry := OracleGtidSetEntry {UUID : this .UUID , TaggedIv : taggedIv }
251231 result = append (result , & entry )
252232 }
253233 } else if submatch := singleValueInterval .FindStringSubmatch (interval ); submatch != nil {
254- // Single-value interval
255- var ti TagInterval
256- ti .Tag = * tag
257- ti .Interval = append (ti .Interval , interval )
258234
259- var taggedIv []TagInterval
260- taggedIv = append (taggedIv , ti )
235+ // Single-value interval
236+ ti := TagInterval {
237+ Tag : tag ,
238+ Interval : []string {interval }}
239+ taggedIv := []TagInterval {ti }
261240
262241 entry := OracleGtidSetEntry {UUID : this .UUID , TaggedIv : taggedIv }
263242 result = append (result , & entry )
@@ -271,7 +250,7 @@ func (this *OracleGtidSetEntry) Explode() (result [](*OracleGtidSetEntry)) {
271250 // Process tagged intervals next
272251 for _ , v := range this .TaggedIv {
273252 for _ , iv := range v .Interval {
274- AppendTaggedInterval (& v .Tag , iv )
253+ AppendTaggedInterval (v .Tag , iv )
275254 }
276255 }
277256 return result
0 commit comments