desc.c 14 KB

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