[tarantool-patches] Re: [PATCH v2 2/2] lua: add key_def lua module
Kirill Shcherbatov
kshcherbatov at tarantool.org
Thu Mar 28 10:38:41 MSK 2019
> Consider the case when a user get tuples from a local space (or merger)
> and they have a format that allows to compare faster using precalculated
> offsets. I think we should not create a new tuple(s) in the case.
>
> Applicable for other functions too.
If we decide to support setting arguments in the form of tables, I propose to
introduce the following function. In my opinion, it does not look unnatural.
diff --git a/src/box/lua/key_def.c b/src/box/lua/key_def.c
index 18d5d961d..9cacf3cca 100644
--- a/src/box/lua/key_def.c
+++ b/src/box/lua/key_def.c
@@ -224,6 +224,23 @@ lbox_key_def_gc(struct lua_State *L)
return 0;
}
+/**
+ * Take existent tuple from LUA stack or build a new tuple with
+ * default format from table, check for compatibility with a
+ * given key_def.
+ */
+static struct tuple *
+lbox_key_def_check_tuple(struct lua_State *L, struct key_def *key_def, int idx)
+{
+ struct tuple *tuple = luaT_istuple(L, idx);
+ if (tuple == NULL)
+ tuple = luaT_tuple_new(L, 2, box_tuple_format_default());
+ if (tuple == NULL || tuple_validate_parts(key_def, tuple) != 0)
+ return NULL;
+ tuple_ref(tuple);
+ return tuple;
+}
+
static int
lbox_key_def_extract_key(struct lua_State *L)
{
@@ -231,12 +248,9 @@ lbox_key_def_extract_key(struct lua_State *L)
if (lua_gettop(L) != 2 || (key_def = check_key_def(L, 1)) == NULL)
return luaL_error(L, "Usage: key_def:extract_key(tuple)");
- struct tuple_format *format = box_tuple_format_default();
struct tuple *tuple;
- if ((tuple = luaT_tuple_new(L, 2, format)) == NULL ||
- tuple_validate_parts(key_def, tuple) != 0)
+ if ((tuple = lbox_key_def_check_tuple(L, key_def, 2)) == NULL)
return luaT_error(L);
- tuple_ref(tuple);
uint32_t key_size;
char *key = tuple_extract_key(tuple, key_def, &key_size);
@@ -244,7 +258,8 @@ lbox_key_def_extract_key(struct lua_State *L)
if (key == NULL)
return luaT_error(L);
- struct tuple *ret = box_tuple_new(format, key, key + key_size);
+ struct tuple *ret =
+ box_tuple_new(box_tuple_format_default(), key, key + key_size);
if (ret == NULL)
return luaT_error(L);
luaT_pushtuple(L, ret);
@@ -261,17 +276,12 @@ lbox_key_def_compare(struct lua_State *L)
}
struct tuple *tuple_a, *tuple_b;
- struct tuple_format *format = box_tuple_format_default();
- if ((tuple_a = luaT_tuple_new(L, 2, format)) == NULL ||
- tuple_validate_parts(key_def, tuple_a) != 0)
+ if ((tuple_a = lbox_key_def_check_tuple(L, key_def, 2)) == NULL)
return luaT_error(L);
- tuple_ref(tuple_a);
- if ((tuple_b = luaT_tuple_new(L, 3, format)) == NULL ||
- tuple_validate_parts(key_def, tuple_b) != 0) {
+ if ((tuple_b = lbox_key_def_check_tuple(L, key_def, 3)) == NULL) {
tuple_unref(tuple_a);
return luaT_error(L);
}
- tuple_ref(tuple_b);
int rc = tuple_compare(tuple_a, tuple_b, key_def);
tuple_unref(tuple_a);
@@ -291,10 +301,8 @@ lbox_key_def_compare_with_key(struct lua_State *L)
struct tuple *tuple, *key_tuple = NULL;
struct tuple_format *format = box_tuple_format_default();
- if ((tuple = luaT_tuple_new(L, 2, format)) == NULL ||
- tuple_validate_parts(key_def, tuple) != 0)
+ if ((tuple = lbox_key_def_check_tuple(L, key_def, 2)) == NULL)
return luaT_error(L);
- tuple_ref(tuple);
if ((key_tuple = luaT_tuple_new(L, 3, format)) == NULL) {
tuple_unref(tuple);
return luaT_error(L);
More information about the Tarantool-patches
mailing list