Browse Source

Add graph remove and contain; nsm iterator.

scossu 1 tuần trước cách đây
mục cha
commit
2516108052
6 tập tin đã thay đổi với 127 bổ sung21 xóa
  1. 8 1
      Makefile
  2. 3 3
      lsup-scm-1.rockspec
  3. 24 2
      src/lua_graph.c
  4. 38 2
      src/lua_namespace.c
  5. 29 7
      src/lua_store.c
  6. 25 6
      test.lua

+ 8 - 1
Makefile

@@ -10,7 +10,9 @@ OBJ = lsup.so
 OBJPATH = lib/$(OBJ)
 
 LUA_VER=5.4
-INSTALL_DIR=$(PREFIX)/lib/lua/$(LUA_VER)
+INSTALL_SUFFIX=/lib/lua/$(LUA_VER)
+INSTALL_DIR=$(PREFIX)$(INSTALL_SUFFIX)
+LOCAL_INSTALL_DIR=$(shell luarocks config home_tree)$(INSTALL_SUFFIX)
 
 VALGRIND_LOG=/tmp/lua_lsup_valgrind.log
 
@@ -30,6 +32,11 @@ install: $(OBJPATH)
 	cp $(OBJPATH) $(INSTALL_DIR)
 
 
+local_install: $(OBJPATH)
+	mkdir -p $(INSTALL_DIR)
+	cp $(OBJPATH) $(LOCAL_INSTALL_DIR)
+
+
 .PHONY: uninstall
 uninstall:
 	$(RM) $(INSTALL_DIR)/$(OBJ)

+ 3 - 3
lsup-scm-1.rockspec

@@ -1,7 +1,7 @@
 package = "lsup"
 version = "scm-1"
 source = {
-   url = "git://git.knowledgetx.com/scossu/lsup_rdf.git",
+   url = "git://git.knowledgetx.com/scossu/lsup_lua.git",
    branch = "master",
    tag = "HEAD",
 }
@@ -12,8 +12,8 @@ description = {
       Linked Data. It handles terms, triples, graphs, and has an in-memory and
       a filesystem-based storage back ends.
    ]],
-   homepage = "http://git.knowledgetx.com/scossu/lsup_rdf",
-   license = "https://git.knowledgetx.com/scossu/lsup_rdf/src/master/LICENSE"
+   homepage = "http://git.knowledgetx.com/scossu/lsup_lua",
+   license = "https://git.knowledgetx.com/scossu/lsup_lua/src/master/LICENSE"
 }
 dependencies = {
    "lua >= 5.4, < 6",

+ 24 - 2
src/lua_graph.c

@@ -126,7 +126,6 @@ static int l_graph_contains (lua_State *L)
 static int l_graph_add (lua_State *L)
 {
     LSUP_Graph *gr = check_graph (L);
-    LOG_DEBUG ("Triples type: %s", lua_typename (L, lua_type (L, 2)));
     int rc;
     LSUP_rc lsup_rc= LSUP_NOACTION;
     size_t i = 0, ct = 0;
@@ -154,6 +153,27 @@ static int l_graph_add (lua_State *L)
 }
 
 
+static int l_graph_remove (lua_State *L)
+{
+    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");
+
+    size_t ct;
+    LSUP_rc rc = LSUP_graph_remove (gr, s, p, o, &ct);
+
+    LUA_PCHECK (rc, "Error removing triples from graph.");
+    lua_pushinteger (L, ct);
+
+    return 1;
+}
+
+
 static int graph_iter_next (lua_State *L)
 {
     LSUP_GraphIterator **it_p = lua_touserdata (L, lua_upvalueindex (1));
@@ -327,10 +347,12 @@ static const luaL_Reg graph_lib_meth [] = {
 
     {"copy", l_graph_copy},
     {"copy_contents", l_graph_copy_contents},
-    {"contains", l_graph_contains},
 
     {"add", l_graph_add},
+    {"remove", l_graph_remove},
+
     {"lookup", l_graph_lookup},
+    {"contains", l_graph_contains},
     {"connections", l_graph_connections},
     {"term_set", l_graph_term_set},
     {"unique_terms", l_graph_unique_terms},

+ 38 - 2
src/lua_namespace.c

@@ -1,4 +1,5 @@
 #include "lua_lsup.h"
+#include "stackdump.h"
 
 #define check_nsm(L) \
     *(LSUP_NSMap **)luaL_checkudata(L, 1, "LSUP.NSMap")
@@ -28,6 +29,7 @@ int l_nsmap_new (lua_State *L)
 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;
@@ -134,11 +136,43 @@ static int l_nsmap_iter_next (lua_State *L)
 }
 */
 
+static int nsmap_iter_next (lua_State *L)
+{
+    const char ****data_p = lua_touserdata (L, lua_upvalueindex (1));
+    const char ***data = *data_p;
+    size_t *i = lua_touserdata (L, lua_upvalueindex (2));
+
+    if ((data)[*i]) {
+        lua_pushstring (L, data[*i][0]);
+        lua_pushstring (L, data[*i][1]);
+        (*i)++;
+
+        return 2;
+    } else {
+        free (data);
+        *data_p = NULL;
+        free (i);
+
+        return 0;
+    }
+}
+
 
 static int l_nsmap_iter (lua_State *L)
 {
-    //LSUP_NSMap *nsm = check_nsm (L);
-    // TODO
+    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);
+    LUA_NLCHECK (*data_p, "Error creating NS map iterator.");
+
+    size_t *i = malloc (sizeof (*i));
+    LUA_NLCHECK (i, "Memory error in NS map iterator.");
+    *i = 0;
+    lua_pushlightuserdata (L, i);
+
+    lua_pushcclosure (L, nsmap_iter_next, 2);
 
     return 1;
 }
@@ -178,5 +212,7 @@ int luaopen_lsup_namespace (lua_State *L)
     luaL_setfuncs (L, nsmap_lib_meth, 0);
     luaL_newlib (L, nsmap_lib_fn);
 
+    //luaL_newmetatable (L, "LSUP.String2DIterator");
+
     return 1;
 }

