From d98de2b1d26a810c1e614d9edc78c3473eea2581 Mon Sep 17 00:00:00 2001 From: Konstantin Osipov <kostja@tarantool.org> Date: Thu, 29 Nov 2012 23:34:53 +0400 Subject: [PATCH] temp-spaces: review fixes --- include/ipc.h | 6 +- include/tarantool.h | 2 + src/CMakeLists.txt | 1 - src/box/CMakeLists.txt | 2 +- src/box/box.lua | 2 +- src/{lua/space.h => box/box_lua_space.h} | 7 +- src/{lua/space.m => box/box_lua_space.m} | 83 +++++++++++++----------- src/lua/init.m | 23 +++---- src/lua/lua_ipc.m | 10 +-- test/box/lua.result | 5 +- 10 files changed, 73 insertions(+), 68 deletions(-) rename src/{lua/space.h => box/box_lua_space.h} (99%) rename src/{lua/space.m => box/box_lua_space.m} (78%) diff --git a/include/ipc.h b/include/ipc.h index f13e32da48..a2eccedb0f 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -1,5 +1,5 @@ -#ifndef TARANTOOL_IFC_H_INCLUDED -#define TARANTOOL_IFC_H_INCLUDED +#ifndef TARANTOOL_IPC_H_INCLUDED +#define TARANTOOL_IPC_H_INCLUDED /* * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following @@ -177,4 +177,4 @@ ipc_channel_has_writers(struct ipc_channel *ch); extern const ev_tstamp IPC_TIMEOUT_INFINITY; -#endif /* TARANTOOL_IFC_H_INCLUDED */ +#endif /* TARANTOOL_IPC_H_INCLUDED */ diff --git a/include/tarantool.h b/include/tarantool.h index 4fc14067f8..5c6c63e417 100644 --- a/include/tarantool.h +++ b/include/tarantool.h @@ -35,6 +35,7 @@ struct tarantool_cfg; struct tbuf; struct log_io; struct fio_batch; +struct lua_State; void mod_init(void); void mod_free(void); @@ -42,6 +43,7 @@ void mod_free(void); extern const char *mod_name; i32 mod_check_config(struct tarantool_cfg *conf); i32 mod_reload_config(struct tarantool_cfg *old_conf, struct tarantool_cfg *new_conf); +void mod_lua_load_cfg(struct lua_State *L); int mod_cat(const char *filename); void mod_snapshot(struct log_io *, struct fio_batch *batch); void mod_info(struct tbuf *out); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f4b27c6cd6..bc8a9e86c8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -132,7 +132,6 @@ set (common_sources lua/slab.m lua/uuid.m lua/lua_ipc.m - lua/space.m ) if (ENABLE_TRACE) diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt index 3675aa94f9..cd7d531ac5 100644 --- a/src/box/CMakeLists.txt +++ b/src/box/CMakeLists.txt @@ -26,4 +26,4 @@ add_custom_target(generate_lua_sources} set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES ${lua_sources}) tarantool_module("box" tuple.m index.m tree.m space.m port.m request.m - txn.m box.m ${lua_sources} box_lua.m) + txn.m box.m ${lua_sources} box_lua.m box_lua_space.m) diff --git a/src/box/box.lua b/src/box/box.lua index 238aee1925..9787738792 100644 --- a/src/box/box.lua +++ b/src/box/box.lua @@ -254,7 +254,7 @@ function box.bless_space(space) end space_mt.pairs = function(space) return space.index[0]:pairs() end space_mt.__index = space_mt - + setmetatable(space, space_mt) if type(space.index) == 'table' and space.enabled then for j, index in pairs(space.index) do diff --git a/src/lua/space.h b/src/box/box_lua_space.h similarity index 99% rename from src/lua/space.h rename to src/box/box_lua_space.h index a6ee38d2a4..0ce25b473a 100644 --- a/src/lua/space.h +++ b/src/box/box_lua_space.h @@ -1,3 +1,5 @@ +#ifndef INCLUDES_TARANTOOL_LUA_SPACE_H +#define INCLUDES_TARANTOOL_LUA_SPACE_H /* * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following @@ -26,11 +28,6 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - -#ifndef INCLUDES_TARANTOOL_LUA_SPACE_H -#define INCLUDES_TARANTOOL_LUA_SPACE_H - - struct lua_State; struct space; diff --git a/src/lua/space.m b/src/box/box_lua_space.m similarity index 78% rename from src/lua/space.m rename to src/box/box_lua_space.m index c3526535c7..72bbfbee3b 100644 --- a/src/lua/space.m +++ b/src/box/box_lua_space.m @@ -26,15 +26,20 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ - +#include "box_lua_space.h" #include "space.h" -#include "../box/space.h" #include "lua.h" #include "lauxlib.h" #include "lualib.h" #include <say.h> - +/** + * Make a single space available in Lua, + * via box.space[] array. + * + * @return A new table representing a space on top of the Lua + * stack. + */ int lbox_pushspace(struct lua_State *L, struct space *space) { @@ -63,31 +68,29 @@ lbox_pushspace(struct lua_State *L, struct space *space) /* space.index */ lua_pushstring(L, "index"); lua_newtable(L); - - - for(int i = 0; i < space->key_count; i++) { + /* + * Fill space.index table with + * all defined indexes. + */ + for (int i = 0; i < space->key_count; i++) { lua_pushnumber(L, i); lua_newtable(L); /* space.index[i] */ - lua_pushstring(L, "idx"); - lua_pushfstring(L, "index %d in space %d", i, space->no); - lua_settable(L, -3); - lua_pushstring(L, "unique"); lua_pushboolean(L, space->key_defs[i].is_unique); lua_settable(L, -3); lua_pushstring(L, "type"); - switch(space->key_defs[i].type) { - case HASH: - lua_pushstring(L, "HASH"); - break; - case TREE: - lua_pushstring(L, "TREE"); - break; - default: - panic("unknown index type %d", - space->key_defs[i].parts[0].type); + switch (space->key_defs[i].type) { + case HASH: + lua_pushstring(L, "HASH"); + break; + case TREE: + lua_pushstring(L, "TREE"); + break; + default: + panic("unknown index type %d", + space->key_defs[i].parts[0].type); } lua_settable(L, -3); @@ -99,19 +102,19 @@ lbox_pushspace(struct lua_State *L, struct space *space) lua_newtable(L); lua_pushstring(L, "type"); - switch(space->key_defs[i].parts[j].type) { - case NUM: - lua_pushstring(L, "NUM"); - break; - case NUM64: - lua_pushstring(L, "NUM64"); - break; - case STRING: - lua_pushstring(L, "STR"); - break; - default: - lua_pushstring(L, "UNKNOWN"); - break; + switch (space->key_defs[i].parts[j].type) { + case NUM: + lua_pushstring(L, "NUM"); + break; + case NUM64: + lua_pushstring(L, "NUM64"); + break; + case STRING: + lua_pushstring(L, "STR"); + break; + default: + lua_pushstring(L, "UNKNOWN"); + break; } lua_settable(L, -3); @@ -129,15 +132,10 @@ lbox_pushspace(struct lua_State *L, struct space *space) lua_settable(L, -3); /* push space.index */ - - /* on_reload_configuration hook */ lua_getfield(L, LUA_GLOBALSINDEX, "box"); lua_pushstring(L, "bless_space"); lua_gettable(L, -2); - if (!lua_isfunction(L, -1)) - panic("box.bless_space isn't a function"); - lua_pushvalue(L, -3); /* box, bless, space */ lua_call(L, 1, 0); lua_pop(L, 1); /* cleanup stack */ @@ -145,6 +143,9 @@ lbox_pushspace(struct lua_State *L, struct space *space) return 1; } +/** + * A callback adapter for space_foreach(). + */ static void lbox_add_space(struct space *space, struct lua_State *L) { @@ -153,8 +154,12 @@ lbox_add_space(struct space *space, struct lua_State *L) lua_settable(L, -3); } +/** + * Make all spaces available in Lua via box.space + * array. + */ void -lbox_space_init(struct lua_State *L) +mod_lua_load_cfg(struct lua_State *L) { lua_getfield(L, LUA_GLOBALSINDEX, "box"); lua_pushstring(L, "space"); diff --git a/src/lua/init.m b/src/lua/init.m index e1b7beae3e..d46e469f1b 100644 --- a/src/lua/init.m +++ b/src/lua/init.m @@ -39,16 +39,15 @@ #include "lj_cdata.h" #include "lj_cconv.h" #include "lj_state.h" +#include <ctype.h> #include "pickle.h" #include "fiber.h" #include "lua_ipc.h" -#include <ctype.h> #include "lua/info.h" #include "lua/slab.h" #include "lua/stat.h" #include "lua/uuid.h" -#include "space.h" #include TARANTOOL_CONFIG @@ -1380,8 +1379,7 @@ tarantool_lua_load_cfg(struct lua_State *L, struct tarantool_cfg *cfg) " table[index] = {}\n" " setmetatable(table[index], getmetatable(table))\n" " return rawget(table, index)\n" - "end\n" - ); + "end\n"); while ((key = tarantool_cfg_iterator_next(i, cfg, &value)) != NULL) { if (value == NULL) continue; @@ -1399,8 +1397,7 @@ tarantool_lua_load_cfg(struct lua_State *L, struct tarantool_cfg *cfg) "function(table, index)\n" " error('Attempt to modify a read-only table')\n" "end\n" - "getmetatable(box.cfg).__index = nil\n" - ); + "getmetatable(box.cfg).__index = nil\n"); luaL_pushresult(&b); if (luaL_loadstring(L, lua_tostring(L, -1)) != 0 || lua_pcall(L, 0, 0, 0) != 0) { @@ -1408,14 +1405,18 @@ tarantool_lua_load_cfg(struct lua_State *L, struct tarantool_cfg *cfg) } lua_pop(L, 1); /* cleanup stack */ - lbox_space_init(L); - - /* on_reload_configuration hook */ + mod_lua_load_cfg(L); + /* + * Invoke a user-defined on_reload_configuration hook, + * if it exists. Do it after everything else is done. + */ lua_getfield(L, LUA_GLOBALSINDEX, "box"); lua_pushstring(L, "on_reload_configuration"); lua_gettable(L, -2); - if (lua_isfunction(L, -1)) - lua_call(L, 0, 0); + if (lua_isfunction(L, -1) && lua_pcall(L, 0, 0, 0) != 0) { + say_error("on_reload_configuration() hook failed: %s", + lua_tostring(L, -1)); + } lua_pop(L, 1); /* cleanup stack */ } diff --git a/src/lua/lua_ipc.m b/src/lua/lua_ipc.m index 986bd1badc..802fe2fb13 100644 --- a/src/lua/lua_ipc.m +++ b/src/lua/lua_ipc.m @@ -27,13 +27,15 @@ * SUCH DAMAGE. */ #include "lua_ipc.h" -#include <ipc.h> + +#include <stdlib.h> + #include "lua.h" #include "lauxlib.h" #include "lualib.h" -#include <lua/init.h> -#include <stdlib.h> +#include "ipc.h" +#include "lua/init.h" static const char channel_lib[] = "box.ipc.channel"; @@ -75,7 +77,6 @@ lbox_check_channel(struct lua_State *L, int narg) return * (void **) luaL_checkudata(L, narg, channel_lib); } - static int lbox_ipc_channel_gc(struct lua_State *L) { @@ -87,7 +88,6 @@ lbox_ipc_channel_gc(struct lua_State *L) return 0; } - static int lbox_ipc_channel_is_full(struct lua_State *L) { diff --git a/test/box/lua.result b/test/box/lua.result index 4e07b73339..c60791ec19 100644 --- a/test/box/lua.result +++ b/test/box/lua.result @@ -699,11 +699,12 @@ lua box.fiber.resume(f, 'hello', 'world') ... lua box.fiber.resume(f, 'wide') --- -error: 'fiber.resume(): the fiber is dead' + - world + - wide ... lua box.fiber.resume(f) --- -error: 'fiber.resume(): the fiber is dead' + - true ... lua function y() print('started') box.fiber.detach() while true do box.replace(0, 'test', os.time()) box.fiber.sleep(0.001) end end --- -- GitLab