term.c 22 KB

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