+ 29 - 7
src/lua_store.c

@@ -12,16 +12,15 @@ static int l_store_new (lua_State *L)
     lua_setmetatable (L, -2);
 
     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);
+    LUA_NLCHECK (sif, "No interface defined for store type: %d.", store_type);
 
     if (clear) LOG_DEBUG("Clearing old store.");
     *store_p = LSUP_store_new (store_type, id, 0);
-    if (!*store_p) return luaL_error (L, "Error creating back end store.");
+    LUA_NLCHECK (*store_p, "Error creating back end store.");
 
     // Set up the store if a function for that is defined.
-    if (sif->setup_fn && sif->setup_fn (id, clear) < LSUP_OK)
-        return luaL_error (L, "Error initializing back end store.");
+    if (sif->setup_fn) LUA_PCHECK (
+            sif->setup_fn (id, clear), "Error initializing back end store.");
 
     return 1;
 }
@@ -38,11 +37,35 @@ static int store_gc (lua_State *L)
 }
 
 
+static int l_store_tostring (lua_State *L)
+{
+    const LSUP_Store *store = *(LSUP_Store **) luaL_checkudata (
+            L, 1, "LSUP.Store");
+    lua_pushfstring (
+            L, "LSUP.Store @%p <%s>: %s",
+            store,
+            LSUP_store_type_label (store->type),
+            store->id);
+
+    return 1;
+}
+
+
 // TODO add transaction handling.
 
 
 static const luaL_Reg store_lib_fn [] = {
     {"new", l_store_new},
+    {"__gc", store_gc},
+
+    {NULL}
+};
+
+
+static const luaL_Reg store_lib_meth [] = {
+    {"__gc", store_gc},
+    {"__tostring", l_store_tostring},
+
     {NULL}
 };
 
@@ -61,8 +84,7 @@ int luaopen_lsup_store (lua_State *L)
     luaL_newmetatable (L, "LSUP.Store");
     lua_pushvalue (L, -1);
     lua_setfield (L, -2, "__index");
-    lua_pushcfunction (L, store_gc);
-    lua_setfield (L, -2, "__gc");
+    luaL_setfuncs (L, store_lib_meth, 0);
 
     luaL_newlib (L, store_lib_fn);
     push_int_const (L, store_enums);

+ 25 - 6
scratch.lua → test.lua

@@ -2,6 +2,7 @@ term = require "lsup.term"
 triple = require "lsup.triple"
 graph = require "lsup.graph"
 store = require "lsup.store"
+namespace = require "lsup.namespace"
 
 ---[[
 t1 = term.new_bnode()
@@ -23,16 +24,19 @@ triples = {
 }
 
 mdb_store = store.new(store.MDB, "file:///tmp/lsup_lua.db", true)
-print("MDB store: ".. type(mdb_store))
+print(("MDB store: %s"):format(mdb_store))
 gr1 = graph.new(mdb_store)
 
 ct = gr1:add(triples)
 print("Triples added: " .. ct)
---[[
-print("graph 2")
-gr2 = graph.new(mdb_store)
-print("graph 3")
-gr3 = graph.new()
+---[[
+gr2 = graph.new(mdb_store)  -- LMDB-backed
+gr3 = graph.new()  -- hashmap-backed
+gr1:copy(gr2)
+gr1:copy(gr3)
+assert(gr1 == gr2)
+assert(gr1 == gr3)
+
 --]]
 for i in gr1:lookup(t1) do print(i) end
 ---[[
@@ -57,3 +61,18 @@ print("Terms connected to t1 t2:")
 tset = gr1:term_set(t2, triple.POS_P, t1, triple.POS_S)
 for t in pairs(tset) do print(t) end
 --]]
+
+nsm = namespace.new()
+nsm:add("ns1", "urn:ns:1#")
+nsm:add("ns2", "urn:ns:2#")
+nsm:add("ns3", "urn:ns:3#")
+
+local nsm_map = {}
+for k, v in pairs(nsm) 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)
+
+local gr2 = graph.new(mdb_store_ns, "ns1:gr1", nsm)