Parcourir la source

Reorganize into a single package.

scossu il y a 1 jour
Parent
commit
53e9c7f223
12 fichiers modifiés avec 179 ajouts et 423 suppressions
  1. 3 0
      Makefile
  2. 15 1
      include/graph.h
  3. 12 8
      lua/Makefile
  4. 0 348
      lua/lua_term.c
  5. 4 3
      lua/scratch.lua
  6. 130 8
      lua/src/graph.c
  7. 0 33
      lua/src/lsup.c
  8. 6 2
      lua/src/lua_lsup.h
  9. 1 1
      lua/src/namespace.c
  10. 0 0
      lua/src/store.c
  11. 7 7
      lua/src/term.c
  12. 1 12
      lua/src/triple.c

+ 3 - 0
Makefile

@@ -98,9 +98,12 @@ help:
 		| column -t  -s '|'
 	
 
+
+.PHONY: lib
 lib: codec $(LIBS) ## Compile main library (static and dynamic linking).
 
 
+.PHONY: debug
 debug: codec_dbg $(DBG_LIBS) ## Compile main library with debug symbols.
 
 

+ 15 - 1
include/graph.h

@@ -99,7 +99,21 @@ LSUP_graph_copy_contents_txn (
         const LSUP_Term *s, const LSUP_Term *p, const LSUP_Term *o);
 
 
-/// Non-transactional version of #LSUP_graph_copy_contents_txn().
+/* @brief Copy all triples from a graph (non-transactional).
+ *
+ * This is a shortcut for #LSUP_graph_copy_contents_txn(). If you need to
+ * specify a transaction handle for the copy, use that.
+ *
+ * @param[in] src Source graph.
+ *
+ * @param[in] dest Destination graph.
+ *
+ * @param[in] s, p, o Terms to look up for filtering. Any and all terms can be
+ * NULL, which indicate unbound terms.
+ *
+ * @return LSUP_OK on success; LSUP_NOACTION if no triples were copied; <0
+ *  if an error occurred.
+ */
 #define LSUP_graph_copy_contents(...) \
     LSUP_graph_copy_contents_txn (NULL, __VA_ARGS__)
 

+ 12 - 8
lua/Makefile

@@ -1,18 +1,22 @@
-.DEFAULT_GOAL := lib
-
 INCLUDE = -I/usr/local/include/lsup
