Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] [PATCH v2 1/1] schema:frommap() to create table or tuple
@ 2018-03-28 17:51 Kirill Shcherbatov
  2018-03-28 18:28 ` Kirill Shcherbatov
  0 siblings, 1 reply; 18+ messages in thread
From: Kirill Shcherbatov @ 2018-03-28 17:51 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Kirill Shcherbatov

Closes #3282
---
 src/box/lua/schema.lua          | 14 +++++++
 src/box/lua/space.cc            | 86 +++++++++++++++++++++++++++++++++++++++++
 test/box/space_frommap.result   | 76 ++++++++++++++++++++++++++++++++++++
 test/box/space_frommap.test.lua | 20 ++++++++++
 4 files changed, 196 insertions(+)
 create mode 100644 test/box/space_frommap.result
 create mode 100644 test/box/space_frommap.test.lua

diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua
index 30c6bc6..afe81e8 100644
--- a/src/box/lua/schema.lua
+++ b/src/box/lua/schema.lua
@@ -1419,6 +1419,20 @@ function box.schema.space.bless(space)
         end
         builtin.space_run_triggers(s, yesno)
     end
+    space_mt.frommap = function(space, map, options)
+        check_space_arg(space, 'frommap')
+        check_space_exists(space)
+        if map == nil then
+            -- allow C module process error itself
+            box.internal.space._table_frommap(space)
+        end
+        local ret = box.internal.space._table_frommap(space, map)
+        if options and options['tuple'] == true then
+            ret = box.tuple.new(ret)
+        end
+        return ret
+    end
+    space_mt._ptr = box.internal.space._ptr
     space_mt.__index = space_mt
 
     setmetatable(space, space_mt)
diff --git a/src/box/lua/space.cc b/src/box/lua/space.cc
index 29a9aca..918f8f0 100644
--- a/src/box/lua/space.cc
+++ b/src/box/lua/space.cc
@@ -32,6 +32,7 @@
 #include "box/lua/tuple.h"
 #include "lua/utils.h"
 #include "lua/trigger.h"
+#include "box/schema.h"
 
 extern "C" {
 	#include <lua.h>
@@ -174,6 +175,16 @@ lbox_fillspace(struct lua_State *L, struct space *space, int i)
 	lua_pushstring(L, space->def->engine_name);
 	lua_settable(L, i);
 
+	/* space.schema_version */
+	lua_pushstring(L, "schema_version");
+	luaL_pushuint64(L, box_schema_version());
+	lua_settable(L, i);
+
+	/* space._space */
+	lua_pushstring(L, "_space");
+	lua_pushlightuserdata(L, space);
+	lua_settable(L, i);
+
 	lua_pushstring(L, "enabled");
 	lua_pushboolean(L, space_index(space, 0) != 0);
 	lua_settable(L, i);
@@ -386,6 +397,73 @@ static struct trigger on_alter_space_in_lua = {
 	RLIST_LINK_INITIALIZER, box_lua_space_new_or_delete, NULL, NULL
 };
 
+static int
+lbox_space_ptr_cached(struct lua_State *L)
+{
+	if (lua_gettop(L) < 1 || !lua_istable(L, 1))
+		luaL_error(L, "Usage: space:_ptr_cached()");
+
+	// take care about stack to call this function in frommap directly
+	lua_getfield(L, 1, "schema_version");
+	uint64_t schema_version = luaL_touint64(L, -1);
+	lua_pop(L, 1);
+	uint64_t global_shema_version = box_schema_version();
+
+	void *space = nullptr;
+	if (schema_version == global_shema_version) {
+		lua_getfield(L, 1, "_space");
+		space = (void *)lua_topointer(L, -1);
+		lua_pop(L, 1);
+	} else {
+		lua_getfield(L, 1, "id");
+		uint32_t id = luaL_touint64(L, -1);
+		lua_pop(L, 1);
+		space = space_by_id(id);
+
+		luaL_pushuint64(L, global_shema_version);
+		lua_setfield(L, 1, "schema_version");
+
+		lua_pushlightuserdata(L, space);
+		lua_setfield(L, 1, "_space");
+	}
+
+	lua_pushlightuserdata(L, space);
+	return 1;
+}
+
+static int
+lbox_space_table_frommap(struct lua_State *L)
+{
+	int res = 0;
+	struct tuple_dictionary *dict = nullptr;
+	struct space *space = nullptr;
+	int argc = lua_gettop(L);
+	if (argc != 2)
+		goto error;
+
+	res = lbox_space_ptr_cached(L);
+	space = (struct space *)lua_topointer(L, -1);
+	lua_pop(L, res);
+
+	dict = space->format->dict;
+	lua_createtable(L, space->def->field_count, 0);
+
+	lua_pushnil(L);
+	while (lua_next(L, 2) != 0) {
+		uint32_t fieldno;
+		size_t key_len;
+		const char *key = lua_tolstring(L, -2, &key_len);
+		uint32_t key_hash = lua_hashstring(L, -2);
+		if (tuple_fieldno_by_name(dict, key, key_len, key_hash, &fieldno))
+			luaL_error(L, "Unknown field '%s'", key);
+		lua_rawseti(L, -3, fieldno+1);
+	}
+	return 1;
+error:
+	luaL_error(L, "Usage: space:frommap(map, opts)");
+	return 1;
+}
+
 void
 box_lua_space_init(struct lua_State *L)
 {
@@ -464,4 +542,12 @@ box_lua_space_init(struct lua_State *L)
 	lua_pushnumber(L, VCLOCK_MAX);
 	lua_setfield(L, -2, "REPLICA_MAX");
 	lua_pop(L, 2); /* box, schema */
+
+	static const struct luaL_Reg space_internal_lib[] = {
+		{"_table_frommap", lbox_space_table_frommap},
+		{"_ptr", lbox_space_ptr_cached},
+		{NULL, NULL}
+	};
+	luaL_register(L, "box.internal.space", space_internal_lib);
+	lua_pop(L, 1);
 }
diff --git a/test/box/space_frommap.result b/test/box/space_frommap.result
new file mode 100644
index 0000000..811aa60
--- /dev/null
+++ b/test/box/space_frommap.result
@@ -0,0 +1,76 @@
+-- box.tuple test
+env = require('test_run')
+---
+...
+test_run = env.new()
+---
+...
+test_run:cmd("push filter ".."'\\.lua.*:[0-9]+: ' to '.lua...\"]:<line>: '")
+---
+- true
+...
+format = {}
+---
+...
+format[1] = {name = 'aaa', type = 'unsigned'}
+---
+...
+format[2] = {name = 'bbb', type = 'unsigned'}
+---
+...
+format[3] = {name = 'ccc', type = 'unsigned'}
+---
+...
+format[4] = {name = 'ddd', type = 'unsigned'}
+---
+...
+subscriber = box.schema.space.create('subscriber', {format = format})
+---
+...
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, bbb = 4})
+---
+- - 2
+  - 4
+  - 3
+  - 1
+...
+subscriber:frommap({ddd = 1, aaa = 2, bbb = 3})
+---
+- - 2
+  - 3
+  - null
+  - 1
+...
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, eee = 4})
+---
+- error: 'builtin/box/schema.lua..."]:<line>: Unknown field ''eee'''
+...
+subscriber:frommap()
+---
+- error: 'builtin/box/schema.lua..."]:<line>: Usage: space:frommap(map, opts)'
+...
+subscriber:frommap({})
+---
+- []
+...
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, bbb = 4}, {tuple = true})
+---
+- [2, 4, 3, 1]
+...
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, bbb = 4}, {tuple = false})
+---
+- - 2
+  - 4
+  - 3
+  - 1
+...
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, bbb = 4}, {dummy = true})
+---
+- - 2
+  - 4
+  - 3
+  - 1
+...
+subscriber:drop()
+---
+...
diff --git a/test/box/space_frommap.test.lua b/test/box/space_frommap.test.lua
new file mode 100644
index 0000000..4760b3a
--- /dev/null
+++ b/test/box/space_frommap.test.lua
@@ -0,0 +1,20 @@
+-- box.tuple test
+env = require('test_run')
+test_run = env.new()
+test_run:cmd("push filter ".."'\\.lua.*:[0-9]+: ' to '.lua...\"]:<line>: '")
+
+format = {}
+format[1] = {name = 'aaa', type = 'unsigned'}
+format[2] = {name = 'bbb', type = 'unsigned'}
+format[3] = {name = 'ccc', type = 'unsigned'}
+format[4] = {name = 'ddd', type = 'unsigned'}
+subscriber = box.schema.space.create('subscriber', {format = format})
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, bbb = 4})
+subscriber:frommap({ddd = 1, aaa = 2, bbb = 3})
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, eee = 4})
+subscriber:frommap()
+subscriber:frommap({})
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, bbb = 4}, {tuple = true})
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, bbb = 4}, {tuple = false})
+subscriber:frommap({ddd = 1, aaa = 2, ccc = 3, bbb = 4}, {dummy = true})
+subscriber:drop()
-- 
2.7.4

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2018-04-10 10:31 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-28 17:51 [tarantool-patches] [PATCH v2 1/1] schema:frommap() to create table or tuple Kirill Shcherbatov
2018-03-28 18:28 ` Kirill Shcherbatov
2018-03-29  7:36   ` [tarantool-patches] " Kirill Shcherbatov
2018-03-29 10:14     ` v.shpilevoy
2018-04-02 10:26       ` Kirill Shcherbatov
2018-04-02 11:19         ` v.shpilevoy
2018-04-02 19:42           ` Kirill Shcherbatov
2018-04-03  9:47             ` Vladislav Shpilevoy
2018-04-04 14:14               ` Kirill Shcherbatov
2018-04-04 16:35                 ` [tarantool-patches] " Kirill Shcherbatov
2018-04-04 16:43                   ` Vladislav Shpilevoy
2018-04-06 13:47                   ` Vladimir Davydov
2018-04-06 15:44                   ` [tarantool-patches] " Konstantin Osipov
2018-04-09  8:30                     ` Kirill Shcherbatov
2018-04-09 10:44                       ` Vladislav Shpilevoy
2018-04-09 17:23                         ` Kirill Shcherbatov
2018-04-09 17:45                           ` Vladislav Shpilevoy
2018-04-10 10:31                           ` Vladimir Davydov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox