Skip to content

Commit 3e59511

Browse files
committed
add my suggestions
Signed-off-by: lance6716 <[email protected]>
1 parent c87af9a commit 3e59511

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

replication/event.go

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -246,27 +246,17 @@ const (
246246
// decode_nsids_format in mysql/mysql-server
247247
// https://github.com/mysql/mysql-server/blob/61a3a1d8ef15512396b4c2af46e922a19bf2b174/sql/rpl_gtid_set.cc#L1363-L1378
248248
func decodeSid(data []byte) (format GtidFormat, sidnr uint64) {
249-
gtidinfo := make([]byte, 8)
250-
copy(gtidinfo, data[:8])
251-
252-
if gtidinfo[7] == 1 {
249+
if data[7] == 1 {
253250
format = GtidFormatTagged
254251
}
255252

256253
if format == GtidFormatTagged {
257-
sid_mask := []byte{0, 255, 255, 255, 255, 255, 255, 0}
258-
259-
// Apply the mask
260-
for i, _ := range gtidinfo[:8] {
261-
gtidinfo[i] &= sid_mask[i]
262-
}
263-
gtidinfo = append(gtidinfo, 0)
264-
265-
// sidnr
266-
sidnr = binary.LittleEndian.Uint64(gtidinfo[1:])
254+
masked := make([]byte, 8)
255+
copy(masked, data[1:7])
256+
sidnr = binary.LittleEndian.Uint64(masked)
267257
return
268258
}
269-
sidnr = binary.LittleEndian.Uint64(gtidinfo)
259+
sidnr = binary.LittleEndian.Uint64(data[:8])
270260
return
271261
}
272262

@@ -277,45 +267,53 @@ func (e *PreviousGTIDsEvent) Decode(data []byte) error {
277267
pos += 8
278268

279269
previousGTIDSets := make([]string, uuidCount)
270+
280271
currentSetnr := 0
272+
var buf strings.Builder
281273
for range previousGTIDSets {
282274
uuid := e.decodeUuid(data[pos : pos+16])
283275
pos += 16
284-
isTag := false
285276
var tag string
286277
if format == GtidFormatTagged {
287278
tagLength := int(data[pos]) / 2
288279
pos += 1
289280
if tagLength > 0 { // 0 == no tag, >0 == tag
290-
isTag = true
291281
tag = string(data[pos : pos+tagLength])
292282
pos += tagLength
293283
}
294284
}
285+
286+
if len(tag) > 0 {
287+
buf.WriteString(":")
288+
buf.WriteString(tag)
289+
} else {
290+
if currentSetnr != 0 {
291+
buf.WriteString(",")
292+
}
293+
buf.WriteString(uuid)
294+
currentSetnr += 1
295+
}
296+
295297
sliceCount := binary.LittleEndian.Uint16(data[pos : pos+8])
296298
pos += 8
297-
intervals := make([]string, sliceCount)
298-
for i := range intervals {
299+
for range sliceCount {
300+
buf.WriteString(":")
301+
299302
start := e.decodeInterval(data[pos : pos+8])
300303
pos += 8
301304
stop := e.decodeInterval(data[pos : pos+8])
302305
pos += 8
303-
interval := ""
304306
if stop == start+1 {
305-
interval = fmt.Sprintf("%d", start)
307+
fmt.Fprintf(&buf, "%d", start)
306308
} else {
307-
interval = fmt.Sprintf("%d-%d", start, stop-1)
309+
fmt.Fprintf(&buf, "%d-%d", start, stop-1)
308310
}
309-
intervals[i] = interval
310311
}
311-
if isTag {
312-
previousGTIDSets[currentSetnr-1] += fmt.Sprintf(":%s:%s", tag, strings.Join(intervals, ":"))
313-
} else {
314-
previousGTIDSets[currentSetnr] = fmt.Sprintf("%s:%s", uuid, strings.Join(intervals, ":"))
312+
if len(tag) == 0 {
315313
currentSetnr += 1
316314
}
317315
}
318-
e.GTIDSets = strings.Join(previousGTIDSets[:currentSetnr], ",")
316+
e.GTIDSets = buf.String()
319317
return nil
320318
}
321319

0 commit comments

Comments
 (0)