term.c 22 KB

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