-CFLAGS = -DDEBUG -shared -Wall -fPIC $(INCLUDE)
+CFLAGS = -DDEBUG -Wall -fPIC $(INCLUDE)
 LDFLAGS = -L/usr/local/lib -llua -llsuprdf_dbg
 
 LUAC_SRC = $(wildcard src/*.c)
-OBJ = $(patsubst src/%.c, lib/%.so, $(LUAC_SRC))
+#OBJ = $(patsubst src/%.c, lib/%.so, $(LUAC_SRC))
+OBJ = lib/lsup.so
+
+.DEFAULT_GOAL := lib
+
 
 .PHONY: lib
-lib: $(OBJ) src/lua_lsup.h
+lib: $(OBJ)
+
+
+$(OBJ): src/*.c src/lua_lsup.h
+	$(CC) -shared $(CFLAGS) $(LDFLAGS) -o $@ src/*.c
 
-lib/%.so: src/%.c
-	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
 
 .PHONY: clean
 clean:
-	rm lib/*.so
+	$(RM) lib/*.so

+ 0 - 348
lua/lua_term.c

@@ -1,348 +0,0 @@
-#include "lua_lsup.h"
-
-#define check_term(L) \
-    *(LSUP_Term **)luaL_checkudata(L, 1, "LSUP.Term")
-
-
-static LSUP_Term **allocate_term (lua_State *L)
-{
-    LSUP_Term **tp = lua_newuserdatauv (L, sizeof (*tp), 1);
-    luaL_getmetatable (L, "LSUP.Term");
-    lua_setmetatable (L, -2);
-
-    return tp;
-}
-
-
-/*
- * Factory methods.
- */
-
-static int l_iriref_new (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);
-    if (!*tp) luaL_error (L, "Error while creating a term!");
-
-    return 1;
-}
-
-
-static int l_iriref_new_abs (lua_State *L)
-{
-    LSUP_Term
-        *root = check_term (L),
-        *iri = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
-
-    LSUP_Term **tp = allocate_term (L);
-
-    *tp = LSUP_iriref_absolute (root, iri);
-    if (!*tp) luaL_error (L, "Error while creating a term!");
-
-    return 1;
-}
-
-
-static int l_iriref_new_rel (lua_State *L)
-{
-    LSUP_Term
-        *root = check_term (L),
-        *iri = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
-
-    LSUP_Term **tp = allocate_term (L);
-
-    *tp = LSUP_iriref_relative (root, iri);
-    if (!*tp) luaL_error (L, "Error while creating a term!");
-
-    return 1;
-}
-
-
-static int l_lit_new (lua_State *L)
-{
-    const char
-        *data = luaL_checkstring (L, 1),
-        *dtype_str = luaL_checkstring (L, 2);
-    const char *lang = luaL_checkstring (L, 3);
-
-    LSUP_Term **tp = allocate_term (L);
-    LSUP_Term *dtype;
-
-    if (lang) *tp = LSUP_lt_literal_new (data, (char *)lang);
-    else {
-        dtype = (dtype_str) ? LSUP_iriref_new (dtype_str, NULL) : NULL;
-        *tp = LSUP_literal_new (data, dtype);
-    }
-
-    if (!*tp) luaL_error (L, "Error while creating a term!");
-
-    return 1;
-}
-
-
-static int l_bnode_new (lua_State *L)
-{
-    const char *data = luaL_checkstring (L, 1);
-
-    LSUP_Term **tp = allocate_term (L);
-
-    *tp = LSUP_bnode_new (data);
-    if (!*tp) luaL_error (L, "Error while creating a term!");
-
-    return 1;
-}
-
-
-static int new_copy (lua_State *L)
-{
-    LSUP_Term *src = check_term (L);
-
-    LSUP_Term **tp = allocate_term (L);
-
-    *tp = LSUP_term_copy (src);
-    if (!*tp) luaL_error (L, "Error while creating a term!");
-
-    return 1;
-}
-
-
-/*
- * Class methods.
- */
-static int equals (lua_State *L)
-{
-    LSUP_Term
-        *t1 = check_term (L),
-        *t2 = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
-
-    lua_pushboolean (L, LSUP_term_equals (t1, t2));
-
-    return 1;
-}
-
-
-static int gc (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    if (t) LSUP_term_free (t);
-
-    return 0;
-}
-
-
-static int get_hash (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-
-    lua_pushinteger (L, LSUP_term_hash (t));
-
-    return 1;
-}
-
-
-/*
-static int serialize (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-
-    LSUP_Buffer *buf = LSUP_term_serialize (t);
-    if (!buf) luaL_error (L, "Error while serializing a term!");
-
-    lua_pushfstring (L, buf->addr, buf->size);
-    LSUP_buffer_free (buf);
-
-    return 1;
-}
-*/
-
-
-static int to_string (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-
-    LSUP_AddrBuffer *addr;
-    sprintf (addr, "%p", t);
-    lua_pushstring (L, "LSUP.Term @ 0x");
-    lua_pushstring (L, 
-    lua_pushstring (L, t->data);
-
-    return 1;
-}
-
-
-/*
- * Getters.
- */
-
-// Forward declaration.
-static const struct luaL_Reg term_getters [];
-
-
-static int get_attr (lua_State *L)
-{
-    const char *attr = luaL_checkstring (L, 2);
-
-    for (int i = 0; term_getters[i].name != NULL; i++) {
-        if (strcmp(term_getters[i].name, attr) == 0)
-            return term_getters[i].func (L);
-    }
-
-    // If the attribute doesn't exist, return nil.
-    return 0;
-}
-
-static int get_data (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    lua_pushstring (L, t->data);
-
-    return 1;
-}
-
-
-static int get_type (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    lua_pushinteger (L, t->type);
-
-    return 1;
-}
-
-
-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);
-    lua_pushstring (L, LSUP_iriref_prefix (t));
-
-    return 1;
-}
-
-
-static int get_iriref_path (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    lua_pushstring (L, LSUP_iriref_path (t));
-
-    return 1;
-}
-
-
-static int get_iriref_frag (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    lua_pushstring (L, LSUP_iriref_frag (t));
-
-    return 1;
-}
-
-
-static int get_lit_datatype (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    if (!LSUP_IS_LITERAL (t)) luaL_error (L, "Term is not a literal.");
-
-    lua_pushstring (L, t->datatype->data);
-
-    return 1;
-}
-
-
-static int get_lit_lang (lua_State *L)
-{
-    LSUP_Term *t = check_term (L);
-    if (!LSUP_IS_LITERAL (t)) luaL_error (L, "Term is not a literal.");
-
-    lua_pushstring (L, t->lang);
-
-    return 1;
-}
-
-
-/*
- * Setters.
- */
-
-// Very simple for now.
-static int set_attr (lua_State *L)
-{ return luaL_error (L, "Direct setting is not allowed for this type.", 2); }
-
-
-/*
- * Library setup.
- */
-
-static const luaL_Reg term_lib_fn [] = {
-    {"iriref_new", l_iriref_new},
-    {"iriref_new_abs", l_iriref_new_abs},
-    {"iriref_new_rel", l_iriref_new_rel},
-    {"lit_new", l_lit_new},
-    {"bnode_new", l_bnode_new},
-    {"term_new_copy", new_copy},
-    {NULL}
-};
-
-
-static const luaL_Reg term_getters [] = {
-    // General getters.
-    {"data", get_data},
-    {"type", get_type},
-
-    // IRIRef getters.
-    {"nsm", get_iriref_nsm},
-    {"prefix", get_iriref_prefix},
-    {"path", get_iriref_path},
-    {"frag", get_iriref_frag},
-
-    // Literal getters.
-    {"datatype", get_lit_datatype},
-    {"lang", get_lit_lang},
-
-    {NULL}
-};
-
-
-/*
-static const luaL_Reg term_setters [] = {
-    {NULL}
-};
-*/
-
-static const luaL_Reg term_lib_meth [] = {
-    {"__eq", equals},
-    {"__gc", gc},
-    {"__index", get_attr},
-    {"__tostring", to_string},
-    {"__newindex", set_attr},
-
-    {"hash", get_hash},
-    //{"serialize", serialize},
-
-    {NULL}
-};
-
-
-int luaopen_term (lua_State *L)
-{
-    luaL_newmetatable (L, "LSUP.Term");
-    lua_pushvalue (L, -1);
-    lua_setfield (L, -2, "__index");
-    luaL_setfuncs (L, term_lib_meth, 0);
-    luaL_newlib (L, term_lib_fn);
-
-    return 1;
-}

