-
Notifications
You must be signed in to change notification settings - Fork 15
Support replace and upsert operations #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f68719
Support replace and upsert operations
savolgin 34c019f
* Small perfomance fix
savolgin 854ae06
Small performance fix
savolgin 1a12727
Change bucket_id name check
savolgin 078a812
Update changelog
savolgin 93c09d1
remove unused variable
e303fd6
Merge branch 'master' into upsert_and_replace
dokshina 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,101 @@ | ||
| local checks = require('checks') | ||
| local errors = require('errors') | ||
| local vshard = require('vshard') | ||
|
|
||
| local call = require('crud.common.call') | ||
| local registry = require('crud.common.registry') | ||
| local utils = require('crud.common.utils') | ||
|
|
||
| require('crud.common.checkers') | ||
|
|
||
| local ReplaceError = errors.new_class('Replace', { capture_stack = false }) | ||
|
|
||
| local replace = {} | ||
|
|
||
| local REPLACE_FUNC_NAME = '__replace' | ||
|
|
||
| local function call_replace_on_storage(space_name, tuple) | ||
| checks('string', 'table') | ||
|
|
||
| local space = box.space[space_name] | ||
| if space == nil then | ||
| return nil, ReplaceError:new("Space %q doesn't exists", space_name) | ||
| end | ||
|
|
||
| return space:replace(tuple) | ||
| end | ||
|
|
||
| function replace.init() | ||
| registry.add({ | ||
| [REPLACE_FUNC_NAME] = call_replace_on_storage, | ||
| }) | ||
| end | ||
|
|
||
| --- Insert or replace a tuple in the specifed space | ||
| -- | ||
| -- @function call | ||
| -- | ||
| -- @param string space_name | ||
| -- A space name | ||
| -- | ||
| -- @param table obj | ||
| -- Tuple object (according to space format) | ||
| -- | ||
| -- @tparam ?number opts.timeout | ||
| -- Function call timeout | ||
| -- | ||
| -- @return[1] object | ||
| -- @treturn[2] nil | ||
| -- @treturn[2] table Error description | ||
| -- | ||
| function replace.call(space_name, obj, opts) | ||
| checks('string', 'table', { | ||
| timeout = '?number', | ||
| }) | ||
|
|
||
| opts = opts or {} | ||
|
|
||
| local space = utils.get_space(space_name, vshard.router.routeall()) | ||
| if space == nil then | ||
| return nil, ReplaceError:new("Space %q doesn't exists", space_name) | ||
| end | ||
|
|
||
| local space_format = space:format() | ||
| -- compute default buckect_id | ||
| local tuple, err = utils.flatten(obj, space_format) | ||
| if err ~= nil then | ||
| return nil, ReplaceError:new("Object is specified in bad format: %s", err) | ||
| end | ||
|
|
||
| local key = utils.extract_key(tuple, space.index[0].parts) | ||
|
|
||
| local bucket_id = vshard.router.bucket_id_strcrc32(key) | ||
| local replicaset, err = vshard.router.route(bucket_id) | ||
| if replicaset == nil then | ||
| return nil, ReplaceError:new("Failed to get replicaset for bucket_id %s: %s", bucket_id, err.err) | ||
| end | ||
|
|
||
| local tuple, err = utils.flatten(obj, space_format, bucket_id) | ||
| if err ~= nil then | ||
| return nil, ReplaceError:new("Object is specified in bad format: %s", err) | ||
| end | ||
|
|
||
| local results, err = call.rw(REPLACE_FUNC_NAME, {space_name, tuple}, { | ||
| replicasets = {replicaset}, | ||
| timeout = opts.timeout, | ||
| }) | ||
|
|
||
| if err ~= nil then | ||
| return nil, ReplaceError:new("Failed to replace: %s", err) | ||
| end | ||
|
|
||
| local tuple = results[replicaset.uuid] | ||
| local object, err = utils.unflatten(tuple, space_format) | ||
| if err ~= nil then | ||
| return nil, ReplaceError:new("Received tuple that doesn't match space format: %s", err) | ||
| end | ||
|
|
||
| return object | ||
| end | ||
|
|
||
| return replace |
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.
a nit: since we already have
system_fieldsconception, maybe it can be