Skip to content

Add MysqlGTIDSet.Add() and Minus() methods #667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions mysql/mysql_gtid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"fmt"
"io"
"math"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -234,11 +235,15 @@ func (s *UUIDSet) MinusInterval(in IntervalSlice) {
i, j := 0, 0
var minuend Interval
var subtrahend Interval
for j < len(in) && i < len(s.Intervals) {
for i < len(s.Intervals) {
if minuend.Stop != s.Intervals[i].Stop { // `i` changed?
minuend = s.Intervals[i]
}
subtrahend = in[j]
if j < len(in) {
subtrahend = in[j]
} else {
subtrahend = Interval{math.MaxInt64, math.MaxInt64}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can

append minuend to n
check if i+1 out of range and append s.Intervals[i+1:]... to n

it's OK not to change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There were 2 bugs in 50 lines :(
So, I would prefer not to increase cyclomatic complexity of this code and keep it as it in PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait @atercattus 's review as this function is complex

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @atercattus have you had a chance to look into this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I'm going to merge this PR if he does not respond it tomorrow)

}

if minuend.Stop <= subtrahend.Start {
// no overlapping
Expand All @@ -248,33 +253,25 @@ func (s *UUIDSet) MinusInterval(in IntervalSlice) {
// no overlapping
j++
} else {
if minuend.Start < subtrahend.Start && minuend.Stop < subtrahend.Stop {
if minuend.Start < subtrahend.Start && minuend.Stop <= subtrahend.Stop {
n = append(n, Interval{minuend.Start, subtrahend.Start})
i++
} else if minuend.Start > subtrahend.Start && minuend.Stop > subtrahend.Stop {
} else if minuend.Start >= subtrahend.Start && minuend.Stop > subtrahend.Stop {
minuend = Interval{subtrahend.Stop, minuend.Stop}
j++
} else if minuend.Start >= subtrahend.Start && minuend.Stop <= subtrahend.Stop {
// minuend is completely removed
i++
} else {
} else if minuend.Start < subtrahend.Start && minuend.Stop > subtrahend.Stop {
n = append(n, Interval{minuend.Start, subtrahend.Start})
minuend = Interval{subtrahend.Stop, minuend.Stop}
j++
} else {
panic("should never be here")
}
}
}

lastSub := in[len(in)-1]
for ; i < len(s.Intervals); i++ {
minuend = s.Intervals[i]
if minuend.Start < lastSub.Stop {
n = append(n, Interval{lastSub.Stop, minuend.Stop})
} else {
n = append(n, s.Intervals[i])
}
}

s.Intervals = n.Normalize()
}

Expand Down
3 changes: 3 additions & 0 deletions mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ func (t *mysqlTestSuite) TestMysqlGTIDMinus(c *check.C) {
{"3E11FA47-71CA-11E1-9E33-C80AA9429562:20-57:60-90", "3E11FA47-71CA-11E1-9E33-C80AA9429562:23", "3E11FA47-71CA-11E1-9E33-C80AA9429562:20-22:24-57:60-90"},
{"3E11FA47-71CA-11E1-9E33-C80AA9429562:20-57:60-90", "3E11FA47-71CA-11E1-9E33-C80AA9429562:22-70", "3E11FA47-71CA-11E1-9E33-C80AA9429562:20-21:71-90"},
{"3E11FA47-71CA-11E1-9E33-C80AA9429562:28-57", "3E11FA47-71CA-11E1-9E33-C80AA9429562:28-57", ""},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found a failed case

		{"3E11FA47-71CA-11E1-9E33-C80AA9429562:20-21", "3E11FA47-71CA-11E1-9E33-C80AA9429562:21", "3E11FA47-71CA-11E1-9E33-C80AA9429562:20"},

please check it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You right.

New version with changes:

  • merge 2 loops into one, so it will be easier to reason about
  • explicit IF conditions to make it clear which cases are handled in each branch

Hope, this time it is correct for all inputs

{"3E11FA47-71CA-11E1-9E33-C80AA9429562:20-21", "3E11FA47-71CA-11E1-9E33-C80AA9429562:21", "3E11FA47-71CA-11E1-9E33-C80AA9429562:20"},
{"582A11ED-786C-11EC-ACCC-E0356662B76E:1-209692", "582A11ED-786C-11EC-ACCC-E0356662B76E:1-146519", "582A11ED-786C-11EC-ACCC-E0356662B76E:146520-209692"},
{"582A11ED-786C-11EC-ACCC-E0356662B76E:1-209692", "582A11ED-786C-11EC-ACCC-E0356662B76E:2-146519", "582A11ED-786C-11EC-ACCC-E0356662B76E:1:146520-209692"},
}

for _, tc := range testCases {
Expand Down