dres.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /** @file dres.h
  2. *
  3. * @brief Descriptive (RDF) resource.
  4. *
  5. * The Descriptive Resource (D-RES), together with the Data Resource (DATA-R),
  6. * is the building block of lsup_repo information. Its contents are fully
  7. * understood by the library.
  8. */
  9. #ifndef _LSR_DRES_H
  10. #define _LSR_DESC_H
  11. #include "repo_core.h"
  12. /*
  13. * Typedefs.
  14. */
  15. /** @brief D-RES structure.
  16. *
  17. * A Descriptive Resource (D-RES) is made up of several RDF named graphs.
  18. * Each graph has a function defined by the framework and may be managed by
  19. * the framework or by the user.
  20. *
  21. * the URI of each graph is derived from the ID of the resource. This is just
  22. * for readability. Actually, when stored, the resource generates triples that
  23. * explicitly link these graphs together. These URIs have an URN prefix so that
  24. * they are portable and, in a Linked Data server, can be easily replaced with
  25. * absolute URLs based on the server root.
  26. *
  27. * The `user_data` handle is a `{NULL}`-terminated array of user-defined
  28. * graph handles. Each graph URI is considered relative to the resource URN
  29. * and may or may not be consistent across resources, depending on how
  30. * repository managers organize their knowledge.
  31. *
  32. * The `admin_data` graph contains exclusively triples managed by the
  33. * repository. They may have special functionality attached and may be created
  34. * as a direct consequence of a user action (e.g. adding a member to a Set or
  35. * List).
  36. *
  37. * Relationships between these graphs are expressed by triples stored in the
  38. * `main_data` graph.
  39. */
  40. typedef struct dres_t {
  41. LSUP_Graph ** user_data; ///< User-defined data graphs.
  42. LSUP_Graph * admin_data; ///< Managed data (one graph).
  43. LSUP_Graph * main_data; ///< Relationships between graphs.
  44. uuid_t id; ///< Resource identifier (RFC4122).
  45. LSR_ResFlags flags; ///< Flags.
  46. } LSR_Dres;
  47. /*
  48. * API functions.
  49. */
  50. /** @brief Create an in-memory D-RES from multiple graphs.
  51. *
  52. * The resource is volatile until it is stored in a persistent back end. It
  53. * must be stored in a context-capable back end (e.g. `LSUP_STORE_MDB`).
  54. *
  55. * The resource is assigned a UUID4. The resource URI, used in
  56. * relationships, is the ID prefixed with the `LSR_NS_DESC` namespace.
  57. *
  58. * @param[in] data NULL-terminated array of graph handles, each with a set of
  59. * triples to populate the new resource. Triples and terms are copied and get
  60. * freed when #LSR_dres_free() is called. All URIs, including the graph URI,
  61. * if not absolute, are intended as relative to the resource URI. Hence, to
  62. * reference the resource itself, an IRIRef with an empty string as data may
  63. * be used. The graph URI is maintained within the resource. As a convention,
  64. * it is recommended to use a fragment URI with a short label, e.g.
  65. * `#changeset37205`, `#~alice`, or `#admin-md`. Fragments beginning with two
  66. * underscores are reserved for internal use and are not allowed in the input.
  67. *
  68. * @param[out] rsrc Resource handle pointer. The handle should be freed with
  69. * #LSR_dres_free(). On a non-success return code, the handle may be garbage.
  70. *
  71. * @return LSUP_OK on success; < 0 on error.
  72. */
  73. LSUP_rc
  74. LSR_dres_new_multi (LSUP_Graph *const *data, LSR_Dres **rsrc);
  75. /** @brief Shortcut to create a resource from a single graph.
  76. *
  77. * The graph is automatically assigned the `_main` label. See
  78. * #LSR_dres_new_multi for other documentation.
  79. *
  80. * @param[in] data Single graph to insert.
  81. *
  82. * @param[out] rsrc Resource handle pointer. The handle should be freed with
  83. * #LSR_dres_free(). On a non-success return code, the handle may be garbage.
  84. *
  85. * @return LSUP_OK on success; < 0 on error.
  86. */
  87. LSUP_rc
  88. LSR_dres_new (const LSUP_Graph *data, LSR_Dres **rsrc);
  89. /** @brief Create an in-memory D-RES from a stored resource.
  90. *
  91. * Once created, the resource may be modified independently from its stored
  92. * counterpart. In order to make changes permanent, it must be stored again
  93. * using #LSR_dres_store().
  94. *
  95. * @param[in] id ID of the resource to be retrieved, without the namespace.
  96. *
  97. * @param[out] rsrc Resource handle to be populated with the found resource. It
  98. * should be freed with #LSR_dres_free(). If NULL, the resource is only
  99. * checked for existence (much faster). On a non-success return code, the
  100. * handle may be garbage.
  101. *
  102. * @return LSUP_OK if the resource is found; LSUP_NORESULT if not found; <0
  103. * on error.
  104. */
  105. LSUP_rc
  106. LSR_dres_get (const uuid_t id, LSR_Dres **rsrc);
  107. /** @brief Update an existing resource.
  108. *
  109. * This operation works by first removing triples by graph and term matching,
  110. * if provided, then adding triples grouped by graphs. Both steps are optional.
  111. *
  112. * Updates to triples containing #LSR_managed_preds and #LSR_managed_types are
  113. * ignored.
  114. *
  115. * @param[in] rsrc Resource handle.
  116. *
  117. * @param[in] rm_data `{NULL}`-terminated array of term arrays. Each term array
  118. * contains, in the following order: s, p, o, graph URI (c). Any and all terms
  119. * in each array may be NULL, acting as wildcards. This function calls
  120. * #LSUP_graph_remove_txn() on each matching graph, or on all graphs if the
  121. * first term in an array is NULL.
  122. *
  123. * @important Passing `NULL` (note: not `{NULL}`) as an argument to `rm_data`
  124. * indicates that the resource is new. Some internal functions use this
  125. * feature. This should not be done by an API consumer because it may leave the
  126. * resource an inconsistent state.
  127. *
  128. * @param[in] add_data NULL-terminated array of graph handles to add. If the
  129. * resource has already a matching graph for a graph being added, the triples
  130. * are added to the existing graph.
  131. *
  132. * @return LSUP_OK on successful update; TODO
  133. */
  134. LSUP_rc LSR_dres_update ( LSR_Dres *rsrc, LSUP_Term *const rm_data[][4],
  135. LSUP_Graph *const *add_data);
  136. /** @brief Free a D-RES.
  137. */
  138. void
  139. LSR_dres_free (LSR_Dres *rsrc);
  140. /** @brief Get resource metadata.
  141. *
  142. * Return all repository-managed triples about the resource.
  143. *
  144. * @param[in] rsrc Resource to be inspected.
  145. *
  146. * @return In-memory graph with metadata triples.
  147. */
  148. LSUP_Graph *
  149. LSR_dres_metadata (const LSR_Dres *rsrc);
  150. /** @brief Get user-defined data.
  151. *
  152. * Return all user-defined triples in the resource in a single graph.
  153. *
  154. * The resulting graph IRI is the resource IRI with a random fragment string
  155. * appended.
  156. *
  157. * @param[in] rsrc Resource to be inspected.
  158. *
  159. * @return NULL-terminated array of in-memory graphs with data triples.
  160. */
  161. LSUP_Graph **
  162. LSR_dres_user_data (const LSR_Dres *rsrc);
  163. /** @brief All resource triples in a single graph.
  164. *
  165. * Return the union of all the user data and the admin metadata in one flat
  166. * graph.
  167. *
  168. * @param[in] rsrc Resource handle.
  169. *
  170. * @return New graph with all the resource triples. The graph URI is the
  171. * resource URN.
  172. */
  173. LSUP_Graph *
  174. LSR_dres_triples (const LSR_Dres *rsrc);
  175. /** @brief Push an in-memory D-RES into the persistent store.
  176. *
  177. * This is a "create or overwrite" function that deletes any existing resource
  178. * under the given ID before storing the content of the in-memory resource
  179. * at hand.
  180. *
  181. * All data are copied and the resource may be freed after this operation.
  182. *
  183. * @param[in] rsrc Resource to be created or overwritten.
  184. */
  185. LSUP_rc
  186. LSR_dres_store (const LSR_Dres *rsrc);
  187. /** @brief Delete a D-RES.
  188. *
  189. * TODO
  190. *
  191. * @param[in] id Resource ID.
  192. *
  193. * @return LSUP_OK if the resource was found and deleted; LSUP_NOACTION if no
  194. * resource was found for the given ID.
  195. */
  196. LSUP_rc
  197. LSR_dres_delete (LSR_id id);
  198. /** @brief Get resource URN.
  199. *
  200. * @param[in] rsrc Resource pointer.
  201. *
  202. * @return New URI term. It must be freed with #LSUP_term_free() eventually.
  203. */
  204. inline LSUP_Term *
  205. LSR_dres_urn (const LSR_Dres *rsrc)
  206. { return LSR_id_to_urn (rsrc->id, NULL); }
  207. #endif /* _LSR_DESC_H */