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

12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 @c 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 @p error_sink will be called with @p 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 @c str, which is a common bottleneck.
  151. Use sord_node_from_serd_node instead if @c 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 @c str, which is a common bottleneck.
  167. Use sord_node_from_serd_node instead if @c 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 @c str, which is a common bottleneck.
  175. Use sord_node_from_serd_node instead if @c 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 @c 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 @c 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 @c a is equal to @c 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 @c model.
  288. */
  289. SORD_API
  290. void
  291. sord_free(SordModel* model);
  292. /**
  293. Get the world associated with @c model.
  294. */
  295. SORD_API
  296. SordWorld*
  297. sord_get_world(SordModel* model);
  298. /**
  299. Return the number of nodes stored in @c world.
  300. Nodes are included in this count iff they are a part of a quad in @c world.
  301. */
  302. SORD_API
  303. size_t
  304. sord_num_nodes(const SordWorld* world);
  305. /**
  306. Return the number of quads stored in @c model.
  307. */
  308. SORD_API
  309. size_t
  310. sord_num_quads(const SordModel* model);
  311. /**
  312. Return an iterator to the start of @c 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 @p s, @p p, @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 @a 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. */
  378. SORD_API
  379. bool
  380. sord_add(SordModel* model, const SordQuad quad);
  381. /**
  382. Remove a quad from a model.
  383. Note that is it illegal to remove while iterating over @c model.
  384. */
  385. SORD_API
  386. void
  387. sord_remove(SordModel* model, const SordQuad quad);
  388. /**
  389. @}
  390. @name Inserter
  391. @{
  392. */
  393. /**
  394. Create an inserter for writing statements to a model.
  395. */
  396. SORD_API
  397. SordInserter*
  398. sord_inserter_new(SordModel* model,
  399. SerdEnv* env);
  400. /**
  401. Free an inserter.
  402. */
  403. SORD_API
  404. void
  405. sord_inserter_free(SordInserter* inserter);
  406. /**
  407. Set the current base URI for writing to the model.
  408. Note this function can be safely casted to SerdBaseSink.
  409. */
  410. SORD_API
  411. SerdStatus
  412. sord_inserter_set_base_uri(SordInserter* inserter,
  413. const SerdNode* uri);
  414. /**
  415. Set a namespace prefix for writing to the model.
  416. Note this function can be safely casted to SerdPrefixSink.
  417. */
  418. SORD_API
  419. SerdStatus
  420. sord_inserter_set_prefix(SordInserter* inserter,
  421. const SerdNode* name,
  422. const SerdNode* uri);
  423. /**
  424. Write a statement to the model.
  425. Note this function can be safely casted to SerdStatementSink.
  426. */
  427. SORD_API
  428. SerdStatus
  429. sord_inserter_write_statement(SordInserter* inserter,
  430. SerdStatementFlags flags,
  431. const SerdNode* graph,
  432. const SerdNode* subject,
  433. const SerdNode* predicate,
  434. const SerdNode* object,
  435. const SerdNode* object_datatype,
  436. const SerdNode* object_lang);
  437. /**
  438. @}
  439. @name Iteration
  440. @{
  441. */
  442. /**
  443. Set @c quad to the quad pointed to by @c iter.
  444. */
  445. SORD_API
  446. void
  447. sord_iter_get(const SordIter* iter, SordQuad quad);
  448. /**
  449. Return a field of the quad pointed to by @c iter.
  450. */
  451. SORD_API
  452. const SordNode*
  453. sord_iter_get_node(const SordIter* iter, SordQuadIndex index);
  454. /**
  455. Return the store pointed to by @c iter.
  456. */
  457. SORD_API
  458. const SordModel*
  459. sord_iter_get_model(SordIter* iter);
  460. /**
  461. Increment @c iter to point to the next statement.
  462. */
  463. SORD_API
  464. bool
  465. sord_iter_next(SordIter* iter);
  466. /**
  467. Return true iff @c iter is at the end of its range.
  468. */
  469. SORD_API
  470. bool
  471. sord_iter_end(const SordIter* iter);
  472. /**
  473. Free @c iter.
  474. */
  475. SORD_API
  476. void
  477. sord_iter_free(SordIter* iter);
  478. /**
  479. @}
  480. @name Utilities
  481. @{
  482. */
  483. /**
  484. Match two quads (using ID comparison only).
  485. This function is a straightforward and fast equivalence match with wildcard
  486. support (ID 0 is a wildcard). It does not actually read node data.
  487. @return true iff @c x and @c y match.
  488. */
  489. SORD_API
  490. bool
  491. sord_quad_match(const SordQuad x, const SordQuad y);
  492. /**
  493. @}
  494. @name Serialisation
  495. @{
  496. */
  497. /**
  498. Return a reader that will read into @c model.
  499. */
  500. SORD_API
  501. SerdReader*
  502. sord_new_reader(SordModel* model,
  503. SerdEnv* env,
  504. SerdSyntax syntax,
  505. SordNode* graph);
  506. /**
  507. Write a model to a writer.
  508. */
  509. SORD_API
  510. bool
  511. sord_write(SordModel* model,
  512. SerdWriter* writer,
  513. SordNode* graph);
  514. /**
  515. Write a range to a writer.
  516. This increments @c iter to its end, then frees it.
  517. */
  518. SORD_API
  519. bool
  520. sord_write_iter(SordIter* iter,
  521. SerdWriter* writer);
  522. /**
  523. @}
  524. @}
  525. */
  526. #ifdef __cplusplus
  527. } /* extern "C" */
  528. #endif
  529. #endif /* SORD_SORD_H */