1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include "lua_lsup.h"
- static int l_store_new (lua_State *L)
- {
- const LSUP_StoreType store_type = luaL_checkinteger (L, 1);
- const char *id = luaL_optstring (L, 2, NULL);
- const bool clear = lua_toboolean (L, 3);
- LSUP_Store **store_p = lua_newuserdatauv (L, sizeof (*store_p), 1);
- luaL_getmetatable (L, "LSUP.Store");
- lua_setmetatable (L, -2);
- const LSUP_StoreInt *sif = LSUP_store_int (store_type);
- if (UNLIKELY (!sif)) return luaL_error (
- L, "No interface defined for store type: %d.", store_type);
- if (clear) LOG_DEBUG("Clearing old store.");
- *store_p = LSUP_store_new (store_type, id, 0);
- if (!*store_p) return luaL_error (L, "Error creating back end store.");
- // Set up the store if a function for that is defined.
- if (sif->setup_fn && sif->setup_fn (id, clear) < LSUP_OK)
- return luaL_error (L, "Error initializing back end store.");
- return 1;
- }
- static int store_gc (lua_State *L)
- {
- LSUP_Store **sp = luaL_checkudata(L, 1, "LSUP.Store");
- LSUP_store_free (*sp);
- *sp = NULL;
- return 0;
- }
- // TODO add transaction handling.
- static const luaL_Reg store_lib_fn [] = {
- {"new", l_store_new},
- {NULL}
- };
- static const LEnumConst store_enums[] = {
- {"HTABLE", LSUP_STORE_HTABLE},
- {"MDB", LSUP_STORE_MDB},
- {NULL, 0}
- };
- int luaopen_lsup_store (lua_State *L)
- {
- LSUP_init(); // This is idempotent: no problem calling it multiple times.
- luaL_newmetatable (L, "LSUP.Store");
- lua_pushvalue (L, -1);
- lua_setfield (L, -2, "__index");
- lua_pushcfunction (L, store_gc);
- lua_setfield (L, -2, "__gc");
- luaL_newlib (L, store_lib_fn);
- push_int_const (L, store_enums);
- return 1;
- }
|