Skip to content

Commit

Permalink
*: clean up code for insert/update statement (#8867)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp authored Dec 29, 2018
1 parent 5e0a4f1 commit 0e89c02
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 13 deletions.
4 changes: 1 addition & 3 deletions executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,19 @@ func updateRecord(ctx sessionctx.Context, h int64, oldData, newData []types.Datu
// 5. If handle changed, remove the old then add the new record, otherwise update the record.
var err error
if handleChanged {
skipHandleCheck := false
if sc.DupKeyAsWarning {
// For `UPDATE IGNORE`/`INSERT IGNORE ON DUPLICATE KEY UPDATE`
// If the new handle exists, this will avoid to remove the record.
err = tables.CheckHandleExists(ctx, t, newHandle, newData)
if err != nil {
return false, handleChanged, newHandle, errors.Trace(err)
}
skipHandleCheck = true
}
if err = t.RemoveRecord(ctx, h, oldData); err != nil {
return false, false, 0, errors.Trace(err)
}
// the `affectedRows` is increased when adding new record.
newHandle, err = t.AddRecord(ctx, newData, skipHandleCheck)
newHandle, err = t.AddRecord(ctx, newData, sc.DupKeyAsWarning)
if err != nil {
return false, false, 0, errors.Trace(err)
}
Expand Down
2 changes: 0 additions & 2 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const (
PresumeKeyNotExistsError
// BinlogInfo contains the binlog data and client.
BinlogInfo
// Skip existing check when "prewrite".
SkipCheckForWrite
// SchemaChecker is used for checking schema-validity.
SchemaChecker
// IsolationLevel sets isolation level for current transaction. The default level is SI.
Expand Down
5 changes: 4 additions & 1 deletion store/tikv/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (s *tikvSnapshot) SetPriority(priority int) {
// BatchGet gets all the keys' value from kv-server and returns a map contains key/value pairs.
// The map will not contain nonexistent keys.
func (s *tikvSnapshot) BatchGet(keys []kv.Key) (map[string][]byte, error) {
m := make(map[string][]byte)
if len(keys) == 0 {
return m, nil
}
metrics.TiKVTxnCmdCounter.WithLabelValues("batch_get").Inc()
start := time.Now()
defer func() { metrics.TiKVTxnCmdHistogram.WithLabelValues("batch_get").Observe(time.Since(start).Seconds()) }()
Expand All @@ -78,7 +82,6 @@ func (s *tikvSnapshot) BatchGet(keys []kv.Key) (map[string][]byte, error) {

// Create a map to collect key-values from region servers.
var mu sync.Mutex
m := make(map[string][]byte)
err := s.batchGetKeysByRegions(bo, bytesKeys, func(k, v []byte) {
if len(v) == 0 {
return
Expand Down
8 changes: 1 addition & 7 deletions table/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,6 @@ func (t *tableCommon) AddRecord(ctx sessionctx.Context, r []types.Datum, skipHan
}

sessVars := ctx.GetSessionVars()
// when LightningMode or BatchCheck is true,
// no needs to check the key constrains, so we names the variable skipCheck.
skipCheck := sessVars.LightningMode || ctx.GetSessionVars().StmtCtx.BatchCheck
if skipCheck {
txn.SetOption(kv.SkipCheckForWrite, true)
}

rm, err := t.getRollbackableMemStore(ctx)
// Insert new entries into indices.
Expand Down Expand Up @@ -565,7 +559,7 @@ func (t *tableCommon) addIndices(ctx sessionctx.Context, recordID int64, r []typ
return 0, errors.Trace(err2)
}
var dupKeyErr error
if !skipCheck && (v.Meta().Unique || v.Meta().Primary) {
if !skipCheck && v.Meta().Unique {
entryKey, err1 := t.genIndexKeyStr(indexVals)
if err1 != nil {
return 0, errors.Trace(err1)
Expand Down

0 comments on commit 0e89c02

Please sign in to comment.