term.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. /// Connection type: inbound or outbound.
  84. typedef enum {
  85. LSUP_CONN_INBOUND, ///< Inbound connection.
  86. LSUP_CONN_OUTBOUND, ///< Outbound connection.
  87. } LSUP_ConnectionType;
  88. /** @brief Connection list.
  89. *
  90. * A list of predicates and related lists of terms, that can be used to list
  91. * inbound or outbound connections to a node.
  92. *
  93. * Each term in the NUL-terminated `p` list represent a predicate which is
  94. * paired with a list of terms in the `tl` list. The index of each predicate
  95. * corresponds to the same index of a term list.
  96. *
  97. * If the type of the connection list is `LSUP_CONN_INBOUND`, the term list
  98. * represent subjects and a term that is associated with the connection list is
  99. * the related object; if `LSUP_CONN_OUTBOUND`, the term list represents
  100. * objects, and a term that is associated with the connection list represents
  101. * the subject.
  102. *
  103. */
  104. typedef struct {
  105. LSUP_ConnectionType type; ///< Inbound or outbound connection.
  106. LSUP_Term ** p; ///< NUL-terminated array of term handles.
  107. LSUP_Term *** tl; /**<
  108. * NUL-terminated array of
  109. * NUL-terminated arrays of term handles.
  110. */
  111. } LSUP_ConnectionList;
  112. /*
  113. * Extern variables.
  114. */
  115. /** @brief Global term cache.
  116. *
  117. * Stores frequently used terms, e.g. data type URIs.
  118. */
  119. extern struct hashmap *LSUP_term_cache;
  120. /** @brief Compiled hash of default literal data type.
  121. */
  122. extern uint32_t LSUP_default_dtype_key;
  123. /** @brief URI validation pattern, compiled in #LSUP_init().
  124. */
  125. extern regex_t *LSUP_uri_ptn;
  126. /** @brief Default literal data type URI.
  127. *
  128. * Literal terms created with undefined data type will have it set to this
  129. * URI implicitly.
  130. */
  131. extern LSUP_Term *LSUP_default_datatype;
  132. /*
  133. * API functions.
  134. */
  135. /** @brief Create a new term.
  136. *
  137. * This is a generic function; it is recommended to use specialized functions
  138. * such as #LSUP_term_new(), #LSUP_literal_new(), etc. as they have strict type
  139. * checks for the metadata parameter.
  140. *
  141. * @param type[in] Term type. One of #LSUP_TermType.
  142. *
  143. * @param data[in] Term data: textual URI, literal value without data type
  144. * or langtag, etc. It may be NULL for IRI refs and BNodes, in which case a
  145. * random identifier is generated.
  146. *
  147. * @param metadata[in] Namespace map (LSUP_NSMap *) for IRI refs; language tag
  148. * (LSUP_LangTag *) for language-tagged literals; or data type (LSUP_Term *)
  149. * for other literals. It may be NULL.
  150. *
  151. * @return New term, which must be freed with #LSUP_term_free after use; or
  152. * NULL on error.
  153. */
  154. LSUP_Term *
  155. LSUP_term_new (LSUP_TermType type, const char *data, void *metadata);
  156. /** @brief Placeholder term to use with LSUP_term_reset.
  157. */
  158. #define TERM_DUMMY LSUP_term_new (LSUP_TERM_UNDEFINED, NULL, NULL)
  159. /** @brief Shortcut to create an IRI reference.
  160. *
  161. * Must be freed with #LSUP_term_free.
  162. *
  163. * @param data[in] The URI string. If NULL, a UUID4-based URN is generated.
  164. * This cannot be NULL if the nsm parameter is not NULL.
  165. *
  166. * @param nsm[in] Namespace map. If not NULL, a namespace-prefixed
  167. * (#LSUP_TERM_NS_IRIREF) is created, otherwise a regular one
  168. * (#LSUP_TERM_IRIREF).
  169. *
  170. * @return same as #LSUP_term_new().
  171. */
  172. inline LSUP_Term *
  173. LSUP_iriref_new (const char *data, LSUP_NSMap *nsm)
  174. {
  175. return (
  176. nsm ? LSUP_term_new (LSUP_TERM_NS_IRIREF, data, nsm) :
  177. LSUP_term_new (LSUP_TERM_IRIREF, data, NULL));
  178. }
  179. /** @brief Create a new absolute IRI from a path relative to a root IRI.
  180. *
  181. * The term is always of type LSUP_TERM_IRIREF (i.e. not namespace-prefixed).
  182. *
  183. * If the provided IRI is already a fully qualified IRI (i.e. it has a prefix)
  184. * the result is semantically identical to the input.
  185. *
  186. * If the relative IRI begins with a '/', the resulting IRI is relative to the
  187. * web root of the root IRI. I.e. if a root IRI has a path after the webroot,
  188. * it is ignored.
  189. *
  190. * Otherwise, the resulting IRI is relative to the full root string.
  191. *
  192. * @param[in] root Root IRI that the new IRI should be relative to.
  193. *
  194. * @param[in] iri Term with an IRI relative to the webroot.
  195. *
  196. * @return New absolute IRI, or NULL if either term is not an IRI.
  197. */
  198. LSUP_Term *
  199. LSUP_iriref_absolute (const LSUP_Term *root, const LSUP_Term *iri);
  200. /** @brief Create a new relative IRI from an absolute IRI and a web root IRI.
  201. *
  202. * This works with namespace-prefixed IRIs and returns a term of the same type
  203. * as the input.
  204. *
  205. * @param[in] iri Full IRI.
  206. *
  207. * @param[in] root Root IRI that the new IRI should be relative to.
  208. *
  209. * @return New IRI, or NULL if either term is not an IRI. If the input IRI is
  210. * not a path under the root IRI, the result will be identical to the input.
  211. */
  212. LSUP_Term *
  213. LSUP_iriref_relative (const LSUP_Term *root, const LSUP_Term *iri);
  214. /** @brief Shortcut to create a literal term.
  215. *
  216. * Must be freed with #LSUP_term_free.
  217. *
  218. * @param data[in] The literal string.
  219. *
  220. * @param datatype[in] Data type URI string. If NULL, the default data type
  221. * (xsd:string) is used. The new term takes ownership of the pointer.
  222. *
  223. * @return same as #LSUP_term_new().
  224. */
  225. inline LSUP_Term *
  226. LSUP_literal_new (const char *data, LSUP_Term *datatype)
  227. { return LSUP_term_new (LSUP_TERM_LITERAL, data, datatype); }
  228. /** @brief Shortcut to create a language-tagged literal term.
  229. *
  230. * Must be freed with #LSUP_term_free.
  231. *
  232. * @param data[in] The literal string.
  233. *
  234. * @param lang[in] Language tag string.
  235. *
  236. * @return same as #LSUP_term_new().
  237. */
  238. inline LSUP_Term *
  239. LSUP_lt_literal_new (const char *data, char *lang)
  240. { return LSUP_term_new (LSUP_TERM_LT_LITERAL, data, lang); }
  241. /** @brief Shortcut to create a blank node.
  242. *
  243. * Must be freed with #LSUP_term_free.
  244. *
  245. * @param data[in] The BNode identifier.
  246. *
  247. * @return same as #LSUP_term_new().
  248. */
  249. inline LSUP_Term *
  250. LSUP_bnode_new (const char *data)
  251. { return LSUP_term_new (LSUP_TERM_BNODE, data, NULL); }
  252. /** @brief Copy a term.
  253. *
  254. * @param[in] src The term to copy.
  255. *
  256. * @return A new duplicate term handle.
  257. */
  258. LSUP_Term *
  259. LSUP_term_copy (const LSUP_Term *src);
  260. /** @brief Deserialize a buffer into a term.
  261. *
  262. * @param[in] sterm Buffer to convert into a term. It must be a valid
  263. * serialized term from store or obtained with #LSUP_term_serialize().
  264. *
  265. * @return New term handle. It must be freed with #LSUP_term_free().
  266. */
  267. LSUP_Term *
  268. LSUP_term_new_from_buffer (const LSUP_Buffer *sterm);
  269. /** @brief Serialize a term into a buffer.
  270. *
  271. * @param[in] sterm Term to convert into a buffer.
  272. *
  273. * @return New buffer handle. It must be freed with #LSUP_buffer_free().
  274. */
  275. LSUP_Buffer *
  276. LSUP_term_serialize (const LSUP_Term *term);
  277. /** @brief Hash a buffer.
  278. */
  279. LSUP_Key
  280. LSUP_term_hash (const LSUP_Term *term);
  281. /** @brief Compare two terms.
  282. *
  283. * The terms evaluate as equal if their hashes are equal—i.e. if they are
  284. * semantically equivalent.
  285. */
  286. inline bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2)
  287. { return LSUP_term_hash (term1) == LSUP_term_hash (term2); }
  288. void
  289. LSUP_term_free (LSUP_Term *term);
  290. /** @brief Namespace map of a IRI ref.
  291. *
  292. * @param[in] iri IRI reference handle.
  293. *
  294. * @return A pointer to the namespace map associated with the IRI. It is
  295. * freed at program shutdown.
  296. */
  297. LSUP_NSMap *
  298. LSUP_iriref_nsm (const LSUP_Term *iri);
  299. /** @brief Get the prefix portion of a IRI ref.
  300. *
  301. * @param[in] iri IRI reference handle.
  302. *
  303. * @return String containing the protocol and domain name part of the IRI. It
  304. * should be freed after use.
  305. */
  306. char *
  307. LSUP_iriref_prefix (const LSUP_Term *iri);
  308. /** @brief Get the path portion of a IRI ref.
  309. *
  310. * @param[in] iri IRI reference handle.
  311. *
  312. * @return String containing the path of the IRI relative to the web root. For
  313. * a URN, such as `urn:myns:myid`, it would be `myns:myid`. This string should
  314. * be freed after use.
  315. */
  316. char *
  317. LSUP_iriref_path (const LSUP_Term *iri);
  318. /** @brief Get the fragment portion of a IRI ref.
  319. *
  320. * @param[in] iri IRI reference handle.
  321. *
  322. * @return String containing the fragment part of the IRI, or NULL if the IRI
  323. * contains no fragment. It should be freed after use.
  324. */
  325. char *
  326. LSUP_iriref_frag (const LSUP_Term *iri);
  327. /*
  328. * TRIPLES
  329. */
  330. /** @brief Create a new triple from three terms.
  331. *
  332. * Terms are NOT copied. To free them with the triple, use #LSUP_triple_free().
  333. * To only free the triple, use free().
  334. *
  335. * TODO Term types are not validated at the moment.
  336. *
  337. * @param[in] s Triple subject. It must be an IRIRef or BNode.
  338. *
  339. * @param[in] p Triple predicate. It must be an IRIRef.
  340. *
  341. * @param[in] o Triple object.
  342. *
  343. */
  344. LSUP_Triple *
  345. LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  346. /** @brief Dummy triple with NULL slots. It is not a valid triple.
  347. */
  348. #define TRP_DUMMY LSUP_triple_new (NULL, NULL, NULL)
  349. LSUP_Triple *
  350. LSUP_triple_new_from_btriple (const LSUP_BufferTriple *sspo);
  351. LSUP_BufferTriple *
  352. LSUP_triple_serialize (const LSUP_Triple *spo);
  353. /** @brief Initialize internal term pointers in a heap-allocated triple.
  354. *
  355. * Terms are NOT copied. To free them with the triple, use #LSUP_triple_free().
  356. * To only free the triple, use free().
  357. *
  358. * @param spo[in] Triple pointer to initialize.
  359. */
  360. LSUP_rc
  361. LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o);
  362. /** @brief Free the internal pointers of a triple.
  363. *
  364. * @param spo[in] Triple to be freed.
  365. */
  366. void
  367. LSUP_triple_done (LSUP_Triple *spo);
  368. /** @brief Free a triple and all its internal pointers.
  369. *
  370. * NOTE: If the term pointers are not to be freed (e.g. they are owned by a
  371. * back end), use a simple free(spo) instead of this.
  372. *
  373. * @param spo[in] Triple to be freed.
  374. */
  375. void
  376. LSUP_triple_free (LSUP_Triple *spo);
  377. /** @brief Get triple by term position.
  378. *
  379. * Useful for looping over all terms.
  380. *
  381. * @param trp[in] Triple pointer.
  382. *
  383. * @param n[in] A number between 0÷2.
  384. *
  385. * @return Corresponding triple term or NULL if n is out of range.
  386. */
  387. inline LSUP_Term *
  388. LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n)
  389. {
  390. if (n == TRP_POS_S) return trp->s;
  391. if (n == TRP_POS_P) return trp->p;
  392. if (n == TRP_POS_O) return trp->o;
  393. return NULL;
  394. }
  395. /** @brief Hash a triple.
  396. *
  397. * TODO This doesn't handle blank nodes correctly.
  398. */
  399. inline LSUP_Key
  400. LSUP_triple_hash (const LSUP_Triple *trp)
  401. {
  402. LSUP_BufferTriple *strp = LSUP_triple_serialize (trp);
  403. LSUP_Key hash = LSUP_btriple_hash (strp);
  404. LSUP_btriple_free (strp);
  405. return hash;
  406. }
  407. /** @brief Add an identifier to the term cache.
  408. *
  409. * @param[in] key Hash of the inserted term.
  410. *
  411. * @param[in] term Term to insert. A copy of the term is stored in the cache,
  412. * which is freed on application teardown.
  413. */
  414. LSUP_rc
  415. LSUP_tcache_add (const LSUP_Key key, const LSUP_Term *term);
  416. /** @brief Get an identifier from the cache.
  417. *
  418. * @param[in] key Key for the queried term.
  419. *
  420. * @return The retrieved term if found, or NULL. The string must not be
  421. * modified or freed.
  422. */
  423. const LSUP_Term *
  424. LSUP_tcache_get (LSUP_Key key);
  425. /** @brief Add term to a term list.
  426. *
  427. * @param[in] tl Array of term handles to be added to. The handle must be NUL-
  428. * terminated. On success, this handle will be reallocated and the new address
  429. * returned, so the passed handle should no longer be used. On failure, it
  430. * remains unchanged and may be reused.
  431. *
  432. * @param[in] t Term to be added to the list. The object list will take
  433. * ownership of the term.
  434. *
  435. * @return Reallocated list on success; NULL on failure.
  436. */
  437. LSUP_Term **
  438. LSUP_term_list_add (LSUP_Term **tl, LSUP_Term *t);
  439. /** @brief New predicate-object list.
  440. *
  441. * The initial state of the returned list is: `{p: [NULL], tl: [NULL]}`
  442. *
  443. * Predicates and term lists can be added with #LSUP_conn_list_add, and terms
  444. * can be added to a term list with #LSUP_term_list_add.
  445. *
  446. * @return a new empty predicate-object list.
  447. */
  448. LSUP_ConnectionList *
  449. LSUP_conn_list_list_new (LSUP_ConnectionType type);
  450. /** @brief Free a predicate-object list.
  451. *
  452. * All arrays and term handles are recursively freed.
  453. *
  454. * @param[in] pol Predicate-object list handle obtained with
  455. * #LSUP_conn_list_list_new().
  456. */
  457. void
  458. LSUP_conn_list_list_free (LSUP_ConnectionList *pol);
  459. /** @brief Add a predicate-object list pair to a PO list.
  460. *
  461. * @param[in] pol Predicate-object list handle obtained with
  462. * #LSUP_conn_list_list_new().
  463. *
  464. * @param[in] p Predicate to be associated with the given object list. The
  465. * PO structure takes ownership of the term.
  466. *
  467. * @param[in] o NULL-terminated array of object term handles to be associated
  468. * with the given predicate. The PO structire takes ownership of the whole
  469. * term array.
  470. *
  471. * @return LSUP_OK on success; LSUP_MEM_ERR on allocation error.
  472. */
  473. LSUP_rc
  474. LSUP_conn_list_list_add (LSUP_ConnectionList *pol, LSUP_Term *p, LSUP_Term **o);
  475. #endif