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

*: clean up code for insert/update statement #8867

Merged
merged 3 commits into from
Dec 29, 2018
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
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
}
jackysp marked this conversation as resolved.
Show resolved Hide resolved
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)
}
jackysp marked this conversation as resolved.
Show resolved Hide resolved

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 {
jackysp marked this conversation as resolved.
Show resolved Hide resolved
entryKey, err1 := t.genIndexKeyStr(indexVals)
if err1 != nil {
return 0, errors.Trace(err1)
Expand Down