-
Notifications
You must be signed in to change notification settings - Fork 15
Introduce crud.min and crud.max #156
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
315a41b
Introduce crud.min and crud.max
dokshina 9ce2df6
Support specifying index ID
dokshina 1081583
Fix wrapping box space function
dokshina d4d9910
Doc
dokshina d287179
Update CHANGELOG
dokshina 8f08d6a
Review fixes
dokshina 1dcf977
Fix compare sign
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
local checks = require('checks') | ||
local errors = require('errors') | ||
local vshard = require('vshard') | ||
|
||
local dev_checks = require('crud.common.dev_checks') | ||
local call = require('crud.common.call') | ||
local utils = require('crud.common.utils') | ||
local schema = require('crud.common.schema') | ||
local Keydef = require('crud.compare.keydef') | ||
|
||
local BorderError = errors.new_class('Border', {capture_stack = false}) | ||
|
||
local borders = {} | ||
|
||
local STAT_FUNC_NAME = '_crud.get_border_on_storage' | ||
|
||
|
||
local function get_border_on_storage(border_name, space_name, index_id, field_names) | ||
dev_checks('string', 'string', 'number', '?table') | ||
|
||
assert(border_name == 'min' or border_name == 'max') | ||
|
||
local space = box.space[space_name] | ||
if space == nil then | ||
return nil, BorderError:new("Space %q doesn't exist", space_name) | ||
end | ||
|
||
local index = space.index[index_id] | ||
if index == nil then | ||
return nil, BorderError:new("Index %q of space doesn't exist", index_id, space_name) | ||
end | ||
|
||
local function get_index_border(index) | ||
return index[border_name](index) | ||
end | ||
|
||
return schema.wrap_func_result(space, get_index_border, {index}, { | ||
add_space_schema_hash = true, | ||
field_names = field_names, | ||
}) | ||
end | ||
|
||
function borders.init() | ||
_G._crud.get_border_on_storage = get_border_on_storage | ||
end | ||
|
||
local function is_closer(compare_sign, keydef, tuple, res_tuple) | ||
if res_tuple == nil then | ||
return true | ||
end | ||
|
||
local cmp = keydef:compare(tuple, res_tuple) | ||
|
||
return cmp * compare_sign > 0 | ||
end | ||
|
||
local function call_get_border_on_router(border_name, space_name, index_name, opts) | ||
checks('string', 'string', '?string|number', { | ||
timeout = '?number', | ||
fields = '?table', | ||
}) | ||
|
||
opts = opts or {} | ||
|
||
local space = utils.get_space(space_name, vshard.router.routeall()) | ||
if space == nil then | ||
return nil, BorderError:new("Space %q doesn't exist", space_name), true | ||
end | ||
|
||
local index | ||
if index_name == nil then | ||
index = space.index[0] | ||
else | ||
index = space.index[index_name] | ||
end | ||
dokshina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if index == nil then | ||
return nil, BorderError:new("Index %q of space %q doesn't exist", index_name, space_name), true | ||
end | ||
|
||
local primary_index = space.index[0] | ||
|
||
local cmp_key_parts = utils.merge_primary_key_parts(index.parts, primary_index.parts) | ||
local field_names = utils.enrich_field_names_with_cmp_key(opts.fields, cmp_key_parts, space:format()) | ||
|
||
local replicasets = vshard.router.routeall() | ||
local call_opts = { | ||
mode = 'read', | ||
replicasets = replicasets, | ||
timeout = opts.timeout, | ||
} | ||
local results, err = call.map( | ||
STAT_FUNC_NAME, | ||
{border_name, space_name, index.id, field_names}, | ||
call_opts | ||
) | ||
|
||
if err ~= nil then | ||
return nil, BorderError:new("Failed to get %s: %s", border_name, err) | ||
end | ||
|
||
local keydef = Keydef.new(space, field_names, index.id) | ||
local compare_sign = border_name == 'max' and 1 or -1 | ||
|
||
local res_tuple = nil | ||
for _, storage_result in pairs(results) do | ||
local storage_result = storage_result[1] | ||
if storage_result.err ~= nil then | ||
local need_reload = schema.result_needs_reload(space, storage_result) | ||
return nil, BorderError:new("Failed to get %s: %s", border_name, storage_result.err), need_reload | ||
end | ||
|
||
local tuple = storage_result.res | ||
if tuple ~= nil and is_closer(compare_sign, keydef, tuple, res_tuple) then | ||
res_tuple = tuple | ||
end | ||
end | ||
|
||
local result = utils.format_result({res_tuple}, space, field_names) | ||
|
||
if opts.fields ~= nil then | ||
result = utils.cut_rows(result.rows, result.metadata, opts.fields) | ||
end | ||
|
||
return result | ||
end | ||
|
||
local function get_border(border_name, space_name, index_name, opts) | ||
return schema.wrap_func_reload( | ||
call_get_border_on_router, border_name, space_name, index_name, opts | ||
) | ||
end | ||
|
||
--- Find the minimum value in the specified index | ||
-- | ||
-- @function min | ||
-- | ||
-- @param string space_name | ||
-- A space name | ||
-- | ||
-- @param ?string index_name | ||
-- An index name (by default, primary index is used) | ||
-- | ||
-- @tparam ?number opts.timeout | ||
-- Function call timeout | ||
-- | ||
-- @tparam ?table opts.fields | ||
-- Field names for getting only a subset of fields | ||
-- | ||
-- @return[1] result | ||
-- @treturn[2] nil | ||
-- @treturn[2] table Error description | ||
function borders.min(space_name, index_id, opts) | ||
return get_border('min', space_name, index_id, opts) | ||
end | ||
|
||
--- Find the maximum value in the specified index | ||
-- | ||
-- @function min | ||
-- | ||
-- @param string space_name | ||
-- A space name | ||
-- | ||
-- @param ?string index_name | ||
-- An index name (by default, primary index is used) | ||
-- | ||
-- @tparam ?number opts.timeout | ||
-- Function call timeout | ||
-- | ||
-- @tparam ?table opts.fields | ||
-- Field names for getting only a subset of fields | ||
-- | ||
-- @return[1] result | ||
-- @treturn[2] nil | ||
-- @treturn[2] table Error description | ||
function borders.max(space_name, index_id, opts) | ||
return get_border('max', space_name, index_id, opts) | ||
end | ||
|
||
return borders |
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
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.
Uh oh!
There was an error while loading. Please reload this page.