Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sord.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. Copyright 2011-2016 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. /**
  15. @file sord.h API for Sord, a lightweight RDF model library.
  16. */
  17. #ifndef SORD_SORD_H
  18. #define SORD_SORD_H
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include "serd/serd.h"
  23. #ifdef SORD_SHARED
  24. # ifdef _WIN32
  25. # define SORD_LIB_IMPORT __declspec(dllimport)
  26. # define SORD_LIB_EXPORT __declspec(dllexport)
  27. # else
  28. # define SORD_LIB_IMPORT __attribute__((visibility("default")))
  29. # define SORD_LIB_EXPORT __attribute__((visibility("default")))
  30. # endif
  31. # ifdef SORD_INTERNAL
  32. # define SORD_API SORD_LIB_EXPORT
  33. # else
  34. # define SORD_API SORD_LIB_IMPORT
  35. # endif
  36. #else
  37. # define SORD_API
  38. #endif
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #else
  42. # include <stdbool.h>
  43. #endif
  44. /**
  45. @defgroup sord Sord
  46. A lightweight RDF model library.
  47. Sord stores RDF (subject object predicate context) quads, where the context
  48. may be omitted (to represent triples in the default graph).
  49. @{
  50. */
  51. /**
  52. Sord World.
  53. The World represents all library state, including interned strings.
  54. */
  55. typedef struct SordWorldImpl SordWorld;
  56. /**
  57. Sord Model.
  58. A model is an indexed set of Quads (i.e. it can contain several RDF
  59. graphs). It may be searched using various patterns depending on which
  60. indices are enabled.
  61. */
  62. typedef struct SordModelImpl SordModel;
  63. /**
  64. Model Inserter.
  65. An inserter is used for writing statements to a model using the Serd sink
  66. interface. This makes it simple to write to a model directly using a
  67. SerdReader, or any other code that writes statements to a SerdStatementSink.
  68. */
  69. typedef struct SordInserterImpl SordInserter;
  70. /**
  71. Model Iterator.
  72. */
  73. typedef struct SordIterImpl SordIter;
  74. /**
  75. RDF Node.
  76. A Node is a component of a Quad. Nodes may be URIs, blank nodes, or
  77. (in the case of quad objects only) string literals. Literal nodes may
  78. have an associate language or datatype (but not both).
  79. */
  80. typedef struct SordNodeImpl SordNode;
  81. /**
  82. Quad of nodes (a statement), or a quad pattern.
  83. Nodes are ordered (S P O G). The ID of the default graph is 0.
  84. */
  85. typedef const SordNode* SordQuad[4];
  86. /**
  87. Index into a SordQuad.
  88. */
  89. typedef enum {
  90. SORD_SUBJECT = 0, /**< Subject */
  91. SORD_PREDICATE = 1, /**< Predicate ("key") */
  92. SORD_OBJECT = 2, /**< Object ("value") */
  93. SORD_GRAPH = 3 /**< Graph ("context") */
  94. } SordQuadIndex;
  95. /**
  96. Type of a node.
  97. */
  98. typedef enum {
  99. SORD_URI = 1, /**< URI */
  100. SORD_BLANK = 2, /**< Blank node identifier */
  101. SORD_LITERAL = 3 /**< Literal (string with optional lang or datatype) */
  102. } SordNodeType;
  103. /**
  104. Indexing option.
  105. */
  106. typedef enum {
  107. SORD_SPO = 1, /**< Subject, Predicate, Object */
  108. SORD_SOP = 1 << 1, /**< Subject, Object, Predicate */
  109. SORD_OPS = 1 << 2, /**< Object, Predicate, Subject */
  110. SORD_OSP = 1 << 3, /**< Object, Subject, Predicate */
  111. SORD_PSO = 1 << 4, /**< Predicate, Subject, Object */
  112. SORD_POS = 1 << 5 /**< Predicate, Object, Subject */
  113. } SordIndexOption;
  114. /**
  115. @name World
  116. @{
  117. */
  118. /**
  119. Create a new Sord World.
  120. It is safe to use multiple worlds in one process, though no data
  121. (e.g. nodes) can be shared between worlds, and this should be avoided if
  122. possible for performance reasons.
  123. */
  124. SORD_API
  125. SordWorld*
  126. sord_world_new(void);
  127. /**
  128. Free `world`.
  129. */
  130. SORD_API
  131. void
  132. sord_world_free(SordWorld* world);
  133. /**
  134. Set a function to be called when errors occur.
  135. The `error_sink` will be called with `handle` as its first argument. If
  136. no error function is set, errors are printed to stderr.
  137. */
  138. SORD_API
  139. void
  140. sord_world_set_error_sink(SordWorld* world,
  141. SerdErrorSink error_sink,
  142. void* handle);
  143. /**
  144. @}
  145. @name Node
  146. @{
  147. */
  148. /**
  149. Get a URI node from a string.
  150. Note this function measures `str`, which is a common bottleneck.
  151. Use sord_node_from_serd_node() instead if `str` is already measured.
  152. */
  153. SORD_API
  154. SordNode*
  155. sord_new_uri(SordWorld* world, const uint8_t* uri);
  156. /**
  157. Get a URI node from a relative URI string.
  158. */
  159. SORD_API
  160. SordNode*
  161. sord_new_relative_uri(SordWorld* world,
  162. const uint8_t* str,
  163. const uint8_t* base_uri);
  164. /**
  165. Get a blank node from a string.
  166. Note this function measures `str`, which is a common bottleneck.
  167. Use sord_node_from_serd_node() instead if `str` is already measured.
  168. */
  169. SORD_API
  170. SordNode*
  171. sord_new_blank(SordWorld* world, const uint8_t* str);
  172. /**
  173. Get a literal node from a string.
  174. Note this function measures `str`, which is a common bottleneck.
  175. Use sord_node_from_serd_node() instead if `str` is already measured.
  176. */
  177. SORD_API
  178. SordNode*
  179. sord_new_literal(SordWorld* world,
  180. SordNode* datatype,
  181. const uint8_t* str,
  182. const char* lang);
  183. /**
  184. Copy a node (obtain a reference).
  185. Node that since nodes are interned and reference counted, this does not
  186. actually create a deep copy of `node`.
  187. */
  188. SORD_API
  189. SordNode*
  190. sord_node_copy(const SordNode* node);
  191. /**
  192. Free a node (drop a reference).
  193. */
  194. SORD_API
  195. void
  196. sord_node_free(SordWorld* world, SordNode* node);
  197. /**
  198. Return the type of a node (SORD_URI, SORD_BLANK, or SORD_LITERAL).
  199. */
  200. SORD_API
  201. SordNodeType
  202. sord_node_get_type(const SordNode* node);
  203. /**
  204. Return the string value of a node.
  205. */
  206. SORD_API
  207. const uint8_t*
  208. sord_node_get_string(const SordNode* node);
  209. /**
  210. Return the string value of a node, and set `bytes` to its length in bytes.
  211. */
  212. SORD_API
  213. const uint8_t*
  214. sord_node_get_string_counted(const SordNode* node, size_t* bytes);
  215. /**
  216. Return the string value of a node, and set `bytes` to its length in bytes,
  217. and `count` to its length in characters.
  218. */
  219. SORD_API
  220. const uint8_t*
  221. sord_node_get_string_measured(const SordNode* node,
  222. size_t* bytes,
  223. size_t* chars);
  224. /**
  225. Return the language of a literal node (or NULL).
  226. */
  227. SORD_API
  228. const char*
  229. sord_node_get_language(const SordNode* node);
  230. /**
  231. Return the datatype URI of a literal node (or NULL).
  232. */
  233. SORD_API
  234. SordNode*
  235. sord_node_get_datatype(const SordNode* node);
  236. /**
  237. Return the flags (string attributes) of a node.
  238. */
  239. SORD_API
  240. SerdNodeFlags
  241. sord_node_get_flags(const SordNode* node);
  242. /**
  243. Return true iff node can be serialised as an inline object.
  244. More specifically, this returns true iff the node is the object field
  245. of exactly one statement, and therefore can be inlined since it needn't
  246. be referred to by name.
  247. */
  248. SORD_API
  249. bool
  250. sord_node_is_inline_object(const SordNode* node);
  251. /**
  252. Return true iff `a` is equal to `b`.
  253. Note this is much faster than comparing the node's strings.
  254. */
  255. SORD_API
  256. bool
  257. sord_node_equals(const SordNode* a,
  258. const SordNode* b);
  259. /**
  260. Return a SordNode as a SerdNode.
  261. The returned node is shared and must not be freed or modified.
  262. */
  263. SORD_API
  264. const SerdNode*
  265. sord_node_to_serd_node(const SordNode* node);
  266. /**
  267. Create a new SordNode from a SerdNode.
  268. The returned node must be freed using sord_node_free().
  269. */
  270. SORD_API
  271. SordNode*
  272. sord_node_from_serd_node(SordWorld* world,
  273. SerdEnv* env,
  274. const SerdNode* node,
  275. const SerdNode* datatype,
  276. const SerdNode* lang);
  277. /**
  278. @}
  279. @name Model
  280. @{
  281. */
  282. /**
  283. Create a new model.
  284. @param world The world in which to make this model.
  285. @param indices SordIndexOption flags (e.g. SORD_SPO|SORD_OPS). Be sure to
  286. enable an index where the most significant node(s) are not variables in your
  287. queries (e.g. to make (? P O) queries, enable either SORD_OPS or SORD_POS).
  288. @param graphs If true, store (and index) graph contexts.
  289. */
  290. SORD_API
  291. SordModel*
  292. sord_new(SordWorld* world,
  293. unsigned indices,
  294. bool graphs);
  295. /**
  296. Close and free `model`.
  297. */
  298. SORD_API
  299. void
  300. sord_free(SordModel* model);
  301. /**
  302. Get the world associated with `model`.
  303. */
  304. SORD_API
  305. SordWorld*
  306. sord_get_world(SordModel* model);
  307. /**
  308. Return the number of nodes stored in `world`.
  309. Nodes are included in this count iff they are a part of a quad in `world`.
  310. */
  311. SORD_API
  312. size_t
  313. sord_num_nodes(const SordWorld* world);
  314. /**
  315. Return the number of quads stored in `model`.
  316. */
  317. SORD_API
  318. size_t
  319. sord_num_quads(const SordModel* model);
  320. /**
  321. Return an iterator to the start of `model`.
  322. */
  323. SORD_API
  324. SordIter*
  325. sord_begin(const SordModel* model);
  326. /**
  327. Search for statements by a quad pattern.
  328. @return an iterator to the first match, or NULL if no matches found.
  329. */
  330. SORD_API
  331. SordIter*
  332. sord_find(SordModel* model, const SordQuad pat);
  333. /**
  334. Search for statements by nodes.
  335. @return an iterator to the first match, or NULL if no matches found.
  336. */
  337. SORD_API
  338. SordIter*
  339. sord_search(SordModel* model,
  340. const SordNode* s,
  341. const SordNode* p,
  342. const SordNode* o,
  343. const SordNode* g);
  344. /**
  345. Search for a single node that matches a pattern.
  346. Exactly one of `s`, `p`, `o` must be NULL.
  347. This function is mainly useful for predicates that only have one value.
  348. The returned node must be freed using sord_node_free().
  349. @return the first matching node, or NULL if no matches are found.
  350. */
  351. SORD_API
  352. SordNode*
  353. sord_get(SordModel* model,
  354. const SordNode* s,
  355. const SordNode* p,
  356. const SordNode* o,
  357. const SordNode* g);
  358. /**
  359. Return true iff a statement exists.
  360. */
  361. SORD_API
  362. bool
  363. sord_ask(SordModel* model,
  364. const SordNode* s,
  365. const SordNode* p,
  366. const SordNode* o,
  367. const SordNode* g);
  368. /**
  369. Return the number of matching statements.
  370. */
  371. SORD_API
  372. uint64_t
  373. sord_count(SordModel* model,
  374. const SordNode* s,
  375. const SordNode* p,
  376. const SordNode* o,
  377. const SordNode* g);
  378. /**
  379. Check if `model` contains a triple pattern.
  380. @return true if `model` contains a match for `pat`, otherwise false.
  381. */
  382. SORD_API
  383. bool
  384. sord_contains(SordModel* model, const SordQuad pat);
  385. /**
  386. Add a quad to a model.
  387. Calling this function invalidates all iterators on `model`.
  388. @return true on success, false, on error.
  389. */
  390. SORD_API
  391. bool
  392. sord_add(SordModel* model, const SordQuad quad);
  393. /**
  394. Remove a quad from a model.
  395. Calling this function invalidates all iterators on `model`. To remove quads
  396. while iterating, use sord_erase() instead.
  397. */
  398. SORD_API
  399. void
  400. sord_remove(SordModel* model, const SordQuad quad);
  401. /**
  402. Remove a quad from a model via an iterator.
  403. Calling this function invalidates all iterators on `model` except `iter`.
  404. @param model The model which `iter` points to.
  405. @param iter Iterator to the element to erase, which is incremented to the
  406. next value on return.
  407. */
  408. SORD_API
  409. SerdStatus
  410. sord_erase(SordModel* model, SordIter* iter);
  411. /**
  412. @}
  413. @name Inserter
  414. @{
  415. */
  416. /**
  417. Create an inserter for writing statements to a model.
  418. */
  419. SORD_API
  420. SordInserter*
  421. sord_inserter_new(SordModel* model,
  422. SerdEnv* env);
  423. /**
  424. Free an inserter.
  425. */
  426. SORD_API
  427. void
  428. sord_inserter_free(SordInserter* inserter);
  429. /**
  430. Set the current base URI for writing to the model.
  431. Note this function can be safely casted to SerdBaseSink.
  432. */
  433. SORD_API
  434. SerdStatus
  435. sord_inserter_set_base_uri(SordInserter* inserter,
  436. const SerdNode* uri);
  437. /**
  438. Set a namespace prefix for writing to the model.
  439. Note this function can be safely casted to SerdPrefixSink.
  440. */
  441. SORD_API
  442. SerdStatus
  443. sord_inserter_set_prefix(SordInserter* inserter,
  444. const SerdNode* name,
  445. const SerdNode* uri);
  446. /**
  447. Write a statement to the model.
  448. Note this function can be safely casted to SerdStatementSink.
  449. */
  450. SORD_API
  451. SerdStatus
  452. sord_inserter_write_statement(SordInserter* inserter,
  453. SerdStatementFlags flags,
  454. const SerdNode* graph,
  455. const SerdNode* subject,
  456. const SerdNode* predicate,
  457. const SerdNode* object,
  458. const SerdNode* object_datatype,
  459. const SerdNode* object_lang);
  460. /**
  461. @}
  462. @name Iteration
  463. @{
  464. */
  465. /**
  466. Set `quad` to the quad pointed to by `iter`.
  467. */
  468. SORD_API
  469. void
  470. sord_iter_get(const SordIter* iter, SordQuad quad);
  471. /**
  472. Return a field of the quad pointed to by `iter`.
  473. Returns NULL if `iter` is NULL or is at the end.
  474. */
  475. SORD_API
  476. const SordNode*
  477. sord_iter_get_node(const SordIter* iter, SordQuadIndex index);
  478. /**
  479. Return the store pointed to by `iter`.
  480. */
  481. SORD_API
  482. const SordModel*
  483. sord_iter_get_model(SordIter* iter);
  484. /**
  485. Increment `iter` to point to the next statement.
  486. */
  487. SORD_API
  488. bool
  489. sord_iter_next(SordIter* iter);
  490. /**
  491. Return true iff `iter` is at the end of its range.
  492. */
  493. SORD_API
  494. bool
  495. sord_iter_end(const SordIter* iter);
  496. /**
  497. Free `iter`.
  498. */
  499. SORD_API
  500. void
  501. sord_iter_free(SordIter* iter);
  502. /**
  503. @}
  504. @name Utilities
  505. @{
  506. */
  507. /**
  508. Match two quads (using ID comparison only).
  509. This function is a straightforward and fast equivalence match with wildcard
  510. support (ID 0 is a wildcard). It does not actually read node data.
  511. @return true iff `x` and `y` match.
  512. */
  513. SORD_API
  514. bool
  515. sord_quad_match(const SordQuad x, const SordQuad y);
  516. /**
  517. @}
  518. @name Serialisation
  519. @{
  520. */
  521. /**
  522. Return a reader that will read into `model`.
  523. */
  524. SORD_API
  525. SerdReader*
  526. sord_new_reader(SordModel* model,
  527. SerdEnv* env,
  528. SerdSyntax syntax,
  529. SordNode* graph);
  530. /**
  531. Write a model to a writer.
  532. */
  533. SORD_API
  534. bool
  535. sord_write(SordModel* model,
  536. SerdWriter* writer,
  537. SordNode* graph);
  538. /**
  539. Write a range to a writer.
  540. This increments `iter` to its end, then frees it.
  541. */
  542. SORD_API
  543. bool
  544. sord_write_iter(SordIter* iter,
  545. SerdWriter* writer);
  546. /**
  547. @}
  548. @}
  549. */
  550. #ifdef __cplusplus
  551. } /* extern "C" */
  552. #endif
  553. #endif /* SORD_SORD_H */