|
@@ -0,0 +1,127 @@
|
|
|
+#include "lua_lsup.h"
|
|
|
+
|
|
|
+#define check_graph(L) \
|
|
|
+ *(LSUP_Graph **)luaL_checkudata(L, 1, "LSUP.Graph")
|
|
|
+
|
|
|
+
|
|
|
+static int graph_new (lua_State *L)
|
|
|
+{
|
|
|
+ const LSUP_StoreType store_type = lua_tointeger (L, 1);
|
|
|
+ const char *uri_str = lua_tostring (L, 2);
|
|
|
+
|
|
|
+ LSUP_Graph **gp = lua_newuserdatauv (L, sizeof (*gp), 1);
|
|
|
+ luaL_getmetatable (L, "LSUP.Graph");
|
|
|
+ lua_setmetatable (L, -2);
|
|
|
+
|
|
|
+ LSUP_Store *store = NULL;
|
|
|
+ if (store_type) {
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // TODO Move store creation fn and handle into a separate module.
|
|
|
+ store = LSUP_store_new (store_type, NULL, 0);
|
|
|
+ // Set up the store if a function for that is defined.
|
|
|
+ if (sif->setup_fn) {
|
|
|
+ if (sif->setup_fn(NULL, false) < LSUP_OK)
|
|
|
+ return luaL_error (L, "Error initializing back end store.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO Make store ID, nsm and initial size accessible.
|
|
|
+ *gp = LSUP_graph_new (store, uri_str, NULL);
|
|
|
+ if (!*gp) return luaL_error (L, "Error while creating graph!");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+ * Class methods.
|
|
|
+ */
|
|
|
+
|
|
|
+static int graph_gc (lua_State *L)
|
|
|
+{
|
|
|
+ LSUP_Graph *gr = check_graph (L);
|
|
|
+ printf ("Freeing graph @%p", gr);
|
|
|
+ if (gr) LSUP_graph_free (gr);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static int graph_to_string (lua_State *L)
|
|
|
+{
|
|
|
+ const LSUP_Graph *gr = check_graph (L);
|
|
|
+ lua_pushfstring (
|
|
|
+ L, "LSUP.Graph @%p <%s>: %d triples",
|
|
|
+ gr, LSUP_graph_uri (gr)->data, LSUP_graph_size (gr));
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static int graph_len (lua_State *L)
|
|
|
+{
|
|
|
+ const LSUP_Graph *gr = check_graph (L);
|
|
|
+ lua_pushinteger (L, LSUP_graph_size (gr));
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+ * Library setup.
|
|
|
+ */
|
|
|
+
|
|
|
+static const luaL_Reg graph_lib_fn [] = {
|
|
|
+ {"new", graph_new},
|
|
|
+
|
|
|
+ {NULL}
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+static const luaL_Reg graph_getters [] = {
|
|
|
+
|
|
|
+ {NULL}
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+/*
|
|
|
+static const luaL_Reg graph_setters [] = {
|
|
|
+ {NULL}
|
|
|
+};
|
|
|
+*/
|
|
|
+
|
|
|
+static const luaL_Reg graph_lib_meth [] = {
|
|
|
+ //{"__eq", graph_equals},
|
|
|
+ {"__gc", graph_gc},
|
|
|
+ //{"__index", get_attr},
|
|
|
+ {"__tostring", graph_to_string},
|
|
|
+ {"__len", graph_len},
|
|
|
+ //{"__newindex", set_attr},
|
|
|
+
|
|
|
+ //{"to_n3", graph_to_n3},
|
|
|
+ {NULL}
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+int luaopen_graph (lua_State *L)
|
|
|
+{
|
|
|
+ LSUP_init(); // This is idempotent: no problem calling it multiple times.
|
|
|
+ luaL_newmetatable (L, "LSUP.Graph");
|
|
|
+ luaL_setfuncs (L, graph_lib_meth, 0);
|
|
|
+
|
|
|
+ // Getters table.
|
|
|
+ lua_newtable (L);
|
|
|
+ for (int i = 0; graph_getters[i].name != NULL; i++) {
|
|
|
+ lua_pushcfunction (L, graph_getters[i].func);
|
|
|
+ lua_setfield (L, -2, graph_getters[i].name);
|
|
|
+ }
|
|
|
+ // Set getters table as a value for the Graph metatable.
|
|
|
+ lua_setfield (L, -2, "getters");
|
|
|
+
|
|
|
+ luaL_newlib (L, graph_lib_fn);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|