desc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #include "desc.h"
  2. LSUP_rc
  3. LSR_desc_new_multi (LSUP_Graph *const *data, LSR_Desc **rsrc_p)
  4. {
  5. LSUP_rc rc = LSUP_OK;
  6. LSR_Desc *rsrc;
  7. MALLOC_GUARD (rsrc, LSUP_MEM_ERR);
  8. uuid_generate_random (rsrc->id);
  9. // Default context graph.
  10. rsrc->main_data = LSUP_graph_new (
  11. NULL, LSUP_term_copy (LSUP_default_ctx), NULL);
  12. log_debug (
  13. "Main data graph: %s",
  14. LSUP_graph_uri(rsrc->main_data)->data);
  15. LSUP_GraphIterator
  16. *lu_it, // Lookup iterator.
  17. *add_it, // Main graph add iterator.
  18. *admin_add_it = NULL; // Admin graph add iterator.
  19. LSUP_Term *dest_s, *dest_p, *dest_o;
  20. LSUP_Triple src_spo_s, dest_spo_s;
  21. LSUP_Triple
  22. *src_spo = &src_spo_s,
  23. *dest_spo = &dest_spo_s;
  24. LSUP_Term *rsrc_uri = LSR_id_to_urn (rsrc->id, NULL);
  25. LSUP_Term *rdf_t = LSUP_iriref_new ("rdf:type", LSUP_default_nsm);
  26. // Count graphs inserted and allocate space.
  27. size_t ct = 0;
  28. while (data[ct]) ct++;
  29. rsrc->user_data = calloc (sizeof (*rsrc->user_data), ct + 1);
  30. if (UNLIKELY (! rsrc->user_data)) return LSUP_MEM_ERR;
  31. /* BEGIN adding user data. */
  32. // Loop over input graphs.
  33. for (size_t i = 0; i < ct; i++) {
  34. LSUP_Term *gr_uri = LSUP_graph_uri (data[i]);
  35. LSUP_Term *rel_uri = LSUP_iriref_relative (rsrc_uri, gr_uri);
  36. if (strstr (rel_uri->data, "#__") == rel_uri->data) {
  37. log_error ("Fragment URI cannot start with double underscore.");
  38. rc = LSUP_VALUE_ERR;
  39. }
  40. LSUP_term_free (rel_uri);
  41. if (rc < 0) goto finally;
  42. rsrc->user_data[i] = LSUP_graph_new (
  43. NULL, LSUP_term_copy (gr_uri), NULL);
  44. log_debug (
  45. "User data graph (@%p): %s",
  46. LSUP_graph_uri(rsrc->user_data[i]),
  47. LSUP_graph_uri(rsrc->user_data[i])->data);
  48. add_it = LSUP_graph_add_init (rsrc->user_data[i]);
  49. lu_it = LSUP_graph_lookup (rsrc->user_data[i], NULL, NULL, NULL, NULL);
  50. // Loop over graph triples.
  51. while (LSUP_graph_iter_next (lu_it, &src_spo) == LSUP_OK) {
  52. dest_s = LSUP_IS_IRI (src_spo->s) ?
  53. LSUP_iriref_relative (rsrc_uri, src_spo->s) : src_spo->s;
  54. dest_p = LSUP_term_copy (src_spo->p);
  55. dest_o = LSUP_IS_IRI (src_spo->s) ?
  56. LSUP_iriref_relative (rsrc_uri, src_spo->s) : src_spo->s;
  57. LSUP_triple_init (dest_spo, dest_s, dest_p, dest_o);
  58. // if the pred is managed, ignore the triple and send a warning.
  59. if (hashmap_get(LSR_managed_preds, dest_spo->p)) {
  60. log_warn (
  61. "Predicate %s is managed. Skipping triple.",
  62. dest_p->data);
  63. goto loop_end;
  64. }
  65. /*
  66. * If the subject or object is a resource, check if it exists; if
  67. * it does, add triple to user_data; if not, return an error.
  68. */
  69. uuid_t id_tmp;
  70. LSUP_rc tmp_rc;
  71. // Check subject.
  72. if (LSR_IS_RSRC_IRI (dest_s)) {
  73. uuid_parse (dest_s->data + strlen (LSR_RSRC_NS), id_tmp);
  74. tmp_rc = LSR_desc_get (id_tmp, NULL);
  75. if (tmp_rc != LSUP_OK) {
  76. log_error (
  77. "Referenced subject does not exist: %s",
  78. dest_s->data + strlen (LSR_RSRC_NS));
  79. rc = LSUP_VALUE_ERR;
  80. goto finally;
  81. }
  82. }
  83. // Check object.
  84. if (LSR_IS_RSRC_IRI (dest_o)) {
  85. uuid_parse (dest_o->data + strlen (LSR_RSRC_NS), id_tmp);
  86. tmp_rc = LSR_desc_get (id_tmp, NULL);
  87. if (tmp_rc != LSUP_OK) {
  88. log_error (
  89. "Referenced object does not exist: %s",
  90. dest_o->data + strlen (LSR_RSRC_NS));
  91. rc = LSUP_VALUE_ERR;
  92. goto finally;
  93. }
  94. }
  95. // RDF type check.
  96. if (
  97. LSUP_term_equals (
  98. gr_uri, LSUP_iriref_absolute (rsrc_uri, dest_spo->s))
  99. && LSUP_term_equals (rdf_t, dest_spo->p)
  100. ) {
  101. // If the resource is a special type, handle specific workflow.
  102. // TODO
  103. if (hashmap_get (LSR_managed_types, dest_spo->o)) {
  104. }
  105. }
  106. // Add triple to user_data.
  107. LSUP_graph_add_iter (add_it, dest_spo);
  108. loop_end:
  109. if (dest_s != src_spo->s) LSUP_term_free (dest_s);
  110. if (dest_o != src_spo->o) LSUP_term_free (dest_o);
  111. }
  112. // Add user graph metadata to default graph.
  113. admin_add_it = LSUP_graph_add_init (rsrc->main_data);
  114. dest_s = gr_uri;
  115. dest_p = LSUP_iriref_new ("rdf:type", LSUP_default_nsm);
  116. dest_o = LSUP_iriref_new ("lsup:Metadata", LSUP_default_nsm);
  117. LSUP_triple_init (dest_spo, dest_s, dest_p, dest_o);
  118. LSUP_graph_add_iter (admin_add_it, dest_spo);
  119. LSUP_term_free (dest_o);
  120. dest_o = LSUP_iriref_new ("lsup:UserMetadata", LSUP_default_nsm);
  121. LSUP_triple_init (dest_spo, dest_s, dest_p, dest_o);
  122. LSUP_graph_add_iter (admin_add_it, dest_spo);
  123. LSUP_term_free (dest_o);
  124. LSUP_term_free (dest_p);
  125. // Relationship between data graph and resource.
  126. dest_p = LSUP_iriref_new ("foaf:primaryTopic", LSUP_default_nsm);
  127. dest_o = rsrc_uri;
  128. LSUP_triple_init (dest_spo, dest_s, dest_p, dest_o);
  129. LSUP_graph_add_iter (admin_add_it, dest_spo);
  130. LSUP_term_free (dest_p);
  131. LSUP_graph_iter_free (lu_it);
  132. LSUP_graph_add_done (add_it);
  133. LSUP_graph_add_done (admin_add_it);
  134. lu_it = add_it = admin_add_it = NULL;
  135. }
  136. /* END adding user data. */
  137. /* BEGIN adding managed (admin) data. */
  138. LSUP_Term *gr_uri = LSR_id_to_urn (rsrc->id, "__admin");
  139. rsrc->admin_data = LSUP_graph_new (NULL, gr_uri, NULL);
  140. log_debug (
  141. "Admin data graph (@%p): %s",
  142. LSUP_graph_uri(rsrc->admin_data),
  143. LSUP_graph_uri(rsrc->admin_data)->data);
  144. admin_add_it = LSUP_graph_add_init (rsrc->admin_data);
  145. dest_s = LSUP_iriref_new("", NULL); // Relative to resource URI.
  146. LSUP_Triple admin_spo_s;
  147. LSUP_Triple *admin_spo = &admin_spo_s;
  148. // RDF types.
  149. dest_p = rdf_t;
  150. dest_o = LSUP_iriref_new ("lsup:Resource", LSUP_default_nsm);
  151. LSUP_triple_init (admin_spo, dest_s, dest_p, dest_o);
  152. LSUP_graph_add_iter (admin_add_it, admin_spo);
  153. LSUP_term_free (dest_o);
  154. dest_o = LSUP_iriref_new ("lsup:DescriptiveResource", LSUP_default_nsm);
  155. LSUP_triple_init (admin_spo, dest_s, dest_p, dest_o);
  156. LSUP_graph_add_iter (admin_add_it, admin_spo);
  157. LSUP_term_free (dest_o);
  158. // Timestamps. For now, second precision is fine.
  159. time_t now;
  160. time (&now);
  161. char buf [sizeof ("0000-00-00T00:00:00Z")];
  162. strftime (buf, sizeof (buf), "%FT%TZ", gmtime (&now));
  163. dest_p = LSUP_iriref_new ("lsup:created", LSUP_default_nsm);
  164. dest_o = LSUP_literal_new (
  165. buf, LSUP_iriref_new ("xsd:dateTime", LSUP_default_nsm));
  166. LSUP_triple_init (admin_spo, dest_s, dest_p, dest_o);
  167. LSUP_graph_add_iter (admin_add_it, admin_spo);
  168. LSUP_term_free (dest_p);
  169. dest_p = LSUP_iriref_new ("lsup:lastModified", LSUP_default_nsm);
  170. LSUP_triple_init (admin_spo, dest_s, dest_p, dest_o);
  171. LSUP_graph_add_iter (admin_add_it, admin_spo);
  172. LSUP_term_free (dest_p);
  173. LSUP_term_free (dest_o);
  174. LSUP_graph_add_done (admin_add_it);
  175. /* END adding admin data. */
  176. /* BEGIN adding graph metadata (main). */
  177. admin_add_it = LSUP_graph_add_init (rsrc->main_data);
  178. LSUP_term_free (dest_s);
  179. dest_s = gr_uri;
  180. dest_p = rdf_t;
  181. dest_o = LSUP_iriref_new ("lsup:Metadata", LSUP_default_nsm);
  182. LSUP_triple_init (dest_spo, dest_s, dest_p, dest_o);
  183. LSUP_graph_add_iter (admin_add_it, dest_spo);
  184. LSUP_term_free (dest_o);
  185. dest_o = LSUP_iriref_new ("lsup:AdminMetadata", LSUP_default_nsm);
  186. LSUP_triple_init (dest_spo, dest_s, dest_p, dest_o);
  187. LSUP_graph_add_iter (admin_add_it, dest_spo);
  188. LSUP_term_free (dest_o);
  189. // Relationship between data graph and resource.
  190. dest_p = LSUP_iriref_new ("foaf:primaryTopic", LSUP_default_nsm);
  191. dest_o = rsrc_uri;
  192. LSUP_triple_init (dest_spo, dest_s, dest_p, dest_o);
  193. LSUP_graph_add_iter (admin_add_it, dest_spo);
  194. LSUP_term_free (dest_p);
  195. LSUP_graph_add_done (admin_add_it);
  196. admin_add_it = NULL;
  197. /* END adding graph metadata. */
  198. finally:
  199. LSUP_term_free (rsrc_uri);
  200. LSUP_term_free (rdf_t);
  201. if (rc < 0) goto fail;
  202. rsrc->flags |= LSR_RS_DIRTY;
  203. *rsrc_p = rsrc;
  204. return rc;
  205. fail:
  206. LSUP_graph_iter_free (lu_it);
  207. LSUP_graph_add_done (add_it);
  208. LSUP_graph_add_done (admin_add_it);
  209. LSR_desc_free (rsrc);
  210. *rsrc_p = NULL;
  211. return rc;
  212. }
  213. LSUP_rc
  214. LSR_desc_store (const LSR_Desc *rsrc)
  215. {
  216. // TODO Make atomic. Needs to implement transactions in backend.
  217. LSR_Desc *old_rsrc;
  218. PRCCK (LSR_desc_get (rsrc->id, &old_rsrc));
  219. /*
  220. * BEGIN txn
  221. */
  222. void *txn;
  223. LSUP_store_begin (LSR_store, 0, &txn);
  224. // Remove all existing user graphs.
  225. if (old_rsrc) {
  226. // TODO Handle managed preds and types.
  227. // TODO Handle conflict between disjoint managed types.
  228. // TODO Retain created and created_by.
  229. for (size_t i = 0; old_rsrc->user_data[i] != NULL; i++) {
  230. LSUP_Term *gr_uri = LSUP_graph_uri (old_rsrc->user_data[i]);
  231. size_t ct;
  232. // Remove triples from user graph.
  233. PCHECK (LSUP_graph_remove_txn (
  234. txn, old_rsrc->user_data[i],
  235. NULL, NULL, NULL, &ct), fail);
  236. log_debug ("Removed %lu triples from graph %s", ct, gr_uri->data);
  237. // Remove user graph metadata.
  238. PCHECK (LSUP_graph_remove_txn (
  239. txn, old_rsrc->main_data, gr_uri,
  240. NULL, NULL, NULL), fail);
  241. PCHECK (LSUP_graph_remove_txn (
  242. txn, old_rsrc->main_data,
  243. NULL, NULL, gr_uri, NULL), fail);
  244. }
  245. }
  246. LSUP_Graph *tmp_gr;
  247. // Add new triples.
  248. LSUP_rc rc = LSUP_NOACTION;
  249. for (size_t i = 0; rsrc->user_data[i] != NULL; i++) {
  250. tmp_gr = LSUP_graph_new (
  251. LSR_store,
  252. LSUP_term_copy (LSUP_graph_uri (rsrc->user_data[i])), NULL);
  253. if (UNLIKELY (!tmp_gr)) {
  254. rc = LSUP_MEM_ERR;
  255. goto fail;
  256. }
  257. PCHECK (LSUP_graph_copy_contents_txn (
  258. txn, rsrc->user_data[i], tmp_gr), fail);
  259. LSUP_graph_free (tmp_gr);
  260. }
  261. // Update admin data.
  262. tmp_gr = LSUP_graph_new (
  263. LSR_store,
  264. LSUP_term_copy (LSUP_graph_uri (rsrc->admin_data)), NULL);
  265. if (UNLIKELY (!tmp_gr)) {
  266. rc = LSUP_MEM_ERR;
  267. goto fail;
  268. }
  269. PCHECK (LSUP_graph_copy_contents_txn (
  270. txn, rsrc->admin_data, tmp_gr), fail);
  271. LSUP_graph_free (tmp_gr);
  272. // Update graph metadata.
  273. tmp_gr = LSUP_graph_new (
  274. LSR_store,
  275. LSUP_term_copy (LSUP_graph_uri (rsrc->main_data)), NULL);
  276. if (UNLIKELY (!tmp_gr)) {
  277. rc = LSUP_MEM_ERR;
  278. goto fail;
  279. }
  280. PCHECK (LSUP_graph_copy_contents_txn (
  281. txn, rsrc->main_data, tmp_gr), fail);
  282. LSUP_graph_free (tmp_gr);
  283. PCHECK (LSUP_store_commit (LSR_store, txn), fail);
  284. /*
  285. * END txn
  286. */
  287. return LSUP_OK;
  288. fail:
  289. LSUP_store_abort (LSR_store, txn);
  290. return rc;
  291. }
  292. LSUP_rc
  293. LSUP_desc_update (LSR_id id, LSUP_Term **remove, LSUP_Triple *add)
  294. {
  295. return LSUP_OK;
  296. }
  297. LSUP_rc
  298. LSR_desc_get (const uuid_t id, LSR_Desc **rsrc_p)
  299. {
  300. LSUP_rc rc = LSUP_OK;
  301. LSUP_Graph *main_gr = LSUP_graph_new (
  302. LSR_store, LSUP_term_copy (LSUP_default_ctx), NULL);
  303. if (!main_gr) return LSUP_DB_ERR;
  304. LSUP_Triple *spo = LSUP_triple_new (
  305. LSR_id_to_urn (id, NULL),
  306. LSUP_iriref_new ("rdf:type", LSUP_default_nsm),
  307. LSUP_iriref_new ("lsup:Resource", LSUP_default_nsm)
  308. );
  309. if (!LSUP_graph_contains (main_gr, spo)) {
  310. rc = LSUP_NORESULT;
  311. goto finally;
  312. }
  313. LSUP_term_free (spo->o);
  314. spo->o = LSUP_iriref_new ("lsup:DescriptiveResource", LSUP_default_nsm);
  315. if (!LSUP_graph_contains (main_gr, spo)) {
  316. log_error ("%s is not a descriptive resource.", spo->o->data);
  317. rc = LSUP_NORESULT;
  318. goto finally;
  319. }
  320. LSUP_term_free (spo->p);
  321. spo->p = LSUP_iriref_new ("foaf:primaryTopic", LSUP_default_nsm);
  322. size_t ct = 0, i = 0;
  323. // Find all graphs making up the resource.
  324. LSUP_GraphIterator *it = LSUP_graph_lookup (
  325. main_gr, NULL, spo->p, spo->s, &ct);
  326. LSUP_Graph **data = calloc (sizeof (*data), ct + 1);
  327. while (LSUP_graph_iter_next (it, &spo)) {
  328. data[i] = LSUP_graph_new (LSR_store, LSUP_term_copy (spo->s), NULL);
  329. if (! data[i++]) break; // Last slot remains NULL (sentinel).
  330. }
  331. LSUP_graph_iter_free (it);
  332. rc = LSR_desc_new_multi (data, rsrc_p);
  333. i = 0;
  334. while (i < ct) LSUP_graph_free (data[i++]);
  335. free (data);
  336. finally:
  337. LSUP_triple_free (spo);
  338. LSUP_graph_free (main_gr);
  339. return rc;
  340. }
  341. LSUP_Graph *
  342. LSR_desc_metadata (const LSR_Desc *rsrc)
  343. {
  344. LSUP_Graph *res = LSUP_graph_new (
  345. LSR_store, LSUP_term_copy (LSUP_graph_uri (rsrc->admin_data)),
  346. NULL);
  347. LSUP_graph_copy_contents (rsrc->admin_data, res);
  348. return res;
  349. }
  350. LSUP_Graph **
  351. LSR_desc_user_data (const LSR_Desc *rsrc)
  352. {
  353. size_t ct = 0;
  354. while (rsrc->user_data[ct++]);
  355. LSUP_Graph **res = malloc (sizeof (*res) * (ct + 1));
  356. if (UNLIKELY (!res)) return NULL;
  357. for (size_t i = 0; i < ct; i++) {
  358. res[i] = LSUP_graph_new (
  359. LSR_store,
  360. LSUP_term_copy (LSUP_graph_uri (rsrc->user_data[i])), NULL);
  361. LSUP_graph_copy_contents (rsrc->user_data[i], res[i]);
  362. }
  363. res[ct] = NULL;
  364. return res;
  365. }
  366. void LSR_desc_free (LSR_Desc *rsrc)
  367. {
  368. size_t i = 0;
  369. while (rsrc->user_data[i])
  370. LSUP_graph_free (rsrc->user_data[i++]);
  371. free (rsrc->user_data);
  372. LSUP_graph_free (rsrc->admin_data);
  373. LSUP_graph_free (rsrc->main_data);
  374. free (rsrc);
  375. }