desc.c 15 KB

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