From 8f76e920ab4a342355408978d77b14196676d297 Mon Sep 17 00:00:00 2001 From: Roman Tsisyk <roman@tsisyk.com> Date: Thu, 19 May 2016 18:39:07 +0300 Subject: [PATCH] phia: move box.phia() to box.info.phia() Split keys by '.' to make output to be more YAML-friendly. --- src/box/CMakeLists.txt | 1 - src/box/lua/info.c | 72 +++++++++++++++++++++++++++++++++++ src/box/lua/init.c | 2 - src/box/lua/phia.c | 50 ------------------------ src/box/lua/phia.h | 44 --------------------- test/box/info.result | 1 + test/box/misc.result | 1 - test/phia/monitoring.result | 2 +- test/phia/monitoring.test.lua | 2 +- 9 files changed, 75 insertions(+), 100 deletions(-) delete mode 100644 src/box/lua/phia.h diff --git a/src/box/CMakeLists.txt b/src/box/CMakeLists.txt index afec422f42..69ab315770 100644 --- a/src/box/CMakeLists.txt +++ b/src/box/CMakeLists.txt @@ -76,7 +76,6 @@ add_library(box STATIC lua/misc.cc lua/info.c lua/stat.c - lua/phia.c lua/error.cc lua/session.c lua/net_box.c diff --git a/src/box/lua/info.c b/src/box/lua/info.c index 6d4189e80e..a39fcc1dd7 100644 --- a/src/box/lua/info.c +++ b/src/box/lua/info.c @@ -199,6 +199,77 @@ lbox_info_cluster(struct lua_State *L) return 1; } +typedef void (*phia_info_f)(const char*, const char*, void *); + +extern int phia_info(const char *name, phia_info_f, void *); + +static void +lbox_phia_cb(const char *key, const char *value, void *arg) +{ + struct lua_State *L; + L = (struct lua_State*)arg; + if (value == NULL || key[0] == '\0') + return; + + /* stack: box.info.phia */ + lua_pushvalue(L, -1); /* current = box.info.phia */ + const char *part = key; + while(1) { + /* stack: box.info.phia, current */ + const char *part_end = strchrnul(part, '.'); + if (*part_end == '\0') { + lua_pushlstring(L, part, part_end - part); + lua_pushstring(L, value); + /* stack: box.info.phia, current, part, value */ + lua_settable(L, -3); /* current[part] = value */ + /* stack: box.info.phia, current */ + lua_pop(L, 1); + /* stack: box.info.phia */ + break; + } + + lua_pushlstring(L, part, part_end - part); + lua_gettable(L, -2); + /* stack: box.info.phia, current, current[part] */ + if (!lua_istable(L, -1)) { + lua_pop(L, 1); /* pop current[part] */ + lua_newtable(L); + lua_pushlstring(L, part, part_end - part); + lua_pushvalue(L, -2); + /* stack: box.info.phia, current, new, part, new */ + lua_settable(L, -4); /* current[part] = new */ + } + /* stack: box.info.phia, current, current[part] */ + lua_replace(L, -2); + /* stack: box.info.phia, current */ + part = part_end + 1; + } +} + +static int +lbox_info_phia_call(struct lua_State *L) +{ + lua_newtable(L); + phia_info(NULL, lbox_phia_cb, (void*)L); + return 1; +} + +static int +lbox_info_phia(struct lua_State *L) +{ + lua_newtable(L); + + lua_newtable(L); /* metatable */ + + lua_pushstring(L, "__call"); + lua_pushcfunction(L, lbox_info_phia_call); + lua_settable(L, -3); + + lua_setmetatable(L, -2); + + return 1; +} + static const struct luaL_reg lbox_info_dynamic_meta [] = { @@ -209,6 +280,7 @@ lbox_info_dynamic_meta [] = {"uptime", lbox_info_uptime}, {"pid", lbox_info_pid}, {"cluster", lbox_info_cluster}, + {"phia", lbox_info_phia}, {NULL, NULL} }; diff --git a/src/box/lua/init.c b/src/box/lua/init.c index 702da93f18..39cfcb1010 100644 --- a/src/box/lua/init.c +++ b/src/box/lua/init.c @@ -47,7 +47,6 @@ #include "box/lua/space.h" #include "box/lua/misc.h" #include "box/lua/stat.h" -#include "box/lua/phia.h" #include "box/lua/info.h" #include "box/lua/session.h" #include "box/lua/net_box.h" @@ -119,7 +118,6 @@ box_lua_init(struct lua_State *L) box_lua_misc_init(L); box_lua_info_init(L); box_lua_stat_init(L); - box_lua_phia_init(L); box_lua_session_init(L); luaopen_net_box(L); lua_pop(L, 1); diff --git a/src/box/lua/phia.c b/src/box/lua/phia.c index d99a100ddf..dae3204e5a 100644 --- a/src/box/lua/phia.c +++ b/src/box/lua/phia.c @@ -139,56 +139,6 @@ seek_stat_item(const char *name, int rps, int64_t total, void *cb_ctx) } #endif -typedef void (*phia_info_f)(const char*, const char*, void *); - -extern int phia_info(const char *name, phia_info_f, void *); - -static void -lbox_phia_cb_index(const char *key, const char *value, void *arg) -{ - (void) key; - struct lua_State *L; - L = (struct lua_State*)arg; - if (value == NULL) { - lua_pushnil(L); - return; - } - lua_pushstring(L, value); -} - -static int -lbox_phia_index(struct lua_State *L) -{ - luaL_checkstring(L, -1); - const char *name = lua_tostring(L, -1); - return phia_info(name, lbox_phia_cb_index, (void*)L); -} - -static void -lbox_phia_cb(const char *key, const char *value, void *arg) -{ - struct lua_State *L; - L = (struct lua_State*)arg; - if (value == NULL) - return; - lua_pushstring(L, key); - lua_pushstring(L, value); - lua_settable(L, -3); -} - -static int -lbox_phia_call(struct lua_State *L) -{ - lua_newtable(L); - phia_info(NULL, lbox_phia_cb, (void*)L); - return 1; -} - -static const struct luaL_reg lbox_phia_meta [] = { - {"__index", lbox_phia_index}, - {"__call", lbox_phia_call}, - {NULL, NULL} -}; void box_lua_phia_init(struct lua_State *L) diff --git a/src/box/lua/phia.h b/src/box/lua/phia.h deleted file mode 100644 index ce05001251..0000000000 --- a/src/box/lua/phia.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef INCLUDES_TARANTOOL_LUA_PHIA_H -#define INCLUDES_TARANTOOL_LUA_PHIA_H -/* - * Copyright 2010-2015, Tarantool AUTHORS, please see AUTHORS file. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * 1. Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR - * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF - * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -#if defined(__cplusplus) -extern "C" { -#endif /* defined(__cplusplus) */ - -struct lua_State; -void box_lua_phia_init(struct lua_State *L); - -#if defined(__cplusplus) -} /* extern "C" */ -#endif /* defined(__cplusplus) */ - -#endif /* INCLUDES_TARANTOOL_LUA_PHIA_H */ diff --git a/test/box/info.result b/test/box/info.result index 7266934f1c..2aade619b9 100644 --- a/test/box/info.result +++ b/test/box/info.result @@ -56,6 +56,7 @@ table.sort(t) t --- - - cluster + - phia - pid - replication - server diff --git a/test/box/misc.result b/test/box/misc.result index 4c6a32b652..04a187a2e8 100644 --- a/test/box/misc.result +++ b/test/box/misc.result @@ -63,7 +63,6 @@ t - info - internal - once - - phia - rollback - runtime - schema diff --git a/test/phia/monitoring.result b/test/phia/monitoring.result index 99db3b8f1b..2a37e884b2 100644 --- a/test/phia/monitoring.result +++ b/test/phia/monitoring.result @@ -4,7 +4,7 @@ space = box.schema.space.create('test', { engine = 'phia' }) index = space:create_index('primary', { type = 'tree', parts = {1, 'str'} }) --- ... -box.phia['phia.version'] +box.info.phia().phia.version --- - 2.1.1 ... diff --git a/test/phia/monitoring.test.lua b/test/phia/monitoring.test.lua index 634bada95a..564fea647e 100644 --- a/test/phia/monitoring.test.lua +++ b/test/phia/monitoring.test.lua @@ -1,5 +1,5 @@ space = box.schema.space.create('test', { engine = 'phia' }) index = space:create_index('primary', { type = 'tree', parts = {1, 'str'} }) -box.phia['phia.version'] +box.info.phia().phia.version space:drop() -- GitLab