Переглянути джерело

Update nsm API; add graph.attr; other updates.

scossu 1 день тому
батько
коміт
75f5ff8772
9 змінених файлів з 149 додано та 147 видалено
  1. 0 0
      debug/parse_valgrind_supp.sh
  2. 0 0
      debug/valgrind-lua-noreadline.supp
  3. 73 8
      src/lua_graph.c
  4. 14 0
      src/lua_lsup.h
  5. 24 71
      src/lua_namespace.c
  6. 0 21
      src/lua_store.c
  7. 21 22
      src/lua_term.c
  8. 5 13
      src/lua_triple.c
  9. 12 12
      test.lua

+ 0 - 0
parse_valgrind_supp.sh → debug/parse_valgrind_supp.sh


+ 0 - 0
valgrind-lua-noreadline.supp → debug/valgrind-lua-noreadline.supp


+ 73 - 8
src/lua_graph.c

@@ -1,8 +1,5 @@
 #include "lua_lsup.h"
 
-#define check_graph(L) \
-    *(LSUP_Graph **)luaL_checkudata(L, 1, "LSUP.Graph")
-
 
 static LSUP_Graph **allocate_graph (lua_State *L)
 {
@@ -17,16 +14,14 @@ static LSUP_Graph **allocate_graph (lua_State *L)
 static int l_graph_new (lua_State *L)
 {
     LSUP_Store *store;
+    if (lua_isnoneornil (L, 1)) store = NULL;
+    else store = *(LSUP_Store **)luaL_checkudata (L, 1, "LSUP.Store");
 
-    if (!lua_isnoneornil (L, 1))
-        store = *(LSUP_Store **)luaL_checkudata (L, 1, "LSUP.Store");
-    else store = NULL;
     const char *uri_str = lua_tostring (L, 2);
 
     LSUP_Graph **gp = allocate_graph (L);
 
-    // TODO Make store ID, nsm and initial size accessible.
-    *gp = LSUP_graph_new (store, uri_str, NULL);
+    *gp = LSUP_graph_new (store, uri_str);
     LOG_DEBUG ("New graph URI @%p: %s", *gp, LSUP_graph_uri (*gp)->data);
     LUA_NLCHECK (*gp, "Error creating graph.");
 
@@ -66,6 +61,21 @@ static int l_graph_gc (lua_State *L)
 }
 
 
+static int l_graph_get_uri (lua_State *L)
+{
+    const LSUP_Graph *gr = check_graph (L);
+
+    LSUP_Term **tp = lua_newuserdata (L, sizeof *tp);
+    luaL_getmetatable (L, "LSUP.Term");
+    lua_setmetatable (L, -2);
+
+    *tp = LSUP_term_copy (LSUP_graph_uri (gr));
+    LUA_NLCHECK (*tp, "Error getting graph URI.");
+
+    return 1;
+}
+
+
 static int l_graph_to_string (lua_State *L)
 {
     const LSUP_Graph *gr = check_graph (L);
@@ -250,6 +260,20 @@ static int l_graph_lookup (lua_State *L)
 }
 
 
+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");
+
+    LSUP_Graph **gp = allocate_graph (L);
+    size_t ct;
+    *gp = LSUP_graph_get (store, gr_uri, &ct);
+    lua_pushinteger (L, ct);
+
+    return 2;
+}
+
+
 /** @brief Free an iterator handle.
  *
  * This is only called if the iterator is abandoned before the
@@ -328,6 +352,44 @@ 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.
+ *
+ * @param[in] gr Graph to query from.
+ *
+ * @param[in] s Subject to query.
+ *
+ * @param[in] p Predicate to query.
+ *
+ * @return Table (set) of objects found per sp combination.
+ */
+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");
+
+    size_t ct;
+    LSUP_GraphIterator *it = LSUP_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");
+        lua_setmetatable (L, -2);
+        *op = LSUP_term_copy (spo->o);
+        lua_pushboolean (L, true);
+
+        lua_rawset (L, -3);
+    }
+    LSUP_graph_iter_free (it);
+
+    if (rc != LSUP_END) LUA_PCHECK (rc, "Error iterating attr values");
+
+    return 1;
+}
+
 /*
  * Library setup.
  */
@@ -335,6 +397,7 @@ static int l_graph_unique_terms (lua_State *L)
 static const luaL_Reg graph_lib_fn [] = {
     {"new", l_graph_new},
     {"list", l_graph_list},
+    {"get", l_graph_get},
 
     {NULL}
 };
@@ -372,11 +435,13 @@ static const luaL_Reg graph_lib_meth [] = {
     {"add", l_graph_add},
     {"remove", l_graph_remove},
 
+    {"get_uri", l_graph_get_uri},
     {"lookup", l_graph_lookup},
     {"contains", l_graph_contains},
     {"connections", l_graph_connections},
     {"term_set", l_graph_term_set},
     {"unique_terms", l_graph_unique_terms},
+    {"attr", l_graph_attr},
 
     //{"to_n3", l_graph_to_n3},
     {NULL}

+ 14 - 0
src/lua_lsup.h

@@ -17,6 +17,20 @@
 } while (0)
 
 
+/*
+ * Type check macros.
+ */
+
+#define check_term(L) \
+    *(LSUP_Term **)luaL_checkudata(L, 1, "LSUP.Term")
+
+#define check_triple(L) \
+    *(LSUP_Triple **)luaL_checkudata(L, 1, "LSUP.Triple")
+
+#define check_graph(L) \
+    *(LSUP_Graph **)luaL_checkudata(L, 1, "LSUP.Graph")
+
+
 /// Enum (int) constants to be passed to the module.
 typedef struct l_enum_const {
     const char  *k;

+ 24 - 71
src/lua_namespace.c

@@ -1,49 +1,18 @@
 #include "lua_lsup.h"
 
-#define check_nsm(L) \
-    *(LSUP_NSMap **)luaL_checkudata(L, 1, "LSUP.NSMap")
-
-
-/*
- * Factory methods.
- */
-
-int l_nsmap_new (lua_State *L)
-{
-    LSUP_NSMap **nsm_p = lua_newuserdatauv (L, sizeof (*nsm_p), 1);
-    luaL_getmetatable (L, "LSUP.NSMap");
-    lua_setmetatable (L, -2);
-
-    *nsm_p = LSUP_nsmap_new ();
-    if (!*nsm_p) return luaL_error (L, "Error while creating namespace map.");
-
-    return 1;
-}
-
 
 /*
  * Class methods.
  */
 
-static int l_nsmap_gc (lua_State *L)
-{
-    LSUP_NSMap *nsm = check_nsm (L);
-    LOG_DEBUG ("Garbage collecting NSM @%p", nsm);
-    if (nsm) LSUP_nsmap_free (nsm);
-
-    return 0;
-}
-
-
 static int l_nsmap_add (lua_State *L)
 {
-    LSUP_NSMap *nsm = check_nsm (L);
     const char
-        *pfx = luaL_checkstring (L, 2),
-        *nsstr = luaL_checkstring (L, 3);
+        *pfx = luaL_checkstring (L, 1),
+        *nsstr = luaL_checkstring (L, 2);
 
     LUA_PCHECK (
-            LSUP_nsmap_add (nsm, pfx, nsstr),
+            LSUP_nsmap_add (pfx, nsstr),
             "Error adding member to NS map");
 
     return 0;
@@ -52,11 +21,10 @@ static int l_nsmap_add (lua_State *L)
 
 static int l_nsmap_remove (lua_State *L)
 {
-    LSUP_NSMap *nsm = check_nsm (L);
-    const char *pfx = luaL_checkstring (L, 2);
+    const char *pfx = luaL_checkstring (L, 1);
 
     LUA_PCHECK (
-            LSUP_nsmap_remove (nsm, pfx),
+            LSUP_nsmap_remove (pfx),
             "Error removing member to NS map");
 
     return 0;
@@ -65,10 +33,9 @@ static int l_nsmap_remove (lua_State *L)
 
 static int l_nsmap_get_ns (lua_State *L)
 {
-    LSUP_NSMap *nsm = check_nsm (L);
-    const char *pfx = luaL_checkstring (L, 2);
+    const char *pfx = luaL_checkstring (L, 1);
 
-    const char *ns = LSUP_nsmap_get_ns (nsm, pfx);
+    const char *ns = LSUP_nsmap_get_ns (pfx);
 
     if (ns) lua_pushstring (L, ns);
     else return 0;
@@ -79,10 +46,9 @@ static int l_nsmap_get_ns (lua_State *L)
 
 static int l_nsmap_get_pfx (lua_State *L)
 {
-    LSUP_NSMap *nsm = check_nsm (L);
-    const char *ns = luaL_checkstring (L, 2);
+    const char *ns = luaL_checkstring (L, 1);
 
-    const char *pfx = LSUP_nsmap_get_pfx (nsm, ns);
+    const char *pfx = LSUP_nsmap_get_pfx (ns);
 
     if (pfx) lua_pushstring (L, pfx);
     else lua_pushnil (L);
@@ -93,12 +59,11 @@ static int l_nsmap_get_pfx (lua_State *L)
 
 static int l_nsmap_normalize_uri (lua_State *L)
 {
-    LSUP_NSMap *nsm = check_nsm (L);
-    const char *pfx_uri = luaL_checkstring (L, 2);
+    const char *pfx_uri = luaL_checkstring (L, 1);
 
     char *fq_uri;
     LUA_PCHECK (
-            LSUP_nsmap_normalize_uri (nsm, pfx_uri, &fq_uri),
+            LSUP_nsmap_normalize_uri (pfx_uri, &fq_uri),
             "Error normalizing URI");
 
     if (fq_uri) lua_pushstring (L, fq_uri);
@@ -111,12 +76,11 @@ static int l_nsmap_normalize_uri (lua_State *L)
 
 static int l_nsmap_denormalize_uri (lua_State *L)
 {
-    LSUP_NSMap *nsm = check_nsm (L);
-    const char *fq_uri = luaL_checkstring (L, 2);
+    const char *fq_uri = luaL_checkstring (L, 1);
 
     char *pfx_uri;
     LUA_PCHECK (
-            LSUP_nsmap_denormalize_uri (nsm, fq_uri, &pfx_uri),
+            LSUP_nsmap_denormalize_uri (fq_uri, &pfx_uri),
             "Error denormalizing URI");
 
     if (pfx_uri) lua_pushstring (L, pfx_uri);
@@ -127,14 +91,6 @@ static int l_nsmap_denormalize_uri (lua_State *L)
 }
 
 
-/*
-static int l_nsmap_iter_next (lua_State *L)
-{
-    // TODO
-    return 0;
-}
-*/
-
 static int nsmap_iter_next (lua_State *L)
 {
     const char ****data_p = lua_touserdata (L, lua_upvalueindex (1));
@@ -159,11 +115,10 @@ static int nsmap_iter_next (lua_State *L)
 
 static int l_nsmap_iter (lua_State *L)
 {
-    LSUP_NSMap *nsm = check_nsm (L);
     const char ****data_p = lua_newuserdata (L, sizeof (*data_p));
     //luaL_getmetatable (L, "LSUP.String2DIterator");
     //lua_setmetatable (L, -2);
-    *data_p = LSUP_nsmap_dump (nsm);
+    *data_p = LSUP_nsmap_dump ();
     LUA_NLCHECK (*data_p, "Error creating NS map iterator.");
 
     size_t *i = malloc (sizeof (*i));
@@ -182,33 +137,31 @@ static int l_nsmap_iter (lua_State *L)
  */
 
 static const luaL_Reg nsmap_lib_fn [] = {
-    {"new", l_nsmap_new},
-    {NULL}
-};
-
-
-static const luaL_Reg nsmap_lib_meth [] = {
-    {"__gc", l_nsmap_gc},
-    {"__pairs", l_nsmap_iter},
-
     {"add", l_nsmap_add},
     {"remove", l_nsmap_remove},
     {"get_ns", l_nsmap_get_ns},
     {"get_pfx", l_nsmap_get_pfx},
     {"normalize_uri", l_nsmap_normalize_uri},
     {"denormalize_uri", l_nsmap_denormalize_uri},
-    //{"iter", l_nsmap_iter},
+    {"iter", l_nsmap_iter},
 
     {NULL}
 };
 
 
+/*
+static const luaL_Reg nsmap_lib_meth [] = {
+    {NULL}
+};
+*/
+
+
 int luaopen_lsup_namespace (lua_State *L)
 {
     luaL_newmetatable (L, "LSUP.NSMap");
     lua_pushvalue (L, -1);
-    lua_setfield (L, -2, "__index");
-    luaL_setfuncs (L, nsmap_lib_meth, 0);
+    //lua_setfield (L, -2, "__index");
+    //luaL_setfuncs (L, nsmap_lib_meth, 0);
     luaL_newlib (L, nsmap_lib_fn);
 
     //luaL_newmetatable (L, "LSUP.String2DIterator");

+ 0 - 21
src/lua_store.c

@@ -11,9 +11,6 @@ static int l_store_new (lua_State *L)
     luaL_getmetatable (L, "LSUP.Store");
     lua_setmetatable (L, -2);
 
-    const LSUP_StoreInt *sif = LSUP_store_int (store_type);
-    LUA_NLCHECK (sif, "No interface defined for store type: %d.", store_type);
-
     // Set up the store if a function for that is defined.
     if (clear) log_info ("Clearing old store.");
     *store_p = LSUP_store_new (store_type, id, 0, clear);
@@ -35,22 +32,6 @@ static int store_gc (lua_State *L)
 }
 
 
-static int l_store_nsm_update (lua_State *L)
-{
-    const LSUP_Store *store = *(LSUP_Store **) luaL_checkudata (
-            L, 1, "LSUP.Store");
-    const char *pfx = luaL_checkstring (L, 2);
-    const char *ns = luaL_checkstring (L, 3);
-
-    if (store->sif->nsm_update_fn)
-        LUA_PCHECK (
-                store->sif->nsm_update_fn (store->data, pfx, ns, NULL),
-                "Error adding namespace to NS map");
-
-    return 0;
-}
-
-
 static int l_store_tostring (lua_State *L)
 {
     const LSUP_Store *store = *(LSUP_Store **) luaL_checkudata (
@@ -79,8 +60,6 @@ static const luaL_Reg store_lib_meth [] = {
     {"__gc", store_gc},
     {"__tostring", l_store_tostring},
 
-    {"nsm_update", l_store_nsm_update},
-
     {NULL}
 };
 

+ 21 - 22
src/lua_term.c

@@ -1,9 +1,6 @@
 #include "lua_lsup.h"
 #include "stackdump.h"
 
-#define check_term(L) \
-    *(LSUP_Term **)luaL_checkudata(L, 1, "LSUP.Term")
-
 
 LSUP_Term **allocate_term (lua_State *L)
 {
@@ -32,12 +29,23 @@ LSUP_TermSet **allocate_tset (lua_State *L)
 static int l_new_iriref (lua_State *L)
 {
     const char *data = luaL_checkstring (L, 1);
-    // TODO handle nsm.
-    LSUP_NSMap *nsm = lua_touserdata (L, 2);
 
     LSUP_Term **tp = allocate_term (L);
 
-    *tp = LSUP_iriref_new (data, nsm);
+    *tp = LSUP_iriref_new (data);
+    LUA_NLCHECK (*tp, "Error creating term.");
+
+    return 1;
+}
+
+
+static int l_new_iriref_ns (lua_State *L)
+{
+    const char *data = luaL_checkstring (L, 1);
+
+    LSUP_Term **tp = allocate_term (L);
+
+    *tp = LSUP_iriref_new_ns (data);
     LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
@@ -52,7 +60,7 @@ static int l_new_iriref_abs (lua_State *L)
 
     LSUP_Term **tp = allocate_term (L);
 
-    *tp = LSUP_iriref_absolute (root, iri);
+    *tp = LSUP_iriref_new_abs (root, iri);
     LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
@@ -67,7 +75,7 @@ static int l_new_iriref_rel (lua_State *L)
 
     LSUP_Term **tp = allocate_term (L);
 
-    *tp = LSUP_iriref_relative (root, iri);
+    *tp = LSUP_iriref_new_rel (root, iri);
     LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
@@ -92,7 +100,7 @@ static int l_new_lit (lua_State *L)
 
     if (dtype_str) {
         // TODO check memory leak.
-        dtype = LSUP_iriref_new (dtype_str, LSUP_default_nsm);
+        dtype = LSUP_iriref_new (dtype_str);
         *tp = LSUP_literal_new (data, dtype);
     }
     else if (lang) *tp = LSUP_lt_literal_new (data, (char *)lang);
@@ -180,7 +188,7 @@ static int l_term_to_n3 (lua_State *L)
     LSUP_Term *t = check_term (L);
     char *nt_term = NULL;
 
-    CHECK (nt_codec.encode_term (t, NULL, &nt_term), fail);
+    CHECK (nt_codec.encode_term (t, &nt_term), fail);
     lua_pushstring (L, nt_term);
     free (nt_term);
 
@@ -234,16 +242,6 @@ static int get_type (lua_State *L)
 }
 
 
-static int get_iriref_nsm (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    // TODO
-    lua_pushlightuserdata (L, LSUP_iriref_nsm (t));
-
-    return 1;
-}
-
-
 static int get_iriref_prefix (lua_State *L)
 {
     LSUP_Term *t = check_term (L);
@@ -289,6 +287,7 @@ static int get_lit_lang (lua_State *L)
     LSUP_Term *t = check_term (L);
     if (!LSUP_IS_LITERAL (t))
         return luaL_error (L, "Term is not a literal.", 2);
+    if (t->type != LSUP_TERM_LT_LITERAL) return 0;
 
     lua_pushstring (L, t->lang);
 
@@ -486,6 +485,7 @@ static int lmap_iter_gc (lua_State *L)
 
 static const luaL_Reg term_lib_fn [] = {
     {"new_iriref", l_new_iriref},
+    {"new_iriref_ns", l_new_iriref_ns},
     {"new_iriref_abs", l_new_iriref_abs},
     {"new_iriref_rel", l_new_iriref_rel},
     {"new_lit", l_new_lit},
@@ -502,7 +502,6 @@ static const luaL_Reg term_getters [] = {
     {"type", get_type},
 
     // IRIRef getters.
-    {"nsm", get_iriref_nsm},
     {"prefix", get_iriref_prefix},
     {"path", get_iriref_path},
     {"frag", get_iriref_frag},
@@ -537,7 +536,6 @@ static const luaL_Reg term_lib_meth [] = {
 
 static const LEnumConst term_enums[] = {
     {"TYPE_IRIREF", LSUP_TERM_IRIREF},
-    {"TYPE_NS_IRIREF", LSUP_TERM_NS_IRIREF},
     {"TYPE_LITERAL", LSUP_TERM_LITERAL},
     {"TYPE_LT_LITERAL", LSUP_TERM_LT_LITERAL},
     {"TYPE_BNODE", LSUP_TERM_BNODE},
@@ -555,6 +553,7 @@ static const LStringConst term_strings[] = {
     {"RDF_TYPE_NS", LSUP_RDF_TYPE_NS},
     {"DEFAULT_DTYPE", DEFAULT_DTYPE},
     {"DEFAULT_DTYPE_NS", DEFAULT_DTYPE_NS},
+    {"DEFAULT_CTX", DEFAULT_CTX_LABEL},
 
     {NULL, 0}
 };

+ 5 - 13
src/lua_triple.c

@@ -1,8 +1,5 @@
 #include "lua_lsup.h"
 
-#define check_triple(L) \
-    *(LSUP_Triple **)luaL_checkudata(L, 1, "LSUP.Triple")
-
 
 /*
  * Factory methods.
@@ -58,18 +55,13 @@ static int l_triple_get_term (lua_State *L)
     if (pos == 's') pn = TRP_POS_S;
     else if (pos == 'p') pn = TRP_POS_P;
     else if (pos == 'o') pn = TRP_POS_O;
-    else return luaL_error(L, "Out of range position: %c.", pos);
+    else return luaL_error (L, "Out of range position: %c.", pos);
 
-    LSUP_Term *t = LSUP_triple_pos (trp, pn);
-    char *nt_term = NULL;
-    CHECK (nt_codec.encode_term (t, NULL, &nt_term), fail);
-    lua_pushfstring (L, "LSUP.Term @ %p %s", t, nt_term);
+    LSUP_Term **tp = allocate_term (L);
+    *tp = LSUP_term_copy (LSUP_triple_pos (trp, pn));
+    if (!*tp) return luaL_error (L, "Error while copying a triple term!");
 
     return 1;
-
-fail:
-    if (nt_term) free (nt_term);
-    return luaL_error (L, "Error encoding triple term for display!");
 }
 
 
@@ -121,7 +113,7 @@ static int l_triple_to_n3 (lua_State *L)
 
     for (int i = TRP_POS_S; i <= TRP_POS_O; i++) {
         CHECK (nt_codec.encode_term (
-                    LSUP_triple_pos (trp, i), NULL, &nt_term), fail);
+                    LSUP_triple_pos (trp, i), &nt_term), fail);
         lua_pushstring (L, nt_term);
         lua_pushstring (L, " ");
     }

+ 12 - 12
test.lua

@@ -62,29 +62,29 @@ tset = gr1:term_set(t2, triple.POS_P, t1, triple.POS_S)
 for t in pairs(tset) do print(t) end
 --]]
 
-local nsm = namespace.new()
-nsm:add("ns1", "urn:ns:1#")
-nsm:add("ns2", "urn:ns:2#")
-nsm:add("ns3", "urn:ns:3#")
+namespace.add("ns1", "urn:ns:1#")
+namespace.add("ns2", "urn:ns:2#")
+namespace.add("ns3", "urn:ns:3#")
 
 local nsm_map = {}
-for k, v in pairs(nsm) do nsm_map[k] = v end
+for k, v in namespace.iter() do nsm_map[k] = v end
 assert(nsm_map.ns1 == "urn:ns:1#")
 assert(nsm_map.ns2 == "urn:ns:2#")
 assert(nsm_map.ns3 == "urn:ns:3#")
 
 local mdb_store_ns = store.new(store.MDB, "file:///tmp/lsup_nsm.db", true)
-mdb_store_ns:nsm_update("ns1", "urn:ns:1#")
-mdb_store_ns:nsm_update("ns2", "urn:ns:2#")
-mdb_store_ns:nsm_update("ns3", "urn:ns:3#")
 
-local mdb_gr2 = graph.new(mdb_store_ns, "ns1:gr2")
-local mdb_gr3 = graph.new(mdb_store_ns, "ns1:gr3")
-local mdb_gr4 = graph.new(mdb_store_ns, "ns1:gr4")
-local mdb_gr5 = graph.new(mdb_store_ns, "ns1:gr5")
+local mdb_gr2 = graph.new(mdb_store_ns, "ns1:gr2", nsm)
+local mdb_gr3 = graph.new(mdb_store_ns, "ns1:gr3", nsm)
+local mdb_gr4 = graph.new(mdb_store_ns, "ns1:gr4", nsm)
+local mdb_gr5 = graph.new(mdb_store_ns, "ns1:gr5", nsm)
 mdb_gr2:add(triples)
 mdb_gr3:add(triples)
 mdb_gr4:add(triples)
 
 local idx = graph.list(mdb_store_ns)
 assert(#idx == 3)
+
+attr = mdb_gr2:attr(t1, t2)
+--assert (#attr == 3)
+--for _, t in ipairs(attr) do print(t) end