lua: add key_def lua module
Needed for #3276. Fixes #3398. Fixes #4025. @TarantoolBot document Title: lua: key_def module It is convenient to have tuple compare function into lua-land for the following case: - exporting key from tuple to iterate over secondary non-unique index and delete tuples from space - support comparing a tuple with a key / a tuple, support merging key_defs from Lua - factor out key parts parsing code from the tuples merger A key_def instance has the following methods: - :extract_key(tuple) -> key (as tuple) Receives tuple or Lua table. Returns tuple representing extracted key. - :compare(tuple_a, tuple_b) -> number Receives tuples or Lua tables. Returns: - a value > 0 when tuple_a > tuple_b, - a value == 0 when tuple_a == tuple_b, - a value < 0 otherwise. - :compare_with_key(tuple, key) -> number - a value > 0 when key(tuple) > key, - a value == 0 when key(tuple) == key, - a value < 0 otherwise. - :merge(another_key_def) -> new key_def instance Constructs a new key definition with a set union of key parts from first and second key defs - :totable() -> table Dump key_def object as a Lua table (needed to support __serialize method) The root key_def library exports all instance methods directly. The format of `parts` parameter in the `key_def.new(parts)` call is compatible with the following structures: * box.space[...].index[...].parts; * net_box_conn.space[...].index[...].parts. Example for extract_key(): ```lua -- Remove values got from a secondary non-unique index. local key_def_lib = require('key_def') local s = box.schema.space.create('test') local pk = s:create_index('pk') local sk = s:create_index('test', {unique = false, parts = { {2, 'number', path = 'a'}, {2, 'number', path = 'b'}}}) s:insert{1, {a = 1, b = 1}} s:insert{2, {a = 1, b = 2}} local key_def = key_def_lib.new(pk.parts) for _, tuple in sk:pairs({1})) do local key = key_def:extract_key(tuple) pk:delete(key) end ```
Showing
- src/CMakeLists.txt 1 addition, 0 deletionssrc/CMakeLists.txt
- src/box/CMakeLists.txt 2 additions, 0 deletionssrc/box/CMakeLists.txt
- src/box/key_def.h 12 additions, 0 deletionssrc/box/key_def.h
- src/box/lua/init.c 5 additions, 0 deletionssrc/box/lua/init.c
- src/box/lua/key_def.c 480 additions, 0 deletionssrc/box/lua/key_def.c
- src/box/lua/key_def.h 69 additions, 0 deletionssrc/box/lua/key_def.h
- src/box/lua/key_def.lua 19 additions, 0 deletionssrc/box/lua/key_def.lua
- src/box/lua/space.cc 2 additions, 32 deletionssrc/box/lua/space.cc
- src/box/tuple_extract_key.cc 22 additions, 0 deletionssrc/box/tuple_extract_key.cc
- test/box-tap/key_def.test.lua 453 additions, 0 deletionstest/box-tap/key_def.test.lua
Loading
Please register or sign in to comment