|
| 1 | +local checks = require('checks') |
| 2 | +local errors = require('errors') |
| 3 | +local vshard = require('vshard') |
| 4 | + |
| 5 | +local dev_checks = require('crud.common.dev_checks') |
| 6 | +local call = require('crud.common.call') |
| 7 | +local utils = require('crud.common.utils') |
| 8 | +local schema = require('crud.common.schema') |
| 9 | +local Keydef = require('crud.compare.keydef') |
| 10 | + |
| 11 | +local BorderError = errors.new_class('Border', {capture_stack = false}) |
| 12 | + |
| 13 | +local borders = {} |
| 14 | + |
| 15 | +local STAT_FUNC_NAME = '_crud.get_border_on_storage' |
| 16 | + |
| 17 | + |
| 18 | +local function get_border_on_storage(border_name, space_name, index_name, field_names) |
| 19 | + dev_checks('string', 'string', 'string', '?table') |
| 20 | + |
| 21 | + assert(border_name == 'min' or border_name == 'max') |
| 22 | + |
| 23 | + local space = box.space[space_name] |
| 24 | + if space == nil then |
| 25 | + return nil, BorderError:new("Space %q doesn't exist", space_name) |
| 26 | + end |
| 27 | + |
| 28 | + local index = space.index[index_name] |
| 29 | + if index == nil then |
| 30 | + return nil, BorderError:new("Index %q of space doesn't exist", index_name, space_name) |
| 31 | + end |
| 32 | + |
| 33 | + local function get_border(index) |
| 34 | + return index[border_name](index) |
| 35 | + end |
| 36 | + |
| 37 | + return schema.wrap_func_result(space, get_border, {index}, { |
| 38 | + add_space_schema_hash = true, |
| 39 | + field_names = field_names, |
| 40 | + }) |
| 41 | +end |
| 42 | + |
| 43 | +function borders.init() |
| 44 | + _G._crud.get_border_on_storage = get_border_on_storage |
| 45 | +end |
| 46 | + |
| 47 | +local function is_closer(border_name, keydef, tuple, res_tuple) |
| 48 | + assert(border_name == 'min' or border_name == 'max') |
| 49 | + |
| 50 | + if res_tuple == nil then |
| 51 | + return true |
| 52 | + end |
| 53 | + |
| 54 | + local cmp = keydef:compare(tuple, res_tuple) |
| 55 | + |
| 56 | + return border_name == 'min' and cmp < 0 or border_name == 'max' and cmp > 0 |
| 57 | +end |
| 58 | + |
| 59 | +local function get_border(border_name, space_name, index_name, opts) |
| 60 | + checks('string', 'string', '?string', { |
| 61 | + timeout = '?number', |
| 62 | + fields = '?table', |
| 63 | + }) |
| 64 | + |
| 65 | + opts = opts or {} |
| 66 | + |
| 67 | + local space = utils.get_space(space_name, vshard.router.routeall()) |
| 68 | + if space == nil then |
| 69 | + return nil, BorderError:new("Space %q doesn't exist", space_name), true |
| 70 | + end |
| 71 | + |
| 72 | + if index_name == nil then |
| 73 | + index_name = space.index[0].name |
| 74 | + end |
| 75 | + local index = space.index[index_name] |
| 76 | + if index == nil then |
| 77 | + return nil, BorderError:new("Index %q of space doesn't exist", index_name, space_name) |
| 78 | + end |
| 79 | + |
| 80 | + local primary_index = space.index[0] |
| 81 | + |
| 82 | + local cmp_key_parts = utils.merge_primary_key_parts(index.parts, primary_index.parts) |
| 83 | + local field_names = utils.enrich_field_names_with_cmp_key(opts.fields, cmp_key_parts, space:format()) |
| 84 | + |
| 85 | + local replicasets = vshard.router.routeall() |
| 86 | + local call_opts = { |
| 87 | + mode = 'read', |
| 88 | + replicasets = replicasets, |
| 89 | + timeout = opts.timeout, |
| 90 | + } |
| 91 | + local results, err = call.map( |
| 92 | + STAT_FUNC_NAME, |
| 93 | + {border_name, space_name, index_name, field_names}, |
| 94 | + call_opts |
| 95 | + ) |
| 96 | + |
| 97 | + if err ~= nil then |
| 98 | + return nil, BorderError:new("Failed to get %s: %s", border_name, err) |
| 99 | + end |
| 100 | + |
| 101 | + local keydef = Keydef.new(replicasets, space_name, field_names, index_name) |
| 102 | + |
| 103 | + local tuples = {} |
| 104 | + for _, result in pairs(results) do |
| 105 | + if result[1].err ~= nil then |
| 106 | + return nil, BorderError:new("Failed to get %s: %s", border_name, result.err) |
| 107 | + end |
| 108 | + |
| 109 | + local tuple = result[1].res |
| 110 | + if tuple ~= nil then |
| 111 | + table.insert(tuples, tuple) |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + local res_tuple = nil |
| 116 | + for _, tuple in ipairs(tuples) do |
| 117 | + if tuple ~= nil and is_closer(border_name, keydef, tuple, res_tuple) then |
| 118 | + res_tuple = tuple |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + local result = utils.format_result({res_tuple}, space, field_names) |
| 123 | + |
| 124 | + if opts.fields ~= nil then |
| 125 | + result = utils.cut_rows(result.rows, result.metadata, opts.fields) |
| 126 | + end |
| 127 | + |
| 128 | + return result |
| 129 | +end |
| 130 | + |
| 131 | +--- Find the minimum value in the specified index |
| 132 | +-- |
| 133 | +-- @function min |
| 134 | +-- |
| 135 | +-- @param string space_name |
| 136 | +-- A space name |
| 137 | +-- |
| 138 | +-- @param ?string index_name |
| 139 | +-- An index name (by default, primary index is used) |
| 140 | +-- |
| 141 | +-- @tparam ?number opts.timeout |
| 142 | +-- Function call timeout |
| 143 | +-- |
| 144 | +-- @tparam ?table opts.fields |
| 145 | +-- Field names for getting only a subset of fields |
| 146 | +-- |
| 147 | +-- @return[1] result |
| 148 | +-- @treturn[2] nil |
| 149 | +-- @treturn[2] table Error description |
| 150 | +function borders.min(space_name, index_name, opts) |
| 151 | + return get_border('min', space_name, index_name, opts) |
| 152 | +end |
| 153 | + |
| 154 | +--- Find the maximum value in the specified index |
| 155 | +-- |
| 156 | +-- @function min |
| 157 | +-- |
| 158 | +-- @param string space_name |
| 159 | +-- A space name |
| 160 | +-- |
| 161 | +-- @param ?string index_name |
| 162 | +-- An index name (by default, primary index is used) |
| 163 | +-- |
| 164 | +-- @tparam ?number opts.timeout |
| 165 | +-- Function call timeout |
| 166 | +-- |
| 167 | +-- @tparam ?table opts.fields |
| 168 | +-- Field names for getting only a subset of fields |
| 169 | +-- |
| 170 | +-- @return[1] result |
| 171 | +-- @treturn[2] nil |
| 172 | +-- @treturn[2] table Error description |
| 173 | +function borders.max(space_name, index_name, opts) |
| 174 | + return get_border('max', space_name, index_name, opts) |
| 175 | +end |
| 176 | + |
| 177 | +return borders |
0 commit comments