Skip to content
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

simplify the if/else logic #179

Merged
merged 13 commits into from
Jan 25, 2019
25 changes: 14 additions & 11 deletions pkg/diff/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,27 +281,30 @@ func GenerateCheckJob(table *TableInstance, splitField, limits string, chunkSize
chunks = chunks[1:]

args := make([]interface{}, 0, 2)
var condition1, condition2 string
var (
condition1 = "TRUE"
condition2 = "TRUE"
)
if !chunk.noBegin {
format := "`%s`%s > ?"
if chunk.containBegin {
condition1 = fmt.Sprintf("`%s`%s >= ?", column.Name, collation)
} else {
condition1 = fmt.Sprintf("`%s`%s > ?", column.Name, collation)
format = "`%s`%s >= ?"
}

condition1 = fmt.Sprintf(format, column.Name, collation)
args = append(args, chunk.begin)
} else {
condition1 = "TRUE"
}

if !chunk.noEnd {
format := "`%s`%s < ?"
if chunk.containEnd {
condition2 = fmt.Sprintf("`%s`%s <= ?", column.Name, collation)
} else {
condition2 = fmt.Sprintf("`%s`%s < ?", column.Name, collation)
format = "`%s`%s <= ?"
}

condition2 = fmt.Sprintf(format, column.Name, collation)
args = append(args, chunk.end)
} else {
condition2 = "TRUE"
}

where := fmt.Sprintf("(%s AND %s AND %s)", condition1, condition2, limits)

log.Debugf("%s.%s create dump job, where: %s, begin: %v, end: %v", table.Schema, table.Table, where, chunk.begin, chunk.end)
Expand Down
32 changes: 19 additions & 13 deletions pkg/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,30 +506,36 @@ func compareData(map1, map2 map[string][]byte, null1, null2 map[string]bool, ord
return false, 0, errors.Errorf("don't have key %s", col.Name.O)
}
if needQuotes(col.FieldType) {
if string(data1) > string(data2) {
cmp = 1
break
} else if string(data1) < string(data2) {
cmp = -1
break
} else {

strData1 := string(data1)
strData2 := string(data2)

if strData1 == strData2 {
lewgun marked this conversation as resolved.
Show resolved Hide resolved
continue
}

cmp = -1
if strData1 > strData2 {
cmp = 1
}
break

} else {
num1, err1 := strconv.ParseFloat(string(data1), 64)
num2, err2 := strconv.ParseFloat(string(data2), 64)
if err1 != nil || err2 != nil {
return false, 0, errors.Errorf("convert %s, %s to float failed, err1: %v, err2: %v", string(data1), string(data2), err1, err2)
}

if num1 == num2 {
continue
}

cmp = -1
if num1 > num2 {
cmp = 1
break
} else if num1 < num2 {
cmp = -1
break
} else {
continue
}
break
}
}

Expand Down
23 changes: 14 additions & 9 deletions pkg/diff/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ func (r RowDatas) Less(i, j int) bool {
data1 = r.Rows[i].Data[col.Name.O]
data2 = r.Rows[j].Data[col.Name.O]
if needQuotes(col.FieldType) {
if string(data1) > string(data2) {
return false
} else if string(data1) < string(data2) {
return true
} else {
strData1 := string(data1)
strData2 := string(data2)

if strData1 == strData2 {
lonng marked this conversation as resolved.
Show resolved Hide resolved
// `NULL` is less than ""
if r.Rows[i].Null[col.Name.O] {
return true
Expand All @@ -55,19 +54,25 @@ func (r RowDatas) Less(i, j int) bool {
}
continue
}
if strData1 > strData2 {
return false
}
return true
} else {
Copy link
Contributor

@amyangfei amyangfei Jan 19, 2019

Choose a reason for hiding this comment

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

recommend usage from golint: if block ends with a return statement, so drop this else and outdent its block

num1, err1 := strconv.ParseFloat(string(data1), 64)
num2, err2 := strconv.ParseFloat(string(data2), 64)
if err1 != nil || err2 != nil {
log.Fatalf("convert %s, %s to float failed, err1: %v, err2: %v", string(data1), string(data2), err1, err2)
}

if num1 == num2 {
continue
}
if num1 > num2 {
return false
} else if num1 < num2 {
return true
} else {
continue
}
return true

}
}

Expand Down