term.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. #include "tpl.h"
  2. #include "term.h"
  3. /** @brief tpl packing format for a term.
  4. *
  5. * The pack elements are: 1. term type (char); 2. data (string); 3. void* type
  6. * metadata, cast to 8-byte unsigned.
  7. */
  8. #define TERM_PACK_FMT "csU"
  9. #define MAX_VALID_TERM_TYPE LSUP_TERM_BNODE /* For type validation. */
  10. /*
  11. * Data structures.
  12. */
  13. /// Sub-match coordinates in IRI parsing results.
  14. typedef struct match_coord_t {
  15. size_t offset; ///< Offset of match from start of string.
  16. size_t size; ///< Length of match.
  17. } MatchCoord;
  18. /// Matching sub-patterns for IRI parts.
  19. struct iri_info_t {
  20. LSUP_NSMap * nsm; ///< NSM handle for prefixed IRI.
  21. MatchCoord prefix; ///< URI prefix (scheme + authority).
  22. MatchCoord path; ///< URI path (including fragment).
  23. MatchCoord frag; ///< URI fragment.
  24. };
  25. /// Key-term pair in term set.
  26. typedef struct keyed_term {
  27. LSUP_Key key; ///< Key (hash) of the term.
  28. LSUP_Term * term; ///< Term handle.
  29. } KeyedTerm;
  30. /** @brief Single link between a term and a term set.
  31. *
  32. * This link is not qualified and must not be used by itself. It belongs
  33. * in a #LSUP_LinkMap which qualifies all links of the same type.
  34. */
  35. typedef struct link {
  36. KeyedTerm * term; ///< Linked term.
  37. LSUP_TermSet * tset; ///< Term set linked to the term.
  38. } Link;
  39. /// Opaque link map iterator.
  40. struct link_map_iter {
  41. const LSUP_LinkMap *map; ///< Link map to iterate.
  42. size_t i; ///< Linking term loop cursor.
  43. size_t j; ///< Term set loop cursor.
  44. LSUP_Term * ext; ///< External link to look for connections.
  45. Link * link; ///< Current link being retrieved.
  46. };
  47. /*
  48. * A link map is thus nested:
  49. *
  50. * - A link map contains a hash map of Link instances (link).
  51. * - Each Link contains a KeyedTerm (term) and a TermSet (tset).
  52. * - Each term set is a hash map of KeyedTerm instances.
  53. * - Each KeyedTerm contains a Term and its hash.
  54. */
  55. typedef struct link_map {
  56. LSUP_LinkType type; ///< Link type.
  57. struct hashmap * links; ///< Map of #Link instances.
  58. } LSUP_LinkMap;
  59. /*
  60. * External variables.
  61. */
  62. uint32_t LSUP_default_dtype_key = 0;
  63. LSUP_Term *LSUP_default_datatype = NULL;
  64. LSUP_TermSet *LSUP_term_cache = NULL;
  65. /*
  66. * Static variables.
  67. */
  68. // Characters not allowed in a URI string.
  69. static const char *invalid_uri_chars = "<>\" {}|\\^`";
  70. /// Minimum valid type code.
  71. static const LSUP_TermType MIN_VALID_TYPE = LSUP_TERM_IRIREF;
  72. /// Maximum valid type code. Change this if adding to enum LSUP_TermType.
  73. static const LSUP_TermType MAX_VALID_TYPE = LSUP_TERM_BNODE;
  74. /*
  75. * Static prototypes.
  76. */
  77. static LSUP_rc
  78. term_init (
  79. LSUP_Term *term, LSUP_TermType type, const char *data, void *metadata);
  80. /*
  81. * Term set callbacks.
  82. */
  83. static uint64_t
  84. tset_hash_fn (
  85. const void *item, uint64_t seed0, uint64_t seed1)
  86. { return ((const KeyedTerm *) item)->key; }
  87. static int
  88. tset_cmp_fn (const void *a, const void *b, void *udata)
  89. {
  90. return
  91. ((const KeyedTerm *) a)->key -
  92. ((const KeyedTerm *) b)->key;
  93. }
  94. static void
  95. tset_free_fn (void *item)
  96. { LSUP_term_free (((KeyedTerm *) item)->term); }
  97. /*
  98. * Link map callbacks.
  99. */
  100. static uint64_t
  101. link_map_hash_fn (
  102. const void *item, uint64_t seed0, uint64_t seed1)
  103. { return ((const Link *)item)->term->key; }
  104. static int
  105. link_map_cmp_fn (const void *a, const void *b, void *udata)
  106. {
  107. return
  108. ((const Link *)a)->term->key -
  109. ((const Link *)b)->term->key;
  110. }
  111. static void
  112. link_map_free_fn (void *item)
  113. {
  114. Link *link = item;
  115. LSUP_term_free (link->term->term);
  116. free (link->term);
  117. LSUP_term_set_free (link->tset);
  118. }
  119. static LSUP_rc parse_iri (char *iri, MatchCoord coords[]);
  120. /*
  121. * Term API.
  122. */
  123. LSUP_Term *
  124. LSUP_term_new (
  125. LSUP_TermType type, const char *data, void *metadata)
  126. {
  127. LSUP_Term *term;
  128. CALLOC_GUARD (term, NULL);
  129. if (UNLIKELY (term_init (
  130. term, type, data, metadata) != LSUP_OK)) {
  131. free (term);
  132. return NULL;
  133. }
  134. return term;
  135. }
  136. LSUP_Term *
  137. LSUP_term_copy (const LSUP_Term *src)
  138. {
  139. void *metadata = NULL;
  140. if (LSUP_IS_IRI (src))
  141. metadata = (void *) LSUP_iriref_nsm (src);
  142. else if (src->type == LSUP_TERM_LITERAL)
  143. metadata = (void *) src->datatype;
  144. else if (src->type == LSUP_TERM_LT_LITERAL) {
  145. metadata = (void *) src->lang;
  146. }
  147. return LSUP_term_new (src->type, src->data, metadata);
  148. }
  149. LSUP_Term *
  150. LSUP_term_new_from_buffer (const LSUP_Buffer *sterm)
  151. {
  152. if (UNLIKELY (!sterm)) return NULL;
  153. LSUP_Term *term = NULL;
  154. LSUP_TermType type = LSUP_TERM_UNDEFINED;
  155. char *data = NULL;
  156. void *metadata;
  157. tpl_node *tn;
  158. tn = tpl_map (TERM_PACK_FMT, &type, &data, &metadata);
  159. if (UNLIKELY (!tn)) goto finally;
  160. if (UNLIKELY (tpl_load (tn, TPL_MEM, sterm->addr, sterm->size) < 0)) {
  161. log_error ("Error loading serialized term.");
  162. goto finally;
  163. }
  164. if (UNLIKELY (tpl_unpack (tn, 0) < 0)) {
  165. log_error ("Error unpacking serialized term.");
  166. goto finally;
  167. }
  168. if (type == LSUP_TERM_LT_LITERAL)
  169. term = LSUP_lt_literal_new (data, (char *)&metadata);
  170. else term = LSUP_term_new (type, data, metadata);
  171. finally:
  172. tpl_free (tn);
  173. free (data);
  174. return term;
  175. }
  176. LSUP_Term *
  177. LSUP_iriref_absolute (const LSUP_Term *root, const LSUP_Term *iri)
  178. {
  179. if (! LSUP_IS_IRI (iri)) {
  180. log_error ("Provided path is not an IRI.");
  181. return NULL;
  182. }
  183. if (! LSUP_IS_IRI (root)) {
  184. log_error ("Provided root is not an IRI.");
  185. return NULL;
  186. }
  187. char
  188. *data,
  189. *pfx = LSUP_iriref_prefix (iri);
  190. if (strlen (pfx) > 0) data = iri->data;
  191. else if (iri->data[0] == '/') {
  192. free (pfx);
  193. pfx = LSUP_iriref_prefix (root);
  194. data = malloc (strlen (iri->data) + strlen (pfx) + 1);
  195. if (!data) return NULL;
  196. sprintf (data, "%s%s", pfx, iri->data);
  197. } else {
  198. data = malloc (strlen (iri->data) + strlen (root->data) + 1);
  199. if (!data) return NULL;
  200. sprintf (data, "%s%s", root->data, iri->data);
  201. }
  202. free (pfx);
  203. LSUP_Term *ret = LSUP_iriref_new (data, NULL);
  204. if (data != iri->data) free (data);
  205. return ret;
  206. }
  207. LSUP_Term *
  208. LSUP_iriref_relative (const LSUP_Term *root, const LSUP_Term *iri)
  209. {
  210. if (! LSUP_IS_IRI (iri)) {
  211. log_error ("Provided path is not an IRI.");
  212. return NULL;
  213. }
  214. if (! LSUP_IS_IRI (root)) {
  215. log_error ("Provided root is not an IRI.");
  216. return NULL;
  217. }
  218. size_t offset = (
  219. strstr (iri->data, root->data) == iri->data ?
  220. strlen (root->data) : 0);
  221. return LSUP_iriref_new (iri->data + offset, LSUP_iriref_nsm (iri));
  222. }
  223. LSUP_Buffer *
  224. LSUP_term_serialize (const LSUP_Term *term)
  225. {
  226. /*
  227. * In serializing a term, the fact that two terms of different types may
  228. * be semantically identical must be taken into account. Specifically, a
  229. * namespace-prefixed IRI ref is identical to its fully qualified version,
  230. * and a LSUP_TERM_LT_LITERAL with no language tag is identical to a
  231. * LSUP_TERM_LITERAL of xsd:string type, made up of the same string. Such
  232. * terms must have identical serializations.
  233. */
  234. if (UNLIKELY (!term)) return NULL;
  235. LSUP_Term *tmp_term;
  236. void *metadata = NULL;
  237. if (term->type == LSUP_TERM_NS_IRIREF) {
  238. // For IRI refs, simply serialize the FQ version of the term.
  239. char *fq_uri;
  240. if (LSUP_nsmap_normalize_uri (
  241. term->iri_info->nsm, term->data, &fq_uri
  242. ) < LSUP_OK) return NULL;
  243. tmp_term = LSUP_iriref_new (fq_uri, NULL);
  244. free (fq_uri);
  245. } else if (term->type == LSUP_TERM_LT_LITERAL) {
  246. // For LT literals with empty lang tag, convert to a normal xsd:string.
  247. if (strlen (term->lang) == 0)
  248. tmp_term = LSUP_literal_new (term->data, NULL);
  249. else tmp_term = LSUP_lt_literal_new (term->data, (char *) term->lang);
  250. } else tmp_term = LSUP_term_new (
  251. term->type, term->data, (void *) term->datatype);
  252. // "datatype" can be anything here since it's cast to void *.
  253. // metadata field is ignored for IRI ref.
  254. if (tmp_term->type == LSUP_TERM_LITERAL)
  255. metadata = tmp_term->datatype;
  256. else if (tmp_term->type == LSUP_TERM_LT_LITERAL)
  257. memcpy (&metadata, tmp_term->lang, sizeof (metadata));
  258. LSUP_Buffer *sterm;
  259. CALLOC_GUARD (sterm, NULL);
  260. //LOG_TRACE("Effective term being serialized: %s", tmp_term->data);
  261. int rc = tpl_jot (
  262. TPL_MEM, &sterm->addr, &sterm->size, TERM_PACK_FMT,
  263. &tmp_term->type, &tmp_term->data, &metadata);
  264. LSUP_term_free (tmp_term);
  265. if (rc != 0) {
  266. LSUP_buffer_free (sterm);
  267. return NULL;
  268. }
  269. return sterm;
  270. }
  271. LSUP_Key
  272. LSUP_term_hash (const LSUP_Term *term)
  273. {
  274. LSUP_Buffer *buf;
  275. if (UNLIKELY (!term)) buf = BUF_DUMMY;
  276. else buf = LSUP_term_serialize (term);
  277. LSUP_Key key = LSUP_buffer_hash (buf);
  278. LSUP_buffer_free (buf);
  279. return key;
  280. }
  281. void
  282. LSUP_term_free (LSUP_Term *term)
  283. {
  284. if (UNLIKELY (!term)) return;
  285. if (LSUP_IS_IRI (term)) free (term->iri_info);
  286. free (term->data);
  287. free (term);
  288. }
  289. LSUP_NSMap *
  290. LSUP_iriref_nsm (const LSUP_Term *iri)
  291. {
  292. if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
  293. log_error ("Term is not a IRI ref type.");
  294. return NULL;
  295. }
  296. return iri->iri_info->nsm;
  297. }
  298. char *
  299. LSUP_iriref_prefix (const LSUP_Term *iri)
  300. {
  301. if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
  302. log_error ("Term is not a IRI ref type.");
  303. return NULL;
  304. }
  305. // if (iri->iri_info->prefix.size == 0) return NULL;
  306. return strndup (
  307. iri->data + iri->iri_info->prefix.offset,
  308. iri->iri_info->prefix.size);
  309. }
  310. char *
  311. LSUP_iriref_path (const LSUP_Term *iri)
  312. {
  313. if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
  314. log_error ("Term is not a IRI ref type.");
  315. return NULL;
  316. }
  317. // if (iri->iri_info->path.size == 0) return NULL;
  318. return strndup (
  319. iri->data + iri->iri_info->path.offset,
  320. iri->iri_info->path.size);
  321. }
  322. char *
  323. LSUP_iriref_frag (const LSUP_Term *iri)
  324. {
  325. if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
  326. log_error ("Term is not a IRI ref type.");
  327. return NULL;
  328. }
  329. // if (iri->iri_info->frag.size == 0) return NULL;
  330. return strndup (
  331. iri->data + iri->iri_info->frag.offset,
  332. iri->iri_info->frag.size);
  333. }
  334. /*
  335. * Triple API.
  336. */
  337. LSUP_Triple *
  338. LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o)
  339. {
  340. LSUP_Triple *spo = malloc (sizeof (*spo));
  341. if (!spo) return NULL;
  342. if (UNLIKELY (LSUP_triple_init (spo, s, p, o))) {
  343. free (spo);
  344. return NULL;
  345. }
  346. return spo;
  347. }
  348. LSUP_Triple *
  349. LSUP_triple_new_from_btriple (const LSUP_BufferTriple *sspo)
  350. {
  351. LSUP_Triple *spo = malloc (sizeof (*spo));
  352. if (!spo) return NULL;
  353. spo->s = LSUP_term_new_from_buffer (sspo->s);
  354. spo->p = LSUP_term_new_from_buffer (sspo->p);
  355. spo->o = LSUP_term_new_from_buffer (sspo->o);
  356. return spo;
  357. }
  358. LSUP_BufferTriple *
  359. LSUP_triple_serialize (const LSUP_Triple *spo)
  360. {
  361. LSUP_BufferTriple *sspo = malloc (sizeof (*sspo));
  362. if (!sspo) return NULL;
  363. sspo->s = LSUP_term_serialize (spo->s);
  364. sspo->p = LSUP_term_serialize (spo->p);
  365. sspo->o = LSUP_term_serialize (spo->o);
  366. return sspo;
  367. }
  368. LSUP_rc
  369. LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o)
  370. {
  371. /* FIXME TRP_DUMMY is a problem here.
  372. if (! LSUP_IS_IRI (s) && s->type != LSUP_TERM_BNODE) {
  373. log_error ("Subject is not of a valid term type: %d", s->type);
  374. return LSUP_VALUE_ERR;
  375. }
  376. if (! LSUP_IS_IRI (p)) {
  377. log_error ("Predicate is not of a valid term type: %d", p->type);
  378. return LSUP_VALUE_ERR;
  379. }
  380. */
  381. spo->s = s;
  382. spo->p = p;
  383. spo->o = o;
  384. return LSUP_OK;
  385. }
  386. void
  387. LSUP_triple_done (LSUP_Triple *spo)
  388. {
  389. if (UNLIKELY (!spo)) return;
  390. LSUP_term_free (spo->s);
  391. LSUP_term_free (spo->p);
  392. LSUP_term_free (spo->o);
  393. }
  394. void
  395. LSUP_triple_free (LSUP_Triple *spo)
  396. {
  397. if (UNLIKELY (!spo)) return;
  398. LSUP_term_free (spo->s);
  399. LSUP_term_free (spo->p);
  400. LSUP_term_free (spo->o);
  401. free (spo);
  402. }
  403. /*
  404. * Multi-add functions.
  405. */
  406. LSUP_TermSet *
  407. LSUP_term_set_new ()
  408. {
  409. // Capacity of 4 is an arbitrary guess.
  410. LSUP_TermSet *ts = hashmap_new (
  411. sizeof (KeyedTerm), 4, LSUP_HASH_SEED, 0,
  412. tset_hash_fn, tset_cmp_fn, tset_free_fn, NULL);
  413. if (UNLIKELY (hashmap_oom (ts))) return NULL;
  414. return ts;
  415. }
  416. LSUP_rc
  417. LSUP_term_set_add (LSUP_TermSet *ts, LSUP_Term *term, LSUP_Term **existing)
  418. {
  419. LSUP_Hash key = LSUP_term_hash (term);
  420. KeyedTerm entry_s = {.key=key, .term=term};
  421. KeyedTerm *ex = hashmap_get (ts, &entry_s);
  422. if (ex) {
  423. if (existing) *existing = ex->term;
  424. return LSUP_NOACTION;
  425. }
  426. hashmap_set (ts, &entry_s);
  427. if (hashmap_oom (ts)) return LSUP_MEM_ERR;
  428. return LSUP_OK;
  429. }
  430. const LSUP_Term *
  431. LSUP_term_set_get (LSUP_TermSet *ts, LSUP_Key key)
  432. {
  433. KeyedTerm *entry = hashmap_get (ts, &(KeyedTerm){.key=key});
  434. if (entry) LOG_TRACE("ID found for key %lx: %s", key, entry->term->data);
  435. else LOG_TRACE("No ID found for key %lx.", key);
  436. return (entry) ? entry->term : NULL;
  437. }
  438. LSUP_rc
  439. LSUP_term_set_next (LSUP_TermSet *ts, size_t *i, LSUP_Term **term)
  440. {
  441. KeyedTerm *kt = NULL;
  442. if (!hashmap_iter (ts, i, (void **)&kt)) return LSUP_END;
  443. if (term) *term = kt->term;
  444. return LSUP_OK;
  445. }
  446. void
  447. LSUP_term_set_free (LSUP_TermSet *ts)
  448. { hashmap_free (ts); }
  449. LSUP_LinkMap *
  450. LSUP_link_map_new (LSUP_LinkType type)
  451. {
  452. LSUP_LinkMap *cm;
  453. MALLOC_GUARD (cm, NULL);
  454. cm->type = type;
  455. cm->links = hashmap_new (
  456. sizeof (Link), 0, LSUP_HASH_SEED, 0,
  457. link_map_hash_fn, link_map_cmp_fn, link_map_free_fn, NULL);
  458. return cm;
  459. }
  460. void
  461. LSUP_link_map_free (LSUP_LinkMap *cm)
  462. {
  463. hashmap_free (cm->links);
  464. free (cm);
  465. }
  466. LSUP_LinkType
  467. LSUP_link_map_type (const LSUP_LinkMap *map)
  468. { return map->type; }
  469. // TODO Memory error handling.
  470. LSUP_rc
  471. LSUP_link_map_add (
  472. LSUP_LinkMap *cmap, LSUP_Term *term, LSUP_TermSet *tset)
  473. {
  474. // Keyed term to look up the link term and insert it, if necessary.
  475. KeyedTerm entry_s = {.key=LSUP_term_hash (term), .term=term};
  476. Link *ex = hashmap_get (cmap->links, &(Link){.term=&entry_s});
  477. if (ex) {
  478. // Add terms one by one to the existing term set.
  479. LOG_TRACE(
  480. "Linking term %s exists. Adding individual terms.",
  481. ex->term->term->data);
  482. size_t i = 0;
  483. KeyedTerm *kt;
  484. while (hashmap_iter (tset, &i, (void **)&kt)) {
  485. LOG_TRACE(
  486. "Adding term %s to link %s",
  487. kt->term->data, ex->term->term->data);
  488. if (hashmap_get (ex->tset, kt))
  489. // Term already exist, free the new one and move on.
  490. LSUP_term_free (kt->term);
  491. else
  492. // Insert KeyedTerm, the term set now owns the underlying term.
  493. hashmap_set (ex->tset, kt);
  494. }
  495. // Free link term that hasn't been used.
  496. LSUP_term_free (term);
  497. } else {
  498. // Add the new term and the termset wholesale.
  499. LOG_TRACE("Adding new linking term %s.", term->data);
  500. // Allocate inserted member on heap, it will be owned by the map.
  501. KeyedTerm *ins;
  502. MALLOC_GUARD (ins, LSUP_MEM_ERR);
  503. memcpy (ins, &entry_s, sizeof (entry_s));
  504. Link link = {.term=ins, .tset=tset};
  505. hashmap_set (cmap->links, &link);
  506. }
  507. return LSUP_OK;
  508. }
  509. LSUP_LinkMapIterator *
  510. LSUP_link_map_iter_new (const LSUP_LinkMap *lmap, LSUP_Term *ext)
  511. {
  512. LSUP_LinkMapIterator *it;
  513. CALLOC_GUARD (it, NULL);
  514. it->map = lmap;
  515. it->ext = ext;
  516. return it;
  517. }
  518. void
  519. LSUP_link_map_iter_free (LSUP_LinkMapIterator *it)
  520. { free (it); }
  521. LSUP_rc
  522. LSUP_link_map_next (
  523. LSUP_LinkMapIterator *it, LSUP_Term **lt, LSUP_TermSet **ts)
  524. {
  525. if (!hashmap_iter (it->map->links, &it->i, (void **)&it->link))
  526. return LSUP_END;
  527. *lt = it->link->term->term;
  528. *ts = it->link->tset;
  529. return LSUP_OK;
  530. }
  531. // TODO dismantle if the only triple generator is for the graph.
  532. LSUP_rc
  533. LSUP_link_map_triples (
  534. LSUP_LinkMapIterator *it, LSUP_Triple *spo)
  535. {
  536. // Assign external (related) term.
  537. if (it->map->type == LSUP_LINK_INBOUND)
  538. spo->o = it->ext;
  539. else if (it->map->type == LSUP_LINK_OUTBOUND)
  540. spo->s = it->ext;
  541. else spo->p = it->ext;
  542. KeyedTerm *kt;
  543. // If we are already handling a link, continue the internal loop.
  544. if (it->link) goto int_loop;
  545. ext_loop:
  546. // Advance external counter and start new internal loop.
  547. it->j = 0;
  548. if (!hashmap_iter (it->map->links, &it->i, (void **)&it->link))
  549. return LSUP_END;
  550. int_loop:
  551. // If end of the term set is reached, start with a new linking term.
  552. if (!hashmap_iter (it->link->tset, &it->j, (void **)&kt)) goto ext_loop;
  553. // Continue pulling from term set.
  554. // Assign linking term.
  555. if (it->map->type == LSUP_LINK_EDGE) spo->s = it->link->term->term;
  556. else spo->p = it->link->term->term;
  557. // Assign term in term set.
  558. if (it->map->type == LSUP_LINK_INBOUND) spo->s = kt->term;
  559. else spo->o = kt->term;
  560. return LSUP_OK;
  561. }
  562. /*
  563. * Static functions.
  564. */
  565. static LSUP_rc
  566. term_init (
  567. LSUP_Term *term, LSUP_TermType type,
  568. const char *data, void *metadata)
  569. {
  570. // Exit early if environment is not initialized.
  571. // EXCEPT for IRIRef which is used inside of LSUP_init().
  572. if (!LSUP_IS_INIT && type != LSUP_TERM_IRIREF)
  573. return LSUP_ENV_ERR;
  574. // Undefined type. Make quick work of it.
  575. if (type == LSUP_TERM_UNDEFINED) {
  576. term->type = type;
  577. if (data) {
  578. term->data = malloc (strlen (data) + 1);
  579. if (UNLIKELY (!term->data)) return LSUP_MEM_ERR;
  580. strcpy (term->data, data);
  581. }
  582. return LSUP_OK;
  583. }
  584. if (type < MIN_VALID_TYPE || type > MAX_VALID_TYPE) {
  585. log_error ("%d is not a valid term type.", type);
  586. return LSUP_VALUE_ERR;
  587. }
  588. term->type = type;
  589. if (data) {
  590. // Validate IRI.
  591. if (LSUP_IS_IRI (term)) {
  592. char *fquri;
  593. // Find fully qualified IRI to parse.
  594. if (term->type == LSUP_TERM_NS_IRIREF) {
  595. if (LSUP_nsmap_normalize_uri (metadata, data, &fquri) < 0) {
  596. log_error ("Error normalizing IRI data.");
  597. return LSUP_VALUE_ERR;
  598. }
  599. LOG_DEBUG("Fully qualified IRI: %s", fquri);
  600. } else fquri = (char *) data;
  601. if (strpbrk (fquri, invalid_uri_chars) != NULL) {
  602. log_warn (
  603. "Characters %s are not valid in a URI. Got: %s\n",
  604. invalid_uri_chars, fquri);
  605. #if 0
  606. // TODO This causes W3C TTL test #29 to fail. Remove?
  607. return LSUP_VALUE_ERR;
  608. #endif
  609. }
  610. // Capture interesting IRI parts.
  611. MatchCoord matches[7] = {}; // Initialize all to 0.
  612. if (UNLIKELY (parse_iri (fquri, matches) != LSUP_OK)) {
  613. log_error ("Error matching URI pattern.");
  614. return LSUP_VALUE_ERR;
  615. }
  616. if (term->type == LSUP_TERM_NS_IRIREF) free (fquri);
  617. MALLOC_GUARD (term->iri_info, LSUP_MEM_ERR);
  618. term->iri_info->prefix = matches[1];
  619. term->iri_info->path = matches[4];
  620. term->iri_info->frag = matches[6];
  621. term->iri_info->nsm = metadata;
  622. }
  623. term->data = strdup (data);
  624. } else {
  625. // No data. Make up a random UUID or URI if allowed.
  626. if (type == LSUP_TERM_IRIREF || type == LSUP_TERM_BNODE) {
  627. uuid_t uuid;
  628. uuid_generate_random (uuid);
  629. uuid_str_t uuid_str;
  630. uuid_unparse_lower (uuid, uuid_str);
  631. if (type == LSUP_TERM_IRIREF) {
  632. term->data = malloc (UUID4_URN_SIZE);
  633. snprintf (
  634. term->data, UUID4_URN_SIZE, "urn:uuid4:%s", uuid_str);
  635. MALLOC_GUARD (term->iri_info, LSUP_MEM_ERR);
  636. // Allocate IRI match patterns manually.
  637. term->iri_info->prefix.offset = 0;
  638. term->iri_info->prefix.size = 4;
  639. term->iri_info->path.offset = 4;
  640. term->iri_info->path.size = UUIDSTR_SIZE + 6;
  641. term->iri_info->frag.offset = 0;
  642. term->iri_info->frag.size = 0;
  643. term->iri_info->nsm = NULL;
  644. } else term->data = strdup (uuid_str);
  645. } else {
  646. log_error ("No data provided for term.");
  647. return LSUP_VALUE_ERR;
  648. }
  649. }
  650. if (term->type == LSUP_TERM_LT_LITERAL) {
  651. if (!metadata) {
  652. log_warn ("Lang tag is NULL. Creating a non-tagged literal.");
  653. term->type = LSUP_TERM_LITERAL;
  654. } else {
  655. char *lang_str = (char *) metadata;
  656. LOG_TRACE("Lang string: '%s'", lang_str);
  657. // Lang tags longer than 7 characters will be truncated.
  658. strncpy(term->lang, lang_str, sizeof (term->lang) - 1);
  659. if (strlen (term->lang) < 1) {
  660. log_error ("Lang tag cannot be an empty string.");
  661. return LSUP_VALUE_ERR;
  662. }
  663. term->lang[7] = '\0';
  664. }
  665. }
  666. if (term->type == LSUP_TERM_LITERAL) {
  667. term->datatype = metadata;
  668. if (! term->datatype) term->datatype = LSUP_default_datatype;
  669. LOG_TRACE("Storing data type: %s", term->datatype->data);
  670. if (! LSUP_IS_IRI (term->datatype)) {
  671. log_error (
  672. "Literal data type is not an IRI: %s",
  673. term->datatype->data);
  674. return LSUP_VALUE_ERR;
  675. }
  676. LSUP_Term *ex = NULL;
  677. LSUP_term_set_add (LSUP_term_cache, term->datatype, &ex);
  678. if (ex && ex != term->datatype) {
  679. // Replace datatype handle with the one in term cache, and free
  680. // the new one.
  681. if (term->datatype != LSUP_default_datatype)
  682. LSUP_term_free (term->datatype);
  683. term->datatype = ex;
  684. }
  685. //LOG_TRACE("Datatype address: %p", term->datatype);
  686. LOG_TRACE("Datatype hash: %lx", LSUP_term_hash (term->datatype));
  687. } else if (term->type == LSUP_TERM_BNODE) {
  688. // TODO This is not usable for global skolemization.
  689. term->bnode_id = LSUP_HASH (
  690. term->data, strlen (term->data) + 1, LSUP_HASH_SEED);
  691. }
  692. return LSUP_OK;
  693. }
  694. /**
  695. * @brief scan an IRI string and parse IRI parts.
  696. *
  697. * Experimental replacement of a regex engine for better performance.
  698. *
  699. * Slightly adapted from regex on
  700. * https://datatracker.ietf.org/doc/html/rfc3986#appendix-B to capture relevant
  701. * parts of the IRI.
  702. *
  703. * Reference regex and group numbering:
  704. * ^((?([^:/?#]+):)?(?//([^/?#]*))?)((?[^?#]*)(?\?([^#]*))?(?#(.*))?)
  705. * 1 2 3 4 5 6
  706. *
  707. * Capturing groups:
  708. *
  709. * #0: Full parsed URI (http://example.org/123/456/?query=blah#frag)
  710. * #1: Prefix (http://example.org)
  711. * #2: Scheme (http)
  712. * #3: Authority (example.org)
  713. * #4: Path, including query and fragment (/123/456/?query=blah#frag)
  714. * #5: Query (query=blah)
  715. * #6: Fragment (frag)
  716. *
  717. *
  718. * @param iri_str[in] IRI string to parse.
  719. *
  720. * @param match_coord_t[out] coord Coordinates to be stored. This must be a
  721. * pre-allocated array of at least 7 elements.
  722. *
  723. * The first size_t of each element stores the relative position of a match,
  724. * and the second one stores the length of the match. A length of 0 indicates
  725. * no match.
  726. */
  727. static LSUP_rc
  728. parse_iri (char *iri_str, MatchCoord coord[]) {
  729. char *cur = iri_str;
  730. size_t iri_len = strlen (iri_str);
  731. MatchCoord tmp = {}; // Temporary storage for capture groups
  732. // Redundant if only called by term_init.
  733. // memset (coord, 0, sizeof(*coord));
  734. //LOG_DEBUG("Parsing IRI: %s", iri_str);
  735. // #2: ([^:/?#]+)
  736. while (
  737. *cur != ':' && *cur != '/' && *cur != '?'
  738. && *cur != '#' && *cur != '\0') {
  739. tmp.size++;
  740. cur++;
  741. }
  742. // Non-capturing: (?([^:/?#]+):)?
  743. if (tmp.size > 0 && *cur == ':') {
  744. // Got capture groups #2 and #3. Store them.
  745. coord[2].offset = 0;
  746. coord[2].size = tmp.size;
  747. cur++;
  748. //LOG_DEBUG("Group #2: %lu, %lu", coord[2].offset, coord[2].size);
  749. } else cur = iri_str; // Backtrack if no match.
  750. // Non-capturing: (?//([^/?#]*))?
  751. if (*cur == '/' && *(cur + 1) == '/') {
  752. cur += 2;
  753. tmp.offset = cur - iri_str;
  754. tmp.size = 0;
  755. // #3: ([^/?#]*)
  756. while (*cur != '/' && *cur != '?' && *cur != '#' && *cur != '\0') {
  757. tmp.size++;
  758. cur++;
  759. }
  760. coord[3].offset = tmp.offset;
  761. coord[3].size = tmp.size;
  762. //LOG_DEBUG("Group #3: %lu, %lu", coord[3].offset, coord[3].size);
  763. }
  764. // Capture group 1.
  765. coord[1].offset = 0;
  766. coord[1].size = cur - iri_str;
  767. //LOG_DEBUG("Group #1: %lu, %lu", coord[1].offset, coord[1].size);
  768. tmp.offset = cur - iri_str;
  769. tmp.size = 0;
  770. coord[4].offset = tmp.offset;
  771. coord[4].size = iri_len - tmp.offset;
  772. //LOG_DEBUG("Group #4: %lu, %lu", coord[4].offset, coord[4].size);
  773. // Non-capturing: (?[^?#]*)
  774. while (*cur != '?' && *cur != '#' && *cur != '\0') {
  775. tmp.size++;
  776. cur++;
  777. }
  778. // Non-capturing: (?\?([^#]*))
  779. if (*cur == '?') {
  780. // 5: ([^#]*)
  781. tmp.offset = ++cur - iri_str;
  782. tmp.size = 0;
  783. while (*cur != '#' && *cur != '\0') {
  784. tmp.size++;
  785. cur++;
  786. }
  787. if (tmp.size > 0) {
  788. // Got capture group #5.
  789. coord[5].offset = tmp.offset;
  790. coord[5].size = tmp.size;
  791. //LOG_DEBUG("Group #5: %lu, %lu", coord[5].offset, coord[5].size);
  792. }
  793. }
  794. // Non-capturing: (?#(.*))?
  795. if (*cur == '#') {
  796. // #6: (.*)
  797. coord[6].offset = ++cur - iri_str;
  798. coord[6].size = iri_str + iri_len - cur;
  799. //LOG_DEBUG("Group #6: %lu, %lu", coord[6].offset, coord[6].size);
  800. }
  801. coord[0].offset = 0;
  802. coord[0].size = iri_len;
  803. //LOG_DEBUG("Full match: %lu, %lu", coord[0].offset, coord[0].size);
  804. return LSUP_OK;
  805. }
  806. /*
  807. * Extern inline functions.
  808. */
  809. LSUP_Key LSUP_term_hash (const LSUP_Term *term);
  810. LSUP_Term *LSUP_iriref_new (const char *data, LSUP_NSMap *nsm);
  811. LSUP_Term *LSUP_literal_new (const char *data, LSUP_Term *datatype);
  812. LSUP_Term *LSUP_lt_literal_new (const char *data, char *lang);
  813. LSUP_Term *LSUP_bnode_new (const char *data);
  814. bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2);
  815. LSUP_Term *LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n);
  816. LSUP_Key LSUP_triple_hash (const LSUP_Triple *trp);