term.h 12 KB

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