term.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef _LSUP_TERM_H
  2. #define _LSUP_TERM_H
  3. #include <assert.h>
  4. #include <regex.h>
  5. #include "uthash.h"
  6. #include "buffer.h"
  7. #include "namespace.h"
  8. // "NULL" triple, a value that is never user-provided. Used to fill deleted
  9. // triples in a keyset.
  10. #define NULL_TRP {NULL_KEY, NULL_KEY, NULL_KEY}
  11. #define UUID4_URN_SIZE UUIDSTR_SIZE + 10
  12. /*
  13. * Term types.
  14. */
  15. /* Undefined placeholder or result of an error. Invalid for most operations. */
  16. #define LSUP_TERM_UNDEFINED 0
  17. /* IRI reference. */
  18. #define LSUP_TERM_IRIREF 1
  19. /* Blank node. */
  20. #define LSUP_TERM_BNODE 2
  21. /* Literal without language tag. */
  22. #define LSUP_TERM_LITERAL 3
  23. /* Language-tagged string literal. */
  24. #define LSUP_TERM_LT_LITERAL 4
  25. /*
  26. * In-term identifier types.
  27. */
  28. /* Data type IRI. */
  29. #define LSUP_ID_DATATYPE 10
  30. /* Language tag string. */
  31. #define LSUP_ID_LANG 11
  32. /* Temporary blank node ID. TODO implement. */
  33. #define LSUP_ID_BNODE 12
  34. /** @brief Default data type for untyped literals.
  35. */
  36. #define DEFAULT_DTYPE "http://www.w3.org/2001/XMLSchema#string"
  37. /** @brief URI parsing regular expression. Conforms to RFC3986.
  38. */
  39. #define LSUP_URI_REGEX_STR \
  40. "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"
  41. /*
  42. * Data types.
  43. */
  44. typedef XXH64_hash_t LSUP_Hash64;
  45. typedef char LSUP_TermType;
  46. typedef struct term_t {
  47. char * data; // URI, literal value, or BNode label.
  48. union {
  49. uint32_t datatype; // Data type hash for LSUP_TERM_LITERAL.
  50. uint32_t lang; // Lang tag hash for LSUP_TERM_LT_LITERAL.
  51. uint32_t bnode_id; // Blank node ID. TODO implement.
  52. };
  53. LSUP_TermType type; // Term type.
  54. } LSUP_Term;
  55. /** @brief Hash cache for lang tags and data types.
  56. */
  57. typedef struct id_cache_t {
  58. uint32_t key;
  59. char * data;
  60. UT_hash_handle hh;
  61. } IDCache;
  62. /*
  63. * Extern variables.
  64. */
  65. /** @brief Global ID cache.
  66. *
  67. * Map of internal term identifiers, such as literal data types, language tags
  68. * and BNode identifiers.
  69. */
  70. extern IDCache *LSUP_id_cache;
  71. /** @brief Compiled hash of default literal data type.
  72. */
  73. extern uint32_t LSUP_default_dtype_key;
  74. /** @brief URI validation pattern, compiled in #LSUP_init().
  75. */
  76. extern regex_t *LSUP_uri_ptn;
  77. /*
  78. * Function prototypes.
  79. */
  80. /** @brief Create a new term.
  81. *
  82. * @param type[in] Term type. One of #LSUP_TermType.
  83. *
  84. * @param data[in] Term data: textual URI, literal value without data type
  85. * or langtag, etc.
  86. *
  87. * @param metadata[in]: language tag for language-tagged literals or data type
  88. * for other literals.
  89. *
  90. * @param term[out] Pointer to a new term, which must be freed with
  91. * #LSUP_term_free after use.
  92. *
  93. * @return LSUP_OK if successful, LSUP_VALUE_ERR if validation fails.
  94. */
  95. LSUP_Term *
  96. LSUP_term_new (LSUP_TermType type, const char *data, const char *metadata);
  97. /** @brief Placeholder term to use with LSUP_term_reset.
  98. */
  99. #define TERM_DUMMY LSUP_term_new (LSUP_TERM_UNDEFINED, NULL, NULL)
  100. /** @brief Shortcut to create a URI.
  101. *
  102. * Must be freed with #LSUP_term_free.
  103. *
  104. * @param data[in] The URI string. If NULL, a UUID4-based URN is generated.
  105. *
  106. * @param uri[out] The URI to be created.
  107. *
  108. * @return LSUP_OK if successful, LSUP_VALUE_ERR if validation fails.
  109. */
  110. inline LSUP_Term *
  111. LSUP_uri_new (const char *data)
  112. {
  113. if (!data) {
  114. uuid_t uuid;
  115. uuid_generate_random (uuid);
  116. uuid_str_t uuid_str;
  117. uuid_unparse_lower (uuid, uuid_str);
  118. char uri[UUID4_URN_SIZE];
  119. snprintf (uri, UUID4_URN_SIZE, "urn:uuid4:%s", uuid_str);
  120. data = uri;
  121. }
  122. return LSUP_term_new (LSUP_TERM_IRIREF, data, NULL);
  123. }
  124. /* @brief Initialize or reuse a pre-allocated term structure.
  125. *
  126. * The structure must have been previously created with #LSUP_term_new. It can
  127. * be reinitialized multiple times without freeing it. It must be eventually
  128. * freed with #LSUP_term_free.
  129. */
  130. LSUP_rc
  131. LSUP_term_init(
  132. LSUP_Term *term, LSUP_TermType type,
  133. const char *data, const char *metadata);
  134. LSUP_Term *
  135. LSUP_term_new_from_buffer (const LSUP_Buffer *sterm);
  136. LSUP_Buffer *
  137. LSUP_buffer_new_from_term (const LSUP_Term *term);
  138. /**
  139. * @brief Shortcut to initialize a URI.
  140. */
  141. LSUP_rc
  142. LSUP_uri_init (LSUP_Term *term, const char *data);
  143. /** @brief Hash a buffer.
  144. */
  145. inline LSUP_Key
  146. LSUP_term_hash (const LSUP_Term *term)
  147. {
  148. LSUP_Buffer *buf;
  149. if (UNLIKELY (!term)) buf = BUF_DUMMY;
  150. else buf = LSUP_buffer_new_from_term (term);
  151. LSUP_Key key = LSUP_buffer_hash (buf);
  152. LSUP_buffer_free (buf);
  153. return key;
  154. }
  155. /**
  156. * Compare two terms.
  157. */
  158. bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2);
  159. void
  160. LSUP_term_done (LSUP_Term *term);
  161. void
  162. LSUP_term_free (LSUP_Term *term);
  163. /** @brief Add an identifier to the term cache.
  164. *
  165. * @param[in] key 32-bit hash of the inserted term.
  166. *
  167. * @param[in] data Term to insert.
  168. */
  169. LSUP_rc
  170. LSUP_tcache_add_id (const uint32_t key, const char *data);
  171. /** @brief Get an identifier from the cache.
  172. *
  173. * @param[in] key Key for the queried term.
  174. *
  175. * @return The retieved term if found, or NULL. The string must not be modified
  176. * or freed.
  177. */
  178. const char *
  179. LSUP_tcache_get_id (const uint32_t key);
  180. #endif