graph.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 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. lua_pushnil (L);
  134. lua_pushstring (L, "End of lookup results.");
  135. return 2;
  136. }
  137. LUA_PCHECK (rc, "Error retrieving a lookup result.");
  138. return 1;
  139. }
  140. static int l_graph_lookup (lua_State *L)
  141. {
  142. const LSUP_Graph *gr = check_graph (L);
  143. const LSUP_Term *s, *p, *o;
  144. if lua_isnoneornil (L, 2) s = NULL;
  145. else s = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
  146. if lua_isnoneornil (L, 3) p = NULL;
  147. else p = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
  148. if lua_isnoneornil (L, 4) o = NULL;
  149. else o = *(LSUP_Term **)luaL_checkudata (L, 4, "LSUP.Term");
  150. LSUP_GraphIterator **it_p =
  151. (LSUP_GraphIterator **)lua_newuserdata (L, sizeof *it_p);
  152. *it_p = NULL;
  153. luaL_getmetatable (L, "LSUP.GraphIterator");
  154. lua_setmetatable (L, -2);
  155. size_t ct;
  156. *it_p = LSUP_graph_lookup (gr, s, p, o, &ct);
  157. LUA_NLCHECK (*it_p, "Error creating graph iterator.");
  158. LOG_DEBUG ("Found triples: %d", ct);
  159. lua_pushcclosure (L, graph_iter_next, 1);
  160. return 1;
  161. }
  162. static int graph_iter_gc (lua_State *L)
  163. {
  164. LSUP_GraphIterator *it = *(LSUP_GraphIterator **)lua_touserdata (L, 1);
  165. LSUP_graph_iter_free (it);
  166. }
  167. static int conn_iter_done (lua_State *L)
  168. {
  169. LSUP_LinkMapIterator *it =
  170. *(LSUP_LinkMapIterator **)lua_touserdata (L, lua_upvalueindex (1));
  171. LSUP_link_map_iter_free (it);
  172. return 0;
  173. }
  174. static int lmap_iter_next (lua_State *L)
  175. {
  176. LSUP_LinkMapIterator *it =
  177. *(LSUP_LinkMapIterator **)lua_touserdata (L, lua_upvalueindex (1));
  178. LSUP_Term **link_p = (LSUP_Term **)lua_newuserdata (L, sizeof *link_p);
  179. *link_p = NULL;
  180. LSUP_TermSet *ts = NULL;
  181. LSUP_rc rc = LSUP_link_map_next (it, link_p, &ts);
  182. if (rc != LSUP_OK) {
  183. if (rc == LSUP_END) {
  184. LSUP_link_map_iter_free (it);
  185. lua_pushnil (L);
  186. lua_pushstring (L, "End of the loop.");
  187. return 2;
  188. }
  189. else LUA_PCHECK (rc, "Error iterating through link map");
  190. }
  191. size_t i = 0;
  192. LSUP_Term *conn = NULL;
  193. lua_newtable (L);
  194. while (LSUP_term_set_next (ts, &i, &conn)) {
  195. LSUP_Term **conn_p = (LSUP_Term **)lua_newuserdata (L, sizeof *conn_p);
  196. *conn_p = conn;
  197. lua_pushboolean (L, true);
  198. lua_rawset (L, -3);
  199. }
  200. return 2;
  201. }
  202. static int l_lmap_iter_init (lua_State *L)
  203. {
  204. LSUP_LinkMap *lm = *(LSUP_LinkMap **)luaL_checkudata(L, 1, "LSUP.LinkMap");
  205. LSUP_LinkMapIterator **lmit_p =
  206. (LSUP_LinkMapIterator **)lua_newuserdata (L, sizeof *lmit_p);
  207. *lmit_p = LSUP_link_map_iter_new (lm);
  208. luaL_getmetatable (L, "LSUP.LMapIterator");
  209. lua_setmetatable (L, -2);
  210. stackDump (L, "After allocating LMIter");
  211. lua_pushcclosure (L, lmap_iter_next, 1);
  212. return 1;
  213. }
  214. /** Returns a LinkMap that can be iterated over with iter().
  215. */
  216. static int l_graph_connections (lua_State *L)
  217. {
  218. const LSUP_Graph *gr = check_graph (L);
  219. LSUP_Term *t = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
  220. const LSUP_LinkType type = luaL_checkinteger (L, 3);
  221. LSUP_LinkMap **lm_p =
  222. (LSUP_LinkMap **)lua_newuserdata (L, sizeof *lm_p);
  223. *lm_p = LSUP_graph_connections (gr, t, type);
  224. luaL_getmetatable (L, "LSUP.LinkMap");
  225. lua_setmetatable (L, -2);
  226. return 1;
  227. }
  228. static int link_map_gc (lua_State *L)
  229. {
  230. LSUP_LinkMap *lm = *(LSUP_LinkMap **)lua_touserdata (L, 1);
  231. LSUP_link_map_free (lm);
  232. return 0;
  233. }
  234. static int lmap_iter_gc (lua_State *L)
  235. {
  236. LSUP_LinkMapIterator *it = *(LSUP_LinkMapIterator **)lua_touserdata (L, 1);
  237. LSUP_link_map_iter_free (it);
  238. return 0;
  239. }
  240. static int l_graph_term_set (lua_State *L)
  241. {
  242. const LSUP_Graph *gr = check_graph (L);
  243. return 1;
  244. }
  245. static int l_graph_unique_terms (lua_State *L)
  246. {
  247. const LSUP_Graph *gr = check_graph (L);
  248. return 1;
  249. }
  250. /*
  251. * Library setup.
  252. */
  253. static const luaL_Reg graph_lib_fn [] = {
  254. {"new", l_graph_new},
  255. {NULL}
  256. };
  257. /*
  258. static const luaL_Reg graph_getters [] = {
  259. {"uri", l_graph_get_uri},
  260. {"namespace", l_graph_get_nsm},
  261. {NULL}
  262. };
  263. */
  264. /*
  265. static const luaL_Reg graph_setters [] = {
  266. {"uri", l_graph_set_uri},
  267. {NULL}
  268. };
  269. */
  270. static const luaL_Reg graph_lib_meth [] = {
  271. {"__eq", l_graph_equals},
  272. {"__gc", l_graph_gc},
  273. //{"__index", get_attr},
  274. //{"__newindex", set_attr},
  275. {"__tostring", l_graph_to_string},
  276. {"__len", l_graph_len},
  277. {"copy", l_graph_copy},
  278. {"copy_contents", l_graph_copy_contents},
  279. {"contains", l_graph_contains},
  280. {"add", l_graph_add},
  281. {"lookup", l_graph_lookup},
  282. {"connections", l_graph_connections},
  283. {"term_set", l_graph_term_set},
  284. {"unique_terms", l_graph_unique_terms},
  285. //{"to_n3", l_graph_to_n3},
  286. {NULL}
  287. };
  288. static const LEnumConst graph_enums[] = {
  289. {"LINK_INBOUND", LSUP_LINK_INBOUND},
  290. {"LINK_OUTBOUND", LSUP_LINK_OUTBOUND},
  291. {"LINK_EDGE", LSUP_LINK_EDGE},
  292. {NULL, 0}
  293. };
  294. int luaopen_lsup_graph (lua_State *L)
  295. {
  296. LSUP_init(); // This is idempotent: no problem calling it multiple times.
  297. luaL_newmetatable (L, "LSUP.Graph");
  298. lua_pushvalue (L, -1);
  299. lua_setfield (L, -2, "__index");
  300. luaL_setfuncs (L, graph_lib_meth, 0);
  301. // Metatables for ancillary types.
  302. luaL_newmetatable (L, "LSUP.GraphIterator");
  303. lua_pushcfunction (L, graph_iter_gc);
  304. lua_setfield (L, -2, "__gc");
  305. luaL_newmetatable (L, "LSUP.LinkMap");
  306. lua_pushcfunction (L, link_map_gc);
  307. lua_setfield (L, -2, "__gc");
  308. luaL_newmetatable (L, "LSUP.LMapIterator");
  309. lua_pushcfunction (L, lmap_iter_gc);
  310. lua_setfield (L, -2, "__gc");
  311. /*
  312. // Getters table.
  313. lua_newtable (L);
  314. for (int i = 0; graph_getters[i].name != NULL; i++) {
  315. lua_pushcfunction (L, graph_getters[i].func);
  316. lua_setfield (L, -2, graph_getters[i].name);
  317. }
  318. // Set getters table as a value for the Graph metatable.
  319. lua_setfield (L, -2, "getters");
  320. */
  321. luaL_newlib (L, graph_lib_fn);
  322. // Module-level constants.
  323. push_int_const (L, graph_enums);
  324. return 1;
  325. }