Skip to content

Commit

Permalink
box: move info_handler interface into src/info
Browse files Browse the repository at this point in the history
Box/info.h defines info_handler interface with a set
of virtual functions. It allows to hide Lua from code
not depending on this language, and is used in things
line index:info(), box.info() to build Lua table with
some info. But is does not depend on box/ so move it
to src/.

Also, this API is needed for the forthcoming SWIM
module which is going to be placed into src/.

Needed for #3234
  • Loading branch information
Gerold103 committed Oct 23, 2018
1 parent 129099b commit bc54fd5
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 109 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -171,6 +171,7 @@ set (server_sources
lua/crypto.c
lua/httpc.c
lua/utf8.c
lua/info.c
${lua_sources}
${PROJECT_SOURCE_DIR}/third_party/lua-yaml/lyaml.cc
${PROJECT_SOURCE_DIR}/third_party/lua-yaml/b64.c
Expand Down
4 changes: 2 additions & 2 deletions src/box/lua/index.c
Expand Up @@ -30,10 +30,10 @@
*/
#include "box/lua/index.h"
#include "lua/utils.h"
#include "lua/info.h"
#include <info.h>
#include "box/box.h"
#include "box/index.h"
#include "box/info.h"
#include "box/lua/info.h"
#include "box/lua/tuple.h"
#include "box/lua/misc.h" /* lbox_encode_tuple_on_gc() */

Expand Down
78 changes: 2 additions & 76 deletions src/box/lua/info.c
Expand Up @@ -32,7 +32,7 @@
#define _GNU_SOURCE
#endif

#include "box/lua/info.h"
#include "lua/info.h"

#include <ctype.h> /* tolower() */

Expand All @@ -45,7 +45,7 @@
#include "box/iproto.h"
#include "box/wal.h"
#include "box/replication.h"
#include "box/info.h"
#include <info.h>
#include "box/gc.h"
#include "box/checkpoint.h"
#include "box/engine.h"
Expand Down Expand Up @@ -426,80 +426,6 @@ lbox_info_gc(struct lua_State *L)
return 1;
}

static void
luaT_info_begin(struct info_handler *info)
{
lua_State *L = (lua_State *) info->ctx;
lua_newtable(L);
}

static void
luaT_info_end(struct info_handler *info)
{
(void) info;
}

static void
luaT_info_begin_table(struct info_handler *info, const char *key)
{
lua_State *L = (lua_State *) info->ctx;
lua_pushstring(L, key);
lua_newtable(L);
}

static void
luaT_info_end_table(struct info_handler *info)
{
lua_State *L = (lua_State *) info->ctx;
lua_settable(L, -3);
}

static void
luaT_info_append_double(struct info_handler *info,
const char *key, double value)
{
lua_State *L = (lua_State *) info->ctx;
lua_pushstring(L, key);
lua_pushnumber(L, value);
lua_settable(L, -3);
}

static void
luaT_info_append_int(struct info_handler *info, const char *key,
int64_t value)
{
lua_State *L = (lua_State *) info->ctx;
lua_pushstring(L, key);
luaL_pushint64(L, value);
lua_settable(L, -3);
}

static void
luaT_info_append_str(struct info_handler *info, const char *key,
const char *value)
{
lua_State *L = (lua_State *) info->ctx;
lua_pushstring(L, key);
lua_pushstring(L, value);
lua_settable(L, -3);
}

void
luaT_info_handler_create(struct info_handler *h, struct lua_State *L)
{
static struct info_handler_vtab lua_vtab = {
.begin = luaT_info_begin,
.end = luaT_info_end,
.begin_table = luaT_info_begin_table,
.end_table = luaT_info_end_table,
.append_int = luaT_info_append_int,
.append_str = luaT_info_append_str,
.append_double = luaT_info_append_double
};
h->vtab = &lua_vtab;
h->ctx = L;
}

