dres.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. #include "dres.h"
  2. LSUP_rc
  3. LSR_dres_new_multi (LSUP_Graph *const *data, LSR_Dres **rsrc_p)
  4. {
  5. LSUP_rc rc = LSUP_OK;
  6. LSR_Dres *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_dres_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. *dres_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, dres_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 (dres_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_dres_free (rsrc);
  82. *rsrc_p = NULL;
  83. return rc;
  84. }
  85. void LSR_dres_free (LSR_Dres *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_dres_store (const LSR_Dres *rsrc)
  97. {
  98. LSR_Dres *old_rsrc;
  99. LSUP_rc get_rc = LSR_dres_get (rsrc->id, &old_rsrc);
  100. PRCCK (get_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 (get_rc == LSUP_OK) {
  108. for (size_t i = 0; old_rsrc->user_data[i] != NULL; i++) {
  109. const LSUP_Term *gr_uri = LSUP_graph_uri (old_rsrc->user_data[i]);
  110. size_t ct;
  111. // Remove triples from user graph.
  112. PCHECK (LSUP_graph_remove_txn (
  113. txn, old_rsrc->user_data[i],
  114. NULL, NULL, NULL, &ct), fail);
  115. LOG_DEBUG ("Removed %lu triples from graph %s", ct, gr_uri->data);
  116. // Remove user graph metadata.
  117. PCHECK (LSUP_graph_remove_txn (
  118. txn, old_rsrc->main_data, gr_uri,
  119. NULL, NULL, NULL), fail);
  120. PCHECK (LSUP_graph_remove_txn (
  121. txn, old_rsrc->main_data,
  122. NULL, NULL, gr_uri, NULL), fail);
  123. }
  124. }
  125. LSUP_Graph *tmp_gr;
  126. // Add new triples.
  127. LSUP_rc rc = LSUP_NOACTION;
  128. for (size_t i = 0; rsrc->user_data[i] != NULL; i++) {
  129. tmp_gr = LSUP_graph_new (
  130. LSR_store, LSUP_graph_uri (rsrc->user_data[i]), NULL);
  131. if (UNLIKELY (!tmp_gr)) {
  132. rc = LSUP_MEM_ERR;
  133. goto fail;
  134. }
  135. PCHECK (LSUP_graph_copy_contents_txn (
  136. txn, rsrc->user_data[i], tmp_gr, NULL, NULL, NULL), fail);
  137. LSUP_graph_free (tmp_gr);
  138. }
  139. // Update admin data.
  140. tmp_gr = LSUP_graph_new (
  141. LSR_store, LSUP_graph_uri (rsrc->admin_data), 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->admin_data, tmp_gr, NULL, NULL, NULL), fail);
  148. LSUP_graph_free (tmp_gr);
  149. // Update graph metadata.
  150. tmp_gr = LSUP_graph_new (
  151. LSR_store, LSUP_graph_uri (rsrc->main_data), NULL);
  152. if (UNLIKELY (!tmp_gr)) {
  153. rc = LSUP_MEM_ERR;
  154. goto fail;
  155. }
  156. PCHECK (LSUP_graph_copy_contents_txn (
  157. txn, rsrc->main_data, tmp_gr, NULL, NULL, NULL), fail);
  158. LSUP_graph_free (tmp_gr);
  159. PCHECK (LSUP_store_commit (LSR_store, txn), fail);
  160. /*
  161. * END txn
  162. */
  163. return LSUP_OK;
  164. fail:
  165. LSUP_store_abort (LSR_store, txn);
  166. return rc;
  167. }
  168. LSUP_rc
  169. LSR_dres_delete (LSR_id id)
  170. {
  171. // TODO
  172. return LSUP_NOT_IMPL_ERR;
  173. }
  174. LSUP_rc
  175. LSR_dres_get (const uuid_t id, LSR_Dres **rsrc_p)
  176. {
  177. LSUP_rc rc = LSUP_OK;
  178. LSUP_Graph *main_gr = LSUP_graph_new (LSR_store, LSUP_default_ctx, NULL);
  179. if (!main_gr) return LSUP_DB_ERR;
  180. LSUP_Triple *spo = LSUP_triple_new (
  181. LSR_id_to_urn (id, NULL),
  182. LSUP_iriref_new ("rdf:type", LSUP_default_nsm),
  183. LSUP_iriref_new ("lsup:Resource", LSUP_default_nsm)
  184. );
  185. if (!LSUP_graph_contains (main_gr, spo)) {
  186. rc = LSUP_NORESULT;
  187. goto finally;
  188. }
  189. LSUP_term_free (spo->o);
  190. spo->o = LSUP_iriref_new ("lsup:DescriptiveResource", LSUP_default_nsm);
  191. if (!LSUP_graph_contains (main_gr, spo)) {
  192. log_error ("%s is not a descriptive resource.", spo->o->data);
  193. rc = LSUP_NORESULT;
  194. goto finally;
  195. }
  196. LSUP_term_free (spo->p);
  197. spo->p = LSUP_iriref_new ("foaf:primaryTopic", LSUP_default_nsm);
  198. size_t ct = 0, i = 0;
  199. // Find all graphs making up the resource.
  200. LSUP_GraphIterator *it = LSUP_graph_lookup (
  201. main_gr, NULL, spo->p, spo->s, &ct);
  202. LSUP_Graph **data = calloc (sizeof (*data), ct + 1);
  203. while (LSUP_graph_iter_next (it, &spo)) {
  204. data[i] = LSUP_graph_new (LSR_store, spo->s, NULL);
  205. if (! data[i++]) break; // Last slot remains NULL (sentinel).
  206. }
  207. LSUP_graph_iter_free (it);
  208. rc = LSR_dres_new_multi (data, rsrc_p);
  209. i = 0;
  210. while (i < ct) LSUP_graph_free (data[i++]);
  211. free (data);
  212. finally:
  213. LSUP_triple_free (spo);
  214. LSUP_graph_free (main_gr);
  215. return rc;
  216. }
  217. LSUP_Graph *
  218. LSR_dres_metadata (const LSR_Dres *rsrc)
  219. {
  220. LSUP_Graph *res = LSUP_graph_new (
  221. LSR_store, LSUP_graph_uri (rsrc->admin_data), NULL);
  222. LSUP_graph_copy (rsrc->admin_data, res);
  223. return res;
  224. }
  225. LSUP_Graph **
  226. LSR_dres_user_data (const LSR_Dres *rsrc)
  227. {
  228. size_t ct = 0;
  229. while (rsrc->user_data[ct]) ct++;
  230. LSUP_Graph **res = malloc (sizeof (*res) * (ct + 1));
  231. if (UNLIKELY (!res)) return NULL;
  232. for (size_t i = 0; i < ct; i++) {
  233. res[i] = LSUP_graph_new (
  234. LSR_store, LSUP_graph_uri (rsrc->user_data[i]), NULL);
  235. PCHECK (LSUP_graph_copy (rsrc->user_data[i], res[i]), fail);
  236. }
  237. res[ct] = NULL;
  238. return res;
  239. fail:
  240. for (size_t i = 0; i < ct; i++)
  241. LSUP_graph_free (res[i]);
  242. return NULL;
  243. }
  244. LSUP_Graph *
  245. LSR_dres_triples (const LSR_Dres *rsrc)
  246. {
  247. LSUP_Graph *res = LSR_dres_metadata (rsrc);
  248. if (UNLIKELY (!res)) return NULL;
  249. LSUP_graph_set_uri (res, LSR_dres_urn (rsrc));
  250. size_t ct = 0;
  251. while (rsrc->user_data[ct]) ct++;
  252. for (size_t i = 0; i < ct; i++)
  253. PCHECK (LSUP_graph_copy (rsrc->user_data[i], res), fail);
  254. return res;
  255. fail:
  256. LSUP_graph_free (res);
  257. return NULL;
  258. }
  259. LSUP_rc
  260. LSR_dres_update (
  261. LSR_Dres *rsrc, LSUP_Term *const rm_data[][4],
  262. LSUP_Graph *const *add_data)
  263. {
  264. LSUP_rc rc = LSUP_NOACTION;
  265. LSUP_Term *rsrc_uri = LSR_id_to_urn (rsrc->id, NULL);
  266. size_t ct = 0;
  267. /*
  268. * REMOVE user data.
  269. */
  270. bool is_new;
  271. if (rm_data) {
  272. size_t ct_loop, i, j;
  273. for (i = 0; rm_data[i] != NULL; i++) {
  274. LSUP_Buffer *sspoc[4] = {NULL};
  275. for (j = 0; j < 4; j++) {
  276. if (rm_data[i][j])
  277. sspoc[j] = LSUP_term_serialize (rm_data[i][j]);
  278. }
  279. LSR_store->sif->remove_fn (
  280. LSR_store->data,
  281. sspoc[0], sspoc[1], sspoc[2], sspoc[3],
  282. NULL, &ct_loop);
  283. for (j = 0; j < 4; j++)
  284. LSUP_buffer_free (sspoc[j]);
  285. LOG_DEBUG (
  286. "Removed %lu triples for %s",
  287. ct_loop, rm_data[i][0]->data);
  288. ct += ct_loop;
  289. // TODO if graph is empty after removal, remove it.
  290. }
  291. is_new = false;
  292. } else is_new = true;
  293. /*
  294. * ADD user data.
  295. *
  296. * Extensively using URIs relative to each graph, following
  297. * https://www.w3.org/TR/ldp-bp/#use-relative-uris
  298. *
  299. * Note: a URI such as `#doc1` in graph `urn:lsres:1234#__main` is
  300. * considered relative to `urn:lsres:1234` (`#__main` is the fragment of a
  301. * document).
  302. */
  303. // Count graphs to be inserted and allocate space.
  304. ct = 0;
  305. while (add_data[ct]) ct++;
  306. rsrc->user_data = calloc (sizeof (*rsrc->user_data), ct + 1);
  307. if (UNLIKELY (! rsrc->user_data)) return LSUP_MEM_ERR;
  308. LSUP_Triple spo_s;
  309. LSUP_Triple *spo = &spo_s;
  310. LSUP_GraphIterator
  311. *lu_it, // Lookup iterator.
  312. *add_it; // Main graph add iterator.
  313. // Loop over input graphs.
  314. for (size_t i = 0; i < ct; i++) {
  315. const LSUP_Term *gr_uri = LSUP_graph_uri (add_data[i]);
  316. LOG_DEBUG ("Adding user data #%lu with uri: %s", i, gr_uri->data);
  317. LSUP_Term *rel_uri = LSUP_iriref_relative (rsrc_uri, gr_uri);
  318. if (strstr (rel_uri->data, "#__") == rel_uri->data) {
  319. log_error ("Fragment URI cannot start with double underscore.");
  320. rc = LSUP_VALUE_ERR;
  321. }
  322. LSUP_term_free (rel_uri);
  323. if (rc < 0) goto finally;
  324. rsrc->user_data[i] = LSUP_graph_new (NULL, gr_uri, NULL);
  325. LOG_DEBUG (
  326. "User data graph (@%p): %s",
  327. LSUP_graph_uri(rsrc->user_data[i]),
  328. LSUP_graph_uri(rsrc->user_data[i])->data);
  329. add_it = LSUP_graph_add_init (rsrc->user_data[i]);
  330. lu_it = LSUP_graph_lookup (rsrc->user_data[i], NULL, NULL, NULL, NULL);
  331. // Loop over source graph triples.
  332. LSUP_Term *dest_s, *dest_p, *dest_o;
  333. LSUP_Triple *src_spo;
  334. while (LSUP_graph_iter_next (lu_it, &src_spo) == LSUP_OK) {
  335. dest_s = LSUP_IS_IRI (src_spo->s) ?
  336. LSUP_iriref_relative (rsrc_uri, src_spo->s) : src_spo->s;
  337. dest_p = LSUP_term_copy (src_spo->p);
  338. dest_o = LSUP_IS_IRI (src_spo->s) ?
  339. LSUP_iriref_relative (rsrc_uri, src_spo->s) : src_spo->s;
  340. LSUP_triple_init (spo, dest_s, dest_p, dest_o);
  341. // if the pred is managed, ignore the triple and send a warning.
  342. if (hashmap_get (LSR_managed_preds, spo->p)) {
  343. log_warn (
  344. "Predicate %s is managed. Skipping triple.",
  345. dest_p->data);
  346. goto loop_end;
  347. }
  348. /*
  349. * If the subject or object is a resource, check if it exists; if
  350. * it does, add triple to user_data; if not, return an error.
  351. */
  352. uuid_t id_tmp;
  353. LSUP_rc tmp_rc;
  354. // Check subject.
  355. if (LSR_IS_RSRC_IRI (dest_s)) {
  356. uuid_parse (dest_s->data + strlen (LSR_RSRC_NS), id_tmp);
  357. tmp_rc = LSR_dres_get (id_tmp, NULL);
  358. if (tmp_rc != LSUP_OK) {
  359. log_error (
  360. "Referenced subject does not exist: %s",
  361. dest_s->data);
  362. rc = LSUP_VALUE_ERR;
  363. goto finally;
  364. }
  365. }
  366. // Check object.
  367. if (LSR_IS_RSRC_IRI (dest_o)) {
  368. uuid_parse (dest_o->data + strlen (LSR_RSRC_NS), id_tmp);
  369. tmp_rc = LSR_dres_get (id_tmp, NULL);
  370. if (tmp_rc != LSUP_OK) {
  371. log_error (
  372. "Referenced object does not exist: %s",
  373. dest_o->data + strlen (LSR_RSRC_NS));
  374. rc = LSUP_VALUE_ERR;
  375. goto finally;
  376. }
  377. }
  378. // RDF type check.
  379. if (
  380. LSUP_term_equals (
  381. gr_uri, LSUP_iriref_absolute (rsrc_uri, spo->s))
  382. && LSUP_term_equals (LSR_rdf_t, spo->p)
  383. ) {
  384. // If the resource is a special type, handle specific workflow.
  385. if (hashmap_get (LSR_managed_types, spo->o)) {
  386. if (
  387. !is_new && (
  388. strcmp (spo->o->data, "lsup:List") == 0 ||
  389. strcmp (spo->o->data, "lsup:ListItem") == 0 ||
  390. strcmp (spo->o->data, "lsup:Set") == 0 ||
  391. strcmp (spo->o->data, "lsup:Proxy") == 0
  392. )
  393. ) {
  394. // TODO handle system-managed types.
  395. } else {
  396. log_error (
  397. "%s is a managed predicate and cannot "
  398. "be used on creation.", spo->o->data);
  399. rc = LSUP_VALUE_ERR;
  400. goto finally;
  401. }
  402. }
  403. }
  404. // Add triple to user_data.
  405. LSUP_graph_add_iter (add_it, spo);
  406. loop_end:
  407. if (dest_s != src_spo->s) LSUP_term_free (dest_s);
  408. if (dest_o != src_spo->o) LSUP_term_free (dest_o);
  409. }
  410. /*
  411. * UPDATE graph metadata.
  412. */
  413. // Add user graph metadata to default graph.
  414. LSUP_GraphIterator *admin_add_it = LSUP_graph_add_init (
  415. rsrc->main_data);
  416. dest_s = LSUP_iriref_new (gr_uri->data, NULL);
  417. dest_p = LSUP_iriref_new ("rdf:type", LSUP_default_nsm);
  418. dest_o = LSUP_iriref_new ("lsup:Metadata", LSUP_default_nsm);
  419. LSUP_triple_init (spo, dest_s, dest_p, dest_o);
  420. LSUP_graph_add_iter (admin_add_it, spo);
  421. LSUP_term_free (dest_o);
  422. dest_o = LSUP_iriref_new ("lsup:UserMetadata", LSUP_default_nsm);
  423. LSUP_triple_init (spo, dest_s, dest_p, dest_o);
  424. LSUP_graph_add_iter (admin_add_it, spo);
  425. LSUP_term_free (dest_o);
  426. LSUP_term_free (dest_p);
  427. // Relationship between data graph and resource.
  428. dest_p = LSUP_iriref_new ("foaf:primaryTopic", LSUP_default_nsm);
  429. dest_o = rsrc_uri;
  430. LSUP_triple_init (spo, dest_s, dest_p, dest_o);
  431. LSUP_graph_add_iter (admin_add_it, spo);
  432. LSUP_term_free (dest_p);
  433. LSUP_term_free (dest_s);
  434. LSUP_graph_iter_free (lu_it);
  435. LSUP_graph_add_done (add_it);
  436. LSUP_graph_add_done (admin_add_it);
  437. lu_it = add_it = admin_add_it = NULL;
  438. }
  439. finally:
  440. LSUP_term_free (rsrc_uri);
  441. if (lu_it) LSUP_graph_iter_free (lu_it);
  442. if (add_it) LSUP_graph_add_done (add_it);
  443. return rc;
  444. }
  445. /*
  446. * Inline function declarations.
  447. */
  448. LSUP_Term *LSR_dres_urn (const LSR_Dres *rsrc);