Browse Source

Add context list and term set length.

scossu 1 tuần trước cách đây
mục cha
commit
e4c8906c51
9 tập tin đã thay đổi với 68 bổ sung18 xóa
  1. 4 0
      .gitignore
  2. 4 4
      Makefile
  3. 8 2
      lsup-scm-1.rockspec
  4. 23 3
      src/lua_graph.c
  5. 0 1
      src/lua_namespace.c
  6. 2 1
      src/lua_store.c
  7. 16 3
      src/lua_term.c
  8. 1 3
      src/stackdump.h
  9. 10 1
      test.lua

+ 4 - 0
.gitignore

@@ -40,3 +40,7 @@ luac.out
 *.hex
 
 vgcore*
+
+# Local
+tmp/
+!.keep

+ 4 - 4
Makefile

@@ -1,4 +1,4 @@
-PREFIX=/usr/local
+PREFIX ?= /usr/local
 
 INCLUDE = -I/usr/local/include
 CFLAGS = -shared -DDEBUG -Og -ggdb -Wall -fPIC
@@ -10,9 +10,9 @@ OBJ = lsup.so
 OBJPATH = lib/$(OBJ)
 
 LUA_VER=5.4
-INSTALL_SUFFIX=/lib/lua/$(LUA_VER)
-INSTALL_DIR=$(PREFIX)$(INSTALL_SUFFIX)
-LOCAL_INSTALL_DIR=$(shell luarocks config home_tree)$(INSTALL_SUFFIX)
+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
 

+ 8 - 2
lsup-scm-1.rockspec

@@ -1,23 +1,29 @@
 package = "lsup"
 version = "scm-1"
+
 source = {
    url = "git://git.knowledgetx.com/scossu/lsup_lua.git",
    branch = "master",
    tag = "HEAD",
 }
+
 description = {
    summary = "Compact, minimalistic RDF library and persistent store.",
    detailed = [[
       `lsup_rdf` is an embedded library to manipulate and permanently store
-      Linked Data. It handles terms, triples, graphs, and has an in-memory and
-      a filesystem-based storage back ends.
+      Linked Data. It handles terms, triples, graphs, and has in-memory and
+      persistent storage back ends. It can encode and decode Turtle and N3
+      syntax.
    ]],
    homepage = "http://git.knowledgetx.com/scossu/lsup_lua",
    license = "https://git.knowledgetx.com/scossu/lsup_lua/src/master/LICENSE"
 }
+
 dependencies = {
    "lua >= 5.4, < 6",
 }
+
 build = {
    type = "make",
+   install_target = "local_install",
 }

+ 23 - 3
src/lua_graph.c

@@ -16,7 +16,7 @@ static LSUP_Graph **allocate_graph (lua_State *L)
 
 static int l_graph_new (lua_State *L)
 {
-    LSUP_Store *store;  // TODO This might be const. Check LSUP_rdf.
+    LSUP_Store *store;
 
     if (!lua_isnoneornil (L, 1))
         store = *(LSUP_Store **)luaL_checkudata (L, 1, "LSUP.Store");
@@ -33,14 +33,33 @@ 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");
+
+    LSUP_TermSet **ts_p = lua_newuserdata (L, sizeof *ts_p);
+    luaL_getmetatable (L, "LSUP.TermSet");
+    lua_setmetatable (L, -2);
+
+    *ts_p = LSUP_graph_list (store);
+    LUA_NLCHECK (ts_p, "Error retrieving context list.");
+
+    return 1;
+}
+
+
 /*
  * Class methods.
  */
 
 static int l_graph_gc (lua_State *L)
 {
-    LSUP_Graph *gr = check_graph (L);
-    if (gr) LSUP_graph_free (gr);
+    LSUP_Graph **gp = luaL_checkudata(L, 1, "LSUP.Graph");
+    LOG_DEBUG ("Garbage collecting graph @%p", *gp);
+
+    LSUP_graph_free (*gp);
+    *gp = NULL;
 
     return 0;
 }
@@ -314,6 +333,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},
 
     {NULL}
 };

+ 0 - 1
src/lua_namespace.c

@@ -1,5 +1,4 @@
 #include "lua_lsup.h"
-#include "stackdump.h"
 
 #define check_nsm(L) \
     *(LSUP_NSMap **)luaL_checkudata(L, 1, "LSUP.NSMap")

