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.

643 lines
14KB

  1. /*
  2. Copyright 2011-2013 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 (a.k.a. "key") */
  92. SORD_OBJECT = 2, /**< Object (a.k.a. "value") */
  93. SORD_GRAPH = 3 /**< Graph (a.k.a. "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 `len` to its length.
  211. */
  212. SORD_API
  213. const uint8_t*
  214. sord_node_get_string_counted(const SordNode* node, size_t* len);
  215. /**
  216. Return the language of a literal node (or NULL).
  217. */
  218. SORD_API
  219. const char*
  220. sord_node_get_language(const SordNode* node);
  221. /**
  222. Return the datatype URI of a literal node (or NULL).
  223. */
  224. SORD_API
  225. SordNode*
  226. sord_node_get_datatype(const SordNode* node);
  227. /**
  228. Return the flags (string attributes) of a node.
  229. */
  230. SORD_API
  231. SerdNodeFlags
  232. sord_node_get_flags(const SordNode* node);
  233. /**
  234. Return true iff node can be serialised as an inline object.
  235. More specifically, this returns true iff the node is the object field
  236. of exactly one statement, and therefore can be inlined since it needn't
  237. be referred to by name.
  238. */
  239. SORD_API
  240. bool
  241. sord_node_is_inline_object(const SordNode* node);
  242. /**
  243. Return true iff `a` is equal to `b`.
  244. Note this is much faster than comparing the node's strings.
  245. */
  246. SORD_API
  247. bool
  248. sord_node_equals(const SordNode* a,
  249. const SordNode* b);
  250. /**
  251. Return a SordNode as a SerdNode.
  252. The returned node is shared and must not be freed or modified.
  253. */
  254. SORD_API
  255. const SerdNode*
  256. sord_node_to_serd_node(const SordNode* node);
  257. /**
  258. Create a new SordNode from a SerdNode.
  259. The returned node must be freed using sord_node_free.
  260. */
  261. SORD_API
  262. SordNode*
  263. sord_node_from_serd_node(SordWorld* world,
  264. SerdEnv* env,
  265. const SerdNode* node,
  266. const SerdNode* datatype,
  267. const SerdNode* lang);
  268. /**
  269. @}
  270. @name Model
  271. @{
  272. */
  273. /**
  274. Create a new model.
  275. @param world The world in which to make this model.
  276. @param indices SordIndexOption flags (e.g. SORD_SPO|SORD_OPS). Be sure to
  277. enable an index where the most significant node(s) are not variables in your
  278. queries (e.g. to make (? P O) queries, enable either SORD_OPS or SORD_POS).
  279. @param graphs If true, store (and index) graph contexts.
  280. */
  281. SORD_API
  282. SordModel*
  283. sord_new(SordWorld* world,
  284. unsigned indices,
  285. bool graphs);
  286. /**
  287. Close and free `model`.
  288. */
  289. SORD_API
  290. void
  291. sord_free(SordModel* model);
  292. /**
  293. Get the world associated with `model`.
  294. */
  295. SORD_API
  296. SordWorld*
  297. sord_get_world(SordModel* model);
  298. /**
  299. Return the number of nodes stored in `world`.
  300. Nodes are included in this count iff they are a part of a quad in `world`.
  301. */
  302. SORD_API
  303. size_t
  304. sord_num_nodes(const SordWorld* world);
  305. /**
  306. Return the number of quads stored in `model`.
  307. */
  308. SORD_API
  309. size_t
  310. sord_num_quads(const SordModel* model);
  311. /**
  312. Return an iterator to the start of `model`.
  313. */
  314. SORD_API
  315. SordIter*
  316. sord_begin(const SordModel* model);
  317. /**
  318. Search for statements by a quad pattern.
  319. @return an iterator to the first match, or NULL if no matches found.
  320. */
  321. SORD_API
  322. SordIter*
  323. sord_find(SordModel* model, const SordQuad pat);
  324. /**
  325. Search for statements by nodes.
  326. @return an iterator to the first match, or NULL if no matches found.
  327. */
  328. SORD_API
  329. SordIter*
  330. sord_search(SordModel* model,
  331. const SordNode* s,
  332. const SordNode* p,
  333. const SordNode* o,
  334. const SordNode* g);
  335. /**
  336. Search for a single node that matches a pattern.
  337. Exactly one of `s`, `p`, `o` must be NULL.
  338. This function is mainly useful for predicates that only have one value.
  339. The returned node must be freed using sord_node_free.
  340. @return the first matching node, or NULL if no matches are found.
  341. */
  342. SORD_API
  343. SordNode*
  344. sord_get(SordModel* model,
  345. const SordNode* s,
  346. const SordNode* p,
  347. const SordNode* o,
  348. const SordNode* g);
  349. /**
  350. Return true iff a statement exists.
  351. */
  352. SORD_API
  353. bool
  354. sord_ask(SordModel* model,
  355. const SordNode* s,
  356. const SordNode* p,
  357. const SordNode* o,
  358. const SordNode* g);
  359. /**
  360. Return the number of matching statements.
  361. */
  362. SORD_API
  363. uint64_t
  364. sord_count(SordModel* model,
  365. const SordNode* s,
  366. const SordNode* p,
  367. const SordNode* o,
  368. const SordNode* g);
  369. /**
  370. Check if `model` contains a triple pattern.
  371. */
  372. SORD_API
  373. bool
  374. sord_contains(SordModel* model, const SordQuad pat);
  375. /**
  376. Add a quad to a model.
  377. Calling this function invalidates all iterators on `model`.
  378. */
  379. SORD_API
  380. bool
  381. sord_add(SordModel* model, const SordQuad quad);
  382. /**
  383. Remove a quad from a model.
  384. Calling this function invalidates all iterators on `model`. To remove quads
  385. while iterating, use sord_erase() instead.
  386. */
  387. SORD_API
  388. void
  389. sord_remove(SordModel* model, const SordQuad quad);
  390. /**
  391. Remove a quad from a model via an iterator.
  392. Calling this function invalidates all iterators on `model` except `iter`.
  393. @param iter Iterator to the element to erase, which is incremented to the
  394. next value on return.
  395. */
  396. SORD_API
  397. SerdStatus
  398. sord_erase(SordModel* model, SordIter* iter);
  399. /**
  400. @}
  401. @name Inserter
  402. @{
  403. */
  404. /**
  405. Create an inserter for writing statements to a model.
  406. */
  407. SORD_API
  408. SordInserter*
  409. sord_inserter_new(SordModel* model,
  410. SerdEnv* env);
  411. /**
  412. Free an inserter.
  413. */
  414. SORD_API
  415. void
  416. sord_inserter_free(SordInserter* inserter);
  417. /**
  418. Set the current base URI for writing to the model.
  419. Note this function can be safely casted to SerdBaseSink.
  420. */
  421. SORD_API
  422. SerdStatus
  423. sord_inserter_set_base_uri(SordInserter* inserter,
  424. const SerdNode* uri);
  425. /**
  426. Set a namespace prefix for writing to the model.
  427. Note this function can be safely casted to SerdPrefixSink.
  428. */
  429. SORD_API
  430. SerdStatus
  431. sord_inserter_set_prefix(SordInserter* inserter,
  432. const SerdNode* name,
  433. const SerdNode* uri);
  434. /**
  435. Write a statement to the model.
  436. Note this function can be safely casted to SerdStatementSink.
  437. */
  438. SORD_API
  439. SerdStatus
  440. sord_inserter_write_statement(SordInserter* inserter,
  441. SerdStatementFlags flags,
  442. const SerdNode* graph,
  443. const SerdNode* subject,
  444. const SerdNode* predicate,
  445. const SerdNode* object,
  446. const SerdNode* object_datatype,
  447. const SerdNode* object_lang);
  448. /**
  449. @}
  450. @name Iteration
  451. @{
  452. */
  453. /**
  454. Set `quad` to the quad pointed to by `iter`.
  455. */
  456. SORD_API
  457. void
  458. sord_iter_get(const SordIter* iter, SordQuad quad);
  459. /**
  460. Return a field of the quad pointed to by `iter`.
  461. */
  462. SORD_API
  463. const SordNode*
  464. sord_iter_get_node(const SordIter* iter, SordQuadIndex index);
  465. /**
  466. Return the store pointed to by `iter`.
  467. */
  468. SORD_API
  469. const SordModel*
  470. sord_iter_get_model(SordIter* iter);
  471. /**
  472. Increment `iter` to point to the next statement.
  473. */
  474. SORD_API
  475. bool
  476. sord_iter_next(SordIter* iter);
  477. /**
  478. Return true iff `iter` is at the end of its range.
  479. */
  480. SORD_API
  481. bool
  482. sord_iter_end(const SordIter* iter);
  483. /**
  484. Free `iter`.
  485. */
  486. SORD_API
  487. void
  488. sord_iter_free(SordIter* iter);
  489. /**
  490. @}
  491. @name Utilities
  492. @{
  493. */
  494. /**
  495. Match two quads (using ID comparison only).
  496. This function is a straightforward and fast equivalence match with wildcard
  497. support (ID 0 is a wildcard). It does not actually read node data.
  498. @return true iff `x` and `y` match.
  499. */
  500. SORD_API
  501. bool
  502. sord_quad_match(const SordQuad x, const SordQuad y);
  503. /**
  504. @}
  505. @name Serialisation
  506. @{
  507. */
  508. /**
  509. Return a reader that will read into `model`.
  510. */
  511. SORD_API
  512. SerdReader*
  513. sord_new_reader(SordModel* model,
  514. SerdEnv* env,
  515. SerdSyntax syntax,
  516. SordNode* graph);
  517. /**
  518. Write a model to a writer.
  519. */
  520. SORD_API
  521. bool
  522. sord_write(SordModel* model,
  523. SerdWriter* writer,
  524. SordNode* graph);
  525. /**
  526. Write a range to a writer.
  527. This increments `iter` to its end, then frees it.
  528. */
  529. SORD_API
  530. bool
  531. sord_write_iter(SordIter* iter,
  532. SerdWriter* writer);
  533. /**
  534. @}
  535. @}
  536. */
  537. #ifdef __cplusplus
  538. } /* extern "C" */
  539. #endif
  540. #endif /* SORD_SORD_H */