|
@@ -1,51 +1,47 @@
|
|
|
-#include "lua_lsup.h"
|
|
|
+#include "lua_volksdata.h"
|
|
|
|
|
|
|
|
|
-LSUP_rc
|
|
|
-term_set_to_table (lua_State *L, LSUP_TermSet *ts)
|
|
|
+static VOLK_Graph **allocate_graph (lua_State *L)
|
|
|
{
|
|
|
- size_t i = 0;
|
|
|
- LSUP_Term *tmp = NULL;
|
|
|
- LSUP_rc rc;
|
|
|
- lua_newtable (L);
|
|
|
- while ((rc = LSUP_term_set_next (ts, &i, &tmp)) == LSUP_OK) {
|
|
|
- LSUP_Term **t2_p = (LSUP_Term **)lua_newuserdata (L, sizeof *t2_p);
|
|
|
- luaL_getmetatable (L, "LSUP.Term");
|
|
|
- lua_setmetatable (L, -2);
|
|
|
- *t2_p = LSUP_term_copy (tmp);
|
|
|
-
|
|
|
- lua_pushboolean (L, true);
|
|
|
- lua_rawset (L, -3);
|
|
|
- }
|
|
|
-
|
|
|
- if (rc < LSUP_OK) return rc;
|
|
|
- return LSUP_OK;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-static LSUP_Graph **allocate_graph (lua_State *L)
|
|
|
-{
|
|
|
- LSUP_Graph **gp = lua_newuserdatauv (L, sizeof (*gp), 1);
|
|
|
- luaL_getmetatable (L, "LSUP.Graph");
|
|
|
+ VOLK_Graph **gp = lua_newuserdatauv (L, sizeof (*gp), 1);
|
|
|
+ luaL_getmetatable (L, "VOLK.Graph");
|
|
|
lua_setmetatable (L, -2);
|
|
|
|
|
|
return gp;
|
|
|
}
|
|
|
|
|
|
|
|
|
+/** @brief Create a new graph.
|
|
|
+ *
|
|
|
+ * @param[in] store The store to use as a back end. if unspecified, an
|
|
|
+ * in-memory hashmap is used.
|
|
|
+ *
|
|
|
+ * @param[in] uri_str String to use as the graph URI. It may be namespace-
|
|
|
+ * prefixed, or fully qualified. If not specified, Volksdata generates a
|
|
|
+ * UUID4 URN.
|
|
|
+ *
|
|
|
+ * @param[in[ is_ns Whether the passed string is a namespace-prefixed URI.
|
|
|
+ * Default is false. If this is true, the `uri_str` parameter MUST be passed.
|
|
|
+ */
|
|
|
static int l_graph_new (lua_State *L)
|
|
|
{
|
|
|
- LSUP_Store *store;
|
|
|
+ VOLK_Store *store;
|
|
|
if (lua_isnoneornil (L, 1)) store = NULL;
|
|
|
- else store = *(LSUP_Store **)luaL_checkudata (L, 1, "LSUP.Store");
|
|
|
+ else store = *(VOLK_Store **)luaL_checkudata (L, 1, "VOLK.Store");
|
|
|
|
|
|
const char *uri_str = lua_tostring (L, 2);
|
|
|
+ bool is_ns = lua_toboolean (L, 3);
|
|
|
|
|
|
- LSUP_Graph **gp = allocate_graph (L);
|
|
|
+ if (is_ns && !uri_str)
|
|
|
+ luaL_error (L, "URI must be passed if is_ns is true.", 2);
|
|
|
|
|
|
- *gp = LSUP_graph_new (store, uri_str);
|
|
|
- LOG_DEBUG ("New graph URI @%p: %s", *gp, LSUP_graph_uri (*gp)->data);
|
|
|
+ VOLK_Graph **gp = allocate_graph (L);
|
|
|
+
|
|
|
+ *gp = is_ns ?
|
|
|
+ VOLK_graph_new_ns (store, uri_str) :
|
|
|
+ VOLK_graph_new (store, uri_str);
|
|
|
LUA_NLCHECK (*gp, "Error creating graph.");
|
|
|
+ LOG_DEBUG ("New graph URI @%p: %s", *gp, VOLK_graph_uri (*gp)->data);
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -53,10 +49,10 @@ static int l_graph_new (lua_State *L)
|
|
|
|
|
|
static int l_graph_list (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Store *store = *(LSUP_Store **)luaL_checkudata (
|
|
|
- L, 1, "LSUP.Store");
|
|
|
+ const VOLK_Store *store = *(VOLK_Store **)luaL_checkudata (
|
|
|
+ L, 1, "VOLK.Store");
|
|
|
|
|
|
- LSUP_TermSet *ts = LSUP_graph_list (store);
|
|
|
+ VOLK_TermSet *ts = VOLK_graph_list (store);
|
|
|
LUA_NLCHECK (ts, "Error retrieving context list.");
|
|
|
LUA_PCHECK (term_set_to_table (L, ts), "Error generating term set table");
|
|
|
|
|
@@ -70,10 +66,10 @@ static int l_graph_list (lua_State *L)
|
|
|
|
|
|
static int l_graph_gc (lua_State *L)
|
|
|
{
|
|
|
- LSUP_Graph **gp = luaL_checkudata(L, 1, "LSUP.Graph");
|
|
|
+ VOLK_Graph **gp = luaL_checkudata(L, 1, "VOLK.Graph");
|
|
|
LOG_DEBUG ("Garbage collecting graph @%p", *gp);
|
|
|
|
|
|
- LSUP_graph_free (*gp);
|
|
|
+ VOLK_graph_free (*gp);
|
|
|
*gp = NULL;
|
|
|
|
|
|
return 0;
|
|
@@ -82,13 +78,13 @@ static int l_graph_gc (lua_State *L)
|
|
|
|
|
|
static int l_graph_get_uri (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr = check_graph (L);
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
|
|
|
- LSUP_Term **tp = lua_newuserdata (L, sizeof *tp);
|
|
|
- luaL_getmetatable (L, "LSUP.Term");
|
|
|
+ VOLK_Term **tp = lua_newuserdata (L, sizeof *tp);
|
|
|
+ luaL_getmetatable (L, "VOLK.Term");
|
|
|
lua_setmetatable (L, -2);
|
|
|
|
|
|
- *tp = LSUP_term_copy (LSUP_graph_uri (gr));
|
|
|
+ *tp = VOLK_term_copy (VOLK_graph_uri (gr));
|
|
|
LUA_NLCHECK (*tp, "Error getting graph URI.");
|
|
|
|
|
|
return 1;
|
|
@@ -97,10 +93,10 @@ static int l_graph_get_uri (lua_State *L)
|
|
|
|
|
|
static int l_graph_to_string (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr = check_graph (L);
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
lua_pushfstring (
|
|
|
- L, "LSUP.Graph @%p <%s>: %d triples",
|
|
|
- gr, LSUP_graph_uri (gr)->data, LSUP_graph_size (gr));
|
|
|
+ L, "VOLK.Graph @%p <%s>: %d triples",
|
|
|
+ gr, VOLK_graph_uri (gr)->data, VOLK_graph_size (gr));
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -108,8 +104,8 @@ static int l_graph_to_string (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));
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
+ lua_pushinteger (L, VOLK_graph_size (gr));
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -117,29 +113,18 @@ static int l_graph_len (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");
|
|
|
-
|
|
|
- LUA_PCHECK (LSUP_graph_copy (src, dest), "Error copying graph");
|
|
|
-
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-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, *p, *o;
|
|
|
+ const VOLK_Graph *src = check_graph (L);
|
|
|
+ VOLK_Graph *dest = *(VOLK_Graph **)luaL_checkudata (L, 2, "VOLK.Graph");
|
|
|
+ const VOLK_Term *s, *p, *o;
|
|
|
|
|
|
if lua_isnoneornil (L, 3) s = NULL;
|
|
|
- else s = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
|
|
|
+ else s = *(VOLK_Term **)luaL_checkudata (L, 3, "VOLK.Term");
|
|
|
if lua_isnoneornil (L, 4) p = NULL;
|
|
|
- else p = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
|
|
|
+ else p = *(VOLK_Term **)luaL_checkudata (L, 4, "VOLK.Term");
|
|
|
if lua_isnoneornil (L, 5) o = NULL;
|
|
|
- else o = *(LSUP_Term **)luaL_checkudata (L, 5, "LSUP.Term");
|
|
|
+ else o = *(VOLK_Term **)luaL_checkudata (L, 5, "VOLK.Term");
|
|
|
|
|
|
- LUA_PCHECK (LSUP_graph_copy_contents (
|
|
|
+ LUA_PCHECK (VOLK_graph_copy_contents (
|
|
|
src, dest, s, p, o), "Error copying graph.");
|
|
|
|
|
|
return 1;
|
|
@@ -148,12 +133,12 @@ static int l_graph_copy_contents (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");
|
|
|
+ const VOLK_Graph *gr1 = check_graph (L);
|
|
|
+ const VOLK_Graph *gr2 =
|
|
|
+ *(VOLK_Graph **)luaL_checkudata (L, 2, "VOLK.Graph");
|
|
|
|
|
|
LOG_DEBUG ("Comparing graphs %p %p", gr1, gr2);
|
|
|
- int eq_rc = LSUP_graph_equals (gr1, gr2);
|
|
|
+ int eq_rc = VOLK_graph_equals (gr1, gr2);
|
|
|
lua_pushboolean (L, eq_rc);
|
|
|
|
|
|
return 1;
|
|
@@ -162,11 +147,11 @@ static int l_graph_equals (lua_State *L)
|
|
|
|
|
|
static int l_graph_contains (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr = check_graph (L);
|
|
|
- const LSUP_Triple *spo =
|
|
|
- *(LSUP_Triple **)luaL_checkudata (L, 2, "LSUP.Triple");
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
+ const VOLK_Triple *spo =
|
|
|
+ *(VOLK_Triple **)luaL_checkudata (L, 2, "VOLK.Triple");
|
|
|
|
|
|
- lua_pushboolean (L, LSUP_graph_contains (gr, spo));
|
|
|
+ lua_pushboolean (L, VOLK_graph_contains (gr, spo));
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -175,13 +160,13 @@ static int l_graph_contains (lua_State *L)
|
|
|
/// Initialize iterative addition.
|
|
|
static int l_graph_add_init (lua_State *L)
|
|
|
{
|
|
|
- LSUP_Graph *gr = check_graph (L);
|
|
|
+ VOLK_Graph *gr = check_graph (L);
|
|
|
|
|
|
- LSUP_GraphIterator **it_p = lua_newuserdata (L, sizeof *it_p);
|
|
|
- luaL_getmetatable (L, "LSUP.GraphIterator");
|
|
|
+ VOLK_GraphIterator **it_p = lua_newuserdata (L, sizeof *it_p);
|
|
|
+ luaL_getmetatable (L, "VOLK.GraphIterator");
|
|
|
lua_setmetatable (L, -2);
|
|
|
|
|
|
- *it_p = LSUP_graph_add_init (gr);
|
|
|
+ *it_p = VOLK_graph_add_init (gr);
|
|
|
LUA_NLCHECK (*it_p, "Error creating graph iterator.");
|
|
|
|
|
|
return 1;
|
|
@@ -191,10 +176,10 @@ static int l_graph_add_init (lua_State *L)
|
|
|
/// Add one triple.
|
|
|
static int l_graph_add_iter (lua_State *L)
|
|
|
{
|
|
|
- LSUP_GraphIterator **it_p = luaL_checkudata (L, 1, "LSUP.GraphIterator");
|
|
|
- const LSUP_Triple **spo = luaL_checkudata (L, 2, "LSUP.Triple");
|
|
|
+ VOLK_GraphIterator **it_p = luaL_checkudata (L, 1, "VOLK.GraphIterator");
|
|
|
+ const VOLK_Triple **spo = luaL_checkudata (L, 2, "VOLK.Triple");
|
|
|
|
|
|
- LUA_PCHECK (LSUP_graph_add_iter (*it_p, *spo), "Error adding triple");
|
|
|
+ LUA_PCHECK (VOLK_graph_add_iter (*it_p, *spo), "Error adding triple");
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -203,8 +188,8 @@ static int l_graph_add_iter (lua_State *L)
|
|
|
/// Finalize iterative addition.
|
|
|
static int l_graph_add_done (lua_State *L)
|
|
|
{
|
|
|
- LSUP_GraphIterator **it_p = lua_touserdata (L, 1);
|
|
|
- LSUP_graph_add_done (*it_p);
|
|
|
+ VOLK_GraphIterator **it_p = lua_touserdata (L, 1);
|
|
|
+ VOLK_graph_add_done (*it_p);
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -214,47 +199,47 @@ static int l_graph_add_done (lua_State *L)
|
|
|
*/
|
|
|
static int l_graph_add (lua_State *L)
|
|
|
{
|
|
|
- LSUP_Graph *gr = check_graph (L);
|
|
|
+ VOLK_Graph *gr = check_graph (L);
|
|
|
int rc;
|
|
|
- LSUP_rc lsup_rc= LSUP_NOACTION;
|
|
|
+ VOLK_rc volk_rc= VOLK_NOACTION;
|
|
|
size_t i = 0, ct = 0;
|
|
|
- LSUP_GraphIterator *it = LSUP_graph_add_init (gr);
|
|
|
+ VOLK_GraphIterator *it = VOLK_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");
|
|
|
+ const VOLK_Triple *spo =
|
|
|
+ *(VOLK_Triple **)luaL_checkudata (L, -1, "VOLK.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);
|
|
|
+ volk_rc = VOLK_graph_add_iter (it, spo);
|
|
|
|
|
|
- if (lsup_rc < LSUP_OK) break;
|
|
|
- if (lsup_rc == LSUP_OK) ct++;
|
|
|
+ if (volk_rc < VOLK_OK) break;
|
|
|
+ if (volk_rc == VOLK_OK) ct++;
|
|
|
};
|
|
|
- LSUP_graph_add_done (it);
|
|
|
+ VOLK_graph_add_done (it);
|
|
|
lua_pushinteger (L, ct);
|
|
|
|
|
|
- if (UNLIKELY (lsup_rc < LSUP_OK)) return luaL_error (
|
|
|
+ if (UNLIKELY (volk_rc < VOLK_OK)) return luaL_error (
|
|
|
L, "Error adding triple at position %d: %s",
|
|
|
- i, LSUP_strerror (lsup_rc));
|
|
|
+ i, VOLK_strerror (volk_rc));
|
|
|
else return 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
static int l_graph_remove (lua_State *L)
|
|
|
{
|
|
|
- LSUP_Graph *gr = check_graph (L);
|
|
|
- const LSUP_Term *s, *p, *o;
|
|
|
+ VOLK_Graph *gr = check_graph (L);
|
|
|
+ const VOLK_Term *s, *p, *o;
|
|
|
if lua_isnoneornil (L, 2) s = NULL;
|
|
|
- else s = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
|
|
|
+ else s = *(VOLK_Term **)luaL_checkudata (L, 2, "VOLK.Term");
|
|
|
if lua_isnoneornil (L, 3) p = NULL;
|
|
|
- else p = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
|
|
|
+ else p = *(VOLK_Term **)luaL_checkudata (L, 3, "VOLK.Term");
|
|
|
if lua_isnoneornil (L, 4) o = NULL;
|
|
|
- else o = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
|
|
|
+ else o = *(VOLK_Term **)luaL_checkudata (L, 4, "VOLK.Term");
|
|
|
|
|
|
size_t ct;
|
|
|
- LSUP_rc rc = LSUP_graph_remove (gr, s, p, o, &ct);
|
|
|
+ VOLK_rc rc = VOLK_graph_remove (gr, s, p, o, &ct);
|
|
|
|
|
|
LUA_PCHECK (rc, "Error removing triples from graph.");
|
|
|
lua_pushinteger (L, ct);
|
|
@@ -265,20 +250,20 @@ static int l_graph_remove (lua_State *L)
|
|
|
|
|
|
static int graph_iter_next (lua_State *L)
|
|
|
{
|
|
|
- LSUP_GraphIterator **it_p = lua_touserdata (L, lua_upvalueindex (1));
|
|
|
+ VOLK_GraphIterator **it_p = lua_touserdata (L, lua_upvalueindex (1));
|
|
|
|
|
|
- LSUP_Triple **spo_p = lua_newuserdatauv (L, sizeof (*spo_p), 1);
|
|
|
- luaL_getmetatable (L, "LSUP.Triple");
|
|
|
+ VOLK_Triple **spo_p = lua_newuserdatauv (L, sizeof (*spo_p), 1);
|
|
|
+ luaL_getmetatable (L, "VOLK.Triple");
|
|
|
lua_setmetatable (L, -2);
|
|
|
*spo_p = NULL;
|
|
|
|
|
|
- LSUP_rc rc = LSUP_graph_iter_next (*it_p, spo_p);
|
|
|
+ VOLK_rc rc = VOLK_graph_iter_next (*it_p, spo_p);
|
|
|
|
|
|
- if (rc != LSUP_OK) {
|
|
|
- LSUP_graph_iter_free (*it_p);
|
|
|
+ if (rc != VOLK_OK) {
|
|
|
+ VOLK_graph_iter_free (*it_p);
|
|
|
*it_p = NULL;
|
|
|
|
|
|
- if (rc == LSUP_END) {
|
|
|
+ if (rc == VOLK_END) {
|
|
|
lua_pushnil (L);
|
|
|
lua_pushstring (L, "End of lookup results.");
|
|
|
return 2;
|
|
@@ -293,22 +278,22 @@ static int graph_iter_next (lua_State *L)
|
|
|
|
|
|
static int l_graph_lookup (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr = check_graph (L);
|
|
|
- const LSUP_Term *s, *p, *o;
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
+ const VOLK_Term *s, *p, *o;
|
|
|
if lua_isnoneornil (L, 2) s = NULL;
|
|
|
- else s = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
|
|
|
+ else s = *(VOLK_Term **)luaL_checkudata (L, 2, "VOLK.Term");
|
|
|
if lua_isnoneornil (L, 3) p = NULL;
|
|
|
- else p = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
|
|
|
+ else p = *(VOLK_Term **)luaL_checkudata (L, 3, "VOLK.Term");
|
|
|
if lua_isnoneornil (L, 4) o = NULL;
|
|
|
- else o = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
|
|
|
+ else o = *(VOLK_Term **)luaL_checkudata (L, 4, "VOLK.Term");
|
|
|
|
|
|
- LSUP_GraphIterator **it_p = lua_newuserdata (L, sizeof *it_p);
|
|
|
+ VOLK_GraphIterator **it_p = lua_newuserdata (L, sizeof *it_p);
|
|
|
*it_p = NULL;
|
|
|
- luaL_getmetatable (L, "LSUP.GraphIterator");
|
|
|
+ luaL_getmetatable (L, "VOLK.GraphIterator");
|
|
|
lua_setmetatable (L, -2);
|
|
|
|
|
|
size_t ct;
|
|
|
- *it_p = LSUP_graph_lookup (gr, s, p, o, &ct);
|
|
|
+ *it_p = VOLK_graph_lookup (gr, s, p, o, &ct);
|
|
|
LUA_NLCHECK (*it_p, "Error creating graph iterator.");
|
|
|
LOG_DEBUG ("Found triples: %d", ct);
|
|
|
|
|
@@ -318,14 +303,52 @@ static int l_graph_lookup (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
+static int l_graph_encode (lua_State *L)
|
|
|
+{
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
+ const char *codec_str = lua_tostring (L, 2);
|
|
|
+
|
|
|
+ VOLK_Codec codec;
|
|
|
+ if (strcmp(codec_str, "nt") == 0)
|
|
|
+ codec = nt_codec;
|
|
|
+ else if (strcmp(codec_str, "ttl") == 0)
|
|
|
+ codec = ttl_codec;
|
|
|
+ else
|
|
|
+ return luaL_error(L, "Invalid encoding format: %s", codec_str);
|
|
|
+
|
|
|
+ char *out = calloc (1, 1);
|
|
|
+ LUA_NLCHECK (out, "Error allocating codec iterator.");
|
|
|
+ void *it = codec.encode_graph_init (gr);
|
|
|
+ LUA_NLCHECK (it, "Error creating codec iterator.");
|
|
|
+
|
|
|
+ char *tmp = NULL;
|
|
|
+ VOLK_rc rc;
|
|
|
+ while ((rc = codec.encode_graph_iter (it, &tmp)) != VOLK_END) {
|
|
|
+ log_debug ("Serialized fragment: %s\n", tmp);
|
|
|
+ LUA_PCHECK (rc, "Encoding failed");
|
|
|
+ out = realloc (out, strlen(out) + strlen (tmp) + 1);
|
|
|
+ LUA_NLCHECK (out, "Error reallocating serialization buffer.");
|
|
|
+ out = strcat (out, tmp);
|
|
|
+ LUA_NLCHECK (out, "Error filling serialization buffer.");
|
|
|
+ }
|
|
|
+ codec.encode_graph_done (it);
|
|
|
+
|
|
|
+ lua_pushstring (L, out);
|
|
|
+ free (tmp);
|
|
|
+ free (out);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
static int l_graph_get (lua_State *L)
|
|
|
{
|
|
|
- LSUP_Term *gr_uri = check_term (L);
|
|
|
- LSUP_Store *store = *(LSUP_Store **)luaL_checkudata (L, 2, "LSUP.Store");
|
|
|
+ VOLK_Term *gr_uri = check_term (L);
|
|
|
+ VOLK_Store *store = *(VOLK_Store **)luaL_checkudata (L, 2, "VOLK.Store");
|
|
|
|
|
|
- LSUP_Graph **gp = allocate_graph (L);
|
|
|
+ VOLK_Graph **gp = allocate_graph (L);
|
|
|
size_t ct;
|
|
|
- *gp = LSUP_graph_get (store, gr_uri, &ct);
|
|
|
+ *gp = VOLK_graph_get (store, gr_uri, &ct);
|
|
|
lua_pushinteger (L, ct);
|
|
|
|
|
|
return 2;
|
|
@@ -344,12 +367,12 @@ static int l_graph_get (lua_State *L)
|
|
|
*/
|
|
|
static int graph_iter_gc (lua_State *L)
|
|
|
{
|
|
|
- LSUP_GraphIterator **it_p = lua_touserdata (L, 1);
|
|
|
+ VOLK_GraphIterator **it_p = lua_touserdata (L, 1);
|
|
|
|
|
|
if (UNLIKELY (!it_p || !*it_p)) return 0;
|
|
|
LOG_DEBUG ("Garbage collecting iterator @%p", it_p);
|
|
|
|
|
|
- LSUP_graph_iter_free (*it_p);
|
|
|
+ VOLK_graph_iter_free (*it_p);
|
|
|
*it_p = NULL;
|
|
|
|
|
|
return 0;
|
|
@@ -360,15 +383,15 @@ static int graph_iter_gc (lua_State *L)
|
|
|
*/
|
|
|
static int l_graph_connections (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr = check_graph (L);
|
|
|
- LSUP_Term *t = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
|
|
|
- const LSUP_LinkType type = luaL_checkinteger (L, 3);
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
+ VOLK_Term *t = *(VOLK_Term **)luaL_checkudata (L, 2, "VOLK.Term");
|
|
|
+ const VOLK_LinkType type = luaL_checkinteger (L, 3);
|
|
|
LOG_DEBUG ("Adding term for connections: @%p", *t);
|
|
|
|
|
|
- LSUP_LinkMap **lm_p = lua_newuserdata (L, sizeof *lm_p);
|
|
|
- *lm_p = LSUP_graph_connections (gr, t, type);
|
|
|
+ VOLK_LinkMap **lm_p = lua_newuserdata (L, sizeof *lm_p);
|
|
|
+ *lm_p = VOLK_graph_connections (gr, t, type);
|
|
|
LUA_NLCHECK (*lm_p, "Error creating Link map.");
|
|
|
- luaL_getmetatable (L, "LSUP.LinkMap");
|
|
|
+ luaL_getmetatable (L, "VOLK.LinkMap");
|
|
|
lua_setmetatable (L, -2);
|
|
|
|
|
|
return 1;
|
|
@@ -377,13 +400,13 @@ static int l_graph_connections (lua_State *L)
|
|
|
|
|
|
static int l_graph_term_set (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr = check_graph (L);
|
|
|
- const LSUP_Term *t1 = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
|
|
|
- const LSUP_TriplePos t1_pos = luaL_checkinteger (L, 3);
|
|
|
- const LSUP_Term *t2 = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
|
|
|
- const LSUP_TriplePos t2_pos = luaL_checkinteger (L, 5);
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
+ const VOLK_Term *t1 = *(VOLK_Term **)luaL_checkudata (L, 2, "VOLK.Term");
|
|
|
+ const VOLK_TriplePos t1_pos = luaL_checkinteger (L, 3);
|
|
|
+ const VOLK_Term *t2 = *(VOLK_Term **)luaL_checkudata (L, 4, "VOLK.Term");
|
|
|
+ const VOLK_TriplePos t2_pos = luaL_checkinteger (L, 5);
|
|
|
|
|
|
- LSUP_TermSet *ts = LSUP_graph_term_set (gr, t1, t1_pos, t2, t2_pos);
|
|
|
+ VOLK_TermSet *ts = VOLK_graph_term_set (gr, t1, t1_pos, t2, t2_pos);
|
|
|
LUA_NLCHECK (ts, "Error creating term set from graph.");
|
|
|
LUA_PCHECK (term_set_to_table (L, ts), "Error generating term set table");
|
|
|
|
|
@@ -393,10 +416,10 @@ static int l_graph_term_set (lua_State *L)
|
|
|
|
|
|
static int l_graph_unique_terms (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr = check_graph (L);
|
|
|
- const LSUP_TriplePos pos = luaL_checkinteger (L, 2);
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
+ const VOLK_TriplePos pos = luaL_checkinteger (L, 2);
|
|
|
|
|
|
- LSUP_TermSet *ts = LSUP_graph_unique_terms (gr, pos);
|
|
|
+ VOLK_TermSet *ts = VOLK_graph_unique_terms (gr, pos);
|
|
|
LUA_NLCHECK (ts, "Error creating term set from unique terms.");
|
|
|
LUA_PCHECK (term_set_to_table (L, ts), "Error generating term set table");
|
|
|
|
|
@@ -404,7 +427,7 @@ static int l_graph_unique_terms (lua_State *L)
|
|
|
}
|
|
|
|
|
|
|
|
|
-/** @brief Get all o's for given s and p as a set (Lua table) of values.
|
|
|
+/** @brief Get all o's for given s and p as a table of values.
|
|
|
*
|
|
|
* @param[in] gr Graph to query from.
|
|
|
*
|
|
@@ -412,32 +435,38 @@ static int l_graph_unique_terms (lua_State *L)
|
|
|
*
|
|
|
* @param[in] p Predicate to query.
|
|
|
*
|
|
|
- * @return Table (set) of objects found per sp combination.
|
|
|
+ * @return Table of objects found per sp combination.
|
|
|
+ *
|
|
|
+ * @todo This could be reformatted as a term set generator, but it seems more
|
|
|
+ * efficient this way because it iterates over the results only once.
|
|
|
*/
|
|
|
static int l_graph_attr (lua_State *L)
|
|
|
{
|
|
|
- const LSUP_Graph *gr = check_graph (L);
|
|
|
- const LSUP_Term *s = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
|
|
|
- const LSUP_Term *p = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
|
|
|
+ const VOLK_Graph *gr = check_graph (L);
|
|
|
+ const VOLK_Term *s = *(VOLK_Term **)luaL_checkudata (L, 2, "VOLK.Term");
|
|
|
+ const VOLK_Term *p = *(VOLK_Term **)luaL_checkudata (L, 3, "VOLK.Term");
|
|
|
|
|
|
size_t ct;
|
|
|
- LSUP_GraphIterator *it = LSUP_graph_lookup (gr, s, p, NULL, &ct);
|
|
|
+ VOLK_GraphIterator *it = VOLK_graph_lookup (gr, s, p, NULL, &ct);
|
|
|
|
|
|
lua_createtable (L, ct, 0);
|
|
|
- LSUP_Triple *spo;
|
|
|
- LSUP_rc rc;
|
|
|
- while ((rc = LSUP_graph_iter_next (it, &spo)) == LSUP_OK) {
|
|
|
- LSUP_Term **op = lua_newuserdata (L, sizeof *op);
|
|
|
- luaL_getmetatable (L, "LSUP.Term");
|
|
|
+ VOLK_Triple *spo;
|
|
|
+ VOLK_rc rc;
|
|
|
+ while ((rc = VOLK_graph_iter_next (it, &spo)) == VOLK_OK) {
|
|
|
+ // Table keys are term hashes.
|
|
|
+ lua_pushinteger (L, VOLK_term_hash (spo->o));
|
|
|
+
|
|
|
+ // Table values are the term sets proper.
|
|
|
+ VOLK_Term **op = lua_newuserdata (L, sizeof *op);
|
|
|
+ luaL_getmetatable (L, "VOLK.Term");
|
|
|
lua_setmetatable (L, -2);
|
|
|
- *op = LSUP_term_copy (spo->o);
|
|
|
- lua_pushboolean (L, true);
|
|
|
+ *op = VOLK_term_copy (spo->o);
|
|
|
|
|
|
lua_rawset (L, -3);
|
|
|
}
|
|
|
- LSUP_graph_iter_free (it);
|
|
|
+ VOLK_graph_iter_free (it);
|
|
|
|
|
|
- if (rc != LSUP_END) LUA_PCHECK (rc, "Error iterating attr values");
|
|
|
+ if (rc != VOLK_END) LUA_PCHECK (rc, "Error iterating attr values");
|
|
|
|
|
|
return 1;
|
|
|
}
|
|
@@ -482,7 +511,6 @@ static const luaL_Reg graph_lib_meth [] = {
|
|
|
{"__len", l_graph_len},
|
|
|
|
|
|
{"copy", l_graph_copy},
|
|
|
- {"copy_contents", l_graph_copy_contents},
|
|
|
|
|
|
{"add", l_graph_add},
|
|
|
{"remove", l_graph_remove},
|
|
@@ -494,8 +522,8 @@ static const luaL_Reg graph_lib_meth [] = {
|
|
|
{"term_set", l_graph_term_set},
|
|
|
{"unique_terms", l_graph_unique_terms},
|
|
|
{"attr", l_graph_attr},
|
|
|
+ {"encode", l_graph_encode},
|
|
|
|
|
|
- //{"to_n3", l_graph_to_n3},
|
|
|
{NULL}
|
|
|
};
|
|
|
|
|
@@ -505,16 +533,16 @@ static const LEnumConst graph_enums[] = {
|
|
|
};
|
|
|
|
|
|
|
|
|
-int luaopen_lsup_graph (lua_State *L)
|
|
|
+int luaopen_volksdata_graph (lua_State *L)
|
|
|
{
|
|
|
- LSUP_init(); // This is idempotent: no problem calling it multiple times.
|
|
|
- luaL_newmetatable (L, "LSUP.Graph");
|
|
|
+ VOLK_init(); // This is idempotent: no problem calling it multiple times.
|
|
|
+ luaL_newmetatable (L, "VOLK.Graph");
|
|
|
lua_pushvalue (L, -1);
|
|
|
lua_setfield (L, -2, "__index");
|
|
|
luaL_setfuncs (L, graph_lib_meth, 0);
|
|
|
|
|
|
// Metatables for ancillary types.
|
|
|
- luaL_newmetatable (L, "LSUP.GraphIterator");
|
|
|
+ luaL_newmetatable (L, "VOLK.GraphIterator");
|
|
|
lua_pushcfunction (L, graph_iter_gc);
|
|
|
lua_setfield (L, -2, "__gc");
|
|
|
|