term.h 4.8 KB

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