|
| 1 | +local checks = require('checks') |
| 2 | +local errors = require('errors') |
| 3 | +local vshard = require('vshard') |
| 4 | + |
| 5 | +local call = require('crud.common.call') |
| 6 | +local registry = require('crud.common.registry') |
| 7 | +local utils = require('crud.common.utils') |
| 8 | + |
| 9 | +require('crud.common.checkers') |
| 10 | + |
| 11 | +local ReplaceError = errors.new_class('Replace', { capture_stack = false }) |
| 12 | + |
| 13 | +local replace = {} |
| 14 | + |
| 15 | +local REPLACE_FUNC_NAME = '__replace' |
| 16 | + |
| 17 | +local function call_replace_on_storage(space_name, tuple) |
| 18 | + checks('string', 'table') |
| 19 | + |
| 20 | + local space = box.space[space_name] |
| 21 | + if space == nil then |
| 22 | + return nil, ReplaceError:new("Space %q doesn't exists", space_name) |
| 23 | + end |
| 24 | + |
| 25 | + return space:replace(tuple) |
| 26 | +end |
| 27 | + |
| 28 | +function replace.init() |
| 29 | + registry.add({ |
| 30 | + [REPLACE_FUNC_NAME] = call_replace_on_storage, |
| 31 | + }) |
| 32 | +end |
| 33 | + |
| 34 | +--- Insert or replace a tuple in the specifed space |
| 35 | +-- |
| 36 | +-- @function call |
| 37 | +-- |
| 38 | +-- @param string space_name |
| 39 | +-- A space name |
| 40 | +-- |
| 41 | +-- @param table obj |
| 42 | +-- Tuple object (according to space format) |
| 43 | +-- |
| 44 | +-- @tparam ?number opts.timeout |
| 45 | +-- Function call timeout |
| 46 | +-- |
| 47 | +-- @return[1] object |
| 48 | +-- @treturn[2] nil |
| 49 | +-- @treturn[2] table Error description |
| 50 | +-- |
| 51 | +function replace.call(space_name, obj, opts) |
| 52 | + checks('string', 'table', { |
| 53 | + timeout = '?number', |
| 54 | + }) |
| 55 | + |
| 56 | + opts = opts or {} |
| 57 | + |
| 58 | + local space = utils.get_space(space_name, vshard.router.routeall()) |
| 59 | + if space == nil then |
| 60 | + return nil, ReplaceError:new("Space %q doesn't exists", space_name) |
| 61 | + end |
| 62 | + |
| 63 | + local space_format = space:format() |
| 64 | + -- compute default buckect_id |
| 65 | + local tuple, err = utils.flatten(obj, space_format) |
| 66 | + if err ~= nil then |
| 67 | + return nil, ReplaceError:new("Object is specified in bad format: %s", err) |
| 68 | + end |
| 69 | + |
| 70 | + local key = utils.extract_key(tuple, space.index[0].parts) |
| 71 | + |
| 72 | + local bucket_id = vshard.router.bucket_id_strcrc32(key) |
| 73 | + local replicaset, err = vshard.router.route(bucket_id) |
| 74 | + if replicaset == nil then |
| 75 | + return nil, ReplaceError:new("Failed to get replicaset for bucket_id %s: %s", bucket_id, err.err) |
| 76 | + end |
| 77 | + |
| 78 | + local tuple, err = utils.flatten(obj, space_format, bucket_id) |
| 79 | + if err ~= nil then |
| 80 | + return nil, ReplaceError:new("Object is specified in bad format: %s", err) |
| 81 | + end |
| 82 | + |
| 83 | + local results, err = call.rw(REPLACE_FUNC_NAME, {space_name, tuple}, { |
| 84 | + replicasets = {replicaset}, |
| 85 | + timeout = opts.timeout, |
| 86 | + }) |
| 87 | + |
| 88 | + if err ~= nil then |
| 89 | + return nil, ReplaceError:new("Failed to replace: %s", err) |
| 90 | + end |
| 91 | + |
| 92 | + local tuple = results[replicaset.uuid] |
| 93 | + local object, err = utils.unflatten(tuple, space_format) |
| 94 | + if err ~= nil then |
| 95 | + return nil, ReplaceError:new("Received tuple that doesn't match space format: %s", err) |
| 96 | + end |
| 97 | + |
| 98 | + return object |
| 99 | +end |
| 100 | + |
| 101 | +return replace |
0 commit comments