+ 4 - 3
lua/test.lua → lua/scratch.lua

@@ -1,5 +1,5 @@
-term = require "term"
-triple = require "triple"
+term = require "lsup.term"
+triple = require "lsup.triple"
 trp = triple.new (
     term.iriref_new("urn:s:1"),
     term.iriref_new("urn:p:1"),
@@ -8,5 +8,6 @@ trp = triple.new (
 t1 = term.iriref_new("urn:s:11")
 t2 = term.lit_new("123", "xsd:int")
 
-graph = require "graph"
+graph = require "lsup.graph"
 gr1 = graph.new()
+gr2 = graph.new()

+ 130 - 8
lua/src/graph.c

@@ -4,14 +4,22 @@
     *(LSUP_Graph **)luaL_checkudata(L, 1, "LSUP.Graph")
 
 
+static LSUP_Graph **allocate_graph (lua_State *L)
+{
+    LSUP_Graph **gp = lua_newuserdatauv (L, sizeof (*gp), 1);
+    luaL_getmetatable (L, "LSUP.Graph");
+    lua_setmetatable (L, -2);
+
+    return gp;
+}
+
+
 static int graph_new (lua_State *L)
 {
     const LSUP_StoreType store_type = lua_tointeger (L, 1);
     const char *uri_str = lua_tostring (L, 2);
 
-    LSUP_Graph **gp = lua_newuserdatauv (L, sizeof (*gp), 1);
-    luaL_getmetatable (L, "LSUP.Graph");
-    lua_setmetatable (L, -2);
+    LSUP_Graph **gp = allocate_graph (L);
 
     LSUP_Store *store = NULL;
     if (store_type) {
@@ -30,7 +38,7 @@ static int graph_new (lua_State *L)
 
     // TODO Make store ID, nsm and initial size accessible.
     *gp = LSUP_graph_new (store, uri_str, NULL);
-    if (!*gp) return luaL_error (L, "Error while creating graph!");
+    LUA_NLCHECK (*gp, "Error creating graph.");
 
     return 1;
 }
@@ -43,7 +51,6 @@ static int graph_new (lua_State *L)
 static int graph_gc (lua_State *L)
 {
     LSUP_Graph *gr = check_graph (L);
-    printf ("Freeing graph @%p", gr);
     if (gr) LSUP_graph_free (gr);
 
     return 0;
@@ -70,6 +77,100 @@ static int graph_len (lua_State *L)
 }
 
 
+static int 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 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");
+
+    LUA_PCHECK (LSUP_graph_copy_contents (
+                src, dest, s, p, o), "Error copying graph.");
+
+    return 1;
+}
+
+
+static int graph_equals (lua_State *L)
+{
+    const LSUP_Graph *gr1 = check_graph (L);
+    const LSUP_Graph *gr2 =
+        *(LSUP_Graph **)luaL_checkudata(L, 2, "LSUP.Graph");
+
+    lua_pushboolean (L, LSUP_graph_equals (gr1, gr2));
+
+    return 1;
+}
+
+
+static int graph_iter (lua_State *L)
+{
+    const LSUP_Graph *gr1 = check_graph (L);
+
+    return 1;
+}
+
+
+static int graph_contains (lua_State *L)
+{
+    const LSUP_Graph *gr1 = check_graph (L);
+
+    return 1;
+}
+
+
+static int graph_add (lua_State *L)
+{
+    const LSUP_Graph *gr1 = check_graph (L);
+
+    return 1;
+}
+
+
+static int graph_lookup (lua_State *L)
+{
+    const LSUP_Graph *gr1 = check_graph (L);
+
+    return 1;
+}
+
+
+static int graph_connections (lua_State *L)
+{
+    const LSUP_Graph *gr1 = check_graph (L);
+
+    return 1;
+}
+
+
+static int graph_term_set (lua_State *L)
+{
+    const LSUP_Graph *gr1 = check_graph (L);
+
+    return 1;
+}
+
+
+static int graph_unique_terms (lua_State *L)
+{
+    const LSUP_Graph *gr1 = check_graph (L);
+
+    return 1;
+}
+
+
 /*
  * Library setup.
  */
@@ -81,37 +182,58 @@ static const luaL_Reg graph_lib_fn [] = {
 };
 
 
+/*
 static const luaL_Reg graph_getters [] = {
+    {"uri", graph_get_uri},
+    {"namespace", graph_get_nsm},
 
     {NULL}
 };
+*/
 
 
 /*
 static const luaL_Reg graph_setters [] = {
+    {"uri", graph_set_uri},
+
     {NULL}
 };
 */
 
 static const luaL_Reg graph_lib_meth [] = {
-    //{"__eq", graph_equals},
+    {"__eq", graph_equals},
     {"__gc", graph_gc},
     //{"__index", get_attr},
     {"__tostring", graph_to_string},
     {"__len", graph_len},
+    {"__ipairs", graph_iter},
     //{"__newindex", set_attr},
 
+    {"copy", graph_copy},
+    {"copy_contents", graph_copy_contents},
+    {"contains", 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},
+
     //{"to_n3", graph_to_n3},
     {NULL}
 };
 
 
-int luaopen_graph (lua_State *L)
+int luaopen_lsup_graph (lua_State *L)
 {
     LSUP_init();  // This is idempotent: no problem calling it multiple times.
     luaL_newmetatable (L, "LSUP.Graph");
+    lua_pushvalue (L, -1);
+    lua_setfield (L, -2, "__index");
     luaL_setfuncs (L, graph_lib_meth, 0);
 
+    /*
     // Getters table.
     lua_newtable (L);
     for (int i = 0; graph_getters[i].name != NULL; i++) {
@@ -120,7 +242,7 @@ int luaopen_graph (lua_State *L)
     }
     // Set getters table as a value for the Graph metatable.
     lua_setfield (L, -2, "getters");
-
+    */
     luaL_newlib (L, graph_lib_fn);
 
     return 1;

+ 0 - 33
lua/src/lsup.c

@@ -1,33 +0,0 @@
-#include <lua.h>
-//#include <lualib.h>
-#include <lauxlib.h>
-#include <lsup_rdf.h>
-
-lua_State *L;
-
-
-static int l_init (lua_State *L)
-{
-    LSUP_rc rc;
-    if ((rc = LSUP_init ()) != LSUP_OK) {
-        lua_pushnil (L);
-        lua_pushstring (L, LSUP_strerror (rc));
-
-        return 2;
-    }
-    return 0;
-}
-
-
-static const struct luaL_Reg lsup_lib [] = {
-    {"init", l_init},
-    {NULL, NULL}
-};
-
-
-int luaopen_lsup (lua_State *L)
-{
-    luaL_newlib (L, lsup_lib);
-
-    return 1;
-}

+ 6 - 2
lua/src/lua_lsup.h

@@ -8,14 +8,18 @@
 
 /// Raise Lua error including LSUP error message on negative rc.
 #define LUA_PCHECK(exp, message) do {\
-    if ((exp) < LSUP_OK) \
+    if (UNLIKELY ((exp) < LSUP_OK)) \
         return luaL_error (L, "%s: %s", message, LSUP_strerror (exp)); \
 } while (0)
 
+#define LUA_NLCHECK(exp, message) do {\
+    if (UNLIKELY ((exp) == NULL)) return luaL_error (L, message); \
+} while (0)
+
 /// Create new namespace map.
 int l_nsmap_new (lua_State *L);
 
 /// Allocate space for a term object.
-///LSUP_Term **allocate_term (lua_State *L);
+LSUP_Term **allocate_term (lua_State *L);
 
 #endif  // _LUA_LSUP_H

+ 1 - 1
lua/src/namespace.c

@@ -161,7 +161,7 @@ static const luaL_Reg nsmap_lib_meth [] = {
 };
 
 
-int luaopen_namespace (lua_State *L)
+int luaopen_lsup_namespace (lua_State *L)
 {
     luaL_newmetatable (L, "LSUP.NSMap");
     lua_pushvalue (L, -1);

+ 0 - 0
lua/src/store.c


+ 7 - 7
lua/src/term.c

@@ -27,7 +27,7 @@ static int l_iriref_new (lua_State *L)
     LSUP_Term **tp = allocate_term (L);
 
     *tp = LSUP_iriref_new (data, nsm);
-    if (!*tp) return luaL_error (L, "Error while creating a term!");
+    LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
 }
@@ -42,7 +42,7 @@ static int l_iriref_new_abs (lua_State *L)
     LSUP_Term **tp = allocate_term (L);
 
     *tp = LSUP_iriref_absolute (root, iri);
-    if (!*tp) return luaL_error (L, "Error while creating a term!");
+    LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
 }