+ 2 - 1
src/lua_store.c

@@ -14,11 +14,11 @@ static int l_store_new (lua_State *L)
     const LSUP_StoreInt *sif = LSUP_store_int (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);
     LUA_NLCHECK (*store_p, "Error creating back end store.");
 
     // Set up the store if a function for that is defined.
+    if (clear) log_info ("Clearing old store.");
     if (sif->setup_fn) LUA_PCHECK (
             sif->setup_fn (id, clear), "Error initializing back end store.");
 
@@ -29,6 +29,7 @@ static int l_store_new (lua_State *L)
 static int store_gc (lua_State *L)
 {
     LSUP_Store **sp = luaL_checkudata(L, 1, "LSUP.Store");
+    LOG_DEBUG ("Garbage collecting store @%p.", *sp);
 
     LSUP_store_free (*sp);
     *sp = NULL;

+ 16 - 3
src/lua_term.c

@@ -358,13 +358,24 @@ static int l_term_set_iter_init (lua_State *L)
     *ip = 0;
 
     lua_pushlightuserdata (L, ip);
-    STACK_DUMP(L, "Before pushing tset next closure");
+    stack_dump (L, "Before pushing tset next closure");
     lua_pushcclosure (L, term_set_iter_next, 2);
 
     return 1;
 }
 
 
+static int l_term_set_size (lua_State *L)
+{
+    LSUP_TermSet *ts =
+        *(LSUP_TermSet **)luaL_checkudata(L, 1, "LSUP.TermSet");
+
+    lua_pushinteger (L, LSUP_term_set_size (ts));
+
+    return 1;
+}
+
+
 static int term_set_gc (lua_State *L)
 {
     LSUP_TermSet **ts_p = lua_touserdata (L, 1);
@@ -427,7 +438,7 @@ static int lmap_iter_next (lua_State *L)
  */
 static int l_lmap_iter_init (lua_State *L)
 {
-    STACK_DUMP (L, "beginning of LMap iter init fn");
+    stack_dump (L, "beginning of LMap iter init fn");
     LSUP_LinkMap *lm = *(LSUP_LinkMap **)luaL_checkudata(L, 1, "LSUP.LinkMap");
 
     LSUP_LinkMapIterator **lmit_p =
@@ -438,7 +449,7 @@ static int l_lmap_iter_init (lua_State *L)
     lua_setmetatable (L, -2);
 
     lua_pushcclosure (L, lmap_iter_next, 1);
-    STACK_DUMP (L, "After pushing iter closure");
+    stack_dump (L, "After pushing iter closure");
 
     return 1;
 }
@@ -573,6 +584,8 @@ int luaopen_lsup_term (lua_State *L)
     lua_setfield (L, -2, "__gc");
     lua_pushcfunction (L, l_term_set_iter_init);
     lua_setfield (L, -2, "__pairs");
+    lua_pushcfunction (L, l_term_set_size);
+    lua_setfield (L, -2, "__len");
 
     // Link map.
     luaL_newmetatable (L, "LSUP.LinkMap");

+ 1 - 3
src/stackdump.h

@@ -2,8 +2,6 @@
 #define _STACK_DUMP_H
 
 #ifdef DEBUG
-#define STACK_DUMP(...) stack_dump (__VA_ARGS__)
-
 static void
 stack_dump (lua_State *L, const char *title)
 {
@@ -71,7 +69,7 @@ stack_dump (lua_State *L, const char *title)
     printf("]\n"); /* end the listing */
 }
 #else
-#define STACK_DUMP(...)
+static void stack_dump (lua_State *L, const char *title) {}
 #endif  // DEBUG
 
 #endif  // _STACK_DUMP_H

+ 10 - 1
test.lua

@@ -75,4 +75,13 @@ 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)
+local gr2 = graph.new(mdb_store_ns, "ns1:gr2")
+local gr3 = graph.new(mdb_store_ns, "ns1:gr3")
+local gr4 = graph.new(mdb_store_ns, "ns1:gr4")
+local gr5 = graph.new(mdb_store_ns, "ns1:gr5")
+gr2:add(triples)
+gr3:add(triples)
+gr4:add(triples)
+
+local idx = graph.list(mdb_store_ns)
+assert(#idx == 3)