Skip to content

Commit

Permalink
executor: cleanup useless code in batch checker (#19511) (#19641)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Sep 3, 2020
1 parent ac0e084 commit 1121f2e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 30 deletions.
22 changes: 3 additions & 19 deletions executor/batch_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,13 @@ import (
"github.com/pingcap/tidb/util/chunk"
)

type keyValue struct {
key kv.Key
value []byte
}

type keyValueWithDupInfo struct {
newKV keyValue
newKey kv.Key
dupErr error
}

type toBeCheckedRow struct {
row []types.Datum
rowValue []byte
handleKey *keyValueWithDupInfo
uniqueKeys []*keyValueWithDupInfo
// t is the table or partition this row belongs to.
Expand Down Expand Up @@ -108,18 +102,11 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D

var handleKey *keyValueWithDupInfo
uniqueKeys := make([]*keyValueWithDupInfo, 0, nUnique)
newRowValue, err := encodeNewRow(ctx, t, row)
if err != nil {
return nil, err
}
// Append record keys and errors.
if handleCol != nil {
handle := row[handleCol.Offset].GetInt64()
handleKey = &keyValueWithDupInfo{
newKV: keyValue{
key: t.RecordKey(handle),
value: newRowValue,
},
newKey: t.RecordKey(handle),
dupErr: kv.ErrKeyExists.FastGenByArgs(strconv.FormatInt(handle, 10), "PRIMARY"),
}
}
Expand Down Expand Up @@ -149,15 +136,12 @@ func getKeysNeedCheckOneRow(ctx sessionctx.Context, t table.Table, row []types.D
return nil, err1
}
uniqueKeys = append(uniqueKeys, &keyValueWithDupInfo{
newKV: keyValue{
key: key,
},
newKey: key,
dupErr: kv.ErrKeyExists.FastGenByArgs(colValStr, v.Meta().Name),
})
}
result = append(result, toBeCheckedRow{
row: row,
rowValue: newRowValue,
handleKey: handleKey,
uniqueKeys: uniqueKeys,
t: t,
Expand Down
12 changes: 6 additions & 6 deletions executor/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ func prefetchUniqueIndices(ctx context.Context, txn kv.Transaction, rows []toBeC
batchKeys := make([]kv.Key, 0, nKeys)
for _, r := range rows {
if r.handleKey != nil {
batchKeys = append(batchKeys, r.handleKey.newKV.key)
batchKeys = append(batchKeys, r.handleKey.newKey)
}
for _, k := range r.uniqueKeys {
batchKeys = append(batchKeys, k.newKV.key)
batchKeys = append(batchKeys, k.newKey)
}
}
return txn.BatchGet(ctx, batchKeys)
Expand All @@ -134,7 +134,7 @@ func prefetchConflictedOldRows(ctx context.Context, txn kv.Transaction, rows []t
batchKeys := make([]kv.Key, 0, len(rows))
for _, r := range rows {
for _, uk := range r.uniqueKeys {
if val, found := values[string(uk.newKV.key)]; found {
if val, found := values[string(uk.newKey)]; found {
handle, err := tablecodec.DecodeHandle(val)
if err != nil {
return err
Expand Down Expand Up @@ -196,7 +196,7 @@ func (e *InsertExec) batchUpdateDupRows(ctx context.Context, newRows [][]types.D

for i, r := range toBeCheckedRows {
if r.handleKey != nil {
handle, err := tablecodec.DecodeRowKey(r.handleKey.newKV.key)
handle, err := tablecodec.DecodeRowKey(r.handleKey.newKey)
if err != nil {
return err
}
Expand All @@ -211,7 +211,7 @@ func (e *InsertExec) batchUpdateDupRows(ctx context.Context, newRows [][]types.D
}

for _, uk := range r.uniqueKeys {
val, err := txn.Get(ctx, uk.newKV.key)
val, err := txn.Get(ctx, uk.newKey)
if err != nil {
if kv.IsErrNotFound(err) {
continue
Expand All @@ -229,7 +229,7 @@ func (e *InsertExec) batchUpdateDupRows(ctx context.Context, newRows [][]types.D
// Data index inconsistent? A unique key provide the handle information, but the
// handle points to nothing.
logutil.BgLogger().Error("get old row failed when insert on dup",
zap.String("uniqueKey", hex.EncodeToString(uk.newKV.key)),
zap.String("uniqueKey", hex.EncodeToString(uk.newKey)),
zap.Int64("handle", handle),
zap.String("toBeInsertedRow", types.DatumsToStrNoErr(r.row)))
}
Expand Down
4 changes: 2 additions & 2 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.D
for i, r := range toBeCheckedRows {
skip := false
if r.handleKey != nil {
_, err := txn.Get(ctx, r.handleKey.newKV.key)
_, err := txn.Get(ctx, r.handleKey.newKey)
if err == nil {
e.ctx.GetSessionVars().StmtCtx.AppendWarning(r.handleKey.dupErr)
continue
Expand All @@ -963,7 +963,7 @@ func (e *InsertValues) batchCheckAndInsert(ctx context.Context, rows [][]types.D
}
}
for _, uk := range r.uniqueKeys {
_, err := txn.Get(ctx, uk.newKV.key)
_, err := txn.Get(ctx, uk.newKey)
if err == nil {
// If duplicate keys were found in BatchGet, mark row = nil.
e.ctx.GetSessionVars().StmtCtx.AppendWarning(uk.dupErr)
Expand Down
6 changes: 3 additions & 3 deletions executor/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func (e *ReplaceExec) replaceRow(ctx context.Context, r toBeCheckedRow) error {
}

if r.handleKey != nil {
handle, err := tablecodec.DecodeRowKey(r.handleKey.newKV.key)
handle, err := tablecodec.DecodeRowKey(r.handleKey.newKey)
if err != nil {
return err
}

if _, err := txn.Get(ctx, r.handleKey.newKV.key); err == nil {
if _, err := txn.Get(ctx, r.handleKey.newKey); err == nil {
rowUnchanged, err := e.removeRow(ctx, txn, handle, r)
if err != nil {
return err
Expand Down Expand Up @@ -147,7 +147,7 @@ func (e *ReplaceExec) replaceRow(ctx context.Context, r toBeCheckedRow) error {
// 3. error: the error.
func (e *ReplaceExec) removeIndexRow(ctx context.Context, txn kv.Transaction, r toBeCheckedRow) (bool, bool, error) {
for _, uk := range r.uniqueKeys {
val, err := txn.Get(ctx, uk.newKV.key)
val, err := txn.Get(ctx, uk.newKey)
if err != nil {
if kv.IsErrNotFound(err) {
continue
Expand Down

0 comments on commit 1121f2e

Please sign in to comment.