Skip to content

Commit 90f3304

Browse files
uneralocker
authored andcommitted
key_def: Introduce __len metamethod
The metamethod is a way to key_def length introspection. Closes tarantool#10111 @TarantoolBot document Title: key_def length introspection To check key_def length (parts count) there is a standard lua operator `#` (`__len` metamethod). Example: ```lua function is_full_pkey(space, key) return #space.index[0].parts == #key end ```
1 parent bde28f0 commit 90f3304

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## feature/lua
2+
3+
* Introduced a standard Lua way to get the length of `key_def` (gh-10111).

src/box/lua/key_def.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,19 @@ lbox_key_def_to_table(struct lua_State *L)
633633
return 1;
634634
}
635635

636+
/**
637+
* Return partitions count for key_def
638+
*/
639+
static int
640+
lbox_key_def_part_count(struct lua_State *L)
641+
{
642+
struct key_def *key_def = luaT_is_key_def(L, 1);
643+
if (key_def == NULL)
644+
return luaL_error(L, "Usage: key_def:part_count()");
645+
lua_pushinteger(L, key_def->part_count);
646+
return 1;
647+
}
648+
636649
int
637650
lbox_key_def_new(struct lua_State *L)
638651
{
@@ -704,6 +717,7 @@ luaopen_key_def(struct lua_State *L)
704717
{"compare_keys", lbox_key_def_compare_keys},
705718
{"merge", lbox_key_def_merge},
706719
{"totable", lbox_key_def_to_table},
720+
{"part_count", lbox_key_def_part_count},
707721
{NULL, NULL}
708722
};
709723
luaT_newmodule(L, "key_def", meta);

src/box/lua/key_def.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ ffi.metatype(key_def_t, {
2020
return methods[key]
2121
end,
2222
__tostring = function(self) return "<struct key_def &>" end,
23+
__len = key_def.part_count,
2324
})

test/box-tap/key_def.test.lua

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ local key_def_new_cases = {
198198

199199
local test = tap.test('key_def')
200200

201-
test:plan(#key_def_new_cases - 1 + 8)
201+
test:plan(#key_def_new_cases - 1 + 9)
202202
for _, case in ipairs(key_def_new_cases) do
203203
if type(case) == 'function' then
204204
case()
@@ -598,4 +598,20 @@ test:test('Usage errors', function(test)
598598
'totable()')
599599
end)
600600

601+
-- Check key_def:part_count
602+
test:test('Key_def part_count', function(test)
603+
test:plan(4)
604+
local kd = key_def_lib.new({
605+
{fieldno = 2, type = 'unsigned'}, {fieldno = 4, type = 'string'}
606+
})
607+
test:ok(kd, 'instance created')
608+
test:is(#kd, 2, 'part_count')
609+
610+
local kd = key_def_lib.new({
611+
{fieldno = 22, type = 'string'},
612+
})
613+
test:ok(kd, 'instance created')
614+
test:is(#kd, 1, 'part_count')
615+
end)
616+
601617
os.exit(test:check() and 0 or 1)

0 commit comments

Comments
 (0)