graph.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "lua_lsup.h"
  2. #include "stackdump.h"
  3. #define check_graph(L) \
  4. *(LSUP_Graph **)luaL_checkudata(L, 1, "LSUP.Graph")
  5. static LSUP_Graph **allocate_graph (lua_State *L)
  6. {
  7. LSUP_Graph **gp = lua_newuserdatauv (L, sizeof (*gp), 1);
  8. luaL_getmetatable (L, "LSUP.Graph");
  9. lua_setmetatable (L, -2);
  10. return gp;
  11. }
  12. static int l_graph_new (lua_State *L)
  13. {
  14. const LSUP_StoreType store_type = lua_tointeger (L, 1);
  15. const char *uri_str = lua_tostring (L, 2);
  16. LSUP_Graph **gp = allocate_graph (L);
  17. LSUP_Store *store = NULL;
  18. if (store_type) {
  19. const LSUP_StoreInt *sif = LSUP_store_int (store_type);
  20. if (UNLIKELY (!sif)) return luaL_error (
  21. L, "No interface defined for store type: %d.", store_type);
  22. // TODO Move store creation fn and handle into a separate module.
  23. store = LSUP_store_new (store_type, NULL, 0);
  24. // Set up the store if a function for that is defined.
  25. if (sif->setup_fn) {
  26. if (sif->setup_fn(NULL, false) < LSUP_OK)
  27. return luaL_error (L, "Error initializing back end store.");
  28. }
  29. }
  30. // TODO Make store ID, nsm and initial size accessible.
  31. *gp = LSUP_graph_new (store, uri_str, NULL);
  32. LUA_NLCHECK (*gp, "Error creating graph.");
  33. return 1;
  34. }
  35. /*
  36. * Class methods.
  37. */
  38. static int l_graph_gc (lua_State *L)
  39. {
  40. LSUP_Graph *gr = check_graph (L);
  41. if (gr) LSUP_graph_free (gr);
  42. return 0;
  43. }
  44. static int l_graph_to_string (lua_State *L)
  45. {
  46. const LSUP_Graph *gr = check_graph (L);
  47. lua_pushfstring (
  48. L, "LSUP.Graph @%p <%s>: %d triples",
  49. gr, LSUP_graph_uri (gr)->data, LSUP_graph_size (gr));
  50. return 1;
  51. }
  52. static int l_graph_len (lua_State *L)
  53. {
  54. const LSUP_Graph *gr = check_graph (L);
  55. lua_pushinteger (L, LSUP_graph_size (gr));
  56. return 1;
  57. }
  58. static int l_graph_copy (lua_State *L)
  59. {
  60. const LSUP_Graph *src = check_graph (L);
  61. LSUP_Graph *dest = *(LSUP_Graph **)luaL_checkudata(L, 2, "LSUP.Graph");
  62. LUA_PCHECK (LSUP_graph_copy (src, dest), "Error copying graph");
  63. return 1;
  64. }
  65. static int l_graph_copy_contents (lua_State *L)
  66. {
  67. const LSUP_Graph *src = check_graph (L);
  68. LSUP_Graph *dest = *(LSUP_Graph **)luaL_checkudata (L, 2, "LSUP.Graph");
  69. const LSUP_Term *s, *p, *o;
  70. if lua_isnoneornil (L, 3) s = NULL;
  71. else s = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
  72. if lua_isnoneornil (L, 4) p = NULL;
  73. else p = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
  74. if lua_isnoneornil (L, 5) o = NULL;
  75. else o = *(LSUP_Term **)luaL_checkudata (L, 5, "LSUP.Term");
  76. LUA_PCHECK (LSUP_graph_copy_contents (
  77. src, dest, s, p, o), "Error copying graph.");
  78. return 1;
  79. }
  80. static int l_graph_equals (lua_State *L)
  81. {
  82. const LSUP_Graph *gr1 = check_graph (L);
  83. const LSUP_Graph *gr2 =
  84. *(LSUP_Graph **)luaL_checkudata (L, 2, "LSUP.Graph");
  85. LOG_DEBUG ("Comparing graphs %p %p", gr1, gr2);
  86. int eq_rc = LSUP_graph_equals (gr1, gr2);
  87. lua_pushboolean (L, eq_rc);
  88. return 1;
  89. }
  90. static int l_graph_contains (lua_State *L)
  91. {
  92. const LSUP_Graph *gr = check_graph (L);
  93. const LSUP_Triple *spo =
  94. *(LSUP_Triple **)luaL_checkudata (L, 2, "LSUP.Triple");
  95. lua_pushboolean (L, LSUP_graph_contains (gr, spo));
  96. return 1;
  97. }
  98. static int l_graph_add (lua_State *L)
  99. {
  100. LSUP_Graph *gr = check_graph (L);
  101. LOG_DEBUG ("Triples type: %s", lua_typename (L, lua_type (L, 2)));
  102. int rc;
  103. LSUP_rc lsup_rc;
  104. size_t i = 0, ct = 0;
  105. LSUP_GraphIterator *it = LSUP_graph_add_init (gr);
  106. while ((rc = lua_rawgeti (L, 2, ++i)) != LUA_TNIL) {
  107. //LOG_DEBUG ("Triple type: %s", lua_typename (L, rc));
  108. const LSUP_Triple *spo =
  109. *(LSUP_Triple **)luaL_checkudata (L, -1, "LSUP.Triple");
  110. LOG_DEBUG (
  111. "Got triple %d: {%s %s %s}\n",
  112. i, spo->s->data, spo->p->data, spo->o->data);
  113. lsup_rc = LSUP_graph_add_iter (it, spo);
  114. if (lsup_rc < LSUP_OK) break;
  115. if (lsup_rc == LSUP_OK) ct++;
  116. };
  117. LSUP_graph_add_done (it);
  118. lua_pushinteger (L, ct);
  119. if (UNLIKELY (lsup_rc < LSUP_OK)) return luaL_error (
  120. L, "Error adding triple at position %d: %s",
  121. i, LSUP_strerror (lsup_rc));
  122. else return 1;
  123. }
  124. static int l_graph_iter_next (lua_State *L)
  125. {
  126. LSUP_GraphIterator *it =
  127. *(LSUP_GraphIterator **)lua_touserdata (L, lua_upvalueindex (1));
  128. LSUP_Triple **spo_p = lua_newuserdatauv (L, sizeof (*spo_p), 1);
  129. luaL_getmetatable (L, "LSUP.Triple");
  130. lua_setmetatable (L, -2);
  131. LSUP_rc rc = LSUP_graph_iter_next (it, spo_p);
  132. if (rc == LSUP_END) {
  133. LSUP_graph_iter_free (it);
  134. lua_pushnil (L);
  135. lua_pushstring (L, "End of lookup results.");
  136. return 2;
  137. }
  138. LUA_PCHECK (rc, "Error retrieving a lookup result.");
  139. return 1;
  140. }
  141. static int l_graph_lookup (lua_State *L)
  142. {
  143. const LSUP_Graph *gr = check_graph (L);
  144. const LSUP_Term *s, *p, *o;
  145. if lua_isnoneornil (L, 2) s = NULL;
  146. else s = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
  147. if lua_isnoneornil (L, 3) p = NULL;
  148. else p = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
  149. if lua_isnoneornil (L, 4) o = NULL;
  150. else o = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
  151. LSUP_GraphIterator **it_p =
  152. (LSUP_GraphIterator **)lua_newuserdata (L, sizeof *it_p);
  153. *it_p = NULL;
  154. luaL_getmetatable (L, "LSUP.GraphIterator");
  155. lua_setmetatable (L, -2);
  156. size_t ct;
  157. *it_p = LSUP_graph_lookup (gr, s, p, o, &ct);
  158. LUA_NLCHECK (*it_p, "Error creating graph iterator.");
  159. LOG_DEBUG ("Found triples: %d", ct);
  160. lua_pushcclosure (L, l_graph_iter_next, 1);
  161. return 1;
  162. }
  163. static int l_graph_connections (lua_State *L)
  164. {
  165. const LSUP_Graph *gr = check_graph (L);
  166. return 1;
  167. }
  168. static int l_graph_term_set (lua_State *L)
  169. {
  170. const LSUP_Graph *gr = check_graph (L);
  171. return 1;
  172. }
  173. static int l_graph_unique_terms (lua_State *L)
  174. {
  175. const LSUP_Graph *gr = check_graph (L);
  176. return 1;
  177. }
  178. /*
  179. * Library setup.
  180. */
  181. static const luaL_Reg graph_lib_fn [] = {
  182. {"new", l_graph_new},
  183. {NULL}
  184. };
  185. /*
  186. static const luaL_Reg graph_getters [] = {
  187. {"uri", l_graph_get_uri},
  188. {"namespace", l_graph_get_nsm},
  189. {NULL}
  190. };
  191. */
  192. /*
  193. static const luaL_Reg graph_setters [] = {
  194. {"uri", l_graph_set_uri},
  195. {NULL}
  196. };
  197. */
  198. static const luaL_Reg graph_lib_meth [] = {
  199. {"__eq", l_graph_equals},
  200. {"__gc", l_graph_gc},
  201. //{"__index", get_attr},
  202. //{"__newindex", set_attr},
  203. {"__tostring", l_graph_to_string},
  204. {"__len", l_graph_len},
  205. {"copy", l_graph_copy},
  206. {"copy_contents", l_graph_copy_contents},
  207. {"contains", l_graph_contains},
  208. {"add", l_graph_add},
  209. {"lookup", l_graph_lookup},
  210. {"connections", l_graph_connections},
  211. {"term_set", l_graph_term_set},
  212. {"unique_terms", l_graph_unique_terms},
  213. //{"to_n3", l_graph_to_n3},
  214. {NULL}
  215. };
  216. int luaopen_lsup_graph (lua_State *L)
  217. {
  218. LSUP_init(); // This is idempotent: no problem calling it multiple times.
  219. luaL_newmetatable (L, "LSUP.Graph");
  220. lua_pushvalue (L, -1);
  221. lua_setfield (L, -2, "__index");
  222. luaL_setfuncs (L, graph_lib_meth, 0);
  223. luaL_newmetatable (L, "LSUP.GraphIterator");
  224. /*
  225. // Getters table.
  226. lua_newtable (L);
  227. for (int i = 0; graph_getters[i].name != NULL; i++) {
  228. lua_pushcfunction (L, graph_getters[i].func);
  229. lua_setfield (L, -2, graph_getters[i].name);
  230. }
  231. // Set getters table as a value for the Graph metatable.
  232. lua_setfield (L, -2, "getters");
  233. */
  234. luaL_newlib (L, graph_lib_fn);
  235. return 1;
  236. }