term.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. int rc = tpl_jot (
  251. TPL_MEM, &sterm->addr, &sterm->size, TERM_PACK_FMT,
  252. &tmp_term->type, &tmp_term->data, &metadata);
  253. LSUP_term_free (tmp_term);
  254. if (rc != 0) {
  255. LSUP_buffer_free (sterm);
  256. return NULL;
  257. }
  258. return sterm;
  259. }
  260. LSUP_Key
  261. LSUP_term_hash (const LSUP_Term *term)
  262. {
  263. LSUP_Buffer *buf;
  264. if (UNLIKELY (!term)) buf = BUF_DUMMY;
  265. else buf = LSUP_term_serialize (term);
  266. LSUP_Key key = LSUP_buffer_hash (buf);
  267. LSUP_buffer_free (buf);
  268. return key;
  269. }
  270. void
  271. LSUP_term_free (LSUP_Term *term)
  272. {
  273. if (UNLIKELY (!term)) return;
  274. if (LSUP_IS_IRI (term)) free (term->iri_info);
  275. free (term->data);
  276. free (term);
  277. }
  278. LSUP_NSMap *
  279. LSUP_iriref_nsm (const LSUP_Term *iri)
  280. {
  281. if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
  282. log_error ("Term is not a IRI ref type.");
  283. return NULL;
  284. }
  285. return iri->iri_info->nsm;
  286. }
  287. char *
  288. LSUP_iriref_prefix (const LSUP_Term *iri)
  289. {
  290. if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
  291. log_error ("Term is not a IRI ref type.");
  292. return NULL;
  293. }
  294. if (iri->iri_info->prefix.rm_so == -1) return NULL;
  295. size_t len = iri->iri_info->prefix.rm_eo - iri->iri_info->prefix.rm_so;
  296. if (len == 0) return NULL;
  297. return strndup (iri->data + iri->iri_info->prefix.rm_so, len);
  298. }
  299. char *
  300. LSUP_iriref_path (const LSUP_Term *iri)
  301. {
  302. if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
  303. log_error ("Term is not a IRI ref type.");
  304. return NULL;
  305. }
  306. if (iri->iri_info->path.rm_so == -1) return NULL;
  307. size_t len = iri->iri_info->path.rm_eo - iri->iri_info->path.rm_so;
  308. if (len == 0) return NULL;
  309. return strndup (iri->data + iri->iri_info->path.rm_so, len);
  310. }
  311. char *
  312. LSUP_iriref_frag (const LSUP_Term *iri)
  313. {
  314. if (iri->type != LSUP_TERM_IRIREF && iri->type != LSUP_TERM_NS_IRIREF) {
  315. log_error ("Term is not a IRI ref type.");
  316. return NULL;
  317. }
  318. if (iri->iri_info->frag.rm_so == -1) return NULL;
  319. size_t len = iri->iri_info->frag.rm_eo - iri->iri_info->frag.rm_so;
  320. return strndup (iri->data + iri->iri_info->frag.rm_so, len);
  321. }
  322. /*
  323. * Triple API.
  324. */
  325. LSUP_Triple *
  326. LSUP_triple_new(LSUP_Term *s, LSUP_Term *p, LSUP_Term *o)
  327. {
  328. LSUP_Triple *spo = malloc (sizeof (*spo));
  329. if (!spo) return NULL;
  330. if (UNLIKELY (LSUP_triple_init (spo, s, p, o))) {
  331. free (spo);
  332. return NULL;
  333. }
  334. return spo;
  335. }
  336. LSUP_Triple *
  337. LSUP_triple_new_from_btriple (const LSUP_BufferTriple *sspo)
  338. {
  339. LSUP_Triple *spo = malloc (sizeof (*spo));
  340. if (!spo) return NULL;
  341. spo->s = LSUP_term_new_from_buffer (sspo->s);
  342. spo->p = LSUP_term_new_from_buffer (sspo->p);
  343. spo->o = LSUP_term_new_from_buffer (sspo->o);
  344. return spo;
  345. }
  346. LSUP_BufferTriple *
  347. LSUP_triple_serialize (const LSUP_Triple *spo)
  348. {
  349. LSUP_BufferTriple *sspo = malloc (sizeof (*sspo));
  350. if (!sspo) return NULL;
  351. sspo->s = LSUP_term_serialize (spo->s);
  352. sspo->p = LSUP_term_serialize (spo->p);
  353. sspo->o = LSUP_term_serialize (spo->o);
  354. return sspo;
  355. }
  356. LSUP_rc
  357. LSUP_triple_init (LSUP_Triple *spo, LSUP_Term *s, LSUP_Term *p, LSUP_Term *o)
  358. {
  359. /* FIXME TRP_DUMMY is a problem here.
  360. if (! LSUP_IS_IRI (s) && s->type != LSUP_TERM_BNODE) {
  361. log_error ("Subject is not of a valid term type: %d", s->type);
  362. return LSUP_VALUE_ERR;
  363. }
  364. if (! LSUP_IS_IRI (p)) {
  365. log_error ("Predicate is not of a valid term type: %d", p->type);
  366. return LSUP_VALUE_ERR;
  367. }
  368. */
  369. spo->s = s;
  370. spo->p = p;
  371. spo->o = o;
  372. return LSUP_OK;
  373. }
  374. void
  375. LSUP_triple_done (LSUP_Triple *spo)
  376. {
  377. if (UNLIKELY (!spo)) return;
  378. LSUP_term_free (spo->s);
  379. LSUP_term_free (spo->p);
  380. LSUP_term_free (spo->o);
  381. }
  382. void
  383. LSUP_triple_free (LSUP_Triple *spo)
  384. {
  385. if (UNLIKELY (!spo)) return;
  386. LSUP_term_free (spo->s);
  387. LSUP_term_free (spo->p);
  388. LSUP_term_free (spo->o);
  389. free (spo);
  390. }
  391. /*
  392. * Multi-add functions.
  393. */
  394. LSUP_TermSet *
  395. LSUP_term_set_new ()
  396. {
  397. // Capacity of 4 is an arbitrary guess.
  398. LSUP_TermSet *ts = hashmap_new (
  399. sizeof (KeyedTerm), 4, LSUP_HASH_SEED, 0,
  400. tset_hash_fn, tset_cmp_fn, tset_free_fn, NULL);
  401. if (UNLIKELY (hashmap_oom (ts))) return NULL;
  402. return ts;
  403. }
  404. LSUP_rc
  405. LSUP_term_set_add (LSUP_TermSet *ts, LSUP_Term *term, LSUP_Term **existing)
  406. {
  407. LSUP_Hash key = LSUP_term_hash (term);
  408. KeyedTerm entry_s = {.key=key, .term=term};
  409. KeyedTerm *ex = hashmap_get (ts, &entry_s);
  410. if (ex) {
  411. if (existing) *existing = ex->term;
  412. return LSUP_NOACTION;
  413. }
  414. hashmap_set (ts, &entry_s);
  415. if (hashmap_oom (ts)) return LSUP_MEM_ERR;
  416. return LSUP_OK;
  417. }
  418. const LSUP_Term *
  419. LSUP_term_set_get (LSUP_TermSet *ts, LSUP_Key key)
  420. {
  421. KeyedTerm *entry = hashmap_get (ts, &(KeyedTerm){.key=key});
  422. if (entry) log_trace ("ID found for key %lx: %s", key, entry->term->data);
  423. else log_trace ("No ID found for key %lx.", key);
  424. return (entry) ? entry->term : NULL;
  425. }
  426. LSUP_rc
  427. LSUP_term_set_next (LSUP_TermSet *ts, size_t *i, LSUP_Term **term)
  428. {
  429. KeyedTerm *kt = NULL;
  430. if (!hashmap_iter (ts, i, (void **)&kt)) return LSUP_END;
  431. if (term) *term = kt->term;
  432. return LSUP_OK;
  433. }
  434. void
  435. LSUP_term_set_free (LSUP_TermSet *ts)
  436. { hashmap_free (ts); }
  437. LSUP_LinkMap *
  438. LSUP_link_map_new (LSUP_LinkType type)
  439. {
  440. LSUP_LinkMap *cm;
  441. MALLOC_GUARD (cm, NULL);
  442. cm->type = type;
  443. cm->links = hashmap_new (
  444. sizeof (Link), 0, LSUP_HASH_SEED, 0,
  445. link_map_hash_fn, link_map_cmp_fn, link_map_free_fn, NULL);
  446. return cm;
  447. }
  448. void
  449. LSUP_link_map_free (LSUP_LinkMap *cm)
  450. {
  451. hashmap_free (cm->links);
  452. free (cm);
  453. }
  454. LSUP_LinkType
  455. LSUP_link_map_type (const LSUP_LinkMap *map)
  456. { return map->type; }
  457. // TODO Memory error handling.
  458. LSUP_rc
  459. LSUP_link_map_add (
  460. LSUP_LinkMap *cmap, LSUP_Term *term, LSUP_TermSet *tset)
  461. {
  462. // Keyed term to look up the link term and insert it, if necessary.
  463. KeyedTerm entry_s = {.key=LSUP_term_hash (term), .term=term};
  464. Link *ex = hashmap_get (cmap->links, &(Link){.term=&entry_s});
  465. if (ex) {
  466. // Add terms one by one to the existing term set.
  467. log_trace (
  468. "Linking term %s exists. Adding individual terms.",
  469. ex->term->term->data);
  470. size_t i = 0;
  471. KeyedTerm *kt;
  472. while (hashmap_iter (tset, &i, (void **)&kt)) {
  473. log_trace (
  474. "Adding term %s to link %s",
  475. kt->term->data, ex->term->term->data);
  476. if (hashmap_get (ex->tset, kt))
  477. // Term already exist, free the new one and move on.
  478. LSUP_term_free (kt->term);
  479. else
  480. // Insert KeyedTerm, the term set now owns the underlying term.
  481. hashmap_set (ex->tset, kt);
  482. }
  483. // Free link term that hasn't been used.
  484. LSUP_term_free (term);
  485. } else {
  486. // Add the new term and the termset wholesale.
  487. log_trace ("Adding new linking term %s.", term->data);
  488. // Allocate inserted member on heap, it will be owned by the map.
  489. KeyedTerm *ins;
  490. MALLOC_GUARD (ins, LSUP_MEM_ERR);
  491. memcpy (ins, &entry_s, sizeof (entry_s));
  492. Link link = {.term=ins, tset=tset};
  493. hashmap_set (cmap->links, &link);
  494. }
  495. return LSUP_OK;
  496. }
  497. LSUP_LinkMapIterator *
  498. LSUP_link_map_iter_new (const LSUP_LinkMap *lmap, LSUP_Term *ext)
  499. {
  500. LSUP_LinkMapIterator *it;
  501. CALLOC_GUARD (it, NULL);
  502. it->map = lmap;
  503. it->ext = ext;
  504. return it;
  505. }
  506. void
  507. LSUP_link_map_iter_free (LSUP_LinkMapIterator *it)
  508. { free (it); }
  509. LSUP_rc
  510. LSUP_link_map_next (
  511. LSUP_LinkMapIterator *it, LSUP_Term **lt, LSUP_TermSet **ts)
  512. {
  513. if (!hashmap_iter (it->map->links, &it->i, (void **)&it->link))
  514. return LSUP_END;
  515. *lt = it->link->term->term;
  516. *ts = it->link->tset;
  517. return LSUP_OK;
  518. }
  519. // TODO dismantle if the only triple generator is for the graph.
  520. LSUP_rc
  521. LSUP_link_map_triples (
  522. LSUP_LinkMapIterator *it, LSUP_Triple *spo)
  523. {
  524. // Assign external (related) term.
  525. if (it->map->type == LSUP_LINK_INBOUND)
  526. spo->o = it->ext;
  527. else if (it->map->type == LSUP_LINK_OUTBOUND)
  528. spo->s = it->ext;
  529. else spo->p = it->ext;
  530. KeyedTerm *kt;
  531. // If we are already handling a link, continue the internal loop.
  532. if (it->link) goto int_loop;
  533. ext_loop:
  534. // Advance external counter and start new internal loop.
  535. it->j = 0;
  536. if (!hashmap_iter (it->map->links, &it->i, (void **)&it->link))
  537. return LSUP_END;
  538. int_loop:
  539. // If end of the term set is reached, start with a new linking term.
  540. if (!hashmap_iter (it->link->tset, &it->j, (void **)&kt)) goto ext_loop;
  541. // Continue pulling from term set.
  542. // Assign linking term.
  543. if (it->map->type == LSUP_LINK_EDGE) spo->s = it->link->term->term;
  544. else spo->p = it->link->term->term;
  545. // Assign term in term set.
  546. if (it->map->type == LSUP_LINK_INBOUND) spo->s = kt->term;
  547. else spo->o = kt->term;
  548. return LSUP_OK;
  549. }
  550. /*
  551. * Static functions.
  552. */
  553. static LSUP_rc
  554. term_init (
  555. LSUP_Term *term, LSUP_TermType type,
  556. const char *data, void *metadata)
  557. {
  558. if (UNLIKELY (!LSUP_uri_ptn)) {
  559. log_error ("Environment not initialized. Did you call LSUP_init()?");
  560. return LSUP_ERROR;
  561. }
  562. // This can never be LSUP_TERM_UNDEFINED.
  563. if (type == LSUP_TERM_UNDEFINED) {
  564. log_error ("%d is not a valid term type.", type);
  565. return LSUP_VALUE_ERR;
  566. }
  567. term->type = type;
  568. if (data) {
  569. // Validate IRI.
  570. if (LSUP_IS_IRI (term)) {
  571. char *fquri;
  572. // Find fully qualified IRI to parse.
  573. if (term->type == LSUP_TERM_NS_IRIREF) {
  574. if (LSUP_nsmap_normalize_uri (metadata, data, &fquri) < 0) {
  575. log_error ("Error normalizing IRI data.");
  576. return LSUP_VALUE_ERR;
  577. }
  578. log_debug ("Fully qualified IRI: %s", fquri);
  579. } else fquri = (char *) data;
  580. if (strpbrk (fquri, invalid_uri_chars) != NULL) {
  581. log_warn (
  582. "Characters %s are not valid in a URI. Got: %s\n",
  583. invalid_uri_chars, fquri);
  584. #if 0
  585. // TODO This causes W3C TTL test #29 to fail. Remove?
  586. return LSUP_VALUE_ERR;
  587. #endif
  588. }
  589. // Capture interesting IRI parts.
  590. regmatch_t matches[11];
  591. if (UNLIKELY (regexec (LSUP_uri_ptn, fquri, 11, matches, 0) != 0)) {
  592. fprintf (stderr, "Error matching URI pattern.\n");
  593. return LSUP_VALUE_ERR;
  594. }
  595. if (term->type == LSUP_TERM_NS_IRIREF) free (fquri);
  596. MALLOC_GUARD (term->iri_info, LSUP_MEM_ERR);
  597. term->iri_info->prefix = matches[1];
  598. term->iri_info->path = matches[5];
  599. term->iri_info->frag = matches[10];
  600. term->iri_info->nsm = metadata;
  601. }
  602. term->data = strdup (data);
  603. } else {
  604. // No data. Make up a random UUID or URI if allowed.
  605. if (type == LSUP_TERM_IRIREF || type == LSUP_TERM_BNODE) {
  606. uuid_t uuid;
  607. uuid_generate_random (uuid);
  608. uuid_str_t uuid_str;
  609. uuid_unparse_lower (uuid, uuid_str);
  610. if (type == LSUP_TERM_IRIREF) {
  611. term->data = malloc (UUID4_URN_SIZE);
  612. snprintf (
  613. term->data, UUID4_URN_SIZE, "urn:uuid4:%s", uuid_str);
  614. MALLOC_GUARD (term->iri_info, LSUP_MEM_ERR);
  615. // Allocate IRI match patterns manually.
  616. term->iri_info->prefix.rm_so = 0;
  617. term->iri_info->prefix.rm_eo = 4;
  618. term->iri_info->path.rm_so = 4;
  619. term->iri_info->path.rm_eo = UUIDSTR_SIZE + 6;
  620. term->iri_info->frag.rm_so = -1;
  621. term->iri_info->frag.rm_eo = -1;
  622. term->iri_info->nsm = NULL;
  623. } else term->data = strdup (uuid_str);
  624. } else {
  625. log_error ("No data provided for term.");
  626. return LSUP_VALUE_ERR;
  627. }
  628. }
  629. if (term->type == LSUP_TERM_LT_LITERAL) {
  630. if (!metadata) {
  631. log_warn ("Lang tag is NULL. Creating a non-tagged literal.");
  632. term->type = LSUP_TERM_LITERAL;
  633. } else {
  634. char *lang_str = (char *) metadata;
  635. log_trace("Lang string: '%s'", lang_str);
  636. // Lang tags longer than 7 characters will be truncated.
  637. strncpy(term->lang, lang_str, sizeof (term->lang) - 1);
  638. if (strlen (term->lang) < 1) {
  639. log_error ("Lang tag cannot be an empty string.");
  640. return LSUP_VALUE_ERR;
  641. }
  642. term->lang[7] = '\0';
  643. }
  644. }
  645. if (term->type == LSUP_TERM_LITERAL) {
  646. term->datatype = metadata;
  647. if (! term->datatype) term->datatype = LSUP_default_datatype;
  648. log_trace ("Storing data type: %s", term->datatype->data);
  649. if (! LSUP_IS_IRI (term->datatype)) {
  650. log_error (
  651. "Literal data type is not an IRI: %s",
  652. term->datatype->data);
  653. return LSUP_VALUE_ERR;
  654. }
  655. LSUP_Term *ex = NULL;
  656. LSUP_term_set_add (LSUP_term_cache, term->datatype, &ex);
  657. if (ex && ex != term->datatype) {
  658. // Replace datatype handle with the one in term cache, and free
  659. // the new one.
  660. if (term->datatype != LSUP_default_datatype)
  661. LSUP_term_free (term->datatype);
  662. term->datatype = ex;
  663. }
  664. //log_trace ("Datatype address: %p", term->datatype);
  665. log_trace ("Datatype hash: %lx", LSUP_term_hash (term->datatype));
  666. } else if (term->type == LSUP_TERM_BNODE) {
  667. // TODO This is not usable for global skolemization.
  668. term->bnode_id = LSUP_HASH (
  669. term->data, strlen (term->data) + 1, LSUP_HASH_SEED);
  670. }
  671. return LSUP_OK;
  672. }
  673. /*
  674. * Extern inline functions.
  675. */
  676. LSUP_Key LSUP_term_hash (const LSUP_Term *term);
  677. LSUP_Term *LSUP_iriref_new (const char *data, LSUP_NSMap *nsm);
  678. LSUP_Term *LSUP_literal_new (const char *data, LSUP_Term *datatype);
  679. LSUP_Term *LSUP_lt_literal_new (const char *data, char *lang);
  680. LSUP_Term *LSUP_bnode_new (const char *data);
  681. bool LSUP_term_equals (const LSUP_Term *term1, const LSUP_Term *term2);
  682. LSUP_Term *LSUP_triple_pos (const LSUP_Triple *trp, LSUP_TriplePos n);
  683. LSUP_Key LSUP_triple_hash (const LSUP_Triple *trp);