desc.c 15 KB

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