|
@@ -1,4 +1,5 @@
|
|
|
#include "lua_lsup.h"
|
|
|
+#include "stackdump.h"
|
|
|
|
|
|
#define check_graph(L) \
|
|
|
*(LSUP_Graph **)luaL_checkudata(L, 1, "LSUP.Graph")
|
|
@@ -14,7 +15,7 @@ static LSUP_Graph **allocate_graph (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_new (lua_State *L)
|
|
|
+static int l_graph_new (lua_State *L)
|
|
|
{
|
|
|
const LSUP_StoreType store_type = lua_tointeger (L, 1);
|
|
|
const char *uri_str = lua_tostring (L, 2);
|
|
@@ -48,7 +49,7 @@ static int graph_new (lua_State *L)
|
|
|
* Class methods.
|
|
|
*/
|
|
|
|
|
|
-static int graph_gc (lua_State *L)
|
|
|
+static int l_graph_gc (lua_State *L)
|
|
|
{
|
|
|
LSUP_Graph *gr = check_graph (L);
|
|
|
if (gr) LSUP_graph_free (gr);
|
|
@@ -57,7 +58,7 @@ static int graph_gc (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_to_string (lua_State *L)
|
|
|
+static int l_graph_to_string (lua_State *L)
|
|
|
{
|
|
|
const LSUP_Graph *gr = check_graph (L);
|
|
|
lua_pushfstring (
|
|
@@ -68,7 +69,7 @@ static int graph_to_string (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_len (lua_State *L)
|
|
|
+static int l_graph_len (lua_State *L)
|
|
|
{
|
|
|
const LSUP_Graph *gr = check_graph (L);
|
|
|
lua_pushinteger (L, LSUP_graph_size (gr));
|
|
@@ -77,7 +78,7 @@ static int graph_len (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_copy (lua_State *L)
|
|
|
+static int l_graph_copy (lua_State *L)
|
|
|
{
|
|
|
const LSUP_Graph *src = check_graph (L);
|
|
|
LSUP_Graph *dest = *(LSUP_Graph **)luaL_checkudata(L, 2, "LSUP.Graph");
|
|
@@ -88,13 +89,18 @@ static int graph_copy (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_copy_contents (lua_State *L)
|
|
|
+static int l_graph_copy_contents (lua_State *L)
|
|
|
{
|
|
|
const LSUP_Graph *src = check_graph (L);
|
|
|
- LSUP_Graph *dest = *(LSUP_Graph **)luaL_checkudata(L, 2, "LSUP.Graph");
|
|
|
- const LSUP_Term *s = *(LSUP_Term **)luaL_testudata(L, 3, "LSUP.Term");
|
|
|
- const LSUP_Term *p = *(LSUP_Term **)luaL_testudata(L, 4, "LSUP.Term");
|
|
|
- const LSUP_Term *o = *(LSUP_Term **)luaL_testudata(L, 5, "LSUP.Term");
|
|
|
+ LSUP_Graph *dest = *(LSUP_Graph **)luaL_checkudata (L, 2, "LSUP.Graph");
|
|
|
+ const LSUP_Term *s, *p, *o;
|
|
|
+
|
|
|
+ if lua_isnoneornil (L, 3) s = NULL;
|
|
|
+ else s = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
|
|
|
+ if lua_isnoneornil (L, 4) p = NULL;
|
|
|
+ else p = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
|
|
|
+ if lua_isnoneornil (L, 5) o = NULL;
|
|
|
+ else o = *(LSUP_Term **)luaL_checkudata (L, 5, "LSUP.Term");
|
|
|
|
|
|
LUA_PCHECK (LSUP_graph_copy_contents (
|
|
|
src, dest, s, p, o), "Error copying graph.");
|
|
@@ -103,69 +109,133 @@ static int graph_copy_contents (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_equals (lua_State *L)
|
|
|
+static int l_graph_equals (lua_State *L)
|
|
|
{
|
|
|
const LSUP_Graph *gr1 = check_graph (L);
|
|
|
const LSUP_Graph *gr2 =
|
|
|
- *(LSUP_Graph **)luaL_checkudata(L, 2, "LSUP.Graph");
|
|
|
+ *(LSUP_Graph **)luaL_checkudata (L, 2, "LSUP.Graph");
|
|
|
|
|
|
- lua_pushboolean (L, LSUP_graph_equals (gr1, gr2));
|
|
|
+ LOG_DEBUG ("Comparing graphs %p %p", gr1, gr2);
|
|
|
+ int eq_rc = LSUP_graph_equals (gr1, gr2);
|
|
|
+ lua_pushboolean (L, eq_rc);
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_iter (lua_State *L)
|
|
|
+static int l_graph_contains (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr1 = check_graph (L);
|
|
|
+ const LSUP_Graph *gr = check_graph (L);
|
|
|
+ const LSUP_Triple *spo =
|
|
|
+ *(LSUP_Triple **)luaL_checkudata (L, 2, "LSUP.Triple");
|
|
|
+
|
|
|
+ lua_pushboolean (L, LSUP_graph_contains (gr, spo));
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_contains (lua_State *L)
|
|
|
+static int l_graph_add (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr1 = check_graph (L);
|
|
|
-
|
|
|
- return 1;
|
|
|
+ LSUP_Graph *gr = check_graph (L);
|
|
|
+ LOG_DEBUG ("Triples type: %s", lua_typename (L, lua_type (L, 2)));
|
|
|
+ int rc;
|
|
|
+ LSUP_rc lsup_rc;
|
|
|
+ size_t i = 0, ct = 0;
|
|
|
+ LSUP_GraphIterator *it = LSUP_graph_add_init (gr);
|
|
|
+
|
|
|
+ while ((rc = lua_rawgeti (L, 2, ++i)) != LUA_TNIL) {
|
|
|
+ //LOG_DEBUG ("Triple type: %s", lua_typename (L, rc));
|
|
|
+ const LSUP_Triple *spo =
|
|
|
+ *(LSUP_Triple **)luaL_checkudata (L, -1, "LSUP.Triple");
|
|
|
+ LOG_DEBUG (
|
|
|
+ "Got triple %d: {%s %s %s}\n",
|
|
|
+ i, spo->s->data, spo->p->data, spo->o->data);
|
|
|
+ lsup_rc = LSUP_graph_add_iter (it, spo);
|
|
|
+
|
|
|
+ if (lsup_rc < LSUP_OK) break;
|
|
|
+ if (lsup_rc == LSUP_OK) ct++;
|
|
|
+ };
|
|
|
+ LSUP_graph_add_done (it);
|
|
|
+ lua_pushinteger (L, ct);
|
|
|
+
|
|
|
+ if (UNLIKELY (lsup_rc < LSUP_OK)) return luaL_error (
|
|
|
+ L, "Error adding triple at position %d: %s",
|
|
|
+ i, LSUP_strerror (lsup_rc));
|
|
|
+ else return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_add (lua_State *L)
|
|
|
+static int l_graph_iter_next (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr1 = check_graph (L);
|
|
|
+ LSUP_GraphIterator *it =
|
|
|
+ *(LSUP_GraphIterator **)lua_touserdata (L, lua_upvalueindex (1));
|
|
|
+
|
|
|
+ LSUP_Triple **spo_p = lua_newuserdatauv (L, sizeof (*spo_p), 1);
|
|
|
+ luaL_getmetatable (L, "LSUP.Triple");
|
|
|
+ lua_setmetatable (L, -2);
|
|
|
+
|
|
|
+ LSUP_rc rc = LSUP_graph_iter_next (it, spo_p);
|
|
|
+
|
|
|
+ if (rc == LSUP_END) {
|
|
|
+ LSUP_graph_iter_free (it);
|
|
|
+ lua_pushnil (L);
|
|
|
+ lua_pushstring (L, "End of lookup results.");
|
|
|
+ return 2;
|
|
|
+ }
|
|
|
+ LUA_PCHECK (rc, "Error retrieving a lookup result.");
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_lookup (lua_State *L)
|
|
|
+static int l_graph_lookup (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr1 = check_graph (L);
|
|
|
+ const LSUP_Graph *gr = check_graph (L);
|
|
|
+ const LSUP_Term *s, *p, *o;
|
|
|
+ if lua_isnoneornil (L, 2) s = NULL;
|
|
|
+ else s = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
|
|
|
+ if lua_isnoneornil (L, 3) p = NULL;
|
|
|
+ else p = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
|
|
|
+ if lua_isnoneornil (L, 4) o = NULL;
|
|
|
+ else o = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
|
|
|
+
|
|
|
+ LSUP_GraphIterator **it_p =
|
|
|
+ (LSUP_GraphIterator **)lua_newuserdata (L, sizeof *it_p);
|
|
|
+ *it_p = NULL;
|
|
|
+ luaL_getmetatable (L, "LSUP.GraphIterator");
|
|
|
+ lua_setmetatable (L, -2);
|
|
|
+
|
|
|
+ size_t ct;
|
|
|
+ *it_p = LSUP_graph_lookup (gr, s, p, o, &ct);
|
|
|
+ LUA_NLCHECK (*it_p, "Error creating graph iterator.");
|
|
|
+ LOG_DEBUG ("Found triples: %d", ct);
|
|
|
+
|
|
|
+ lua_pushcclosure (L, l_graph_iter_next, 1);
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_connections (lua_State *L)
|
|
|
+static int l_graph_connections (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr1 = check_graph (L);
|
|
|
+ const LSUP_Graph *gr = check_graph (L);
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_term_set (lua_State *L)
|
|
|
+static int l_graph_term_set (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr1 = check_graph (L);
|
|
|
+ const LSUP_Graph *gr = check_graph (L);
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
-static int graph_unique_terms (lua_State *L)
|
|
|
+static int l_graph_unique_terms (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr1 = check_graph (L);
|
|
|
+ const LSUP_Graph *gr = check_graph (L);
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -176,7 +246,7 @@ static int graph_unique_terms (lua_State *L)
|
|
|
*/
|
|
|
|
|
|
static const luaL_Reg graph_lib_fn [] = {
|
|
|
- {"new", graph_new},
|
|
|
+ {"new", l_graph_new},
|
|
|
|
|
|
{NULL}
|
|
|
};
|
|
@@ -184,8 +254,8 @@ static const luaL_Reg graph_lib_fn [] = {
|
|
|
|
|
|
/*
|
|
|
static const luaL_Reg graph_getters [] = {
|
|
|
- {"uri", graph_get_uri},
|
|
|
- {"namespace", graph_get_nsm},
|
|
|
+ {"uri", l_graph_get_uri},
|
|
|
+ {"namespace", l_graph_get_nsm},
|
|
|
|
|
|
{NULL}
|
|
|
};
|
|
@@ -194,33 +264,31 @@ static const luaL_Reg graph_getters [] = {
|
|
|
|
|
|
/*
|
|
|
static const luaL_Reg graph_setters [] = {
|
|
|
- {"uri", graph_set_uri},
|
|
|
+ {"uri", l_graph_set_uri},
|
|
|
|
|
|
{NULL}
|
|
|
};
|
|
|
*/
|
|
|
|
|
|
static const luaL_Reg graph_lib_meth [] = {
|
|
|
- {"__eq", graph_equals},
|
|
|
- {"__gc", graph_gc},
|
|
|
+ {"__eq", l_graph_equals},
|
|
|
+ {"__gc", l_graph_gc},
|
|
|
//{"__index", get_attr},
|
|
|
- {"__tostring", graph_to_string},
|
|
|
- {"__len", graph_len},
|
|
|
- {"__ipairs", graph_iter},
|
|
|
//{"__newindex", set_attr},
|
|
|
+ {"__tostring", l_graph_to_string},
|
|
|
+ {"__len", l_graph_len},
|
|
|
|
|
|
- {"copy", graph_copy},
|
|
|
- {"copy_contents", graph_copy_contents},
|
|
|
- {"contains", graph_contains},
|
|
|
+ {"copy", l_graph_copy},
|
|
|
+ {"copy_contents", l_graph_copy_contents},
|
|
|
+ {"contains", l_graph_contains},
|
|
|
|
|
|
- {"add", graph_add},
|
|
|
- // TODO add graph_add_init, graph_add_iter, graph_add_done
|
|
|
- {"lookup", graph_lookup},
|
|
|
- {"connections", graph_connections},
|
|
|
- {"term_set", graph_term_set},
|
|
|
- {"unique_terms", graph_unique_terms},
|
|
|
+ {"add", l_graph_add},
|
|
|
+ {"lookup", l_graph_lookup},
|
|
|
+ {"connections", l_graph_connections},
|
|
|
+ {"term_set", l_graph_term_set},
|
|
|
+ {"unique_terms", l_graph_unique_terms},
|
|
|
|
|
|
- //{"to_n3", graph_to_n3},
|
|
|
+ //{"to_n3", l_graph_to_n3},
|
|
|
{NULL}
|
|
|
};
|
|
|
|
|
@@ -233,6 +301,8 @@ int luaopen_lsup_graph (lua_State *L)
|
|
|
lua_setfield (L, -2, "__index");
|
|
|
luaL_setfuncs (L, graph_lib_meth, 0);
|
|
|
|
|
|
+ luaL_newmetatable (L, "LSUP.GraphIterator");
|
|
|
+
|
|
|
/*
|
|
|
// Getters table.
|
|
|
lua_newtable (L);
|