Browse Source

Add initial graph module, mostly boilerplate.

scossu 3 weeks ago
parent
commit
d9c066e1a7
6 changed files with 152 additions and 14 deletions
  1. 127 0
      lua/src/graph.c
  2. 1 1
      lua/src/lua_lsup.h
  3. 0 0
      lua/src/store.c
  4. 2 2
      lua/src/term.c
  5. 19 7
      lua/src/triple.c
  6. 3 4
      lua/test.lua

+ 127 - 0
lua/src/graph.c

@@ -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;
+}

+ 1 - 1
lua/src/lua_lsup.h

@@ -16,6 +16,6 @@
 int l_nsmap_new (lua_State *L);
 
 /// Allocate space for a term object.
-LSUP_Term **allocate_term (lua_State *L);
+///LSUP_Term **allocate_term (lua_State *L);
 
 #endif  // _LUA_LSUP_H

+ 0 - 0
lua/src/store.c


+ 2 - 2
lua/src/term.c

@@ -134,7 +134,7 @@ static int equals (lua_State *L)
 }
 
 
-static int gc (lua_State *L)
+static int term_gc (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
     if (t) LSUP_term_free (t);
@@ -345,7 +345,7 @@ static const luaL_Reg term_setters [] = {
 
 static const luaL_Reg term_lib_meth [] = {
     {"__eq", equals},
-    {"__gc", gc},
+    {"__gc", term_gc},
     {"__index", get_attr},
     {"__tostring", to_string},
     {"__newindex", set_attr},

+ 19 - 7
lua/src/triple.c

@@ -4,6 +4,17 @@
     *(LSUP_Triple **)luaL_checkudata(L, 1, "LSUP.Triple")
 
 
+// TODO this is duplicate. Find a way to access a common fn.
+LSUP_Term **allocate_term (lua_State *L)
+{
+    LSUP_Term **tp = lua_newuserdatauv (L, sizeof (*tp), 1);
+    luaL_getmetatable (L, "LSUP.Term");
+    lua_setmetatable (L, -2);
+
+    return tp;
+}
+
+
 /*
  * Factory methods.
  */
@@ -140,10 +151,17 @@ static const struct luaL_Reg triple_lib_meth [] = {
 
 int luaopen_triple (lua_State *L)
 {
+    LSUP_init();  // This is idempotent: no problem calling it multiple times.
     luaL_newmetatable (L, "LSUP.Triple");
     lua_pushvalue (L, -1);
 
-    // Enums
+    // MT
+    lua_setfield (L, -1, "__index");
+    luaL_setfuncs (L, triple_lib_meth, 0);
+
+    luaL_newlib (L, triple_lib_fn);
+
+    // Module-level constants.
     lua_pushinteger (L, TRP_POS_S);
     lua_setfield (L, -2, "TRP_POS_S");
     lua_pushinteger (L, TRP_POS_P);
@@ -151,12 +169,6 @@ int luaopen_triple (lua_State *L)
     lua_pushinteger (L, TRP_POS_O);
     lua_setfield (L, -2, "TRP_POS_O");
 
-    // MT
-    lua_setfield (L, -1, "__index");
-    luaL_setfuncs (L, triple_lib_meth, 0);
-
-    luaL_newlib (L, triple_lib_fn);
-
     return 1;
 }
 

+ 3 - 4
lua/test.lua

@@ -7,7 +7,6 @@ trp = triple.new (
 
 t1 = term.iriref_new("urn:s:11")
 t2 = term.lit_new("123", "xsd:int")
-mt = {z =99}
-mt["__index"] = mt
-a = {x = 1, y = 2}
-setmetatable(a, mt)
+
+graph = require "graph"
+gr1 = graph.new()