@@ -57,7 +57,7 @@ static int l_iriref_new_rel (lua_State *L)
     LSUP_Term **tp = allocate_term (L);
 
     *tp = LSUP_iriref_relative (root, iri);
-    if (!*tp) return luaL_error (L, "Error while creating a term!");
+    LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
 }
@@ -87,7 +87,7 @@ static int l_lit_new (lua_State *L)
     else if (lang) *tp = LSUP_lt_literal_new (data, (char *)lang);
     else *tp = LSUP_literal_new (data, NULL);
 
-    if (!*tp) return luaL_error (L, "Error while creating a term!");
+    LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
 }
@@ -100,7 +100,7 @@ static int l_bnode_new (lua_State *L)
     LSUP_Term **tp = allocate_term (L);
 
     *tp = LSUP_bnode_new (data);
-    if (!*tp) return luaL_error (L, "Error while creating a term!");
+    LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
 }
@@ -113,7 +113,7 @@ static int new_copy (lua_State *L)
     LSUP_Term **tp = allocate_term (L);
 
     *tp = LSUP_term_copy (src);
-    if (!*tp) return luaL_error (L, "Error while creating a term!");
+    LUA_NLCHECK (*tp, "Error creating term.");
 
     return 1;
 }
@@ -355,7 +355,7 @@ static const luaL_Reg term_lib_meth [] = {
 };
 
 
-int luaopen_term (lua_State *L)
+int luaopen_lsup_term (lua_State *L)
 {
     LSUP_init();  // This is idempotent: no problem calling it multiple times.
     luaL_newmetatable (L, "LSUP.Term");

+ 1 - 12
lua/src/triple.c

@@ -4,17 +4,6 @@
     *(LSUP_Triple **)luaL_checkudata(L, 1, "LSUP.Triple")
 
 
-// TODO this is duplicate. Find a way to access a common fn.
-LSUP_Term **allocate_term (lua_State *L)
-{
-    LSUP_Term **tp = lua_newuserdatauv (L, sizeof (*tp), 1);
-    luaL_getmetatable (L, "LSUP.Term");
-    lua_setmetatable (L, -2);
-
-    return tp;
-}
-
-
 /*
  * Factory methods.
  */
@@ -149,7 +138,7 @@ static const struct luaL_Reg triple_lib_meth [] = {
 };
 
 
-int luaopen_triple (lua_State *L)
+int luaopen_lsup_triple (lua_State *L)
 {
     LSUP_init();  // This is idempotent: no problem calling it multiple times.
     luaL_newmetatable (L, "LSUP.Triple");