desc.c 15 KB

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