core.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /** @file core.h
  2. *
  3. * @brief LSUP_REPO core module.
  4. */
  5. #ifndef _LSR_CORE_H
  6. #define _LSR_CORE_H
  7. #include "lsup_rdf.h"
  8. /*
  9. * Defines.
  10. */
  11. /// URI prefix for all managed resources.
  12. #define LSR_RSRC_PFX "rsrc"
  13. #define LSR_RSRC_NS "urn:lsres:"
  14. /// Label for descriptive resource type.
  15. #define LSR_TYPE_DESC "DESC_R"
  16. /// Label for data resource type.
  17. #define LSR_TYPE_DATA "DATA_R"
  18. /// Default graph back end.
  19. #define LSR_DEFAULT_BACKEND LSUP_STORE_MDB
  20. /// Determine if an IRI is an LSR resource.
  21. #define LSR_IS_RSRC_IRI(term) \
  22. (LSUP_IS_IRI (term) && strstr ((term)->data, LSR_RSRC_NS) == (term)->data)
  23. /*
  24. * Data types.
  25. */
  26. /// LS identifier type.
  27. typedef char LSR_id[UUIDSTR_SIZE];
  28. /// Term to key hash map.
  29. typedef struct hashmap LSR_TermMap;
  30. /// Resource flag type.
  31. typedef enum res_flags_t {
  32. LSR_RS_MANAGED = 1 << 0, ///< Managed by the repo.
  33. LSR_RS_DIRTY = 1 << 1, ///< Dirty buffer.
  34. } LSR_ResFlags;
  35. /*
  36. * Global variables.
  37. */
  38. /// Whether the environment is initialized.
  39. extern bool LSR_is_init;
  40. /// Managed predicates.
  41. extern LSR_TermMap *LSR_managed_preds;
  42. /// Managed RDF types.
  43. extern LSR_TermMap *LSR_managed_types;
  44. /// Default back end store.
  45. extern LSUP_Store *LSR_store;
  46. /// RDF type URI.
  47. extern LSUP_Term *LSR_rdf_t;
  48. /*
  49. * API.
  50. */
  51. /** @brief Initialize LSUP and LSR environments.
  52. *
  53. * This function is idempotent.
  54. */
  55. LSUP_rc LSR_init (void);
  56. /** @brief Tear down LSUP and LSR environments.
  57. *
  58. * This function is idempotent. It is also called automatically at the end of
  59. * a program (`atexit()`).
  60. */
  61. void LSR_done (void);
  62. /** @brief Create a new URN term from a resource ID and optional fragment.
  63. *
  64. * @param[in] id Resource ID.
  65. *
  66. * @param[in] frag Fragment to add to the URN. If not NULL, it is appended to
  67. * the URN and an additional `#` character.
  68. *
  69. * @return New term. The default namespace map is associated with the term.
  70. * It should be freed with #LSUP_term_free().
  71. */
  72. inline LSUP_Term *LSR_id_to_urn (const uuid_t id, const char *frag)
  73. {
  74. char id_str[UUID_STR_LEN];
  75. uuid_unparse_lower (id, id_str);
  76. char *urn_str;
  77. if (frag) {
  78. // +2 for the : and #, NUL is included in UUID_STR_LEN.
  79. urn_str = malloc (
  80. strlen (LSR_RSRC_PFX) + UUID_STR_LEN + strlen (frag) + 2);
  81. if (! urn_str) return NULL;
  82. sprintf (urn_str, "%s:%s#%s", LSR_RSRC_PFX, id_str, frag);
  83. } else {
  84. urn_str = malloc (strlen (LSR_RSRC_PFX) + UUID_STR_LEN + 1);
  85. if (! urn_str) return NULL;
  86. sprintf (urn_str, "%s:%s", LSR_RSRC_PFX, id_str);
  87. }
  88. LSUP_Term *urn = LSUP_iriref_new (urn_str, LSUP_default_nsm);
  89. free (urn_str);
  90. return urn;
  91. }
  92. #endif /* _LSR_CODE_H */