-
Notifications
You must be signed in to change notification settings - Fork 286
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
reactor:multiple update executor #481
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
053c513
reactor:multiple update executor
VaderKai da553c8
reactor:multiple update executor
VaderKai 7fa450e
fix:multiple update executor code review
VaderKai 7ff8c65
reactor:multiple executor
VaderKai b617b82
reactor:multiple executor
VaderKai 5fe48b4
reactor:multiple executor
VaderKai 6af1da4
reactor:multiple executor
VaderKai 516077b
reactor:multiple executor test
VaderKai 1666ea3
fix:logic error
VaderKai 62448d5
Merge branch 'master' into feature/multi_update
VaderKai 923aea0
fix:logic error
VaderKai 177396d
Merge remote-tracking branch 'origin/feature/multi_update' into featu…
VaderKai 7ac2c8a
fix:beforeImage same to afterImage
VaderKai 87aab1d
Merge branch 'master' into feature/multi_update
VaderKai bb7e0ea
reactor:multiple executor test
VaderKai b495268
reactor:multiple executor test
VaderKai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package at | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/seata/seata-go/pkg/datasource/sql/exec" | ||
"github.com/seata/seata-go/pkg/datasource/sql/types" | ||
) | ||
|
||
type multiExecutor struct { | ||
baseExecutor | ||
parserCtx *types.ParseContext | ||
execContext *types.ExecContext | ||
} | ||
|
||
// NewMultiExecutor get new multi executor | ||
func NewMultiExecutor(parserCtx *types.ParseContext, execContext *types.ExecContext, hooks []exec.SQLHook) executor { | ||
return &multiExecutor{parserCtx: parserCtx, execContext: execContext, baseExecutor: baseExecutor{hooks: hooks}} | ||
} | ||
|
||
// ExecContext exec SQL, and generate before image and after image | ||
func (m *multiExecutor) ExecContext(ctx context.Context, f exec.CallbackWithNamedValue) (types.ExecResult, error) { | ||
m.beforeHooks(ctx, m.execContext) | ||
|
||
defer func() { | ||
m.afterHooks(ctx, m.execContext) | ||
}() | ||
|
||
beforeImages, err := m.beforeImage(ctx, m.parserCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res, err := f(ctx, m.execContext.Query, m.execContext.NamedValues) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
afterImages, err := m.afterImage(ctx, m.parserCtx, beforeImages) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(afterImages) != len(beforeImages) { | ||
return nil, errors.New("Before image size is not equaled to after image size, probably because you updated the primary keys.") | ||
} | ||
|
||
for i, afterImage := range afterImages { | ||
beforeImage := beforeImages[i] | ||
if len(beforeImage.Rows) != len(afterImage.Rows) { | ||
return nil, errors.New("Before image size is not equaled to after image size, probably because you updated the primary keys.") | ||
} | ||
|
||
m.execContext.TxCtx.RoundImages.AppendBeofreImage(beforeImage) | ||
m.execContext.TxCtx.RoundImages.AppendAfterImage(afterImage) | ||
} | ||
|
||
return res, nil | ||
} | ||
func (m *multiExecutor) beforeImage(ctx context.Context, parseContext *types.ParseContext) ([]*types.RecordImage, error) { | ||
if len(parseContext.MultiStmt) == 0 { | ||
return nil, nil | ||
} | ||
tmpImages := make([]*types.RecordImage, 0) | ||
var err error | ||
|
||
var beforeImages = make([]*types.RecordImage, 0) | ||
VaderKai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, multiStmt := range parseContext.MultiStmt { | ||
switch multiStmt.ExecutorType { | ||
case types.UpdateExecutor: | ||
multiUpdateExec := NewMultiUpdateExecutor(m.parserCtx, m.execContext, m.hooks) | ||
tmpImages, err = multiUpdateExec.beforeImage(ctx) | ||
case types.DeleteExecutor: | ||
//todo use MultiDeleteExecutor | ||
default: | ||
return nil, fmt.Errorf("not support sql %s", m.execContext.Query) | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
beforeImages = append(beforeImages, tmpImages...) | ||
} | ||
|
||
return beforeImages, err | ||
} | ||
|
||
func (m *multiExecutor) afterImage(ctx context.Context, parseContext *types.ParseContext, beforeImages []*types.RecordImage) ([]*types.RecordImage, error) { | ||
if len(parseContext.MultiStmt) == 0 { | ||
return nil, nil | ||
} | ||
tmpImages := make([]*types.RecordImage, 0) | ||
VaderKai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var err error | ||
|
||
var afterImages = make([]*types.RecordImage, 0) | ||
VaderKai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, multiStmt := range parseContext.MultiStmt { | ||
switch multiStmt.ExecutorType { | ||
case types.UpdateExecutor: | ||
multiUpdateExec := NewMultiUpdateExecutor(m.parserCtx, m.execContext, m.hooks) | ||
tmpImages, err = multiUpdateExec.afterImage(ctx, beforeImages) | ||
case types.DeleteExecutor: | ||
// todo use MultiDeleteExecutor | ||
} | ||
afterImages = append(afterImages, tmpImages...) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
return afterImages, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package at |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ExecContext 里面,直接判断该使用 MultiUpdateExeCutor 还是使用 MultiDeleteExeCutor,然后直接使用 executor.ExecContext() 方法。下面的beforeImage和afterImage方法就可以用都删了。这里没有逻辑的话,以后应该会更好维护