Skip to content

Commit

Permalink
simplify the if/else logic (#179)
Browse files Browse the repository at this point in the history
refactor to simplify the if/else logic
  • Loading branch information
lewgun authored and lonng committed Jan 25, 2019
1 parent 34c808e commit 695f68a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 40 deletions.
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 len(strData1) == len(strData2) && strData1 == strData2 {
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
36 changes: 20 additions & 16 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 {
// `NULL` is less than ""
if r.Rows[i].Null[col.Name.O] {
return true
Expand All @@ -55,20 +54,25 @@ func (r RowDatas) Less(i, j int) bool {
}
continue
}
} else {
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 {
if strData1 > strData2 {
return false
} else if num1 < num2 {
return true
} else {
continue
}
return true
}
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
}
return true

}

return true
Expand Down

0 comments on commit 695f68a

Please sign in to comment.