core.h 2.5 KB

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