static int
lbox_info_vinyl_call(struct lua_State *L)
{
Expand Down
4 changes: 2 additions & 2 deletions src/box/lua/stat.c
Expand Up @@ -42,8 +42,8 @@
#include "box/iproto.h"
#include "box/engine.h"
#include "box/vinyl.h"
#include "box/info.h"
#include "box/lua/info.h"
#include <info.h>
#include "lua/info.h"
#include "lua/utils.h"

extern struct rmean *rmean_box;
Expand Down
69 changes: 40 additions & 29 deletions src/box/info.h → src/info.h
@@ -1,7 +1,7 @@
#ifndef INCLUDES_TARANTOOL_BOX_INFO_H
#define INCLUDES_TARANTOOL_BOX_INFO_H
#ifndef INCLUDES_TARANTOOL_INFO_H
#define INCLUDES_TARANTOOL_INFO_H
/*
* Copyright 2010-2017, Tarantool AUTHORS, please see AUTHORS file.
* Copyright 2010-2018, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
Expand Down Expand Up @@ -35,9 +35,10 @@

/**
* @file
* This module provides an adapter for Lua/C API to generate box.info()
* and index:info() introspection trees. The primary purpose of this
* adapter is to eliminate Engine <-> Lua interdependency.
* This module provides an adapter for Lua/C API to generate
* box.info() and index:info() introspection trees. The primary
* purpose of this adapter is to eliminate Engine <-> Lua
* interdependency.
*
* TREE STRUCTURE
*
Expand All @@ -57,20 +58,18 @@
*
* IMPLEMENTATION DETAILS
*
* Current implementation calls Lua/C API under the hood without any
* pcall() wrapping. As you may now, idiosyncratic Lua/C API unwinds
* C stacks on errors in a way you can't handle in C. Please ensure that
* all blocks of code which call info_append_XXX() functions are
* exception/longjmp safe.
* Current implementation calls Lua/C API under the hood without
* any pcall() wrapping. As you may now, idiosyncratic Lua/C API
* unwinds C stacks on errors in a way you can't handle in C.
* Please ensure that all blocks of code which call
* info_append_XXX() functions are exception/longjmp safe.
*/

#if defined(__cplusplus)
extern "C" {
#endif /* defined(__cplusplus) */

/**
* Virtual method table for struct info_handler.
*/
/** Virtual method table for struct info_handler. */
struct info_handler_vtab {
/** The begin of document. */
void (*begin)(struct info_handler *);
Expand All @@ -86,13 +85,17 @@ struct info_handler_vtab {
/** Set int64_t value. */
void (*append_int)(struct info_handler *, const char *key,
int64_t value);
/** Set uint64_t value. */
void (*append_uint)(struct info_handler *, const char *key,
uint64_t value);
/** Set double value. */
void (*append_double)(struct info_handler *,
const char *key, double value);
};

/**
* Adapter for Lua/C API to generate box.info() sections from engines.
* Adapter for Lua/C API to generate box.info() sections from
* engines.
*/
struct info_handler {
struct info_handler_vtab *vtab;
Expand All @@ -103,7 +106,6 @@ struct info_handler {
/**
* Starts a new document and creates root-level associative array.
* @param info box.info() adapter.
* @throws C++ exception on OOM, see info.h comments.
* @pre must be called once before any other functions.
*/
static inline void
Expand All @@ -115,7 +117,6 @@ info_begin(struct info_handler *info)
/**
* Finishes the document and closes root-level associative array.
* @param info box.info() adapter.
* @throws C++ exception on OOM, see info.h comments.
* @pre must be called at the end.
*/
static inline void
Expand All @@ -125,11 +126,11 @@ info_end(struct info_handler *info)
}

/**
* Associates int64_t value with @a key in the current associative array.
* Associates int64_t value with @a key in the current associative
* array.
* @param info box.info() adapter.
* @param key key.
* @param value value.
* @throws C++ exception on OOM, see info.h comments.
* @pre associative array is started.
*/
static inline void
Expand All @@ -139,16 +140,29 @@ info_append_int(struct info_handler *info, const char *key, int64_t value)
}

/**
* Associates zero-terminated string with @a key in the current associative
* array.
* Associates uint64_t value with @a key in the current
* associative array.
* @param info box.info() adapter.
* @param key key.
* @param value value.
* @pre associative array is started.
*/
static inline void
info_append_uint(struct info_handler *info, const char *key, uint64_t value)
{
return info->vtab->append_uint(info, key, value);
}

/**
* Associates zero-terminated string with @a key in the current
* associative array.
* @param info box.info() adapter.
* @param key key.
* @param value value.
* @throws C++ exception on OOM, see info.h comments.
*/
static inline void
info_append_str(struct info_handler *info, const char *key,
const char *value)
const char *value)
{
return info->vtab->append_str(info, key, value);
}
Expand All @@ -159,7 +173,6 @@ info_append_str(struct info_handler *info, const char *key,
* @param info box.info() adapter.
* @param key key.
* @param value value.
* @throws C++ exception on OOM, see info.h comments.
*/
static inline void
info_append_double(struct info_handler *info, const char *key,
Expand All @@ -168,22 +181,20 @@ info_append_double(struct info_handler *info, const char *key,
return info->vtab->append_double(info, key, value);
}

/*
/**
* Associates a new associative array with @a key.
* @param info box.info() adapter.
* @param key key.
* @throws C++ exception on OOM, see info.h comments.
*/
static inline void
info_table_begin(struct info_handler *info, const char *key)
{
return info->vtab->begin_table(info, key);
}

/*
/**
* Finishes the current active associative array.
* @param info box.info() adapter
* @throws C++ exception on OOM, see info.h comments.
*/
static inline void
info_table_end(struct info_handler *info)
Expand All @@ -195,4 +206,4 @@ info_table_end(struct info_handler *info)
} /* extern "C" */
#endif /* defined(__cplusplus) */

#endif /* INCLUDES_TARANTOOL_BOX_INFO_H */
#endif /* INCLUDES_TARANTOOL_INFO_H */

0 comments on commit bc54fd5

Please sign in to comment.