term.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 UUID4_URN_SIZE UUIDSTR_SIZE + 10
  8. /** @brief Default data type for untyped literals (prefixed IRI).
  9. */
  10. #define DEFAULT_DTYPE "http://www.w3.org/2001/XMLSchema#string"
  11. /** @brief URI parsing regular expression.
  12. *
  13. * Based on RFC3986 (see https://tools.ietf.org/html/rfc3986#appendix-B) and
  14. * modified for use in this application. Relevant matching groups are the
  15. * following, for a sample URI `http://example.org/123/456/?query=blah#frag`:
  16. *
  17. * #0: Full parsed URI (http://example.org/123/456/?query=blah#frag)
  18. * #1: Domain prefix (http://example.org)
  19. * #2: Protocol (http:)
  20. * #4: Authority (example.org)
  21. * #5: Path relative to domain (/123/456/?query=blah#frag)
  22. * #6: Path, excluding query and fragment (/123/456/)
  23. * #8: Query (query=blah)
  24. * #10: Fragment (frag)
  25. *
  26. * For URN-like URIs, such as `urn:s:0`, the prefix part (#1) is `urn:` and
  27. * the path (#4) is `s:0`.
  28. */
  29. #define LSUP_URI_REGEX_STR \
  30. "^(([^:/?#]+:)?(//([^/?#]*))?)?(([^?#]*)(\\?([^#]*))?(#(.*))?)"
  31. /*
  32. * Data types.
  33. */
  34. /// Language tag, currently restricted to 7 characters.
  35. typedef char LSUP_LangTag[8];
  36. /// Term type.
  37. typedef enum {
  38. LSUP_TERM_UNDEFINED = 0,/**<
  39. * Undefined placeholder or result of an error.
  40. * Invalid for most operations.
  41. */
  42. LSUP_TERM_IRIREF, ///< IRI reference.
  43. LSUP_TERM_NS_IRIREF, ///< Namespace-prefixed IRI reference.
  44. LSUP_TERM_LITERAL, ///< Literal without language tag.
  45. LSUP_TERM_LT_LITERAL, ///< Language-tagged string literal.
  46. LSUP_TERM_BNODE, ///< Blank node.
  47. } LSUP_TermType;
  48. /** @brief IRI information.
  49. *
  50. * See regex matching group for #LSUP_URI_REGEX_STR for more information.
  51. */
  52. typedef struct iri_info_t LSUP_IRIInfo;
  53. /// RDF term.
  54. typedef struct term_t {
  55. char * data; // URI, literal value, or BNode label.
  56. union {
  57. struct term_t * datatype; // Data type IRI for LSUP_TERM_LITERAL.
  58. LSUP_LangTag lang; // Lang tag for LSUP_TERM_LT_LITERAL.
  59. LSUP_Key bnode_id; // BNode ID for comparison & skolemization.
  60. LSUP_IRIInfo * iri_info; // IRI information structure.
  61. };
  62. LSUP_TermType type; // Term type.
  63. } LSUP_Term;
  64. /** @brief Shorthand to test if a term is a IRI of any kind.
  65. */
  66. #define LSUP_IS_IRI(term) \
  67. ((term)->type == LSUP_TERM_IRIREF || (term)->type == LSUP_TERM_NS_IRIREF)
  68. /** @brief Shorthand to test if a term is a literal of any kind.
  69. */
  70. #define LSUP_IS_LITERAL(term) \
  71. ((term)->type == LSUP_TERM_LITERAL || (term)->type == LSUP_TERM_LT_LITERAL)
  72. typedef struct triple_t {
  73. LSUP_Term *s;
  74. LSUP_Term *p;
  75. LSUP_Term *o;
  76. } LSUP_Triple;
  77. /** @brief Key-term pair.
  78. */
  79. typedef struct term_cache_entry_t {
  80. LSUP_Key key; // Key (hash) of the term.
  81. LSUP_Term * term; // Term handle.
  82. } LSUP_KeyedTerm;
  83. /*
  84. * Extern variables.
  85. */
  86. /** @brief Global term cache.
  87. *
  88. * Stores frequently used terms, e.g. data type URIs.
  89. */
  90. extern struct hashmap *LSUP_term_cache;
  91. /** @brief Compiled hash of default literal data type.
  92. */
  93. extern uint32_t LSUP_default_dtype_key;
  94. /** @brief URI validation pattern, compiled in #LSUP_init().
  95. */
  96. extern regex_t *LSUP_uri_ptn;
  97. /** @brief Default literal data type URI.
  98. *
  99. * Literal terms created with undefined data type will have it set to this
  100. * URI implicitly.
  101. */
  102. extern LSUP_Term *LSUP_default_datatype;
  103. /*
  104. * API functions.
  105. */
  106. /** @brief Create a new term.
  107. *
  108. * This is a generic function; it is recommended to use specialized functions
  109. * such as #LSUP_term_new(), #LSUP_literal_new(), etc. as they have strict type
  110. * checks for the metadata parameter.
  111. *
  112. * @param type[in] Term type. One of #LSUP_TermType.
  113. *
  114. * @param data[in] Term data: textual URI, literal value without data type
  115. * or langtag, etc. It may be NULL for IRI refs and BNodes, in which case a
  116. * random identifier is generated.
  117. *
  118. * @param metadata[in] Namespace map (LSUP_NSMap *) for IRI refs; language tag
  119. * (LSUP_LangTag *) for language-tagged literals; or data type (LSUP_Term *)
  120. * for other literals. It may be NULL.
  121. *
  122. * @return New term, which must be freed with #LSUP_term_free after use; or
  123. * NULL on error.
  124. */
  125. LSUP_Term *
  126. LSUP_term_new (LSUP_TermType type, const char *data, void *metadata);
  127. /** @brief Placeholder term to use with LSUP_term_reset.
  128. */
  129. #define TERM_DUMMY LSUP_term_new (LSUP_TERM_UNDEFINED, NULL, NULL)
  130. /** @brief Shortcut to create an IRI reference.
  131. *
  132. * Must be freed with #LSUP_term_free.
  133. *
  134. * @param data[in] The URI string. If NULL, a UUID4-based URN is generated.
  135. * This cannot be NULL if the nsm parameter is not NULL.
  136. *
  137. * @param nsm[in] Namespace map. If not NULL, a namespace-prefixed
  138. * (#LSUP_TERM_NS_IRIREF) is created, otherwise a regular one
  139. * (#LSUP_TERM_IRIREF).
  140. *
  141. * @return same as #LSUP_term_new().
  142. */
  143. inline LSUP_Term *
  144. LSUP_iriref_new (const char *data, LSUP_NSMap *nsm)
  145. {
  146. return (
  147. nsm ? LSUP_term_new (LSUP_TERM_NS_IRIREF, data, nsm) :
  148. LSUP_term_new (LSUP_TERM_IRIREF, data, NULL));
  149. }
  150. /** @brief Create a new absolute IRI from a path relative to a root IRI.
  151. *
  152. * The term is always of type LSUP_TERM_IRIREF (i.e. not namespace-prefixed).
  153. *
  154. * If the provided IRI is already a fully qualified IRI (i.e. it has a prefix)
  155. * the result is semantically identical to the input.
  156. *
  157. * If the relative IRI begins with a '/', the resulting IRI is relative to the
  158. * web root of the root IRI. I.e. if a root IRI has a path after the webroot,
  159. * it is ignored.
  160. *
  161. * Otherwise, the resulting IRI is relative to the full root string.
  162. *
  163. * @param[in] root Root IRI that the new IRI should be relative to.
  164. *
  165. * @param[in] iri Term with an IRI relative to the webroot.
  166. *
  167. * @return New absolute IRI, or NULL if either term is not an IRI.
  168. */
  169. LSUP_Term *
  170. LSUP_iriref_absolute (const LSUP_Term *root, const LSUP_Term *iri);
  171. /** @brief Create a new relative IRI from an absolute IRI and a web root IRI.
  172. *
  173. * This works with namespace-prefixed IRIs and returns a term of the same type
  174. * as the input.
  175. *
  176. * @param[in] iri Full IRI.
  177. *
  178. * @param[in] root Root IRI that the new IRI should be relative to.
  179. *
  180. * @return New IRI, or NULL if either term is not an IRI. If the input IRI is
  181. * not a path under the root IRI, the result will be identical to the input.
  182. */
  183. LSUP_Term *
  184. LSUP_iriref_relative (const LSUP_Term *root, const LSUP_Term *iri);
  185. /** @brief Shortcut to create a literal term.
  186. *
  187. * Must be freed with #LSUP_term_free.
  188. *
  189. * @param data[in] The literal string.
  190. *
  191. * @param datatype[in] Data type URI string. If NULL, the default data type
  192. * (xsd:string) is used. The new term takes ownership of the pointer.
  193. *
  194. * @return same as #LSUP_term_new().
  195. */
  196. inline LSUP_Term *
  197. LSUP_literal_new (const char *data, LSUP_Term *datatype)
  198. { return LSUP_term_new (LSUP_TERM_LITERAL, data, datatype); }
  199. /** @brief Shortcut to create a language-tagged literal term.
  200. *
  201. * Must be freed with #LSUP_term_free.
  202. *
  203. * @param data[in] The literal string.
  204. *
  205. * @param lang[in] Language tag string.
  206. *
  207. * @return same as #LSUP_term_new().
  208. */
  209. inline LSUP_Term *
  210. LSUP_lt_literal_new (const char *data, char *lang)
  211. { return LSUP_term_new (LSUP_TERM_LT_LITERAL, data, lang); }
  212. /** @brief Shortcut to create a blank node.
  213. *
  214. * Must be freed with #LSUP_term_free.
  215. *
  216. * @param data[in] The BNode identifier.
  217. *
  218. * @return same as #LSUP_term_new().
  219. */
  220. inline LSUP_Term *
  221. LSUP_bnode_new (const char *data)
  222. { return LSUP_term_new (LSUP_TERM_BNODE, data, NULL); }
  223. /** @brief Copy a term.
  224. *
  225. * @param[in] src The term to copy.
  226. *
  227. * @return A new duplicate term handle.
  228. */
  229. LSUP_Term *
  230. LSUP_term_copy (const LSUP_Term *src);
  231. /** @brief Deserialize a buffer into a term.
  232. *
  233. * @param[in] sterm Buffer to convert into a term. It must be a valid
  234. * serialized term from store or obtained with #LSUP_term_serialize().
  235. *
  236. * @return New term handle. It must be freed with #LSUP_term_free().
  237. */
  238. LSUP_Term *
  239. LSUP_term_new_from_buffer (const LSUP_Buffer *sterm);
  240. /** @brief Serialize a term into a buffer.
  241. *
  242. * @param[in] sterm Term to convert into a buffer.
  243. *
  244. * @return New buffer handle. It must be freed with #LSUP_buffer_free().
  245. */
  246. LSUP_Buffer *
  247. LSUP_term_serialize (const LSUP_Term *term);
  248. /** @brief Hash a buffer.
  249. */
  250. LSUP_Key
  251. LSUP_term_hash (const LSUP_Term *term);
  252. /** @brief Compare two terms.
  253. *
  254. * The terms evaluate as equal if their hashes are equal—i.e. if they are
  255. * semantically equivalent.
  256. */
  257. inline bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2)
  258. { return LSUP_term_hash (term1) == LSUP_term_hash (term2); }
  259. void
  260. LSUP_term_free (LSUP_Term *term);
  261. /** @brief Namespace map of a IRI ref.
  262. *
  263. * @param[in] iri IRI reference handle.
  264. *
  265. * @return A pointer to the namespace map associated with the IRI. It is
  266. * freed at program shutdown.
  267. */
  268. LSUP_NSMap *
  269. LSUP_iriref_nsm (const LSUP_Term *iri);
  270. /** @brief Get the prefix portion of a IRI ref.
  271. *
  272. * @param[in] iri IRI reference handle.
  273. *
  274. * @return String containing the protocol and domain name part of the IRI. It
  275. * should be freed after use.
  276. */
  277. char *
  278. LSUP_iriref_prefix (const LSUP_Term *iri);
  279. /** @brief Get the path portion of a IRI ref.
  280. *
  281. * @param[in] iri IRI reference handle.
  282. *
  283. * @return String containing the path of the IRI relative to the web root. For
  284. * a URN, such as `urn:myns:myid`, it would be `myns:myid`. This string should
  285. * be freed after use.
  286. */
  287. char *
  288. LSUP_iriref_path (const LSUP_Term *iri);
  289. /** @brief Get the fragment portion of a IRI ref.
  290. *
  291. * @param[in] iri IRI reference handle.
  292. *
  293. * @return String containing the fragment part of the IRI, or NULL if the IRI
  294. * contains no fragment. It should be freed after use.
  295. */
  296. char *
  297. LSUP_iriref_frag (const LSUP_Term *iri);
  298. /*
  299. * TRIPLES
  300. */
  301. /** @brief Create a new triple from three terms.
  302. *
  303. * Terms are NOT copied. To free them with the triple, use #LSUP_triple_free().
  304. * To only free the triple, use free().
  305. *
  306. * TODO Term types are not validated at the moment.
  307. *
  308. * @param[in] s Triple subject. It must be an IRIRef or BNode.
  309. *
  310. * @param[in] p Triple predicate. It must be an IRIRef.
  311. *
  312. * @param[in] o Triple object.
  313. *
  314. */
  315. LSUP_Triple *
  316. LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  317. /** @brief Dummy triple with NULL slots. It is not a valid triple.
  318. */
  319. #define TRP_DUMMY LSUP_triple_new (NULL, NULL, NULL)
  320. LSUP_Triple *
  321. LSUP_triple_new_from_btriple (const LSUP_BufferTriple *sspo);
  322. LSUP_BufferTriple *
  323. LSUP_triple_serialize (const LSUP_Triple *spo);
  324. /** @brief Initialize internal term pointers in a heap-allocated triple.
  325. *
  326. * Terms are NOT copied. To free them with the triple, use #LSUP_triple_free().
  327. * To only free the triple, use free().
  328. *
  329. * @param spo[in] Triple pointer to initialize.
  330. */
  331. LSUP_rc
  332. LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  333. /** @brief Free the internal pointers of a triple.
  334. *
  335. * @param spo[in] Triple to be freed.
  336. */
  337. void
  338. LSUP_triple_done (LSUP_Triple *spo);
  339. /** @brief Free a triple and all its internal pointers.
  340. *
  341. * NOTE: If the term pointers are not to be freed (e.g. they are owned by a
  342. * back end), use a simple free(spo) instead of this.
  343. *
  344. * @param spo[in] Triple to be freed.
  345. */
  346. void
  347. LSUP_triple_free (LSUP_Triple *spo);
  348. /** @brief Get triple by term position.
  349. *
  350. * Useful for looping over all terms.
  351. *
  352. * @param trp[in] Triple pointer.
  353. *
  354. * @param n[in] A number between 0÷2.
  355. *
  356. * @return Corresponding triple term or NULL if n is out of range.
  357. */
  358. inline LSUP_Term *
  359. LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
  360. {
  361. if (n == TRP_POS_S) return trp->s;
  362. if (n == TRP_POS_P) return trp->p;
  363. if (n == TRP_POS_O) return trp->o;
  364. return NULL;
  365. }
  366. /** @brief Hash a triple.
  367. *
  368. * TODO This doesn't handle blank nodes correctly.
  369. */
  370. inline LSUP_Key
  371. LSUP_triple_hash (const LSUP_Triple *trp)
  372. {
  373. LSUP_BufferTriple *strp = LSUP_triple_serialize (trp);
  374. LSUP_Key hash = LSUP_btriple_hash (strp);
  375. LSUP_btriple_free (strp);
  376. return hash;
  377. }
  378. /** @brief Add an identifier to the term cache.
  379. *
  380. * @param[in] key Hash of the inserted term.
  381. *
  382. * @param[in] term Term to insert. A copy of the term is stored in the cache,
  383. * which is freed on application teardown.
  384. */
  385. LSUP_rc
  386. LSUP_tcache_add (const LSUP_Key key, const LSUP_Term *term);
  387. /** @brief Get an identifier from the cache.
  388. *
  389. * @param[in] key Key for the queried term.
  390. *
  391. * @return The retrieved term if found, or NULL. The string must not be
  392. * modified or freed.
  393. */
  394. const LSUP_Term *
  395. LSUP_tcache_get (LSUP_Key key);
  396. #endif