term.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #ifndef LSUP_TERM_H
  2. #define LSUP_TERM_H
  3. #include <assert.h>
  4. #include <regex.h>
  5. #include "buffer.h"
  6. // URI parsing regular expression. Conforms to RFC3986.
  7. #define URI_REGEX_STR \
  8. "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"
  9. #define LANG_SIZE 8 // Size in chars of lang tag
  10. // "NULL" key, a value that is never user-provided. Used to mark special
  11. // values (e.g. deleted records).
  12. #define NULL_KEY 0
  13. // "NULL" triple, a value that is never user-provided. Used to fill deleted
  14. // triples in a keyset.
  15. #define NULL_TRP {NULL_KEY, NULL_KEY, NULL_KEY}
  16. #define UUID4_URN_SIZE UUIDSTR_SIZE + 10
  17. typedef XXH64_hash_t LSUP_TermHash64;
  18. typedef char langtag[LANG_SIZE];
  19. #define TTYPE_TBL \
  20. ENTRY (UNDEFINED, 0) \
  21. ENTRY (URI, 1) \
  22. ENTRY (BNODE, 2) \
  23. ENTRY (LITERAL, 3)
  24. typedef enum LSUP_term_type {
  25. #define ENTRY(a, b) LSUP_TERM_##a = b,
  26. TTYPE_TBL
  27. #undef ENTRY
  28. } LSUP_term_type;
  29. typedef struct LSUP_Term {
  30. LSUP_term_type type;
  31. // This language variable currently supports a 2-digit ISO 639 language
  32. // code and a 2-character ISO 3166-1 country code, separated by a hyphen.
  33. // See https://tools.ietf.org/html/bcp47#section-2.1
  34. langtag lang;
  35. char *datatype;
  36. char *data;
  37. } LSUP_Term;
  38. /** @brief Create a new term.
  39. *
  40. * @param type[in] Term type. One of #LSUP_term_type.
  41. *
  42. * @param data[in] Term data: textual URI, literal value without data type
  43. * or langtag, etc.
  44. *
  45. * @param datatype[in]: data type for literals.
  46. *
  47. * @param lang[in]: language tag for string literals.
  48. *
  49. * @param term[out] Pointer to a new term, which must be freed with
  50. * #LSUP_term_free after use.
  51. *
  52. * @return LSUP_OK if successful, LSUP_VALUE_ERR if validation fails.
  53. */
  54. LSUP_Term *
  55. LSUP_term_new(
  56. LSUP_term_type type, const char *data, char *datatype, char *lang);
  57. /** @brief Placeholder term to use with LSUP_term_reset.
  58. */
  59. #define TERM_DUMMY LSUP_term_new (LSUP_TERM_UNDEFINED, NULL, NULL, NULL)
  60. /** @brief Shortcut to create a URI.
  61. *
  62. * Must be freed with #LSUP_term_free.
  63. *
  64. * @param data[in] The URI string. If NULL, a UUID4-based URN is generated.
  65. *
  66. * @param uri[out] The URI to be created.
  67. *
  68. * @return LSUP_OK if successful, LSUP_VALUE_ERR if validation fails.
  69. */
  70. inline LSUP_Term *
  71. LSUP_uri_new (const char *data)
  72. {
  73. if (!data) {
  74. uuid_t uuid;
  75. uuid_generate_random (uuid);
  76. uuid_str_t uuid_str;
  77. uuid_unparse_lower (uuid, uuid_str);
  78. char uri[UUID4_URN_SIZE];
  79. snprintf (uri, UUID4_URN_SIZE, "urn:uuid4:%s", uuid_str);
  80. data = uri;
  81. }
  82. return LSUP_term_new (LSUP_TERM_URI, data, NULL, NULL);
  83. }
  84. /* @brief Reuse a pre-allocated term structure.
  85. *
  86. * The structure must have been previously created with #LSUP_term_new. It can
  87. * be reinitialized multiple times without freeing it. It must be eventually
  88. * freed with #LSUP_term_free.
  89. */
  90. LSUP_rc
  91. LSUP_term_init(
  92. LSUP_Term *term, LSUP_term_type type,
  93. const char *data, char *datatype, char *lang);
  94. LSUP_Term *
  95. LSUP_term_new_from_buffer (const LSUP_Buffer *sterm);
  96. LSUP_Buffer *
  97. LSUP_buffer_new_from_term (const LSUP_Term *term);
  98. /**
  99. * @brief Shortcut to initialize a URI.
  100. */
  101. inline LSUP_rc
  102. LSUP_uri_init (LSUP_Term *term, const char *data)
  103. {
  104. if (!data) {
  105. uuid_t uuid;
  106. uuid_generate_random (uuid);
  107. uuid_str_t uuid_str;
  108. uuid_unparse_lower (uuid, uuid_str);
  109. char uri[UUIDSTR_SIZE + 10];
  110. sprintf (uri, "urn:uuid4:%s", uuid_str);
  111. data = uri;
  112. }
  113. return LSUP_term_init (term, LSUP_TERM_URI, data, NULL, NULL);
  114. }
  115. /** @brief Simple ad-hoc serialization function.
  116. *
  117. * The resulting term must be freed with #LSUP_term_free after use.
  118. */
  119. LSUP_rc
  120. LSUP_term_serialize (const LSUP_Term *term, LSUP_Buffer *sterm);
  121. /** @brief Deserialize a buffer into a term.
  122. *
  123. * The buffer must be a well-formed serialization of a term, e.g. as obtained
  124. * by #LSUP_term_serialize.
  125. */
  126. LSUP_rc
  127. LSUP_term_deserialize (const LSUP_Buffer *sterm, LSUP_Term *term);
  128. /** @brief Hash a buffer.
  129. */
  130. inline LSUP_Key
  131. LSUP_term_hash (const LSUP_Term *term)
  132. {
  133. LSUP_Buffer *buf;
  134. if (UNLIKELY (!term)) buf = BUF_DUMMY;
  135. else buf = LSUP_buffer_new_from_term (term);
  136. LSUP_Key key = LSUP_buffer_hash (buf);
  137. LSUP_buffer_free (buf);
  138. return key;
  139. }
  140. /**
  141. * Compare two terms.
  142. */
  143. bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2);
  144. /*
  145. // TODO Implement when xxhash v0.8 is released with stable xxhash128 function.
  146. inline XXH128_hash_t
  147. LSUP_term_hash128(const LSUP_Term *term);
  148. */
  149. void
  150. LSUP_term_done (LSUP_Term *term);
  151. void
  152. LSUP_term_free (LSUP_Term *term);
  153. #endif