[Tarantool-patches] [PATCH 1.10 v4 1/5] module api: export box_tuple_validate

Timur Safin tsafin at tarantool.org
Wed Oct 14 03:15:40 MSK 2020


For external merger we need means to validate tuple data,
thus exporting `box_tuple_validate` which is wrapper around
`tuple_validate` without revealing access to tuple
internals.

Part of #5384
---
 extra/exports                    |  1 +
 src/box/tuple.c                  |  8 ++++++
 src/box/tuple.h                  | 11 ++++++++
 test/app-tap/module_api.c        | 48 ++++++++++++++++++++++++++++++++
 test/app-tap/module_api.test.lua | 29 ++++++++++++++++++-
 5 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/extra/exports b/extra/exports
index 960c50635..bd05cb08e 100644
--- a/extra/exports
+++ b/extra/exports
@@ -178,6 +178,7 @@ box_tuple_upsert
 box_tuple_extract_key
 box_tuple_compare
 box_tuple_compare_with_key
+box_tuple_validate
 box_return_tuple
 box_space_id_by_name
 box_index_id_by_name
diff --git a/src/box/tuple.c b/src/box/tuple.c
index 1db69e414..fb83948d7 100644
--- a/src/box/tuple.c
+++ b/src/box/tuple.c
@@ -576,6 +576,14 @@ box_tuple_new(box_tuple_format_t *format, const char *data, const char *end)
 	return tuple_bless(ret);
 }
 
+int
+box_tuple_validate(box_tuple_t *tuple, box_tuple_format_t *format)
+{
+	return tuple_validate(format, tuple);
+}
+
+/* }}} box_tuple_* */
+
 int
 tuple_snprint(char *buf, int size, const struct tuple *tuple)
 {
diff --git a/src/box/tuple.h b/src/box/tuple.h
index 5c6dc6b8c..217c438c5 100644
--- a/src/box/tuple.h
+++ b/src/box/tuple.h
@@ -284,6 +284,17 @@ box_tuple_t *
 box_tuple_upsert(const box_tuple_t *tuple, const char *expr, const
 		 char *expr_end);
 
+/**
+ * Check tuple data correspondence to the space format.
+ * @param tuple  Tuple to validate.
+ * @param format Format to which the tuple must match.
+ *
+ * @retval  0 The tuple is valid.
+ * @retval -1 The tuple is invalid.
+ */
+int
+box_tuple_validate(box_tuple_t *tuple, box_tuple_format_t *format);
+
 /** \endcond public */
 
 /**
diff --git a/test/app-tap/module_api.c b/test/app-tap/module_api.c
index c80896c45..672272e25 100644
--- a/test/app-tap/module_api.c
+++ b/test/app-tap/module_api.c
@@ -1800,6 +1800,52 @@ test_tuple_new(struct lua_State *L)
 
 /* }}} test_tuple_new */
 
+/*
+ * Check that argument is a tuple of any format, without
+ * its verification
+ */
+static int
+test_tuple_validate_default(lua_State *L)
+{
+	int valid = 0;
+	box_tuple_t *tuple = luaT_istuple(L, -1);
+
+	if (tuple != NULL) {
+		box_tuple_format_t *format = box_tuple_format_default();
+		valid = box_tuple_validate(tuple, format) == 0;
+	}
+	lua_pushboolean(L, valid);
+
+	return 1;
+}
+
+/*
+ * Validate tuple with format of single boolean field
+ */
+static int
+test_tuple_validate_formatted(lua_State *L)
+{
+	int valid = 0;
+	box_tuple_t *tuple = luaT_istuple(L, -1);
+
+	if (tuple != NULL) {
+		uint32_t fields[] = { 0 };
+		uint32_t types[] = { FIELD_TYPE_BOOLEAN };
+		box_key_def_t *key_defs[] = {
+			box_key_def_new(fields, types, 1)
+		};
+		assert(key_defs[0] != NULL);
+		struct tuple_format *format =
+			box_tuple_format_new(key_defs, 1);
+		assert(format);
+
+		valid = box_tuple_validate(tuple, format) == 0;
+	}
+	lua_pushboolean(L, valid);
+
+	return 1;
+}
+
 LUA_API int
 luaopen_module_api(lua_State *L)
 {
@@ -1837,6 +1883,8 @@ luaopen_module_api(lua_State *L)
 		{"test_key_def_merge", test_key_def_merge},
 		{"test_key_def_extract_key", test_key_def_extract_key},
 		{"test_key_def_validate_key", test_key_def_validate_key},
+		{"tuple_validate_def", test_tuple_validate_default},
+		{"tuple_validate_fmt", test_tuple_validate_formatted},
 		{NULL, NULL}
 	};
 	luaL_register(L, "module_api", lib);
diff --git a/test/app-tap/module_api.test.lua b/test/app-tap/module_api.test.lua
index 10b719686..f45592ef9 100755
--- a/test/app-tap/module_api.test.lua
+++ b/test/app-tap/module_api.test.lua
@@ -36,6 +36,32 @@ local function test_pushcdata(test, module)
     test:is(gc_counter, 1, 'pushcdata gc')
 end
 
+local function test_tuple_validate(test, module)
+    test:plan(12)
+
+    local nottuple1 = {}
+    local nottuple2 = {true, 2}
+    local nottuple3 = {false, nil, 2}
+    local nottuple4 = {1, box.NULL, 2, 3}
+    local tuple1 = box.tuple.new(nottuple1)
+    local tuple2 = box.tuple.new(nottuple2)
+    local tuple3 = box.tuple.new(nottuple3)
+    local tuple4 = box.tuple.new(nottuple4)
+
+    test:ok(not module.tuple_validate_def(nottuple1), "not tuple 1")
+    test:ok(not module.tuple_validate_def(nottuple2), "not tuple 2")
+    test:ok(not module.tuple_validate_def(nottuple3), "not tuple 3")
+    test:ok(not module.tuple_validate_def(nottuple4), "not tuple 4")
+    test:ok(module.tuple_validate_def(tuple1), "tuple 1")
+    test:ok(module.tuple_validate_def(tuple2), "tuple 2")
+    test:ok(module.tuple_validate_def(tuple3), "tuple 3")
+    test:ok(module.tuple_validate_def(tuple4), "tuple 4")
+    test:ok(not module.tuple_validate_fmt(tuple1), "tuple 1 (fmt)")
+    test:ok(module.tuple_validate_fmt(tuple2), "tuple 2 (fmt)")
+    test:ok(module.tuple_validate_fmt(tuple3), "tuple 3 (fmt)")
+    test:ok(not module.tuple_validate_fmt(tuple4), "tuple 4 (fmt)")
+end
+
 local function test_iscdata(test, module)
     local ffi = require('ffi')
     ffi.cdef([[
@@ -97,7 +123,7 @@ local function test_iscdata(test, module)
 end
 
 local test = require('tap').test("module_api", function(test)
-    test:plan(33)
+    test:plan(34)
     local status, module = pcall(require, 'module_api')
     test:is(status, true, "module")
     test:ok(status, "module is loaded")
@@ -123,6 +149,7 @@ local test = require('tap').test("module_api", function(test)
 
     test:test("pushcdata", test_pushcdata, module)
     test:test("iscdata", test_iscdata, module)
+    test:test("tuple_validate", test_tuple_validate, module)
 
     space:drop()
 end)
-- 
2.20.1



More information about the Tarantool-patches mailing list