Sfoglia il codice sorgente

Implement add_init, add_iter, add_done.

scossu 1 giorno fa
parent
commit
dad4deafc7
3 ha cambiato i file con 29 aggiunte e 8 eliminazioni
  1. 22 7
      src/lua_graph.c
  2. 1 1
      src/lua_term.c
  3. 6 0
      test.lua

+ 22 - 7
src/lua_graph.c

@@ -188,8 +188,9 @@ static int l_graph_add_iter (lua_State *L)
 /// Finalize iterative addition.
 static int l_graph_add_done (lua_State *L)
 {
-    VOLK_GraphIterator **it_p = lua_touserdata (L, 1);
+    VOLK_GraphIterator **it_p = luaL_checkudata (L, 1, "VOLK.GraphIterator");
     VOLK_graph_add_done (*it_p);
+    *it_p = 0;  // it still gets garbage collected, this prevents double-free.
 
     return 0;
 }
@@ -324,7 +325,7 @@ static int l_graph_encode (lua_State *L)
     char *tmp = NULL;
     VOLK_rc rc;
     while ((rc = codec.encode_graph_iter (it, &tmp)) != VOLK_END) {
-        log_debug ("Serialized fragment: %s\n", tmp);
+        //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.");
@@ -362,8 +363,14 @@ static int l_graph_get (lua_State *L)
  *
  * @note A new iterator should not be started without first garbage-collecting
  * an incomplete one. That would cause a MDB_BAD_RSLOT error on an LMDB-backed
- * graph, because it attempts to open a new read transaction while the old
- * iterator is keeping the old one open.
+ * graph, because it attempts to open a new read transaction within the same
+ * thread while the old iterator is keeping the old one open. This could be
+ * fixed with some good judgment.
+ *
+ * From the LMDB manual:
+ *
+ * > A thread can only use one transaction at a time, plus any child
+ * > transactions.
  */
 static int graph_iter_gc (lua_State *L)
 {
@@ -513,6 +520,7 @@ static const luaL_Reg graph_lib_meth [] = {
     {"copy", l_graph_copy},
 
     {"add", l_graph_add},
+    {"add_init", l_graph_add_init},
     {"remove", l_graph_remove},
 
     {"get_uri", l_graph_get_uri},
@@ -532,6 +540,12 @@ static const LEnumConst graph_enums[] = {
     {NULL, 0}
 };
 
+static const luaL_Reg graph_iter_meth [] = {
+    {"add_iter", l_graph_add_iter},
+    {"add_done", l_graph_add_done},
+    {"__gc", graph_iter_gc},
+};
+
 
 int luaopen_volksdata_graph (lua_State *L)
 {
@@ -541,10 +555,11 @@ int luaopen_volksdata_graph (lua_State *L)
     lua_setfield (L, -2, "__index");
     luaL_setfuncs (L, graph_lib_meth, 0);
 
-    // Metatables for ancillary types.
+    // Metatables for graph iterator.
     luaL_newmetatable (L, "VOLK.GraphIterator");
-    lua_pushcfunction (L, graph_iter_gc);
-    lua_setfield (L, -2, "__gc");
+    lua_pushvalue (L, -1);
+    lua_setfield (L, -2, "__index");
+    luaL_setfuncs (L, graph_iter_meth, 0);
 
     /*
     // Getters table.

+ 1 - 1
src/lua_term.c

@@ -181,7 +181,7 @@ static int l_term_equals (lua_State *L)
 static int l_term_gc (lua_State *L)
 {
     VOLK_Term **tp = luaL_checkudata(L, 1, "VOLK.Term");
-    LOG_TRACE ("Garbage collecting term @%p", *tp);
+    //LOG_TRACE ("Garbage collecting term @%p", *tp);
 
     VOLK_term_free (*tp);
     *tp = NULL;

+ 6 - 0
test.lua

@@ -88,3 +88,9 @@ local idx = graph.list(mdb_store_ns)
 attr = mdb_gr2:attr(t1, t2)
 --assert (#attr == 3)
 for t in pairs(attr) do print(t) end
+
+local gr1_copy = graph.new(mdb_store)
+local it = gr1_copy:add_init()
+for _, trp in ipairs(triples) do it:add_iter(trp) end
+it:add_done()
+assert(gr1 == gr1_copy)